/* 盤のコンセプト: 藍染めの布の上に置いた榧（かや）の木盤と、ガラスのおはじき。
   縦持ちでは盤を90度回し、自分の列（左）と貯め穴（下）が手元に来る。 */
:root {
  --cloth: #e7edf4;
  --cloth-2: #d6dfea;
  --ink: #17223a;
  --ink-soft: #4b5873;
  --wood: #c99a62;
  --wood-dark: #a77843;
  --hollow: #8f6236;
  --p0: #13795f;
  --p1: #a3304a;
  --glow: #f4b400;
  --btn: #17223a;
  --btn-ink: #f4f7fb;
  --font-display: "Dela Gothic One", "Hiragino Sans", "Yu Gothic", system-ui, sans-serif;
  --font-body: "Zen Kaku Gothic New", "Hiragino Sans", "Yu Gothic", system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --cloth: #0f1829;
    --cloth-2: #16223a;
    --ink: #e4eaf4;
    --ink-soft: #9fb0cc;
    --wood: #8a6136;
    --wood-dark: #6c4924;
    --hollow: #4f3418;
    --p0: #3fc49c;
    --p1: #ef6f8a;
    --glow: #ffc933;
    --btn: #e4eaf4;
    --btn-ink: #0f1829;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --cloth: #0f1829;
  --cloth-2: #16223a;
  --ink: #e4eaf4;
  --ink-soft: #9fb0cc;
  --wood: #8a6136;
  --wood-dark: #6c4924;
  --hollow: #4f3418;
  --p0: #3fc49c;
  --p1: #ef6f8a;
  --glow: #ffc933;
  --btn: #e4eaf4;
  --btn-ink: #0f1829;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--cloth);
  color: var(--ink);
  font-family: var(--font-body);
  font-weight: 500;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-block: env(safe-area-inset-top, 0px) env(safe-area-inset-bottom, 0px);
}

