:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece5;
  --ink: #1d1f24;
  --ink-2: #5b5f69;
  --ink-3: #8f939c;
  --line: #d9d5cc;
  --line-strong: #2a2d33;
  --accent: #2f6fde;
  --accent-ink: #ffffff;
  --accent-soft: #dbe7fb;
  --peer: #eeeae2;
  --same: #c9dbf8;
  --given: #1d1f24;
  --entry: #2f6fde;
  --hinted: #0f8a5f;
  --bad: #d23c3c;
  --bad-soft: #fbe1e1;
  --easy: #2e9d6a;
  --medium: #2f6fde;
  --hard: #c98a12;
  --expert: #d0582b;
  --extreme: #9b2fae;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 6px 20px rgba(0,0,0,.06);
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181d;
    --surface: #1f2228;
    --surface-2: #2a2e36;
    --ink: #eceef2;
    --ink-2: #a9aeb8;
    --ink-3: #737985;
    --line: #353a44;
    --line-strong: #a9aeb8;
    --accent: #6b9cf0;
    --accent-ink: #0d1220;
    --accent-soft: #23314b;
    --peer: #262a32;
    --same: #2d4166;
    --given: #eceef2;
    --entry: #82adf5;
    --hinted: #4cc492;
    --bad: #f07070;
    --bad-soft: #4a2427;
    --easy: #4cc492;
    --medium: #6b9cf0;
    --hard: #e3ad3c;
    --expert: #ef8156;
    --extreme: #c77ad6;
    --shadow: 0 1px 2px rgba(0,0,0,.3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none; user-select: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
.mono { font-variant-numeric: tabular-nums; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.screen { display: none; max-width: 560px; margin: 0 auto; padding: calc(12px + var(--sat)) 16px calc(16px + var(--sab)); min-height: 100%; }
.screen.active { display: flex; flex-direction: column; }

/* ---------- buttons ---------- */
.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 12px;
  padding: 12px 18px; font-weight: 600; min-height: 46px;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.wide { width: 100%; }
.btn:active { transform: scale(.98); }
.icon-btn { width: 44px; height: 44px; border-radius: 12px; border: 0; background: transparent; display: grid; place-items: center; }
.icon-btn:active { background: var(--surface-2); }

/* ---------- home ---------- */
.home-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 8px 0 14px; }
h1 { margin: 0; font-size: 28px; letter-spacing: -.02em; }
h1 span { color: var(--accent); }
.sub { margin: 2px 0 0; color: var(--ink-2); font-size: 14px; }
.player-chip { display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 4px 14px 4px 4px; max-width: 55%; min-height: 44px; }
.avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 700; flex: none; }
.pname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.progress-bar { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin-bottom: 16px; }
.progress-bar div { height: 100%; width: 0; background: var(--accent); transition: width .4s; }

.band { margin-top: 22px; }
.band h3 { display: flex; align-items: baseline; gap: 8px; margin: 0 0 10px; font-size: 15px; }
.band h3 .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--c); align-self: center; }
.band h3 small { color: var(--ink-3); font-weight: 500; }
.band h3 .count { margin-left: auto; color: var(--ink-2); font-weight: 500; font-size: 13px; }
.levels { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.lvl {
  position: relative; aspect-ratio: 1 / 1; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 2px; min-width: 0;
}
.lvl .n { font-weight: 700; font-size: 18px; line-height: 1.1; }
.lvl .t { font-size: 11px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.lvl.solved { border-color: var(--c); background: color-mix(in srgb, var(--c) 12%, var(--surface)); }
.lvl.solved .t { color: var(--c); font-weight: 600; }
.lvl.locked { opacity: .45; }
.lvl.locked .n { font-weight: 500; }
.lvl.next { border: 2px solid var(--c); }
.lvl .ip { position: absolute; top: 5px; right: 5px; width: 7px; height: 7px; border-radius: 50%; background: var(--c); }
.lvl svg { width: 14px; height: 14px; color: var(--ink-3); }
.home-foot { text-align: center; color: var(--ink-3); font-size: 12px; margin: 28px 0 8px; }

/* ---------- game ---------- */
.game-head { display: flex; align-items: center; justify-content: space-between; }
.game-title { text-align: center; font-weight: 700; font-size: 18px; }
.band-tag { font-size: 12px; font-weight: 600; color: var(--c); text-transform: uppercase; letter-spacing: .06em; }
.stat-row { display: flex; justify-content: space-around; margin: 6px 0 10px; }
.stat { display: flex; flex-direction: column; align-items: center; }
.stat .label { font-size: 11px; color: var(--ink-3); text-transform: uppercase; letter-spacing: .06em; }
.stat .val { font-size: 18px; font-weight: 600; }

.board-wrap { position: relative; width: min(100%, 62vh, 520px); margin: 0 auto; }
#board {
  display: grid; grid-template-columns: repeat(9, 1fr);
  aspect-ratio: 1 / 1; width: 100%;
  border: 2px solid var(--line-strong); border-radius: 6px; overflow: hidden;
  background: var(--surface);
}
.cell {
  position: relative; display: grid; place-items: center;
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  font-size: clamp(18px, 6vw, 30px); font-weight: 500; color: var(--entry);
  background: var(--surface); padding: 0; border-top: 0; border-left: 0; border-radius: 0;
  min-width: 0; min-height: 0;
}
.cell:nth-child(9n) { border-right: 0; }
.cell:nth-child(9n+3), .cell:nth-child(9n+6) { border-right: 2px solid var(--line-strong); }
.cell:nth-child(n+19):nth-child(-n+27), .cell:nth-child(n+46):nth-child(-n+54) { border-bottom: 2px solid var(--line-strong); }
.cell:nth-child(n+73) { border-bottom: 0; }
.cell.given { color: var(--given); font-weight: 650; }
.cell.hinted { color: var(--hinted); font-weight: 650; }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.sel { background: var(--accent-soft); box-shadow: inset 0 0 0 2px var(--accent); }
.cell.bad { color: var(--bad); background: var(--bad-soft); }
.cell.flash { animation: flash .9s ease; }
@keyframes flash { 0%, 100% { } 30% { background: color-mix(in srgb, var(--hinted) 35%, var(--surface)); } }
.notes { position: absolute; inset: 1px; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); }
.notes i { font-style: normal; font-size: clamp(8px, 2.3vw, 12px); color: var(--ink-2); display: grid; place-items: center; line-height: 1; }
.notes i.hl { color: var(--accent); font-weight: 700; }

