:root {
  --paper: #f4efe6;
  --ink: #2a2620;
  --muted: #6b6152;
  --leaf: #d8412c;
  --leaf-deep: #a52a1a;
  --leaf-pale: #fbe2d8;
  --calyx: #4f8a2b;
  --wood: #c9a473;
  --line: rgba(42, 38, 32, .18);
  --panel: rgba(255, 252, 245, .97);
  --vh: 100vh;
}
@supports (height: 100dvh) { :root { --vh: 100dvh; } }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  margin: 0;
  height: 100%;
  color: var(--ink);
  font-family: "Zen Maru Gothic", "Hiragino Maru Gothic ProN", "Hiragino Sans", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  /* 二本指の横スワイプでブラウザーが「戻る」にならないように */
  overscroll-behavior: none;
  overscroll-behavior-x: none;
  touch-action: manipulation;
}
body {
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  background: linear-gradient(#f7f3ec, var(--paper));
}
button { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--leaf-deep); outline-offset: 2px; }

/* ---------- 1画面に収める ---------- */
.app {
  max-width: 760px;
  height: var(--vh);
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
}
.app > :not(.stage) { flex-shrink: 0; }
.head { display: flex; align-items: center; gap: 10px; }
.title { flex: 1; min-width: 0; margin: 0; font-weight: 900; font-size: clamp(20px, 6vw, 28px); line-height: 1.2; white-space: nowrap; }
.title-btn { max-width: 100%; padding: 0; border: 0; background: none; color: inherit; font: inherit; text-align: left; cursor: pointer; overflow: hidden; text-overflow: ellipsis; }
.title-btn::before { content: "🍅"; margin-right: .2em; }
.menu-btn { flex: none; padding: 5px 12px; border: 1.5px solid var(--ink); border-radius: 6px; background: rgba(255, 255, 255, .6); font-size: 12px; font-weight: 700; cursor: pointer; }

