:root {
  --bg: #f4efe6;
  --fg: #2b2118;
  --muted: #6f6253;
  --card: #fffaf2;
  --border: #e2d6c3;
  --accent: #b5482b;
  --accent-fg: #fff;
  --board: #dcb167;
  --board-edge: #b88a3e;
  --grid: #4a3418;
  --shadow: 0 2px 10px rgba(60, 40, 10, 0.12);
  --good: #17784a;
  --danger: #c0281c;
  --pip-empty: rgba(111, 98, 83, 0.55);
  --stone-ring: transparent; /* outline for black stone icons (visible in dark mode) */
  /* v2 かべ: a hatched grey rounded square (never a round disc) */
  --wall-fill: #8f969e;
  --wall-hatch: #6b7178;
  --wall-edge: #4a4f55;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1d1a17;
    --fg: #efe6d8;
    --muted: #b3a593;
    --card: #2a2622;
    --border: #3d3630;
    --accent: #e07a54;
    --accent-fg: #1d1a17;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
    --good: #5fd39a;
    --danger: #ff7a6b;
    --pip-empty: rgba(179, 165, 147, 0.55);
    --stone-ring: rgba(179, 165, 147, 0.85);
    --wall-fill: #6e757d;
    --wall-hatch: #a3a9b0;
    --wall-edge: #c4c9ce;
  }
}
:root[data-theme="dark"] {
  --bg: #1d1a17;
  --fg: #efe6d8;
  --muted: #b3a593;
  --card: #2a2622;
  --border: #3d3630;
  --accent: #e07a54;
  --accent-fg: #1d1a17;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
  --good: #5fd39a;
  --danger: #ff7a6b;
  --pip-empty: rgba(179, 165, 147, 0.55);
  --stone-ring: rgba(179, 165, 147, 0.85);
  --wall-fill: #6e757d;
  --wall-hatch: #a3a9b0;
  --wall-edge: #c4c9ce;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", Meiryo, system-ui, sans-serif;
  line-height: 1.6;
}

.site-header { text-align: center; padding: 18px 16px 4px; }
.site-header h1 { margin: 0; font-size: 1.7rem; letter-spacing: 0.08em; }
.tagline { margin: 2px 0 0; color: var(--muted); font-size: 0.95rem; }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 20px;
  max-width: 1080px;
  margin: 0 auto;
  padding: 12px 16px 24px;
  align-items: start;
}
@media (max-width: 860px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
}

