:root {
  --bg: #f5f6fb;
  --card: #ffffff;
  --text: #1b1e2e;
  --muted: #6a6f87;
  --line: #e1e4f0;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --accent-soft: #e8e7fd;
  --cell: #ffffff;
  --cell-line: #dfe2f0;
  --sel: #4f46e5;
  --danger: #d6336c;
  --warn-bg: #fff4d6;
  --warn-text: #7a5600;
  --found-l: 62%;
  --found-a: .55;
  --shadow: 0 6px 24px rgba(30, 35, 80, .10);
  --radius: 16px;
  --pad: 14px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #12141f; --card: #1c1f2f; --text: #eceefa; --muted: #9ba0bb; --line: #2b2f45;
  --accent: #7c75ff; --accent-text: #0e0f1a; --accent-soft: #2a2850; --cell: #1c1f2f; --cell-line: #2b2f45;
  --sel: #7c75ff; --danger: #ff6b9a; --warn-bg: #3d3216; --warn-text: #ffd978;
  --found-l: 42%; --found-a: .7; --shadow: 0 6px 24px rgba(0, 0, 0, .35);
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #12141f; --card: #1c1f2f; --text: #eceefa; --muted: #9ba0bb; --line: #2b2f45;
    --accent: #7c75ff; --accent-text: #0e0f1a; --accent-soft: #2a2850; --cell: #1c1f2f; --cell-line: #2b2f45;
    --sel: #7c75ff; --danger: #ff6b9a; --warn-bg: #3d3216; --warn-text: #ffd978;
    --found-l: 42%; --found-a: .7; --shadow: 0 6px 24px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
  overscroll-behavior: none;
}
button, select, input { font: inherit; color: inherit; }
h1, h2, h3, p { margin: 0; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ---------- screens ---------- */
.screen {
  display: none; min-height: 100dvh; width: 100%; max-width: 980px; margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + var(--pad)) calc(env(safe-area-inset-right) + var(--pad))
           calc(env(safe-area-inset-bottom) + var(--pad)) calc(env(safe-area-inset-left) + var(--pad));
}
.screen.active { display: block; }
#screen-game.active { display: flex; flex-direction: column; height: 100dvh; min-height: 0; overflow: hidden; }

/* ---------- buttons ---------- */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--card); border-radius: 12px;
  min-height: 44px; padding: 0 16px; font-weight: 600; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn.ghost { background: transparent; border-color: transparent; box-shadow: none; padding: 0 10px; color: var(--accent); }
