:root {
  --bg: #eef1e8;
  --card: #ffffff;
  --ink: #1f241c;
  --muted: #69715f;
  --line: #2b2a22;
  --piece: #4a2a17;
  --accent: #2f7a46;
  --accent-hover: #215a33;
  --accent-ink: #fff;
  --danger: #c8321f;
  --edge: #cdd5c2;
  --hover: #e5eadc;
  --well: #dfe5d5;
  --veil: rgba(238, 241, 232, .86);
  --text2: #333;
  --rule: #eee;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161a14; --card: #20261d; --ink: #e8ece2; --muted: #a2ab97; --line: #0c0e0a;
    --accent: #74c48d; --accent-hover: #97d6aa; --accent-ink: #0d2015; --danger: #ff7a63;
    --edge: #3a4334; --hover: #2a3125; --well: #2b3227; --veil: rgba(22, 26, 20, .86);
    --text2: #cfd6c6; --rule: #343c2f; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #161a14; --card: #20261d; --ink: #e8ece2; --muted: #a2ab97; --line: #0c0e0a;
  --accent: #74c48d; --accent-hover: #97d6aa; --accent-ink: #0d2015; --danger: #ff7a63;
  --edge: #3a4334; --hover: #2a3125; --well: #2b3227; --veil: rgba(22, 26, 20, .86);
  --text2: #cfd6c6; --rule: #343c2f; color-scheme: dark;
}
body { background: var(--bg); color: var(--ink); }
* { box-sizing: border-box; }
/* Phones: a quick second tap (empty → ✕ → bean) must not trigger double-tap zoom. Pinch zoom still works. */
html { touch-action: manipulation; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body { margin: 0; min-height: 100vh; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button, select { font: inherit; color: inherit; }

.app {
  max-width: 560px;
  margin: 0 auto;
  padding-block: 16px 40px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.top { display: flex; align-items: center; justify-content: space-between; }
h1 { margin: 0; font-size: 28px; letter-spacing: -0.5px; display: flex; align-items: center; gap: 8px; }

.bean {
  display: inline-block; width: 1em; height: 1em; vertical-align: -0.12em;
  background: currentColor;
  -webkit-mask: var(--bean) center / contain no-repeat;
  mask: var(--bean) center / contain no-repeat;
  --bean: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill-rule='evenodd' d='M7.6 2.5C4.4 2.9 2.4 5.9 2.6 9.6c.4 6.4 5.6 11.9 11.2 11.9 4.3 0 7.6-3 7.6-7.1 0-2.9-1.9-4.6-4.4-4.8-2-.2-2.9-1-3.6-2.6C12.4 4.4 10.4 2.1 7.6 2.5zM5.6 8.6c.2-1.8 1.4-3.1 2.9-3.2-1 .8-1.7 2-1.8 3.4z'/%3E%3C/svg%3E");
}

button {
  touch-action: manipulation; -webkit-tap-highlight-color: transparent;
  border: 1px solid var(--edge); background: var(--card); border-radius: 999px;
  padding: 8px 16px; cursor: pointer; transition: background .15s, transform .05s;
}
button:hover { background: var(--hover); }
button:active { transform: scale(.97); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.primary:hover { background: var(--accent-hover); }
button.ghost { background: transparent; }
button.small { padding: 4px 12px; font-size: 14px; }
button:disabled { opacity: .5; cursor: default; }

.controls { display: flex; flex-direction: column; gap: 10px; }
.controls .row { display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.seg { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--well); border-radius: 999px; padding: 4px; gap: 4px; }
.seg button { border: none; background: transparent; padding: 7px 0; font-weight: 500; }
.seg button[aria-checked="true"] { background: var(--card); box-shadow: 0 1px 3px rgba(0,0,0,.15); font-weight: 650; }
.size { display: flex; gap: 8px; align-items: center; color: var(--muted); }
select { border: 1px solid var(--edge); border-radius: 8px; padding: 6px 8px; background: var(--card); }

.status { display: flex; align-items: center; gap: 12px; }
.badge { padding: 4px 10px; border-radius: 999px; font-size: 13px; font-weight: 650; text-transform: uppercase; letter-spacing: .5px; background: var(--well); }
.badge.easy { background: #d4f0c8; color: #24631a; }
.badge.medium { background: #ffe7b8; color: #7a4d00; }
.badge.hard { background: #ffd0c4; color: #8a2408; }
.badge.expert { background: #e2d4ff; color: #4a1f99; }
.timer { margin-left: auto; font-variant-numeric: tabular-nums; font-size: 22px; font-weight: 650; }

.board-wrap {
  --board-max: 400px; /* desktop board size (was ~528px) */
  position: relative; aspect-ratio: 1; max-width: 100%; margin: 0 auto;
  width: min(100%, var(--board-max), calc(100svh - 300px));
  min-width: min(100%, 240px);
}
.board {
  display: grid; width: 100%; height: 100%;
  border: 3px solid var(--line); border-radius: 6px; overflow: hidden;
  touch-action: none; user-select: none; -webkit-user-select: none;
  background: var(--line);
}
.cell {
  -webkit-tap-highlight-color: transparent;
  position: relative; display: flex; align-items: center; justify-content: center;
  border-style: solid; border-color: var(--line); cursor: pointer;
}
.cell::after { content: ""; position: absolute; inset: 0; pointer-events: none; }
@media (hover: hover) { .cell:hover::after { background: rgba(255,255,255,.18); } }
.cell .q {
  width: 62%; height: 62%; background: var(--piece);
  -webkit-mask: var(--bean) center / contain no-repeat; mask: var(--bean) center / contain no-repeat;
  --bean: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill-rule='evenodd' d='M7.6 2.5C4.4 2.9 2.4 5.9 2.6 9.6c.4 6.4 5.6 11.9 11.2 11.9 4.3 0 7.6-3 7.6-7.1 0-2.9-1.9-4.6-4.4-4.8-2-.2-2.9-1-3.6-2.6C12.4 4.4 10.4 2.1 7.6 2.5zM5.6 8.6c.2-1.8 1.4-3.1 2.9-3.2-1 .8-1.7 2-1.8 3.4z'/%3E%3C/svg%3E");
  animation: pop .15s ease-out;
}
.cell .x { font-size: clamp(12px, 3.2vw, 20px); color: rgba(0,0,0,.55); line-height: 1; font-weight: 500; }
.cell .x.auto { color: rgba(0,0,0,.3); }
.cell.conflict::after {
  background: repeating-linear-gradient(45deg, rgba(217,48,37,.45) 0 4px, transparent 4px 9px);
}
.cell.conflict .q { background: var(--danger); }
.cell.hl { z-index: 1; }
.cell.hl::before {
  content: ""; position: absolute; inset: 2px; border: 3px solid var(--accent); border-radius: 4px;
  animation: pulse 1s ease-in-out infinite alternate; pointer-events: none;
}
.board.won .cell .q { animation: dance .6s ease-in-out both; animation-delay: var(--d, 0s); }

@keyframes pop { from { transform: scale(.4); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes pulse { from { opacity: .35; } to { opacity: 1; } }
@keyframes dance { 0%,100% { transform: scale(1); } 50% { transform: scale(1.35) rotate(-8deg); } }

.overlay {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: var(--veil); backdrop-filter: blur(8px); border-radius: 6px;
  font-size: 18px; font-weight: 600; text-align: center; padding: 20px;
}
.overlay.hidden { display: none; }
.spinner { width: 36px; height: 36px; border: 4px solid var(--edge); border-top-color: var(--accent); border-radius: 50%; margin: 0 auto 12px; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.toggle { margin-left: auto; display: flex; gap: 6px; align-items: center; cursor: pointer; color: var(--muted); }
.toggle { color: var(--ink); font-weight: 500; user-select: none; }
.toggle input {
  appearance: none; -webkit-appearance: none; margin: 0; cursor: pointer;
  width: 40px; height: 22px; border-radius: 999px; background: var(--edge);
  position: relative; transition: background .15s;
}
.toggle input::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px;
  border-radius: 50%; background: var(--card); box-shadow: 0 1px 2px rgba(0,0,0,.3); transition: transform .15s;
}
.toggle input:checked { background: var(--accent); }
.toggle input:checked::after { transform: translateX(18px); }
.hint { min-height: 1.4em; margin: 0; color: var(--accent); font-weight: 500; }
.hint.warn { color: var(--danger); }

.rules { background: var(--card); border-radius: var(--radius); padding: 12px 16px; }
.rules summary { cursor: pointer; font-weight: 600; }
.rules ul { padding-left: 20px; line-height: 1.5; color: var(--text2); }

.dlg { border: none; background: var(--card); color: var(--ink); border-radius: 16px; padding: 24px 28px; min-width: 300px; max-width: 92vw; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.dlg::backdrop { background: rgba(0,0,0,.35); }
.dlg h2 { margin: 0 0 8px; display: flex; gap: 8px; align-items: center; }
.dlg .big { font-size: 48px; font-weight: 700; margin: 8px 0; font-variant-numeric: tabular-nums; }
.dlg .muted { color: var(--muted); }
.dlg-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }
.stats { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
.stats th, .stats td { padding: 6px 10px; text-align: right; }
.stats th:first-child, .stats td:first-child { text-align: left; text-transform: capitalize; }
.stats tbody tr { border-top: 1px solid var(--rule); }

button:focus-visible, select:focus-visible, .toggle input:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; } }

/* phones: tighter layout so the whole game fits on one screen */
@media (max-width: 520px) {
  .app { gap: 10px; padding-block: 10px 28px; }
  h1 { font-size: 24px; }
  .seg button { padding: 8px 0; font-size: 15px; }
  .controls { gap: 8px; }
  .timer { font-size: 20px; }
  .tools { gap: 6px; }
  .tools button { padding: 8px 14px; }
  .hint { font-size: 14px; }
  .dlg { padding: 20px; min-width: 0; width: calc(100vw - 32px); }
  .dlg .big { font-size: 40px; }
  .board-wrap { width: min(100%, calc(100svh - 270px)); }
}

/* ---------- header with mode switch ---------- */
.top { flex-wrap: wrap; gap: 10px; }
.top-actions { display: flex; align-items: center; gap: 8px; }
.seg-mode { grid-template-columns: repeat(2, 1fr); }
.seg-mode button { padding: 6px 14px; }
.game, .levels { display: flex; flex-direction: column; gap: 14px; }
.game[hidden], .levels[hidden], .controls[hidden] { display: none; }

/* ---------- levels ---------- */
.levels-head { display: flex; flex-direction: column; gap: 4px; }
.progress { margin: 0; font-size: 18px; font-weight: 650; font-variant-numeric: tabular-nums; }
.sync-note { margin: 0; color: var(--muted); font-size: 14px; line-height: 1.4; max-width: 65ch; }
.pages { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: thin; }
.page-chip { flex: none; padding: 5px 12px; font-size: 13px; font-variant-numeric: tabular-nums; }
.page-chip.active { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.page-label { margin: 0; font-size: 13px; font-weight: 650; letter-spacing: .5px; text-transform: uppercase; color: var(--muted); }
.level-grid { display: grid; grid-template-columns: repeat(10, minmax(0, 1fr)); gap: 5px; }
.level-grid.waiting { opacity: .55; }
.tile {
  aspect-ratio: 1; padding: 0; border-radius: 8px; font-size: 14px; font-weight: 600;
  font-variant-numeric: tabular-nums; display: flex; align-items: center; justify-content: center;
  background: var(--card); border: 1px solid var(--edge); min-width: 0;
}
.tile.done { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.tile.open { background: var(--well); border: 2px dashed var(--accent); }
.tile.used { background: var(--well); color: var(--muted); text-decoration: line-through; }
.tile.next { box-shadow: 0 0 0 3px var(--accent); }
.tile:disabled { cursor: default; }
.legend { margin: 0; display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 12px; height: 12px; border-radius: 3px; display: inline-block; border: 1px solid var(--edge); background: var(--card); }
.dot.done { background: var(--accent); border-color: var(--accent); }
.dot.open { background: var(--well); border: 2px dashed var(--accent); }
.dot.used { background: var(--well); }
.dot.next { box-shadow: 0 0 0 2px var(--accent); }

/* ---------- level dialog & leaderboard ---------- */
.lvl-status { line-height: 1.45; max-width: 46ch; }
.leaderboard { margin-top: 8px; max-height: 46vh; overflow-y: auto; }
.lb-head { margin: 0 0 6px; font-weight: 600; }
.lb-note { margin: 0; }
.lb { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.lb li { display: grid; grid-template-columns: 2.2em 24px minmax(0, 1fr) auto; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; }
.lb li:nth-child(odd) { background: var(--well); }
.lb li.me { outline: 2px solid var(--accent); outline-offset: -2px; font-weight: 650; }
.lb li.lb-gap { display: block; text-align: center; color: var(--muted); background: none; padding: 0; }
.lb-rank { font-variant-numeric: tabular-nums; color: var(--muted); text-align: right; }
.lb-av { width: 24px; height: 24px; border-radius: 50%; object-fit: cover; }
.lb-av[hidden] { display: block; visibility: hidden; }
.lb-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-time { font-variant-numeric: tabular-nums; font-weight: 600; }
.lb-time small { font-weight: 400; color: var(--muted); }

@media (max-width: 520px) {
  h1 { font-size: 22px; }
  .seg-mode button { padding: 6px 10px; font-size: 14px; }
  .level-grid { gap: 4px; }
  .tile { font-size: 12px; border-radius: 6px; }
}

/* ---------- account & sign-in ---------- */
.account-btn { max-width: 11em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-btn.signed-in { background: var(--well); border-color: var(--well); font-weight: 600; }
.auth { width: min(420px, calc(100vw - 32px)); }
.auth-step { display: flex; flex-direction: column; gap: 10px; }
.auth-step[hidden] { display: none; }
.auth-step h2, .auth-step p { margin: 0; }
.auth-step .muted { line-height: 1.45; }
.field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 14px; }
.field input {
  font: inherit; font-weight: 400; font-size: 16px; color: var(--ink); background: var(--card);
  border: 1px solid var(--edge); border-radius: 10px; padding: 10px 12px; width: 100%;
}
.field input:focus-visible { outline: 3px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
.field input[aria-invalid="true"] { border-color: var(--danger); }
#authCode { font-size: 24px; letter-spacing: 6px; font-variant-numeric: tabular-nums; text-align: center; }
.field-msg { min-height: 1.3em; font-size: 14px; color: var(--muted); }
.field-msg.error { color: var(--danger); }
.suggest { font-size: 14px; }
.link-btn { border: none; background: none; padding: 0; color: var(--accent); text-decoration: underline; border-radius: 4px; font-size: 14px; }
.link-btn:hover { background: none; color: var(--accent-hover); }
.dlg-actions.spread { justify-content: space-between; align-items: center; flex-wrap: wrap; }
.actions-right { display: flex; gap: 10px; }
.account-info { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 6px 16px; margin: 4px 0; }
.account-info dt { color: var(--muted); }
.account-info dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }

/* ---------- password sign-in ---------- */
.field-group { display: flex; flex-direction: column; gap: 10px; }
.field-group[hidden] { display: none; }
.field-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.field-row .link-btn { font-weight: 400; }
.pass-wrap { position: relative; display: block; }
.pass-wrap input { padding-right: 64px; }
.pass-toggle { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); font-weight: 400; }
.auth-switch { font-size: 14px; color: var(--muted); text-align: center; }
.auth-switch[hidden] { display: none; }
.vh { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
#acctMsg:empty { display: none; }