.board-wrap { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.status-bar {
  display: flex; align-items: center; gap: 10px; justify-content: center;
  font-size: 1.15rem; font-weight: 600; min-height: 2rem;
}
.stone-icon {
  width: 22px; height: 22px; border-radius: 50%; display: inline-block;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}
.stone-icon.black {
  background: radial-gradient(circle at 35% 30%, #666, #111 60%);
  box-shadow: 0 0 0 1.5px var(--stone-ring), 0 1px 2px rgba(0, 0, 0, 0.4);
}
.stone-icon.white { background: radial-gradient(circle at 35% 30%, #fff, #d6d6d6 70%); }
.stone-icon.none { display: none; }

.board-holder { position: relative; width: 100%; max-width: 680px; margin: 0 auto; }
/* Two-column layout: the board also fits the viewport height, so the note under it (wall
   step, previews, ◆ answers, hints) stays in view on a 1366x768 / 1280x800 laptop. */
@media (min-width: 861px) {
  .board-holder { max-width: max(440px, min(680px, calc(100vh - 205px))); }
}
#board {
  display: block; width: 100%; height: auto; aspect-ratio: 1 / 1;
  border-radius: 10px; box-shadow: var(--shadow); touch-action: manipulation; cursor: pointer;
  -webkit-tap-highlight-color: transparent; /* no whole-board flash on every tap */
}

/* Status line and result banner sit above the board; the capture toast briefly
   covers them (it never covers the board). */
.top-bar { position: relative; display: flex; justify-content: center; min-height: 2rem; }
.top-bar.toasting .status-bar { visibility: hidden; }
.result {
  display: flex; align-items: center; gap: 12px; width: 100%; max-width: 680px;
  background: var(--card); color: var(--fg); border: 1px solid var(--border);
  border-radius: 12px; padding: 6px 8px 6px 14px; box-shadow: var(--shadow);
}
.result .stone-icon { width: 26px; height: 26px; flex: none; }
.result-body { flex: 1 1 auto; min-width: 0; }
.result p { margin: 0; }
.result-main { font-size: 1.25rem; font-weight: 700; line-height: 1.3; }
.result-reason { font-size: 0.9rem; color: var(--muted); line-height: 1.3; }
.result button { flex: none; }
.hidden { display: none !important; }

.scoreboard { display: flex; justify-content: center; gap: 16px; flex-wrap: wrap; color: var(--muted); font-size: 0.95rem; }
.scoreboard:empty { display: none; }

.panel { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.card {
  background: var(--card); border: 1px solid var(--border); border-radius: 12px;
  padding: 14px 16px; box-shadow: var(--shadow);
}
.card h2 { margin: 0 0 8px; font-size: 1.05rem; }
.controls { display: grid; gap: 10px; }
.controls label { display: grid; gap: 4px; font-size: 0.9rem; color: var(--muted); }
select, button {
  font: inherit; color: var(--fg); background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 10px;
}
button { cursor: pointer; font-weight: 600; }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.buttons button { flex: 1 1 auto; }

.rules { margin: 0; padding-left: 1.3em; font-size: 0.92rem; }
.rules li { margin: 3px 0; }
.history {
  margin: 0; padding: 0; list-style: none; max-height: 240px; overflow-x: hidden; overflow-y: auto;
  font-size: 0.9rem; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
  column-gap: 10px; align-content: start; font-variant-numeric: tabular-nums;
}
.history li { min-width: 0; padding-left: 2.4em; text-indent: -2.4em; } /* wrapped tags hang under the move */
.history .num { display: inline-block; min-width: 2.1em; text-indent: 0; text-align: right; color: var(--muted); }
.history .ev { color: var(--accent); font-size: 0.8rem; white-space: nowrap; }
.history li.end { grid-column: 1 / -1; color: var(--accent); font-weight: 700; padding-left: 0.4em; text-indent: 0; }

/* ---- v1 はさみ五目: notes, toast, scoreboard pips, legend, rules extras ---- */
.note { text-align: center; font-size: 0.92rem; line-height: 1.45; margin-top: -4px; }
.note.reserve { min-height: 1.45em; }
.note .warn { color: var(--danger); font-weight: 700; }
.note .gain { color: var(--good); font-weight: 700; }
.note .tap, .note .info { color: var(--muted); }

.toast {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  background: rgb(30, 22, 14); color: #fff; font-weight: 700; font-size: 1.05rem; line-height: 1.4;
  padding: 3px 16px; border-radius: 999px; pointer-events: none; white-space: nowrap;
  /* Never wider than the screen: a long stone + wall toast wraps instead (phones). */
  width: max-content; max-width: min(calc(100vw - 24px), 660px);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3); animation: toast-in 0.2s ease-out;
}
@media (max-width: 560px) {
  .toast { white-space: normal; text-align: center; border-radius: 14px; line-height: 1.3; padding: 3px 12px; }
}
@keyframes toast-in { from { transform: translate(-50%, -50%) scale(0.9); } to { transform: translate(-50%, -50%) scale(1); } }

.scoreboard.pips-mode { flex-direction: column; align-items: center; gap: 4px; color: var(--muted); }
.score-title { font-size: 0.85rem; }
.score-row {
  display: flex; align-items: center; gap: 8px; padding: 3px 12px; border-radius: 999px;
  border: 1px solid transparent;
}
.score-row.turn { border-color: var(--border); background: var(--card); }
.score-row .stone-icon { width: 18px; height: 18px; flex: none; }
.pips { display: inline-flex; gap: 3px; }
.pip {
  width: 13px; height: 13px; border-radius: 50%; border: 1.5px solid var(--pip-empty); flex: none;
}
.pip.w { background: radial-gradient(circle at 35% 30%, #fff, #d2d2d2 70%); border-color: #8f8f8f; }
.pip.b { background: radial-gradient(circle at 35% 30%, #777, #111 60%); border-color: #000; }
.pip.handi { background: repeating-linear-gradient(45deg, #ecdcb4 0 2px, #c6a868 2px 4px); border-color: #9b7b3c; }
.pip.new { animation: pip-pop 0.7s ease-out; }
@keyframes pip-pop { 0% { transform: scale(1.9); box-shadow: 0 0 0 4px rgba(240, 180, 40, 0.6); } 100% { transform: scale(1); box-shadow: none; } }
.score-num { font-weight: 700; color: var(--fg); font-variant-numeric: tabular-nums; min-width: 3.2em; text-align: right; }
.score-rest { font-size: 0.85rem; min-width: 4.2em; }
.score-rest.done { color: var(--accent); font-weight: 700; }
.handi-tag { font-size: 0.72rem; border: 1px solid var(--border); border-radius: 6px; padding: 0 4px; }

.legend {
  display: flex; flex-wrap: wrap; justify-content: center; gap: 4px 14px;
  font-size: 0.8rem; color: var(--muted);
}
.legend span { display: inline-flex; align-items: center; gap: 4px; }
.lg { display: inline-block; width: 13px; height: 13px; border-radius: 50%; font-style: normal; flex: none; }
.lg-gain { border: 2.5px solid #17a35e; }
.lg-loss { color: #d62a1e; font-weight: 900; text-align: center; line-height: 13px; font-size: 15px; border-radius: 0; }
.lg-threat { border: 2px dashed #d62a1e; }
.lg-must { background: #d62a1e; border-radius: 1px; width: 10px; height: 10px; margin: 0 2px; transform: rotate(45deg); box-shadow: 0 0 0 1.5px #fff; }
.lg-warn { background: #f08c00; width: 11px; height: 11px; }

.buttons label.check {
  display: inline-flex; align-items: center; gap: 4px; flex: 0 0 auto; color: var(--fg);
  font-size: 0.9rem; cursor: pointer; padding: 0 2px;
}
.buttons label.check input { width: 18px; height: 18px; margin: 0; accent-color: var(--accent); }
.actions-slot:empty { display: none; }
.actions-slot:not(:empty) + .note { margin-top: -2px; }
.actions-slot .buttons { justify-content: center; gap: 6px; }
.actions-slot .buttons button { flex: 0 1 auto; padding: 7px 12px; }

.rules-diagram {
  margin: 8px 0 4px; padding: 7px 10px; background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  font-size: 0.85rem; line-height: 1.5;
}
.rules-diagram .row { display: flex; flex-wrap: wrap; align-items: baseline; column-gap: 10px; }
.rules-diagram .shape {
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Noto Sans Mono CJK JP", monospace;
  white-space: nowrap; letter-spacing: 0.02em;
}
.rules-diagram .cap { color: var(--muted); font-size: 0.8rem; white-space: nowrap; margin-left: auto; }
.rules-diagram .cap.long { white-space: normal; text-align: right; }
.rules-diagram .cap.ok { color: var(--good); font-weight: 600; }
.rules-tip { margin: 6px 0 2px; font-size: 0.88rem; color: var(--muted); }
.rules-more { margin-top: 6px; font-size: 0.92rem; }
.rules-more summary { cursor: pointer; color: var(--accent); font-weight: 600; }
.rules-more ol { margin-top: 4px; }

/* ---- v2 かべはさみ五目: wall button, wall pips, assist levels, legend extras ---- */
.wall-pips { display: inline-flex; gap: 3px; vertical-align: -1px; margin-left: 2px; }
.wpip {
  display: inline-block; width: 12px; height: 12px; border-radius: 2.5px; flex: none;
  background: repeating-linear-gradient(45deg, var(--wall-fill) 0 2.5px, var(--wall-hatch) 2.5px 4px);
  box-shadow: inset 0 0 0 1px var(--wall-edge);
}
.wpip.used { background: transparent; box-shadow: inset 0 0 0 1.5px var(--pip-empty); }
#actions button { min-height: 44px; }
.wall-btn { white-space: nowrap; }
.wall-btn[aria-pressed="true"] {
  background: #3d4247; color: #fff; border-color: #3d4247;
  box-shadow: 0 0 0 2px var(--wall-fill);
}
.wall-btn[aria-pressed="true"] .wpip { box-shadow: inset 0 0 0 1px #fff; }
.wall-btn[aria-pressed="true"] .wpip.used { box-shadow: inset 0 0 0 1.5px rgba(255, 255, 255, 0.6); }
.wall-btn.pulse { animation: wall-pulse 1.2s ease-out 1; }
@keyframes wall-pulse {
  0% { box-shadow: 0 0 0 0 rgba(214, 42, 30, 0.7); }
  100% { box-shadow: 0 0 0 12px rgba(214, 42, 30, 0); }
}
/* In かべモード the board gets a subtle grey dashed frame. */
.board-holder.wall-mode #board { outline: 3px dashed var(--wall-edge); outline-offset: 3px; }

.seg {
  display: inline-flex; align-items: stretch; flex: 0 1 auto; max-width: 100%; border: 1px solid var(--border); border-radius: 8px;
  overflow: hidden; background: var(--bg);
}
.seg .seg-label { display: inline-flex; align-items: center; padding: 0 6px; font-size: 0.85rem; color: var(--muted); white-space: nowrap; }
#actions .seg button {
  flex: 0 0 auto; border: 0; border-left: 1px solid var(--border); border-radius: 0; padding: 6px 8px;
  font-weight: 600; background: transparent;
}
#actions .seg button[aria-checked="true"] { background: var(--accent); color: var(--accent-fg); }

/* With かべ the row becomes a 2-line grid: the wall pips sit under the capture pips. */
.score-row.has-walls {
  display: grid; grid-template-columns: repeat(5, auto); align-items: center; column-gap: 8px; row-gap: 0;
  border-radius: 14px; justify-content: start;
}
.score-walls {
  grid-column: 2 / -1; display: flex; align-items: center; gap: 4px;
  font-size: 0.8rem; line-height: 1.3;
}
.score-walls .wpip { width: 11px; height: 11px; }

.lg-poison {
  width: 0; height: 0; border-radius: 0; border-left: 6px solid transparent; border-right: 6px solid transparent;
  border-bottom: 11px solid #ffd400; filter: drop-shadow(0 0 0.8px #2a1c00) drop-shadow(0 0 0.8px #2a1c00);
}
.lg-win { color: #17a35e; font-weight: 900; text-align: center; line-height: 13px; font-size: 15px; border-radius: 0; }
.lg-loss.n, .lg-gain.n { width: auto; }
/* ■ in the rules text, the diagram and the history: the board's hatched wall piece. */
.wpip.inline { width: 0.82em; height: 0.82em; vertical-align: -0.08em; margin: 0 0.09em; border-radius: 0.14em; }
.history .wpip.inline { margin-left: 0.15em; }
.nowrap { white-space: nowrap; }
.result-reason .nowrap { display: inline-block; }
.result-reason span + span { margin-left: 0.8em; }
/* Narrow banner: the かべ count gets its own line (never an indented wrap). */
@media (max-width: 560px) {
  .result-reason .nowrap { display: block; }
  .result-reason span + span { margin-left: 0; }
}
.note .wallnote { color: var(--fg); font-weight: 600; }
.note .tip { color: var(--danger); font-weight: 700; }

@media (max-width: 420px) {
  .score-row { gap: 6px; padding: 3px 8px; }
  .score-row.has-walls { column-gap: 6px; }
  .pip { width: 12px; height: 12px; }
  .toast { font-size: 0.95rem; }
  .result-main { font-size: 1.1rem; }
  .actions-slot .buttons button { padding: 7px 9px; }
  #actions .seg button { padding: 6px 9px; }
}

/* ---- v3 追いかべはさみ五目: wall toggle / wall step, wall indicator, ◆, リーチ ---- */
.wall-btn.skip {
  font-size: 1.02rem; padding-left: 16px; padding-right: 16px;
  box-shadow: 0 0 0 3px rgba(181, 72, 43, 0.25);
}
.status-bar.wall-step {
  background: var(--card); border: 2px dashed var(--wall-edge); border-radius: 999px; padding: 0 12px;
  font-size: 1.02rem;
}
.legend span.wide { flex-basis: 100%; justify-content: center; text-align: left; }
.scoreboard.big-walls .score-walls { font-size: 0.88rem; font-weight: 600; color: var(--fg); }
.scoreboard.big-walls .score-walls .wpip { width: 16px; height: 16px; border-radius: 3px; }
.walls-out { color: var(--danger); font-weight: 800; margin-left: 4px; }
.score-title.reach { color: var(--danger); font-weight: 800; font-size: 0.95rem; }
.score-row.reach .score-num { color: var(--danger); }
.wall-legend { text-align: center; font-size: 0.85rem; color: var(--muted); margin-top: -6px; line-height: 1.45; }
.note .next { color: #7b26c9; font-weight: 700; }
.lg-next {
  width: 10px; height: 10px; border-radius: 1px; margin: 0 2px; transform: rotate(45deg);
  border: 2.2px solid #8a2be2; background: rgba(138, 43, 226, 0.22); box-shadow: 0 0 0 1.5px #fff;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .note .next { color: #c99bff; }
}
:root[data-theme="dark"] .note .next { color: #c99bff; }

/* ---- v4 行き止まりはさみ五目: 2-line note, run outlines in the legend, ◇ in the diagram ---- */
.note .line { display: block; }
.lg-runred, .lg-rungreen { width: 22px; height: 11px; border-radius: 6px; border: 2px solid #d62a1e; }
.lg-rungreen { border-color: #17a35e; }
.rules-diagram .place {
  display: inline-block; opacity: 0.45; border-radius: 50%;
  outline: 1.5px dashed var(--accent); outline-offset: -1px; line-height: 1;
}
/* An empty point: a small grey board intersection, as wide as a stone glyph. */
.rules-diagram .empty {
  display: inline-block; width: 1em; height: 1em; vertical-align: -0.12em;
  background:
    linear-gradient(var(--muted), var(--muted)) center / 0.72em 1.5px no-repeat,
    linear-gradient(var(--muted), var(--muted)) center / 1.5px 0.72em no-repeat;
}
.rules-diagram .key { color: var(--muted); font-size: 0.78rem; text-align: right; margin-bottom: 2px; }
.score-card .legend { margin-top: 6px; gap: 2px 10px; }

/* Wide layouts: the scoreboard and the wall legend in the side panel (see placeScore). */
.score-card { padding: 10px 10px; }
.score-card .scoreboard.pips-mode { align-items: stretch; }
.score-card .score-title { text-align: center; }
.score-card .score-row { padding: 3px 6px; gap: 6px; }
.score-card .score-row.has-walls { column-gap: 6px; }
.score-card .pips { gap: 2px; }
.score-card .pip { width: 11px; height: 11px; }
.score-card .score-num { min-width: 2.7em; }
.score-card .score-rest { min-width: 0; font-size: 0.8rem; }
.score-card .wall-legend { margin-top: 6px; font-size: 0.8rem; }

.site-footer { text-align: center; padding: 8px 16px 24px; font-size: 0.85rem; }
.site-footer a { color: var(--muted); }

/* ---- v5 追いこみ取り: the dead-end pair key (a small × badge on a stone or wall) ---- */
.lg-deadend {
  width: 13px; height: 13px; border-radius: 50%; background: rgba(255, 255, 255, 0.92);
  box-shadow: inset 0 0 0 1.4px #d62a1e; color: #d62a1e; font-weight: 900; font-size: 10px;
  line-height: 13px; text-align: center;
}

/* ---- v6 あそびかた5問: offer / complete card, lesson buttons, rules-card button, level note ---- */
.offer {
  display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 6px 12px;
  width: 100%; max-width: 680px; margin: 0 auto; padding: 8px 12px;
  background: var(--card); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  border-radius: 12px; box-shadow: var(--shadow);
}
.offer.done { border-left-color: var(--good); }
.offer-text { margin: 0; font-weight: 700; font-size: 0.98rem; line-height: 1.4; flex: 1 1 14em; min-width: 0; }
.offer-buttons { display: flex; gap: 8px; flex: 0 0 auto; }
.offer-buttons button { min-height: 40px; }
.lesson-open { display: block; width: 100%; margin: 8px 0 2px; min-height: 44px; }
.lesson-open .lesson-count { font-variant-numeric: tabular-nums; margin-left: 0.4em; }
.rules-more .rules-diagram { margin-top: 6px; }
.level-note { font-size: 0.8rem; color: var(--muted); }
.note .goal { color: var(--fg); font-weight: 700; }
#l-retry.again { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.status-bar.lesson { font-size: 1.05rem; }
.status-bar.lesson.wall-step { font-size: 1.02rem; } /* as in real games: one line at 360 px */
/* The board stays inside a laptop viewport while a card sits above it. */
@media (min-width: 861px) {
  .board-wrap.offer-on .board-holder { max-width: max(440px, min(680px, calc(100vh - 272px))); }
  /* A lesson note can run to 3 lines (what happened, the reply, the advice): a little less board. */
  .board-wrap.lesson-on .board-holder { max-width: max(440px, min(680px, calc(100vh - 230px))); }
  .board-wrap.lesson-on.offer-on .board-holder { max-width: max(440px, min(680px, calc(100vh - 297px))); }
}
@media (max-width: 560px) {
  .offer { padding: 6px 10px; gap: 4px 8px; }
  .offer-text { font-size: 0.86rem; flex-basis: 100%; text-align: center; }
  .offer-buttons button { min-height: 36px; padding: 5px 16px; }
}
/* v6 review (L3): the toggle a stone + wall answer needs, after a miss or a hint press. */
.wall-btn.attn { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
/* v6 review: the tutorial button beside かべ, on narrow screens only. */
@media (min-width: 861px) { .lesson-quick { display: none; } }