/* ---------- まな板 ---------- */
.stage {
  position: relative;
  flex: 1 0 160px;
  min-height: 160px;
  border-radius: 14px;
  overflow: hidden;
  background: #efe7da;
  box-shadow: inset 0 0 0 1px var(--line), 0 6px 18px rgba(60, 45, 25, .12);
}
#board {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: pan-y pinch-zoom;
  cursor: crosshair;
}
.stage.playing #board { touch-action: none; }
.cuts-left {
  position: absolute;
  top: 12px;
  left: 14px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px 3px 5px;
  border-radius: 8px;
  background: rgba(255, 255, 255, .78);
  font-size: 15px;
  font-weight: 900;
  pointer-events: none;
  visibility: hidden;
}
.stage.playing .cuts-left { visibility: visible; }
/* 8切れのうち、いま何切れになったか。切った線だけ濃くする */
.cuts-left svg { flex: none; width: 26px; height: 26px; }
.cuts-left svg circle { fill: #e0533a; stroke: #a52a1a; stroke-width: 1.5; }
.cuts-left svg line { stroke: rgba(255, 255, 255, .45); stroke-width: 1.6; stroke-dasharray: 2 2; }
.cuts-left svg line.cut { stroke: #fff; stroke-width: 2; stroke-dasharray: none; }
.cuts-left small { font-size: 11px; font-weight: 700; color: var(--muted); }
/* 注文の伝票。遊んでいる間も、目標が8等分だと分かるようにずっと貼っておく */
.order-slip {
  position: absolute;
  left: 12px;
  bottom: 12px;
  display: grid;
  gap: 1px;
  padding: 8px 12px 7px;
  border-radius: 2px;
  background: #fffdf6;
  box-shadow: 0 2px 6px rgba(60, 45, 25, .22);
  transform: rotate(-3deg);
  font-weight: 700;
  line-height: 1.25;
  text-align: center;
  pointer-events: none;
}
.order-slip::before { content: ""; position: absolute; top: -7px; left: 50%; width: 38px; height: 14px; margin-left: -19px; background: rgba(240, 225, 170, .8); transform: rotate(2deg); }
.order-slip span { font-size: 10px; color: var(--muted); letter-spacing: .2em; border-bottom: 1px dashed var(--line); padding-bottom: 2px; }
.order-slip b { font-size: 12px; }
.order-slip strong { font-size: 22px; font-weight: 900; color: #c4372a; }
.clock {
  position: absolute;
  top: 10px;
  right: 12px;
  display: grid;
  justify-items: end;
  gap: 4px;
  pointer-events: none;
}
.clock-time { padding: 2px 9px; border-radius: 8px; background: rgba(255, 255, 255, .78); font-size: 18px; font-weight: 900; font-variant-numeric: tabular-nums; }
.clock-track { display: block; width: 92px; height: 5px; border-radius: 3px; background: rgba(42, 38, 32, .15); overflow: hidden; }
#clockBar { display: block; height: 100%; background: var(--calyx); transform-origin: left; transform: scaleX(0); }
#clockBar.over { background: #c4552d; }
.countdown {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: clamp(40px, 13vw, 72px);
  font-weight: 900;
  color: #fff;
  text-shadow: 0 3px 14px rgba(40, 30, 15, .55);
  pointer-events: none;
}
.countdown.long { font-size: clamp(28px, 9vw, 52px); }
.countdown.pop { animation: pop .35s ease-out; }
@keyframes pop { from { transform: scale(1.4); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* ---------- 記録・メッセージ ---------- */
.stats { display: flex; gap: 8px; margin: 0; }
.stats div { flex: 1; display: flex; align-items: baseline; justify-content: space-between; padding: 5px 10px; border: 1px solid var(--line); border-radius: 8px; background: rgba(255, 255, 255, .55); }
.stats dt { font-size: 11px; color: var(--muted); font-weight: 700; }
.stats dd { margin: 0; font-size: 17px; font-weight: 900; font-variant-numeric: tabular-nums; }
.message { margin: 0; min-height: 1.5em; text-align: center; font-size: clamp(12px, 3.6vw, 15px); font-weight: 700; color: var(--muted); }

/* ---------- ボタン ---------- */
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.btn {
  display: grid;
  place-items: center;
  gap: 1px;
  min-height: 52px;
  padding: 8px 10px;
  border: 0;
  border-radius: 12px;
  font-size: 17px;
  font-weight: 900;
  cursor: pointer;
  transition: transform .08s;
}
.btn:active { transform: translateY(2px); }
.btn:disabled { opacity: .5; cursor: default; }
.btn small { font-size: 10px; font-weight: 700; opacity: .85; }
.btn small.goal { font-size: 12px; font-weight: 900; opacity: 1; }
.btn-main { background: var(--leaf); color: #fff; box-shadow: 0 4px 0 var(--leaf-deep); }
.btn-sub { background: #fff; border: 2px solid var(--leaf); color: var(--leaf-deep); box-shadow: 0 4px 0 rgba(165, 42, 26, .35); }
.btn-x { grid-auto-flow: column; gap: 6px; min-height: 44px; background: var(--ink); color: #fff; font-size: 14px; }
.btn-wide { grid-column: 1 / -1; }
#startBtn { grid-column: 1 / -1; }

/* ---------- パネル ---------- */
.overlay { position: fixed; inset: var(--visible-top, 0px) 0 auto; height: var(--visible-height, var(--vh)); z-index: 10; display: grid; grid-template-rows: minmax(0, 1fr); place-items: center; padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); background: rgba(30, 24, 15, .45); overflow: hidden; }
.panel { position: relative; width: min(100%, 440px); max-height: 100%; overflow-y: auto; overscroll-behavior-y: contain; touch-action: pan-y pinch-zoom; padding: 18px 18px 16px; border-radius: 16px; background: var(--panel); box-shadow: 0 10px 40px rgba(30, 20, 10, .3); }
/* 結果は下に寄せ、上に切り分けたトマトが見えるようにする */
.result-overlay { place-items: end center; background: linear-gradient(transparent 35%, rgba(30, 24, 15, .35)); pointer-events: none; }
/* 結果を出したままタイトルやメニューを押せるよう、パネル以外はクリックを下へ通す */
.result-overlay .panel { pointer-events: auto; }
/*
 * 結果の上端に、おじさんとお皿を丸く切り取った窓を重ねる（半分はパネルの外）。そのまま撮ればスクショになる。
 * セリフはスマホでは丸窓の下、広い画面では丸窓の横に出す。
 */
.result-wrap { --window: 140px; display: flex; flex-direction: column; width: min(100%, 440px); max-height: 100%; min-height: 0; }
.result-wrap .panel { width: 100%; min-height: 0; padding-top: calc(var(--window) / 2 + 10px); }
.uncle-window { position: relative; z-index: 1; flex: none; align-self: center; width: var(--window); height: var(--window); margin-bottom: calc(var(--window) / -2); border: 4px solid #fff; border-radius: 50%; background: #5b3b24; box-shadow: 0 6px 18px rgba(30, 20, 10, .35); }
.uncle-line { position: relative; margin: 0 auto 10px; padding: 8px 14px; width: fit-content; max-width: 100%; border: 2px solid var(--ink); border-radius: 14px; background: #fff; font-size: 15px; font-weight: 900; line-height: 1.4; text-align: center; }
.uncle-line::before { content: ""; position: absolute; left: 50%; top: -9px; width: 14px; height: 14px; margin-left: -7px; border-top: 2px solid var(--ink); border-left: 2px solid var(--ink); background: #fff; transform: rotate(45deg); }
.uncle-line:empty { display: none; }
@media (min-width: 560px) {
  .result-wrap { --window: 180px; }
  .result-wrap .panel { padding-top: 14px; }
  .uncle-window { align-self: flex-start; margin-left: 18px; }
  .uncle-line { display: flex; align-items: center; min-height: calc(var(--window) / 2 - 24px); margin: 0 0 10px calc(var(--window) + 20px); width: auto; text-align: left; }
  .uncle-line::before { left: -9px; top: 50%; margin: -7px 0 0; border-top: 0; border-bottom: 2px solid var(--ink); }
}
#resultPanel:focus { outline: none; }
.result-total { margin: 0; text-align: center; font-size: clamp(30px, 9vw, 40px); font-weight: 900; color: var(--leaf-deep); }
.result-title { margin: 0; text-align: center; font-size: 17px; font-weight: 900; }
.result-best { margin: 2px 0 0; min-height: 1em; text-align: center; font-size: 12px; font-weight: 900; color: #c4552d; }
.result-best:empty { display: none; }
.axes { margin: 10px 0 6px; padding: 0; list-style: none; display: grid; gap: 7px; }
.axes li { display: grid; grid-template-columns: 4.5em 1fr 3.2em; align-items: center; column-gap: 8px; }
.axis-name { font-size: 13px; font-weight: 900; }
.axis-track { height: 9px; border-radius: 5px; background: rgba(42, 38, 32, .12); overflow: hidden; }
.axis-track span { display: block; height: 100%; background: var(--leaf); transform-origin: left; transform: scaleX(0); transition: transform .7s cubic-bezier(.2, .8, .2, 1); }
.axis-score { text-align: right; font-size: 17px; font-weight: 900; font-variant-numeric: tabular-nums; }
.axes small { grid-column: 2 / -1; font-size: 11px; color: var(--muted); white-space: pre-line; }
.wedge-chart { margin: 4px 0 10px; }
#wedges {
  display: block;
  width: 100%;
  height: 90px;
  --wedge-good: #6f9a35;
  --wedge-near: #b9c95a;
  --wedge-off: #f0a54a;
  --wedge-bad: #e0533a;
  --wedge-label: #2a2620;
}
.wedge-chart figcaption { font-size: 10px; color: var(--muted); text-align: center; }
.panel-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

/* ---------- メニュー ---------- */
.menu-close { position: absolute; top: 8px; right: 10px; width: 36px; height: 36px; border: 0; background: none; font-size: 24px; cursor: pointer; }
.menu-label { margin: 6px 0 6px; font-size: 13px; color: var(--muted); }
.menu-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }
.menu-item { display: block; padding: 10px; border: 1.5px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink); text-align: center; text-decoration: none; font-size: 13px; font-weight: 700; cursor: pointer; }
.howto { margin: 0 0 8px; padding-left: 1.3em; font-size: 12.5px; line-height: 1.65; }
.menu-links { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 10px; font-size: 12px; }
.menu-links a { color: var(--muted); }
.menu-reset { margin-left: auto; padding: 0; border: 0; background: none; color: #b0412a; font-size: 12px; text-decoration: underline; cursor: pointer; }

/* 2行の案内も出すので、改行を生かして角丸の箱にする */
.snackbar { position: fixed; left: 50%; bottom: max(18px, env(safe-area-inset-bottom)); z-index: 20; width: max-content; max-width: calc(100% - 32px); transform: translateX(-50%); padding: 8px 16px; border-radius: 14px; background: var(--ink); color: #fff; font-size: 13px; line-height: 1.5; text-align: center; white-space: pre-line; pointer-events: none; }
.snackbar:empty { display: none; }

@media (max-height: 640px) {
  .stats dd { font-size: 14px; }
  .btn { min-height: 44px; font-size: 15px; }
  .axes { gap: 4px; }
  #wedges { height: 110px; }
}

/* ---------- ランキング ---------- */
.rank-switch { grid-column: 1 / -1; display: flex; align-items: center; justify-content: center; gap: 8px; font-size: 14px; font-weight: 900; cursor: pointer; }
.rank-switch input { width: 20px; height: 20px; accent-color: var(--leaf); }
.panel .rank-switch { margin-top: 8px; }
.rank-status { grid-column: 1 / -1; margin: 0; text-align: center; font-size: 12px; font-weight: 700; color: var(--muted); white-space: pre-line; }
.rank-status:empty { display: none; }
.rank-retry { grid-column: 1 / -1; justify-self: center; display: block; margin: 4px auto 0; padding: 6px 14px; border: 1.5px solid var(--leaf); border-radius: 999px; background: #fff; color: var(--leaf-deep); font-size: 13px; font-weight: 900; cursor: pointer; }
.rank-retry[hidden] { display: none; }
.rank-gate { position: fixed; left: 50%; bottom: max(64px, calc(env(safe-area-inset-bottom) + 48px)); z-index: 30; transform: translateX(-50%); }
/* 確認の画面が要るとき以外は見せず、ボタンに重ねない。display:none だと確認が動かないことがあるので、見えなくするだけにする */
.rank-gate:not(.active), .rank-gate.done { pointer-events: none; }
.rank-gate:not(.active) { visibility: hidden; }
/* 右下に出す（上は時計と残りの回数が使う） */
.practice-badge { position: absolute; right: 8px; bottom: 8px; padding: 3px 10px; border-radius: 999px; background: rgba(42, 38, 32, .7); color: #fff; font-size: 11px; font-weight: 700; pointer-events: none; }
.btn-board { min-height: 44px; font-size: 14px; }
.panel-actions .btn-wide { grid-column: 1 / -1; }
.rank-result { margin: 0 0 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: rgba(255, 255, 255, .6); }
.rank-result-text { margin: 0; font-size: 12.5px; font-weight: 700; white-space: pre-line; }
.rank-publish { display: grid; grid-template-columns: 1fr auto; gap: 6px 8px; align-items: end; margin-top: 6px; }
.rank-publish[hidden] { display: none; }
.rank-publish label, .board-name label { display: grid; gap: 2px; font-size: 11px; font-weight: 700; color: var(--muted); }
.rank-publish input, .board-name input { min-width: 0; padding: 8px 10px; border: 1.5px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink); font: inherit; font-size: 16px; }
.rank-publish .btn { min-height: 40px; font-size: 15px; }
.rank-publish small { grid-column: 1 / -1; font-size: 10.5px; color: var(--muted); }
.board-title { margin: 0 0 4px; font-size: 18px; }
.board-lead { margin: 0 0 8px; font-size: 11.5px; color: var(--muted); }
.board-feedback { margin: 0 0 6px; font-size: 12px; font-weight: 700; color: var(--leaf-deep); }
.board-feedback:empty { display: none; }
.board-mine { margin-bottom: 8px; padding: 8px 10px; border-radius: 10px; background: rgba(216, 65, 44, .1); }
.board-mine p { margin: 0; font-size: 14px; font-weight: 900; }
.board-mine small { font-size: 11.5px; color: var(--muted); }
.board-pending { margin: 0 0 8px; padding: 0; list-style: none; display: grid; gap: 6px; }
.board-pending li { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 12.5px; }
.board-pending .btn { min-height: 34px; padding: 4px 14px; font-size: 13px; }
.board-name { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: end; margin-bottom: 10px; }
.board-name .btn { min-height: 40px; font-size: 13px; }
.board-name .btn[hidden] { display: none; }
.board-list { margin: 0 0 8px; padding: 0; list-style: none; display: grid; gap: 4px; }
.board-list li { display: grid; grid-template-columns: 2.4em 1fr auto; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; background: rgba(255, 255, 255, .6); font-size: 14px; }
.board-list li.mine { background: rgba(216, 65, 44, .16); }
.board-rank { text-align: right; font-weight: 900; font-variant-numeric: tabular-nums; }
.board-entry { display: grid; font-weight: 700; overflow-wrap: anywhere; }
.board-entry small { font-size: 10.5px; color: var(--muted); font-weight: 700; }
.board-score { font-weight: 900; font-variant-numeric: tabular-nums; }
.board-empty { margin: 8px 0; text-align: center; font-size: 13px; color: var(--muted); }
.board-refresh { width: 100%; margin-bottom: 8px; }
.board-manage { font-size: 12px; color: var(--muted); }
.board-manage summary { cursor: pointer; font-weight: 700; }
.board-manage p { margin: 6px 0; }
.board-manage .menu-reset { margin: 4px 0; }