.bar {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 52px;
}
.logo {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: 22px;
  letter-spacing: 0.06em;
  margin: 0;
}
.bar:first-child { flex-wrap: wrap; row-gap: 0; padding-top: 4px; }
.status {
  margin: 0;
  flex: 1 0 100%;
  min-height: 2.6em;
  font-weight: 700;
  font-size: 15px;
  line-height: 1.3;
}
@media (min-width: 560px) {
  .status { flex: 1 1 auto; min-height: 0; text-align: right; }
}
.foot { justify-content: center; padding-bottom: 8px; flex-wrap: wrap; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.seg { display: flex; flex-wrap: wrap; border: 0; margin: 0; padding: 0; gap: 6px; }
.seg legend { width: 100%; padding: 0 0 6px; font-size: 13px; color: var(--ink-soft); }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label {
  flex: 1;
  text-align: center;
  padding: 11px 8px;
  border-radius: 12px;
  background: var(--cloth-2);
  white-space: nowrap;
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  color: var(--ink-soft);
}
.seg input:checked + label { background: var(--btn); color: var(--btn-ink); }
.seg input:focus-visible + label { outline: 3px solid var(--glow); }

.btn {
  font: inherit;
  font-weight: 700;
  font-size: 15px;
  border: 0;
  border-radius: 999px;
  padding: 10px 20px;
  background: var(--btn);
  color: var(--btn-ink);
  cursor: pointer;
  min-height: 44px;
}
.btn:focus-visible, .pit:focus-visible { outline: 3px solid var(--glow); outline-offset: 2px; }

/* ---- 盤 ---- */
.stage {
  flex: 1;
  min-height: 0;
  container-type: size;
  display: grid;
  place-items: center;
}
.board {
  --ratio: 0.6;
  width: min(100cqw, 100cqh * var(--ratio));
  height: min(100cqh, 100cqw / var(--ratio));
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1.25fr repeat(6, 1fr) 1.25fr;
  gap: 2.2%;
  padding: 3.5%;
  background:
    repeating-linear-gradient(97deg, transparent 0 14px, rgb(0 0 0 / 0.035) 14px 15px),
    linear-gradient(160deg, var(--wood), var(--wood-dark));
  border-radius: 28px;
  box-shadow: 0 10px 30px rgb(0 0 0 / 0.25), inset 0 1px 0 rgb(255 255 255 / 0.25);
}
.cell {
  grid-row: var(--r);
  grid-column: var(--c);
  position: relative;
  container-type: size;
  border-radius: 999px;
  background: radial-gradient(ellipse at 50% 40%, var(--hollow), color-mix(in srgb, var(--hollow) 70%, black));
  box-shadow: inset 0 4px 10px rgb(0 0 0 / 0.45), 0 1px 0 rgb(255 255 255 / 0.18);
}
.store { border-radius: 26px; }

@media (min-aspect-ratio: 4/3) {
  .board {
    --ratio: 3.1;
    grid-template-columns: 1.25fr repeat(6, 1fr) 1.25fr;
    grid-template-rows: 1fr 1fr;
    gap: 2.6%;
    padding: 1.6%;
  }
  .cell { grid-row: var(--lr); grid-column: var(--lc); }
}

.pit { cursor: default; border: 0; padding: 0; font: inherit; color: inherit; }
.pit.playable { cursor: pointer; box-shadow: inset 0 4px 10px rgb(0 0 0 / 0.45), 0 0 0 3px var(--glow); }

.pit.side-0 { --cx: 15%; --cy: 50%; --sx: 58%; --sy: 50%; }
.pit.side-1 { --cx: 85%; --cy: 50%; --sx: 42%; --sy: 50%; }
.store { --cx: 50%; --cy: 58%; --sx: 50%; --sy: 50%; }
@media (min-aspect-ratio: 4/3) {
  .pit.side-0 { --cx: 50%; --cy: 84%; --sx: 50%; --sy: 40%; }
  .pit.side-1 { --cx: 50%; --cy: 16%; --sx: 50%; --sy: 60%; }
}
.count {
  position: absolute;
  left: var(--cx);
  top: var(--cy);
  transform: translate(-50%, -50%) rotate(var(--flip, 0deg));
  font-family: var(--font-display);
  font-size: 42cqmin;
  line-height: 1;
  color: #fff;
  text-shadow: 0 2px 4px rgb(0 0 0 / 0.6);
  pointer-events: none;
  z-index: 2;
}
.store .count { font-size: 50cqmin; }
.store .who {
  position: absolute;
  left: 50%;
  top: 6%;
  transform: translateX(-50%) rotate(var(--flip, 0deg));
  font-size: 14px;
  font-weight: 700;
  color: #fff;
  opacity: 0.85;
  white-space: nowrap;
}

/* おはじき */
.stones { position: absolute; inset: 0; pointer-events: none; }
.stone {
  --size: 26cqmin;
  position: absolute;
  width: var(--size);
  height: var(--size);
  left: calc(var(--sx) + var(--dx) * 26cqw - var(--size) / 2);
  top: calc(var(--sy) + var(--dy) * 30cqh - var(--size) / 2);
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, rgb(255 255 255 / 0.85), transparent 32%), var(--hue);
  box-shadow: inset 0 -2px 3px rgb(0 0 0 / 0.25), 0 1px 2px rgb(0 0 0 / 0.4);
  opacity: 0.92;
}
.store .stone { --size: 14cqmin; }

.side-0 { --side: var(--p0); }
.side-1 { --side: var(--p1); }
.cell::after {
  content: "";
  position: absolute;
  inset: -3px;
  border-radius: inherit;
  border: 2px solid var(--side);
  opacity: 0.55;
  pointer-events: none;
}
.status .decided { display: block; font-size: 12px; font-weight: 500; color: var(--ink-soft); }
.status .chip {
  display: inline-block;
  width: 0.8em;
  height: 0.8em;
  border-radius: 50%;
  background: var(--chip);
  margin-right: 6px;
  vertical-align: -0.05em;
}

