:root {
  --bg: #f3f6fb;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #cbd5e1;
  --thick: #334155;
  --primary: #1d4ed8;
  --user: #1e3a8a;
  --hl: #3b82f6;
  --hl-soft: #dbeafe;
  --given-bg: #eef2f7;
  --err: #dc2626;
  --err-bg: #fee2e2;
  --size: min(94vw, 58vh, 560px);
}
@supports (height: 100dvh) {
  :root { --size: min(94vw, calc(100dvh - 290px), 560px); }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--ink); }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  overscroll-behavior-y: none;
  -webkit-user-select: none;
  user-select: none;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
svg { display: block; }
img { -webkit-user-drag: none; }

.screen { display: none; flex-direction: column; align-items: center; min-height: 100vh; }
.screen.active { display: flex; }

/* ---------- menu ---------- */
#menu { padding: 20px 16px 40px; gap: 18px; max-width: 640px; margin: 0 auto; }
.menu-head { display: flex; align-items: center; gap: 14px; width: 100%; }
.menu-head img { border-radius: 14px; box-shadow: 0 4px 14px rgba(29, 78, 216, .25); }
.menu-head h1 { margin: 0; font-size: 26px; }
.menu-head p { margin: 2px 0 0; color: var(--muted); }

.block { width: 100%; }
.block h2 { font-size: 14px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 8px; }
.card { background: var(--card); border-radius: 14px; padding: 12px 16px; box-shadow: 0 1px 3px rgba(15, 23, 42, .08); }
.card summary { font-weight: 600; cursor: pointer; }
.check { display: flex; align-items: center; gap: 10px; padding: 10px 0 2px; }
.check input { width: 20px; height: 20px; accent-color: var(--primary); }
.help { padding-left: 18px; line-height: 1.5; color: #334155; margin: 10px 0 4px; }

.resume {
  width: 100%; border: 0; border-radius: 14px; padding: 14px 16px; text-align: left;
  background: linear-gradient(135deg, #1d4ed8, #3b82f6); color: #fff;
  display: flex; flex-direction: column; gap: 2px; box-shadow: 0 6px 16px rgba(29, 78, 216, .3);
}
.resume span { font-weight: 700; font-size: 17px; }
.resume small { opacity: .9; }

.segmented { display: grid; grid-template-columns: 1fr 1fr; width: 100%; background: #e2e8f0; border-radius: 12px; padding: 4px; gap: 4px; }
.segmented button { border: 0; border-radius: 9px; padding: 11px 8px; background: transparent; font-weight: 600; color: #475569; }
.segmented button.on { background: var(--card); color: var(--primary); box-shadow: 0 1px 3px rgba(15, 23, 42, .12); }

.themes { display: flex; gap: 10px; overflow-x: auto; padding: 2px 2px 6px; scroll-snap-type: x mandatory; }
.theme {
  flex: 0 0 170px; scroll-snap-align: start; border: 2px solid transparent; border-radius: 14px;
  background: var(--card); padding: 10px; text-align: left; display: flex; flex-direction: column; gap: 4px;
  box-shadow: 0 1px 3px rgba(15, 23, 42, .08);
}
.theme.on { border-color: var(--hl); background: #f5f9ff; }
.theme .thumbs { display: flex; gap: 4px; margin-bottom: 4px; }
.theme .thumbs img { width: 44px; height: 44px; border-radius: 9px; object-fit: cover; background: #e2e8f0; }
.t-name { font-weight: 700; }
.t-desc { font-size: 12px; color: var(--muted); line-height: 1.3; }

.diff-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.diff {
  border: 0; border-radius: 14px; background: var(--card); padding: 14px; text-align: left;
  display: flex; flex-direction: column; gap: 2px; box-shadow: 0 1px 3px rgba(15, 23, 42, .08);
  transition: transform .1s;
}
.diff:active { transform: scale(.97); }
.d-label { font-weight: 700; font-size: 17px; color: var(--primary); }
.d-sub { font-size: 12px; color: var(--muted); }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
.gallery figure { margin: 0; background: var(--card); border-radius: 12px; padding: 6px; box-shadow: 0 1px 3px rgba(15, 23, 42, .08); }
.gallery img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 8px; display: block; }
.gallery figcaption { font-size: 12px; padding: 4px 2px 0; line-height: 1.25; }
.gallery figcaption span { display: block; color: var(--muted); }

/* ---------- spel ---------- */
#game { padding: 8px 0 16px; gap: 10px; justify-content: center; }
.game-head { width: var(--size); display: flex; align-items: center; gap: 8px; min-height: 44px; }
.icon-btn { border: 0; background: var(--card); border-radius: 12px; width: 42px; height: 42px; display: grid; place-items: center; box-shadow: 0 1px 3px rgba(15, 23, 42, .1); }
.icon-btn svg, .tool svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.g-title { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.g-title strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.g-title span { font-size: 13px; color: var(--muted); }
.g-stats { display: flex; flex-direction: column; align-items: flex-end; font-variant-numeric: tabular-nums; }
#timer { font-weight: 700; font-size: 18px; }
#mistakes { font-size: 12px; color: var(--muted); }

.board {
  width: var(--size); height: var(--size);
  display: grid; grid-template-columns: repeat(9, 1fr); grid-template-rows: repeat(9, 1fr);
  gap: 1px; background: var(--line); border: 3px solid var(--thick); border-radius: 10px; overflow: hidden;
  box-shadow: 0 6px 20px rgba(15, 23, 42, .1);
}
.cell {
  position: relative; background: #fff; display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--size) / 9 * .58); line-height: 1; cursor: pointer; touch-action: manipulation;
  transition: background-color .12s; min-width: 0; min-height: 0; overflow: hidden;
}
.board { position: relative; }
.board::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 2;
  background:
    linear-gradient(to right, transparent calc(33.333% - 1.5px), var(--thick) 0, var(--thick) calc(33.333% + 1.5px), transparent 0),
    linear-gradient(to right, transparent calc(66.666% - 1.5px), var(--thick) 0, var(--thick) calc(66.666% + 1.5px), transparent 0),
    linear-gradient(to bottom, transparent calc(33.333% - 1.5px), var(--thick) 0, var(--thick) calc(33.333% + 1.5px), transparent 0),
    linear-gradient(to bottom, transparent calc(66.666% - 1.5px), var(--thick) 0, var(--thick) calc(66.666% + 1.5px), transparent 0);
}
.cell .val { color: var(--user); font-weight: 500; }
.cell.given { background: var(--given-bg); }
.cell.given .val { color: var(--ink); font-weight: 700; }
.cell.hint .val { color: #7c3aed; font-weight: 700; }
.cell.hint { background: #f3efff; }
.cell.error { background: var(--err-bg); }
.cell.error .val { color: var(--err); }

.notes { position: absolute; inset: 1px; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); }
.note {
  display: flex; align-items: center; justify-content: center; min-width: 0; min-height: 0;
  font-size: calc(var(--size) / 9 * .27); color: #475569; border-radius: 50%; margin: 1px;
}
.board.pictures .note { border-radius: 4px; padding: 1.5px; }
.note img { width: 100%; height: 100%; object-fit: cover; border-radius: 3px; display: block; }

.pic { width: 100%; height: 100%; padding: 9%; display: block; }
.pic img { width: 100%; height: 100%; object-fit: cover; border-radius: 18%; display: block; }
.board.pictures .cell.given .pic img { filter: saturate(.85); }
.board.pictures .cell.error .pic { padding: 12%; }

.board.finished .cell { animation: pop .5s ease both; }
.board.finished .cell:nth-child(3n) { animation-delay: .05s; }
.board.finished .cell:nth-child(3n+1) { animation-delay: .1s; }
@keyframes pop { 50% { transform: scale(.9); } }
.cell.shake { animation: shake .3s; }
@keyframes shake { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }
.cell.flash { animation: flash .8s; }
@keyframes flash { 0%, 100% { filter: none; } 40% { filter: brightness(1.25) drop-shadow(0 0 6px #a78bfa); } }

.caption { width: var(--size); min-height: 20px; text-align: center; font-size: 14px; color: var(--muted); }
.caption b { color: var(--ink); }

.tools { width: var(--size); display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.tool {
  border: 0; background: var(--card); border-radius: 12px; padding: 8px 4px; display: flex; flex-direction: column;
  align-items: center; gap: 3px; font-size: 12px; color: #334155; box-shadow: 0 1px 3px rgba(15, 23, 42, .1);
}
.tool.on { background: var(--primary); color: #fff; }
.tool:disabled { opacity: .4; }

.pad { width: var(--size); display: grid; grid-template-columns: repeat(9, 1fr); gap: 4px; }
.pad-btn {
  border: 0; border-radius: 10px; background: var(--card); padding: 6px 0 4px; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px; box-shadow: 0 1px 3px rgba(15, 23, 42, .12);
  transition: transform .08s, background-color .12s; min-width: 0;
}
.pad-btn:active { transform: scale(.94); }
.pad-btn .face { font-size: calc(var(--size) / 9 * .6); line-height: 1.1; color: var(--primary); font-weight: 600; }
.pad.pictures .pad-btn .face { width: 86%; aspect-ratio: 1; }
.pad-btn .face img { width: 100%; height: 100%; object-fit: cover; border-radius: 6px; display: block; }
.pad-btn .left { font-size: 11px; color: var(--muted); }
.pad-btn.active { background: var(--hl); box-shadow: 0 0 0 2px rgba(0, 0, 0, .25) inset, 0 3px 8px rgba(0, 0, 0, .25); }
.pad-btn.active .face, .pad-btn.active .left { color: #fff; }
.pad-btn.done { opacity: .35; }
.pad.pencil .pad-btn .face { font-size: calc(var(--size) / 9 * .42); font-weight: 500; }
.pad.pencil.pictures .pad-btn .face { width: 64%; }
.pad.pencil .pad-btn:not(.active) { background: #f8fafc; }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px; z-index: 10; color: #fff; padding: 20px;
}
.overlay[hidden] { display: none; }
.spinner { width: 44px; height: 44px; border-radius: 50%; border: 4px solid rgba(255, 255, 255, .3); border-top-color: #fff; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.dialog { background: var(--card); color: var(--ink); border-radius: 20px; padding: 24px; width: min(360px, 100%); text-align: center; box-shadow: 0 20px 50px rgba(0, 0, 0, .3); }
.dialog h2 { margin: 6px 0; }
.dialog p { color: #334155; line-height: 1.5; }
.confetti { font-size: 44px; }
.record { color: #16a34a; font-weight: 700; }
.dialog-actions { display: flex; gap: 10px; margin-top: 12px; }
.btn { flex: 1; border: 0; border-radius: 12px; padding: 12px; background: var(--primary); color: #fff; font-weight: 600; }
.btn.secondary { background: #e2e8f0; color: var(--ink); }
.btn.danger { background: #fee2e2; color: var(--err); }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translate(-50%, 30px);
  background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; opacity: 0;
  pointer-events: none; transition: all .25s; z-index: 20; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-height: 800px) and (min-width: 600px) {
  #game { padding-top: 24px; gap: 14px; }
}
