:root {
  --paper: #f4efe6;
  --ink: #2a2620;
  --muted: #6b6152;
  --leaf: #6f9a35;
  --leaf-deep: #4d7424;
  --leaf-pale: #e4efc4;
  --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; }
.inset-label {
  position: absolute;
  top: 12px;
  left: 14px;
  padding: 1px 7px;
  border-radius: 999px;
  background: rgba(42, 38, 32, .7);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  pointer-events: none;
  visibility: hidden;
}
.stage.playing .inset-label { visibility: visible; }
.guide-label {
  position: absolute;
  top: 12px;
  left: 62px;
  padding: 1px 7px;
  border-radius: 999px;
  background: #4f9e1c;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  pointer-events: none;
  visibility: hidden;
}
.stage.guided .guide-label { visibility: visible; }
.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(--leaf); 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.pop { animation: pop .35s ease-out; }
@keyframes pop { from { transform: scale(1.4); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.mouse-hint {
  position: absolute;
  bottom: 8px;
  left: 50%;
  margin: 0;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(42, 38, 32, .7);
  color: #fff;
  font-size: 11px;
  transform: translateX(-50%);
  pointer-events: none;
  white-space: nowrap;
}

/* ---------- 操作の案内（初回、はじめ！のあと操作がないとき） ---------- */
/* 盤面への入力を妨げないよう、クリックやスクロールは下の盤面へ通す */
.coach { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; padding: 40px 12px 34px; pointer-events: none; animation: coach-in .3s ease-out; }
@keyframes coach-in { from { opacity: 0; transform: translateY(6px); } }
.coach-card { display: grid; justify-items: center; gap: 8px; max-width: 100%; padding: 12px 16px 12px; border-radius: 14px; background: rgba(42, 38, 32, .8); color: #fff; box-shadow: 0 6px 20px rgba(30, 20, 10, .3); }
.coach-demo { position: relative; width: 168px; height: 84px; }
.coach-surface { position: absolute; inset: 6px 12px; border: 1.5px solid rgba(255, 255, 255, .5); border-radius: 10px; background: rgba(255, 255, 255, .12); }
.coach-arrow { position: absolute; top: 4px; left: 0; right: 0; text-align: center; font-size: 18px; font-weight: 900; line-height: 1; color: #b9dc7e; animation: coach-arrow 2.8s ease-in-out infinite; }
.coach-feed { position: absolute; top: 32px; left: 62px; display: flex; gap: 5px; animation: coach-swipe 2.8s ease-in-out infinite; }
.coach-feed i { display: block; width: 18px; height: 18px; border-radius: 50%; background: #fff; box-shadow: 0 0 0 4px rgba(255, 255, 255, .22); }
.coach-feed i + i { margin-top: -5px; }
.coach-cut { position: absolute; top: 31px; left: 74px; width: 20px; height: 20px; border-radius: 50%; background: #fff; opacity: 0; animation: coach-tap 2.8s ease-out infinite; }
.coach-cut::after { content: ""; position: absolute; inset: -4px; border: 2.5px solid #b9dc7e; border-radius: 50%; opacity: 0; animation: coach-ring 2.8s ease-out infinite; }
/* スマホ: 右手の指で画面の端から端までスワイプして送り、左手の指で左下をタップして切る */
.coach[data-kind="touch"] .coach-surface { inset: 2px 4px; border-radius: 14px; }
.coach[data-kind="touch"] .coach-feed { top: 20px; left: 10px; animation-name: coach-swipe-wide; }
.coach[data-kind="touch"] .coach-feed i + i { display: none; }
.coach[data-kind="touch"] .coach-arrow { top: 6px; }
.coach[data-kind="touch"] .coach-cut { top: 54px; left: 14px; }
/* マウス: 右へ動かして送り、左ボタンで切る */
.coach[data-kind="mouse"] .coach-surface { display: none; }
.coach[data-kind="mouse"] .coach-feed { top: 22px; left: 71px; width: 26px; height: 40px; border: 2.5px solid #fff; border-radius: 13px 13px 14px 14px; animation-name: coach-mouse; }
.coach[data-kind="mouse"] .coach-feed::before { content: ""; position: absolute; top: 0; left: 50%; width: 2px; height: 14px; background: #fff; transform: translateX(-50%); }
.coach[data-kind="mouse"] .coach-feed i { display: none; }
.coach[data-kind="mouse"] .coach-cut { top: 25px; left: 100px; width: 10px; height: 13px; border-radius: 10px 0 0 0; background: #b9dc7e; }
.coach[data-kind="mouse"] .coach-cut::after { display: none; }
.coach[data-kind="mouse"] .coach-arrow { top: 66px; }
@keyframes coach-swipe {
  0% { opacity: 0; transform: translateX(-34px); }
  8% { opacity: 1; transform: translateX(-34px); }
  44% { opacity: 1; transform: translateX(34px); }
  52%, 100% { opacity: 0; transform: translateX(34px); }
}
@keyframes coach-swipe-wide {
  0% { opacity: 0; transform: translateX(0); }
  8% { opacity: 1; transform: translateX(0); }
  46% { opacity: 1; transform: translateX(132px); }
  52%, 100% { opacity: 0; transform: translateX(132px); }
}
@keyframes coach-mouse {
  0% { opacity: 0; transform: translateX(-38px); }
  8% { opacity: 1; transform: translateX(-38px); }
  44%, 86% { opacity: 1; transform: translateX(26px); }
  96%, 100% { opacity: 0; transform: translateX(26px); }
}
@keyframes coach-arrow {
  0%, 4% { opacity: 0; transform: translateX(-8px); }
  20%, 40% { opacity: 1; transform: translateX(0); }
  52%, 100% { opacity: 0; transform: translateX(8px); }
}
@keyframes coach-tap {
  0%, 56% { opacity: 0; transform: scale(1.3); }
  62% { opacity: 1; transform: scale(.85); }
  76% { opacity: 1; transform: scale(.85); }
  86%, 100% { opacity: 0; transform: scale(.85); }
}
@keyframes coach-ring {
  0%, 62% { opacity: 0; transform: scale(.8); }
  66% { opacity: 1; transform: scale(1); }
  86%, 100% { opacity: 0; transform: scale(2.1); }
}
.coach-steps { margin: 0; padding: 0; list-style: none; display: grid; gap: 4px; counter-reset: coach; }
.coach-step { display: flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 700; line-height: 1.35; counter-increment: coach; animation: coach-step-feed 2.8s ease-in-out infinite; }
.coach-step + .coach-step { animation-name: coach-step-cut; }
.coach-step::before { content: counter(coach); flex: none; display: grid; place-items: center; width: 18px; height: 18px; border-radius: 50%; background: var(--leaf); font-size: 11px; font-weight: 900; }
.coach-step.done { animation: none; opacity: .6; }
.coach-step.done::before { content: "✓"; }
@keyframes coach-step-feed { 0%, 50% { opacity: 1; } 56%, 100% { opacity: .55; } }
@keyframes coach-step-cut { 0%, 54% { opacity: .55; } 60%, 88% { opacity: 1; } 94%, 100% { opacity: .55; } }
@media (prefers-reduced-motion: reduce) {
  .coach, .coach-feed, .coach-arrow, .coach-step { animation: none; }
  .coach-cut, .coach-cut::after { animation: none; opacity: 1; }
  .coach[data-kind="mouse"] .coach-feed { transform: translateX(26px); }
}
@media (max-height: 640px) {
  .coach { padding-top: 32px; padding-bottom: 28px; }
  .coach-card { gap: 4px; padding: 8px 12px; }
  .coach-demo { margin: -10px 0; transform: scale(.76); }
}

/* ---------- 記録・メッセージ ---------- */
.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-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(77, 116, 36, .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; }
.actions:has(#mouseBtn[hidden]) #startBtn { grid-column: 1 / -1; }
/* マウスを使える端末では「トラックパッド」と「マウス」を同じ重みで並べ、どちらで遊ぶか選ばせる */
.actions:has(#mouseBtn[hidden]) .pair-only, .actions:not(:has(#mouseBtn[hidden])) .solo-only { display: none; }
.mouse-note { grid-column: 1 / -1; margin: -2px 0 0; font-size: 11px; font-weight: 700; color: var(--muted); text-align: center; }

/* ---------- 感度 ---------- */
.sens-control { grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; padding: 4px 2px; }
.panel .sens-control { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); }
.sens-label { flex: none; font-size: 12px; font-weight: 900; color: var(--muted); }
.sens-step { flex: none; width: 34px; height: 34px; border: 1.5px solid var(--leaf); border-radius: 50%; background: #fff; color: var(--leaf-deep); font-size: 18px; font-weight: 900; line-height: 1; cursor: pointer; }
.sens-step:disabled { opacity: .4; cursor: default; }
.sens-range { flex: 1; min-width: 0; accent-color: var(--leaf); }
.sens-value { flex: none; min-width: 2.2em; text-align: right; font-size: 17px; font-weight: 900; font-variant-numeric: tabular-nums; }

/* ---------- パネル ---------- */
.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; }
#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 2.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); }
.histogram { margin: 4px 0 10px; }
#histogram {
  display: block;
  width: 100%;
  height: 96px;
  --bar: #cdbb95;
  --bar-good: #6f9a35;
  --target-band: rgba(111, 154, 53, .14);
  --axis: #6b6152;
}
.histogram 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; }
  #histogram { height: 70px; }
}

/* ---------- ランキング ---------- */
.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; }
/* マウス操作中の案内（下の中央）と並ぶときは、案内を1段上げる */
.stage:has(.practice-badge:not([hidden])) .mouse-hint { bottom: 38px; }
.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(111, 154, 53, .12); }
.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(111, 154, 53, .18); }
.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; }