/* ---- 動きの表現 ---- */
.stage { position: relative; }
.cell.drop { animation: drop 0.28s ease-out; }
@keyframes drop {
  0% { box-shadow: inset 0 4px 10px rgb(0 0 0 / 0.45), 0 0 0 0 var(--glow); }
  40% { box-shadow: inset 0 4px 10px rgb(0 0 0 / 0.45), 0 0 0 5px var(--glow); }
  100% { box-shadow: inset 0 4px 10px rgb(0 0 0 / 0.45), 0 0 0 0 transparent; }
}
.cell.source::before, .cell.last::before, .cell.pick::before, .cell.grab::before {
  content: "";
  position: absolute;
  inset: -5px;
  border-radius: inherit;
  pointer-events: none;
  z-index: 3;
}
.cell.source::before { border: 2px dashed var(--side); opacity: 0.9; }
.cell.last::before { border: 3px solid var(--glow); }
.cell.pick::before { border: 3px solid var(--side); animation: blink 0.4s ease-in-out infinite alternate; }
.cell.grab::before { background: rgb(255 255 255 / 0.25); border: 3px solid var(--glow); animation: blink 0.2s ease-in-out infinite alternate; }
@keyframes blink { from { opacity: 0.35; } to { opacity: 1; } }

.banner {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) rotate(var(--flip, 0deg));
  z-index: 10;
  pointer-events: none;
  font-family: var(--font-display);
  font-size: clamp(26px, 9vw, 44px);
  white-space: nowrap;
  color: #fff;
  padding: 10px 22px;
  border-radius: 18px;
  background: color-mix(in srgb, var(--ink) 82%, transparent);
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.3);
}
.banner[data-tone="extra"] { background: color-mix(in srgb, var(--p0) 90%, black); }
.banner[data-tone="extra"][data-side="1"] { background: color-mix(in srgb, var(--p1) 90%, black); }
.banner[data-tone="combo"] { background: linear-gradient(120deg, color-mix(in srgb, var(--p0) 85%, black), color-mix(in srgb, var(--glow) 70%, #7a4a00)); font-size: clamp(30px, 11vw, 52px); }
.banner[data-tone="combo"][data-side="1"] { background: linear-gradient(120deg, color-mix(in srgb, var(--p1) 85%, black), color-mix(in srgb, var(--glow) 70%, #7a4a00)); }
.banner[data-tone="capture"] { background: color-mix(in srgb, var(--glow) 80%, #7a4a00); }
.banner.show { animation: pop 1.1s ease-out forwards; }
.banner.show.long { animation-duration: 2.3s; }
@keyframes pop {
  0% { opacity: 0; transform: translate(-50%, -50%) rotate(var(--flip, 0deg)) scale(0.6); }
  15% { opacity: 1; transform: translate(-50%, -50%) rotate(var(--flip, 0deg)) scale(1.08); }
  25% { transform: translate(-50%, -50%) rotate(var(--flip, 0deg)) scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .cell.drop, .banner.show, .cell.pick::before, .cell.grab::before { animation: none; }
}

/* ---- 下見 ---- */
.pit.selected { transform: scale(1.03); box-shadow: inset 0 4px 10px rgb(0 0 0 / 0.45), 0 0 0 4px var(--side), 0 0 0 7px var(--glow); }
.cell.path .stones { filter: brightness(1.15); }
.cell.path::after { opacity: 1; border-style: dotted; border-color: var(--glow); }
.cell.land::after { opacity: 1; border: 3px solid var(--glow); inset: -6px; }
.tag {
  position: absolute;
  left: 50%;
  bottom: -2px;
  transform: translate(-50%, 50%) rotate(var(--flip, 0deg));
  z-index: 4;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
  background: var(--ink);
  color: var(--cloth);
  pointer-events: none;
}
.tag[data-good="1"] { background: var(--glow); color: #2a1a00; }
.store .tag { left: auto; right: 7%; bottom: auto; top: 50%; transform: translateY(-50%) rotate(var(--flip, 0deg)); font-size: 13px; padding: 4px 10px; }
.board.previewing .pit.playable:not(.selected) { box-shadow: inset 0 4px 10px rgb(0 0 0 / 0.45); }

/* ---- 新しいゲームの設定 ---- */
.sheet {
  border: 0;
  padding: 0;
  width: min(420px, calc(100% - 32px));
  border-radius: 20px;
  background: var(--cloth);
  color: var(--ink);
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.35);
}
.sheet::backdrop { background: rgb(10 16 30 / 0.45); }
.sheet-body { display: grid; gap: 16px; padding: 20px 18px 18px; }
.sheet { max-height: calc(100% - 24px); overflow-y: auto; }
.sheet-title:focus { outline: none; }
.sheet-title { margin: 0; font-family: var(--font-display); font-weight: 400; font-size: 20px; }
.sheet-actions { display: flex; gap: 10px; justify-content: flex-end; }
.btn.ghost { background: transparent; color: var(--ink); box-shadow: inset 0 0 0 2px var(--cloth-2); }
.seg[disabled] { opacity: 0.4; }

/* ---- 決着と結果 ---- */
.btn.accent { background: var(--glow); color: #2a1a00; }
.result .sheet-body { text-align: center; gap: 10px; }
.result-kicker { margin: 0; font-size: 13px; color: var(--ink-soft); }
.result-title { margin: 0; font-family: var(--font-display); font-weight: 400; font-size: 30px; text-wrap: balance; }
.result-score {
  margin: 4px 0 0;
  display: flex;
  justify-content: center;
  align-items: baseline;
  gap: 14px;
  font-family: var(--font-display);
  font-size: 44px;
  font-variant-numeric: tabular-nums;
}
#scoreMe { color: var(--p0); }
#scoreOp { color: var(--p1); }
.result-score .dash { font-size: 28px; color: var(--ink-soft); }
.result-note { margin: 0 0 8px; font-size: 14px; color: var(--ink-soft); }
.result .sheet-actions { justify-content: center; }

/* ---- 危ない穴 ---- */
:root { --danger: #d6293e; }
.cell.danger::after { opacity: 1; border: 3px solid var(--danger); border-style: solid; }
.warn {
  position: absolute;
  top: -3px;
  left: 50%;
  transform: translate(-50%, -45%) rotate(var(--flip, 0deg));
  z-index: 5;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  background: var(--danger);
  color: #fff;
  pointer-events: none;
  white-space: nowrap;
}
.check { display: flex; gap: 10px; align-items: center; font-size: 14px; }
.check input { width: 20px; height: 20px; accent-color: var(--btn); }

/* ---- 遊び方 ---- */
.link {
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  background: none;
  border: 0;
  color: var(--ink-soft);
  text-decoration: underline;
  text-underline-offset: 3px;
  padding: 8px 4px;
  cursor: pointer;
}
.rules .sheet-body { gap: 14px; }
.rules p { margin: 0; line-height: 1.7; font-size: 14px; }
.rules .lead { font-size: 15px; }
.rule { display: grid; gap: 6px; padding-top: 12px; border-top: 1px solid var(--cloth-2); }
.rule h3 { margin: 0; font-size: 15px; font-weight: 700; }
.rules .note { font-size: 13px; color: var(--ink-soft); }
.mini { display: flex; gap: 6px; padding: 8px; border-radius: 12px; background: var(--wood); width: max-content; max-width: 100%; }
.mini.two { display: grid; grid-template-columns: 52px 52px auto; align-items: center; }
.mini .ghost { visibility: hidden; }
.mini .lbl { font-size: 12px; font-weight: 700; color: #fff; padding-left: 4px; }
.m {
  min-width: 44px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: var(--hollow);
  color: #fff;
  font-family: var(--font-display);
  font-size: 13px;
  box-shadow: inset 0 2px 5px rgb(0 0 0 / 0.4);
}
.m.src { outline: 2px dashed #fff; outline-offset: -4px; }
.m.add { color: var(--glow); }
.m.st { border-radius: 10px; min-width: 52px; }
.m.hit { box-shadow: inset 0 2px 5px rgb(0 0 0 / 0.4), 0 0 0 3px var(--glow); }
.m.grab { box-shadow: inset 0 2px 5px rgb(0 0 0 / 0.4), 0 0 0 3px var(--danger); }
.banner small { display: block; font-family: var(--font-body); font-weight: 700; font-size: 13px; margin-top: 4px; white-space: normal; max-width: 70vw; }

/* ---- 振り返り ---- */
.review { display: grid; gap: 10px; text-align: left; }
.chart-wrap { position: relative; border-radius: 12px; background: var(--cloth-2); padding: 18px 8px; }
.chart { display: block; width: 100%; height: 120px; overflow: visible; }
.axis { position: absolute; left: 10px; font-size: 11px; font-weight: 700; color: var(--ink-soft); }
.axis.top { top: 3px; }
.axis.bottom { bottom: 3px; }
.moments { margin: 0; padding: 0; list-style: none; display: grid; gap: 6px; font-size: 13px; line-height: 1.5; }
.moments li { display: grid; grid-template-columns: 3.4em 1fr; gap: 8px; }
.moments .n { font-variant-numeric: tabular-nums; color: var(--ink-soft); font-weight: 700; }
.moments .bad { color: var(--danger); font-weight: 700; }
#reviewBtn { justify-self: center; }

.moment { font: inherit; text-align: left; background: none; border: 0; padding: 0; color: inherit; cursor: pointer; }
.moment u { color: var(--ink-soft); font-size: 12px; text-underline-offset: 3px; }
.mini-li { display: block !important; }
.mb { display: grid; grid-template-columns: 1.2fr repeat(6, 1fr) 1.2fr; grid-template-rows: 26px 26px; gap: 4px; padding: 6px; border-radius: 10px; background: var(--wood); }
.mc { display: grid; place-items: center; border-radius: 999px; background: var(--hollow); color: #fff; font-family: var(--font-display); font-size: 12px; }
.mc.st { border-radius: 8px; }
.mc.played { box-shadow: 0 0 0 3px var(--danger); }
.mc.better { box-shadow: 0 0 0 3px var(--glow); }
.mb-note { margin: 4px 0 0; font-size: 11px; color: var(--ink-soft); }
.played-key, .better-key { display: inline-block; width: 10px; height: 10px; border-radius: 50%; vertical-align: -1px; margin-right: 3px; border: 2px solid var(--danger); }
.better-key { border-color: var(--glow); margin-left: 6px; }

/* ---- 手番の見分け ---- */
.board[data-turn="0"] .cell.side-1 .stones, .board[data-turn="1"] .cell.side-0 .stones { opacity: 0.55; }
.board[data-turn="0"] .pit.side-1 .count, .board[data-turn="1"] .pit.side-0 .count { opacity: 0.6; }
.banner[data-tone="turn"] { background: color-mix(in srgb, var(--p0) 85%, black); font-size: clamp(22px, 7vw, 34px); }
.banner[data-tone="turn"][data-side="1"] { background: color-mix(in srgb, var(--p1) 85%, black); }

/* 向かい合わせ: プレイヤー2の文字を逆さに（translate の後に回す） */
.board.facing .cell.side-1 { --flip: 180deg; }
.board.facing .store.side-1 .who { top: auto; bottom: 6%; }
.banner.flip { --flip: 180deg; }

/* ---- 戦績 ---- */
.seg label .rec { display: block; font-size: 11px; font-weight: 500; opacity: 0.8; min-height: 1.2em; }
.result-record { margin: 0; font-size: 13px; font-weight: 700; }
.result-record .new { display: inline-block; margin-left: 6px; padding: 1px 8px; border-radius: 999px; background: var(--glow); color: #2a1a00; }
.result .sheet-actions { flex-wrap: wrap; }

/* ---- 効果音ボタン ---- */
.icon-btn { margin-left: auto; width: 44px; height: 44px; display: grid; place-items: center; border: 0; border-radius: 50%; background: transparent; color: var(--ink-soft); cursor: pointer; }
.icon-btn:focus-visible { outline: 3px solid var(--glow); }
.icon-btn[aria-pressed="true"] .mute, .icon-btn[aria-pressed="false"] .wave { display: none; }
@media (min-width: 560px) { .icon-btn { margin-left: 0; order: 3; } }

/* 横持ち: 貯め穴は縦長なので、名前は折り返し、札は下に */
@media (min-aspect-ratio: 4/3) {
  .store .who { white-space: normal; word-break: keep-all; width: 92%; text-align: center; font-size: 12px; line-height: 1.25; }
  .store .tag { left: 50%; right: auto; top: auto; bottom: 6%; transform: translateX(-50%) rotate(var(--flip, 0deg)); }
}

/* パズルの結果: 自分の点と最大点 */
.result.pz #scoreOp { color: var(--ink-soft); }
.result.pz .dash { font-family: var(--font-body); font-size: 16px; font-weight: 700; }
