:root {
  --bg: #f7f6f2;
  --surface: #ffffff;
  --ink: #1a1a1a;
  --muted: #6b6b6b;
  --line: #d9d6cf;
  --line-strong: #2a2a2a;
  --given: #1a1a1a;
  --entry: #2a6df4;
  --note: #6b6b6b;
  --highlight: #e9eef7;
  --peer: #f0eee7;
  --same: #d9e5fb;
  --bad: #f6d6d6;
  --bad-ink: #b0221a;
  --accent: #2a6df4;
  --accent-ink: #ffffff;
  --pad-bg: #ecebe4;
  --pad-key: #ffffff;
  --pad-key-press: #dcdad2;
  --xw-black: #1a1a1a;
  --shadow: 0 1px 2px rgba(0,0,0,0.04), 0 4px 12px rgba(0,0,0,0.06);
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #171a20;
    --ink: #ecedef;
    --muted: #9098a5;
    --line: #2a2e37;
    --line-strong: #ecedef;
    --given: #ecedef;
    --entry: #7fb0ff;
    --note: #8892a1;
    --highlight: #1c2431;
    --peer: #191d25;
    --same: #23324a;
    --bad: #3a1e1e;
    --bad-ink: #ff9a92;
    --accent: #7fb0ff;
    --accent-ink: #0f1115;
    --pad-bg: #14171d;
    --pad-key: #1e222a;
    --pad-key-press: #2a2f39;
    --xw-black: #06070a;
    --shadow: 0 1px 2px rgba(0,0,0,0.4), 0 4px 12px rgba(0,0,0,0.5);
  }
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #171a20;
  --ink: #ecedef;
  --muted: #9098a5;
  --line: #2a2e37;
  --line-strong: #ecedef;
  --given: #ecedef;
  --entry: #7fb0ff;
  --note: #8892a1;
  --highlight: #1c2431;
  --peer: #191d25;
  --same: #23324a;
  --bad: #3a1e1e;
  --bad-ink: #ff9a92;
  --accent: #7fb0ff;
  --accent-ink: #0f1115;
  --pad-bg: #14171d;
  --pad-key: #1e222a;
  --pad-key-press: #2a2f39;
  --xw-black: #06070a;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.3 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bot);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}
.mono { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  gap: 8px;
}
.topbar .meta {
  display: flex;
  gap: 8px;
  align-items: baseline;
  color: var(--muted);
  font-size: 14px;
}
.topbar .dot { opacity: 0.5; }
#difficulty-label { text-transform: capitalize; color: var(--ink); font-weight: 500; }

.btn {
  appearance: none;
  border: 0;
  background: var(--surface);
  color: var(--ink);
  border-radius: 999px;
  padding: 8px 14px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.btn.ghost { background: transparent; box-shadow: none; color: var(--ink); padding: 8px 10px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.wide { width: 100%; padding: 12px; margin-top: 8px; }

.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 0 12px 12px;
  min-height: 0;
}

.board {
  --cell: min(calc((100vw - 24px) / 9), calc((100dvh - 300px) / 9), 46px);
  width: calc(var(--cell) * 9);
  height: calc(var(--cell) * 9);
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  background: var(--surface);
  border: 2px solid var(--line-strong);
  border-radius: 6px;
  box-shadow: var(--shadow);
  overflow: hidden;
  position: relative;
}
.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell) * 0.55);
  font-weight: 400;
  color: var(--entry);
  background: var(--surface);
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  transition: background-color 80ms ease;
}
.cell.given { color: var(--given); font-weight: 600; }
.cell[data-col="2"], .cell[data-col="5"] { border-right-width: 2px; border-right-color: var(--line-strong); }
.cell[data-row="2"], .cell[data-row="5"] { border-bottom-width: 2px; border-bottom-color: var(--line-strong); }
.cell[data-col="8"] { border-right: 0; }
.cell[data-row="8"] { border-bottom: 0; }

.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.selected { background: var(--highlight); box-shadow: inset 0 0 0 2px var(--accent); }
.cell.bad { background: var(--bad); color: var(--bad-ink); }

.cell .notes {
  position: absolute; inset: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  font-size: calc(var(--cell) * 0.24);
  font-weight: 500;
  color: var(--note);
  padding: 2px;
}
.cell .notes span {
  display: flex; align-items: center; justify-content: center;
}

.actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  max-width: 420px;
  margin: 0 auto;
  width: 100%;
}
.action {
  appearance: none;
  border: 0;
  background: var(--surface);
  color: var(--ink);
  border-radius: var(--radius);
  padding: 10px 6px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font: inherit;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 60ms ease, background 80ms ease;
}
.action:active { transform: translateY(1px); background: var(--pad-key-press); }
.action .glyph { font-size: 20px; line-height: 1; }
.action .lbl { font-size: 11px; color: var(--muted); }
.action.toggle[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.action.toggle[aria-pressed="true"] .lbl { color: var(--accent-ink); opacity: 0.85; }

.pad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 6px;
  max-width: 420px;
  margin: 0 auto;
  width: 100%;
  padding: 8px;
  background: var(--pad-bg);
  border-radius: var(--radius);
}
.key {
  appearance: none;
  border: 0;
  background: var(--pad-key);
  color: var(--ink);
  border-radius: 8px;
  aspect-ratio: 1 / 1.15;
  font: inherit;
  font-size: 22px;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  box-shadow: var(--shadow);
  transition: transform 60ms ease, background 80ms ease;
  position: relative;
}
.key:active { transform: translateY(1px); background: var(--pad-key-press); }
.key.exhausted { color: var(--muted); opacity: 0.35; }
.key .remaining {
  font-size: 9px;
  color: var(--muted);
  font-weight: 500;
  line-height: 1;
}

.dialog {
  border: 0;
  border-radius: 16px;
  padding: 20px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
  max-width: 320px;
  width: calc(100% - 32px);
}
.dialog::backdrop { background: rgba(0,0,0,0.4); }
.dialog h2 { margin: 0 0 12px; font-size: 18px; font-weight: 600; }
.dialog p { margin: 0 0 12px; }
.choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.choice {
  appearance: none;
  border: 0;
  background: var(--pad-key);
  color: var(--ink);
  border-radius: 10px;
  padding: 14px 8px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.choice:active { background: var(--pad-key-press); }

.veil {
  position: fixed; inset: 0;
  background: color-mix(in oklab, var(--bg) 92%, transparent);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}
.veil[hidden] { display: none; }

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.06); }
  100% { transform: scale(1); }
}
.cell.win { animation: pulse 600ms ease; }


/* ==== Crossword ==== */

body.crossword-page { user-select: none; }

.xw-meta {
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  gap: 6px;
  flex: 1;
  justify-content: center;
}
.xw-meta #xw-title { color: var(--ink); font-weight: 500; overflow: hidden; text-overflow: ellipsis; }

.xw-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 0;
  min-height: 0;
}

.xw-grid-wrap {
  flex: 1;
  min-height: 0;
  overflow: auto;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 6px;
  -webkit-overflow-scrolling: touch;
}

.xw-grid {
  display: grid;
  background: var(--xw-black);
  border: 2px solid var(--xw-black);
  gap: 1px;
  padding: 0;
}
.xw-cell {
  position: relative;
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
  font-weight: 500;
  color: var(--ink);
}
.xw-cell.black { background: var(--xw-black); cursor: default; }
.xw-cell.in-word { background: var(--peer); }
.xw-cell.selected { background: var(--highlight); box-shadow: inset 0 0 0 2px var(--accent); z-index: 2; }
.xw-cell.wrong { color: var(--bad-ink); }
.xw-cell.revealed { color: var(--accent); }
.xw-cell .num {
  position: absolute;
  top: 1px; left: 2px;
  font-size: 9px;
  line-height: 1;
  color: var(--muted);
  font-weight: 500;
  pointer-events: none;
}
.xw-cell.selected .num,
.xw-cell.in-word .num { color: var(--muted); }
.xw-cell .val {
  font-size: inherit;
  line-height: 1;
  padding-top: 3px;   /* nudge letter below the small number */
}
.xw-cell.circled::before {
  content: '';
  position: absolute; inset: 2px;
  border: 1px solid var(--muted);
  border-radius: 999px;
  pointer-events: none;
}
.xw-cell.rebus .val { font-size: 60%; }

.xw-cluebar {
  display: flex;
  align-items: stretch;
  gap: 4px;
  padding: 6px 8px;
  background: var(--surface);
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  min-height: 52px;
}
.cluebar-nav {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  font-size: 24px;
  font-weight: 400;
  width: 36px;
  cursor: pointer;
  border-radius: 8px;
}
.cluebar-nav:active { background: var(--pad-key-press); }
.cluebar-text {
  flex: 1;
  display: flex;
  gap: 6px;
  align-items: center;
  min-width: 0;
  padding: 0 4px;
}
.cluebar-num {
  font-weight: 700;
  color: var(--accent);
  flex-shrink: 0;
}
.cluebar-clue {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  font-size: 15px;
  line-height: 1.25;
}

.kbd {
  padding: 8px 4px 10px;
  background: var(--pad-bg);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.kbd-row {
  display: flex;
  gap: 5px;
  justify-content: center;
  padding: 0 4px;
}
.kbd-key {
  appearance: none;
  border: 0;
  background: var(--pad-key);
  color: var(--ink);
  border-radius: 6px;
  font: inherit;
  font-size: 18px;
  font-weight: 500;
  min-width: 30px;
  flex: 1;
  max-width: 44px;
  height: 42px;
  cursor: pointer;
  box-shadow: 0 1px 0 rgba(0,0,0,0.15);
  padding: 0;
  transition: transform 60ms ease, background 80ms ease;
}
.kbd-key:active { transform: translateY(1px); background: var(--pad-key-press); }
.kbd-key.wide { flex: 1.6; max-width: none; font-size: 14px; font-weight: 500; }
.kbd-key.toggle[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }

.menu-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 8px;
}
.menu-item {
  appearance: none;
  border: 0;
  background: var(--pad-key);
  color: var(--ink);
  border-radius: 10px;
  padding: 12px 8px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  box-shadow: var(--shadow);
  text-align: center;
}
.menu-item:active { background: var(--pad-key-press); }
.menu-item.danger { color: var(--bad-ink); }