.btn.icon { font-size: 1.2rem; min-width: 44px; padding: 0; }
.btn:disabled { opacity: .45; }
.btn:focus-visible, .variant:focus-visible, .continue:focus-visible, .seg button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- menu ---------- */
.hero { text-align: center; padding: 18px 0 14px; }
.hero h1 { font-size: clamp(2rem, 7vw, 3rem); font-weight: 800; letter-spacing: -.02em; }
.hero h1 span { color: var(--accent); }
.hero p { color: var(--muted); margin-top: 4px; }
.continue {
  width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 12px; text-align: left;
  background: var(--accent); color: var(--accent-text); border: 0; border-radius: var(--radius);
  padding: 14px 18px; margin-bottom: 12px; min-height: 64px; cursor: pointer; box-shadow: var(--shadow);
}
.continue small { display: block; opacity: .85; margin-top: 2px; }
.variant-grid { display: grid; grid-template-columns: 1fr; gap: 10px; }
@media (min-width: 640px) { .variant-grid { grid-template-columns: 1fr 1fr; gap: 14px; } }
.variant {
  position: relative; display: flex; align-items: center; gap: 14px; text-align: left; width: 100%;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; min-height: 76px; cursor: pointer; box-shadow: var(--shadow);
}
.variant:active { transform: scale(.98); }
.v-icon { font-size: 1.9rem; width: 44px; text-align: center; flex: none; }
.v-text strong { display: block; font-size: 1.05rem; }
.v-text small { display: block; color: var(--muted); margin-top: 2px; line-height: 1.25; }
.badge { position: absolute; top: 10px; right: 12px; font-size: .75rem; font-weight: 700; padding: 3px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.badge.ok { background: #d9f6e4; color: #11763d; }
.menu-foot { text-align: center; padding: 18px 0 6px; display: grid; gap: 10px; justify-items: center; }

/* ---------- bars ---------- */
.bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 48px; margin-bottom: 8px; }
.bar h2 { font-size: 1.15rem; }
.bar-spacer { width: 64px; }
.bar-right { display: flex; align-items: center; gap: 2px; }
.game-bar { margin-bottom: 6px; }
.game-title { text-align: center; min-width: 0; flex: 1; }
.game-title strong { display: block; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.game-title small { display: block; color: var(--muted); font-size: .78rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.timer { font-variant-numeric: tabular-nums; font-weight: 700; min-width: 3.2em; text-align: right; }
.timer.low { color: var(--danger); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: .45; } }

/* ---------- options ---------- */
.options { display: grid; gap: 12px; padding-bottom: 24px; }
@media (min-width: 760px) { .options { grid-template-columns: 1fr 1fr; align-items: start; } .options .note { grid-column: 1 / -1; } }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; display: grid; gap: 10px; box-shadow: var(--shadow); }
.card h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 44px; }
.row.stack { flex-direction: column; align-items: stretch; gap: 6px; min-height: 0; }
.row.disabled { opacity: .4; pointer-events: none; }
select {
  min-height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg);
  padding: 0 10px; max-width: 58%;
}
.seg { display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: transparent; min-height: 40px; border-radius: 9px; font-weight: 600; cursor: pointer; }
.seg button.on { background: var(--accent); color: var(--accent-text); }
.switch {
  appearance: none; -webkit-appearance: none; width: 52px; height: 32px; border-radius: 999px; flex: none;
  background: var(--line); position: relative; cursor: pointer; transition: background .15s; border: 0; margin: 0;
}
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .15s; }
.switch:checked { background: #2fb36a; }
.switch:checked::after { transform: translateX(20px); }
.switch:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
#stats-detail p { margin: 2px 0; }

/* ---------- game layout ---------- */
.game-layout { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 10px; }
.board-wrap {
  position: relative; flex: none; align-self: center;
  /* portrait: square, as wide as possible but always leave ~150px for the word list */
  width: min(100%, max(240px, calc(100dvh - 230px - env(safe-area-inset-top) - env(safe-area-inset-bottom))));
  aspect-ratio: 1;
}
.board {
  --n: 12; width: 100%; height: 100%; display: grid; user-select: none; -webkit-user-select: none;
  grid-template-columns: repeat(var(--n), 1fr); grid-template-rows: repeat(var(--n), 1fr);
  background: var(--cell); border: 2px solid var(--cell-line); border-radius: 14px; overflow: hidden;
  touch-action: none; -webkit-touch-callout: none; box-shadow: var(--shadow); container-type: inline-size;
}
.cell {
  display: grid; place-items: center; font-weight: 700; text-transform: uppercase;
  font-size: calc(100cqw / var(--n) * .52); font-family: ui-rounded, "SF Pro Rounded", -apple-system, system-ui, sans-serif;
  box-shadow: inset 0 0 0 .5px var(--cell-line); transition: background .12s; pointer-events: none;
}
.cell.found { background: hsl(var(--hue) 85% var(--found-l) / var(--found-a)); }
.cell.sel { background: var(--sel); color: var(--accent-text); border-radius: 30%; }
.cell.revealed { background: color-mix(in srgb, var(--danger) 28%, transparent); }
.cell.msg { background: #ffd54a; color: #3a2b00; }
.cell.hint { animation: pulse .7s ease-in-out infinite alternate; }
.cell.shake { animation: shake .4s; background: color-mix(in srgb, var(--danger) 30%, transparent); }
@keyframes pulse { from { background: var(--accent-soft); } to { background: var(--accent); color: var(--accent-text); } }
@keyframes shake { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }
@media (prefers-reduced-motion: reduce) { .cell.hint, .cell.shake, .timer.low { animation: none; } .cell.hint { background: var(--accent); color: var(--accent-text); } }

.pause-cover {
  position: absolute; inset: 0; border-radius: 14px; background: var(--card); border: 2px solid var(--cell-line);
  display: grid; place-content: center; justify-items: center; gap: 12px; font-size: 1.4rem; font-weight: 700; z-index: 2;
}

.side { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 8px; width: 100%; max-width: 760px; align-self: center; }
.status { display: flex; flex-wrap: wrap; gap: 6px; }
.pill { font-size: .82rem; font-weight: 600; padding: 4px 10px; border-radius: 999px; background: var(--card); border: 1px solid var(--line); }
.pill.strong { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.pill.soft { color: var(--muted); }
.pill.warn { background: var(--warn-bg); color: var(--warn-text); border-color: transparent; }
.pill.msg { background: #ffd54a; color: #3a2b00; border-color: transparent; letter-spacing: .04em; white-space: pre-wrap; }
.wordlist {
  list-style: none; margin: 0; padding: 2px; flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(108px, 1fr)); gap: 6px; align-content: start;
}
.wordlist li {
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 7px 10px;
  font-weight: 700; font-size: .9rem; text-align: center; letter-spacing: .03em;
}
.wordlist li.hidden { color: var(--muted); font-weight: 600; letter-spacing: .12em; }
.wordlist li.found { background: hsl(var(--hue) 85% var(--found-l) / var(--found-a)); text-decoration: line-through; text-decoration-thickness: 2px; border-color: transparent; }
.wordlist li.missed { border-color: var(--danger); color: var(--danger); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; padding-bottom: 2px; }
.actions .btn { flex: 1; }

/* landscape tablets / large screens: board left, list right */
@media (orientation: landscape) and (min-width: 700px) {
  .game-layout { flex-direction: row; align-items: stretch; gap: 16px; }
  .board-wrap { width: auto; height: 100%; max-width: 62%; align-self: center; }
  .side { align-self: stretch; max-width: none; }
}
/* very short landscape phones */
@media (orientation: landscape) and (max-height: 460px) {
  .game-bar { min-height: 40px; margin-bottom: 2px; }
  .btn { min-height: 40px; }
}

/* ---------- modal & toast ---------- */
.modal { position: fixed; inset: 0; background: rgba(10, 12, 25, .5); display: grid; place-items: center; padding: 20px; z-index: 50; }
.modal-card { background: var(--card); border-radius: 20px; padding: 20px; width: min(420px, 100%); box-shadow: var(--shadow); display: grid; gap: 12px; }
.modal-card h3 { font-size: 1.25rem; }
.modal-body p + p { margin-top: 6px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.modal-actions .btn { flex: 1; min-width: 96px; }
.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 24px); transform: translate(-50%, 20px);
  background: #1b1e2e; color: #fff; padding: 10px 16px; border-radius: 12px; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 60; max-width: calc(100vw - 32px); text-align: center; font-weight: 600;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