.pause-cover {
  position: absolute; inset: 0; background: var(--surface); border-radius: 6px; border: 2px solid var(--line-strong);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; font-size: 22px; font-weight: 700;
}
.pause-cover[hidden] { display: none; }

.hint-msg { min-height: 22px; margin: 8px auto 0; font-size: 14px; color: var(--hinted); text-align: center; max-width: 520px; }
.tools { display: flex; justify-content: space-around; margin: auto auto 6px; width: min(100%, 520px); }
#game .pad { margin-bottom: 8px; }
.tool {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: transparent; border: 0; padding: 8px 10px; border-radius: 12px; color: var(--ink-2); font-size: 12px; min-width: 64px;
}
.tool:active { background: var(--surface-2); }
.tool[aria-pressed="true"] { color: var(--accent); }
.tool[aria-pressed="true"] b { color: var(--accent); }
.tool b { font-weight: 600; }
.tool:disabled { opacity: .35; cursor: default; }
.badge { position: absolute; top: 2px; right: 12px; font-style: normal; font-size: 11px; font-weight: 700; background: var(--accent); color: var(--accent-ink); border-radius: 999px; min-width: 18px; height: 18px; display: grid; place-items: center; padding: 0 4px; }
.badge.zero { background: var(--ink-3); }

.pad { display: grid; grid-template-columns: repeat(9, 1fr); gap: 6px; width: min(100%, 520px); margin: 4px auto 0; }
.key {
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px;
  padding: 8px 0 6px; display: flex; flex-direction: column; align-items: center; min-width: 0;
}
.key .d { font-size: 24px; font-weight: 600; color: var(--accent); line-height: 1.1; }
.key .r { font-size: 10px; color: var(--ink-3); }
.key.done { visibility: hidden; }
.key:active { background: var(--accent-soft); }
.notes-on .key .d { font-size: 18px; font-weight: 500; color: var(--ink-2); }

/* ---------- dialogs ---------- */
dialog {
  border: 0; border-radius: 20px; padding: 22px; width: min(92vw, 400px);
  background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
dialog::backdrop { background: rgba(10,12,16,.45); }
dialog h2 { margin: 0 0 14px; font-size: 20px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--ink-2); }
.field input {
  font: inherit; font-size: 17px; color: var(--ink); padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg); -webkit-user-select: text; user-select: text;
}
.field input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.err { color: var(--bad); font-size: 13px; min-height: 18px; margin: 6px 0; }
.row-btns { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.row-btns .btn { flex: 1 1 auto; }
#otherPlayers:not(:empty) { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 14px; }
#otherPlayers h4 { margin: 0 0 8px; font-size: 13px; color: var(--ink-2); font-weight: 600; }
.pl { display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px; border: 0; background: transparent; border-radius: 10px; text-align: left; }
.pl:active, .pl:hover { background: var(--surface-2); }
.pl .avatar { width: 30px; height: 30px; font-size: 14px; background: var(--surface-2); color: var(--ink); }
.pl small { margin-left: auto; color: var(--ink-3); }
.linkish { background: none; border: 0; color: var(--accent); font-weight: 600; padding: 8px 0; }

.win { text-align: center; }
.win-burst { font-size: 44px; color: var(--hard); line-height: 1; animation: pop .6s ease; }
@keyframes pop { 0% { transform: scale(.2) rotate(-40deg); opacity: 0; } 70% { transform: scale(1.2); } 100% { transform: none; } }
.win-time { font-size: 40px; font-weight: 700; margin: 0; }
.win-note { color: var(--ink-2); margin: 6px 0 18px; }
.win-note b { color: var(--easy); }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--sab)); transform: translate(-50%, 40px);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .25s; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-height: 700px) {
  .stat-row { margin: 2px 0 6px; }
  .key { padding: 5px 0 3px; }
  .tool { padding: 4px 8px; }
}
@media (min-width: 600px) {
  .levels { grid-template-columns: repeat(10, 1fr); }
  .lvl .n { font-size: 16px; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