.clues-dialog {
  max-width: 520px;
  width: calc(100% - 24px);
  height: 80vh;
  height: 80dvh;
  padding: 16px;
}
.clues-dialog form { display: flex; flex-direction: column; height: 100%; }
.clues-panels {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  margin: 8px -6px 8px;
  padding: 0 6px;
}
.clues-panels section + section { margin-top: 12px; }
.clues-panels h3 {
  margin: 0 0 6px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--muted);
  letter-spacing: 0.08em;
  position: sticky; top: 0;
  background: var(--surface);
  padding: 8px 0 4px;
  z-index: 1;
}
.clue-list {
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 14px;
  line-height: 1.35;
}
.clue-list li {
  padding: 4px 6px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  gap: 6px;
}
.clue-list li:active { background: var(--pad-key-press); }
.clue-list .n { color: var(--muted); min-width: 24px; font-variant-numeric: tabular-nums; }
.clue-list li.current { background: var(--highlight); }
.clue-list li.filled .t { color: var(--muted); }

.confirm-buttons { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }

.xw-error {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg); color: var(--ink); padding: 24px;
  z-index: 30;
  text-align: center;
}
.xw-error[hidden] { display: none; }


/* ==== Crosswords list ==== */

.xw-list-main {
  flex: 1;
  padding: 4px 0 20px;
  overflow: auto;
}
.xw-list { display: flex; flex-direction: column; gap: 8px; }
.xw-empty, .xw-loading {
  padding: 40px 20px;
  color: var(--muted);
  text-align: center;
}
.xw-empty p { margin: 0 0 6px; }
.xw-empty code { background: var(--pad-key); padding: 2px 6px; border-radius: 4px; }
.xw-loading { padding: 20px; }

.outlet-section { padding: 4px 0 0; }
.outlet-header { padding: 10px 16px 4px; }
.outlet-header h2 { margin: 0; font-size: 16px; font-weight: 600; }
.outlet-sub { font-size: 12px; margin-top: 2px; }

.puzzle-scroller {
  display: flex;
  gap: 10px;
  padding: 8px 16px 12px;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
}
.puzzle-scroller::-webkit-scrollbar { display: none; }

.puzzle-card {
  flex-shrink: 0;
  scroll-snap-align: start;
  width: 160px;
  min-height: 108px;
  background: var(--surface);
  color: var(--ink);
  text-decoration: none;
  border-radius: 12px;
  padding: 10px 12px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 2px;
  position: relative;
  transition: transform 60ms ease;
}
.puzzle-card:active { transform: translateY(1px); }
.card-date { font-size: 13px; font-weight: 600; color: var(--accent); }
.card-title {
  font-size: 13px;
  line-height: 1.25;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  margin-top: 2px;
}
.card-author {
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.card-size {
  font-size: 10px;
  margin-top: auto;
  padding-top: 4px;
  font-variant-numeric: tabular-nums;
}
.card-progress {
  position: absolute;
  left: 10px; right: 10px; bottom: 6px;
  height: 3px;
  background: var(--pad-key-press);
  border-radius: 2px;
  overflow: hidden;
}
.card-progress-fill { height: 100%; background: var(--accent); }
.card-progress.finished .card-progress-fill { background: var(--muted); }
.card-badge {
  position: absolute;
  top: 8px; right: 8px;
  background: var(--muted);
  color: var(--surface);
  border-radius: 6px;
  font-size: 9px;
  padding: 2px 5px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}
.card-badge.in-progress { background: var(--accent); color: var(--accent-ink); }


/* ==== Home ==== */

body.home-page {
  display: flex;
  flex-direction: column;
}
.home-header {
  padding: 40px 24px 20px;
}
.home-header h1 {
  margin: 0;
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.home-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 8px 16px;
}
.home-tile {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--surface);
  color: var(--ink);
  text-decoration: none;
  border-radius: 16px;
  padding: 18px 18px;
  box-shadow: var(--shadow);
  transition: transform 60ms ease;
}
.home-tile:active { transform: translateY(1px); }
.tile-icon {
  color: var(--accent);
  flex-shrink: 0;
}
.tile-body { flex: 1; min-width: 0; }
.tile-title {
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.tile-sub {
  font-size: 13px;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tile-arrow {
  font-size: 24px;
  color: var(--muted);
  flex-shrink: 0;
}
.home-footer {
  padding: 12px 24px 24px;
  text-align: center;
  font-size: 11px;
}



