@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;700&display=swap');

/* ═══════════════════════════════════════════════════════════════
   CATPPUCCIN — all four flavors
   ═══════════════════════════════════════════════════════════════ */
[data-theme="mocha"] {
  --base:#1e1e2e;--mantle:#181825;--crust:#11111b;
  --surface0:#313244;--surface1:#45475a;--surface2:#585b70;
  --overlay0:#6c7086;--overlay1:#7f849c;--overlay2:#9399b2;
  --subtext0:#a6adc8;--subtext1:#bac2de;--text:#cdd6f4;
  --lavender:#b4befe;--blue:#89b4fa;--sapphire:#74c7ec;
  --sky:#89dceb;--teal:#94e2d5;--green:#a6e3a1;
  --yellow:#f9e2af;--peach:#fab387;--maroon:#eba0ac;
  --red:#f38ba8;--mauve:#cba6f7;--pink:#f5c2e7;
  --flamingo:#f2cdcd;--rosewater:#f5e0dc;
}
[data-theme="macchiato"] {
  --base:#24273a;--mantle:#1e2030;--crust:#181926;
  --surface0:#363a4f;--surface1:#494d64;--surface2:#5b6078;
  --overlay0:#6e738d;--overlay1:#8087a2;--overlay2:#939ab7;
  --subtext0:#a5adcb;--subtext1:#b8c0e0;--text:#cad3f5;
  --lavender:#b7bdf8;--blue:#8aadf4;--sapphire:#7dc4e4;
  --sky:#91d7e3;--teal:#8bd5ca;--green:#a6da95;
  --yellow:#eed49f;--peach:#f5a97f;--maroon:#ee99a0;
  --red:#ed8796;--mauve:#c6a0f6;--pink:#f5bde6;
  --flamingo:#f0c6c6;--rosewater:#f4dbd6;
}
[data-theme="frappe"] {
  --base:#303446;--mantle:#292c3c;--crust:#232634;
  --surface0:#414559;--surface1:#51576d;--surface2:#626880;
  --overlay0:#737994;--overlay1:#838ba7;--overlay2:#949cbb;
  --subtext0:#a5adce;--subtext1:#b5bfe2;--text:#c6d0f5;
  --lavender:#babbf1;--blue:#8caaee;--sapphire:#85c1dc;
  --sky:#99d1db;--teal:#81c8be;--green:#a6d189;
  --yellow:#e5c890;--peach:#ef9f76;--maroon:#ea999c;
  --red:#e78284;--mauve:#ca9ee6;--pink:#f4b8e4;
  --flamingo:#eebebe;--rosewater:#f2d5cf;
}
[data-theme="latte"] {
  --base:#eff1f5;--mantle:#e6e9ef;--crust:#dce0e8;
  --surface0:#ccd0da;--surface1:#bcc0cc;--surface2:#acb0be;
  --overlay0:#9ca0b0;--overlay1:#8c8fa1;--overlay2:#7c7f93;
  --subtext0:#6c6f85;--subtext1:#5c5f77;--text:#4c4f69;
  --lavender:#7287fd;--blue:#1e66f5;--sapphire:#209fb5;
  --sky:#04a5e5;--teal:#179299;--green:#40a02b;
  --yellow:#df8e1d;--peach:#fe640b;--maroon:#e64553;
  --red:#d20f39;--mauve:#8839ef;--pink:#ea76cb;
  --flamingo:#dd7878;--rosewater:#dc8a78;
}

/* ═══════════════════════════════════════════════════════════════
   SEMANTIC TOKENS
   ═══════════════════════════════════════════════════════════════ */
html {
  --ui-bg:       var(--base);
  --ui-surface:  var(--mantle);
  --ui-raised:   var(--surface0);
  --ui-border:   var(--surface1);
  --ui-muted:    var(--overlay0);
  --ui-text:     var(--text);
  --ui-subtext:  var(--subtext0);
  --ui-accent:   var(--mauve);
  --ui-correct:  var(--green);
  --ui-error:    var(--red);
  --ui-info:     var(--sapphire);
  --ui-warn:     var(--yellow);
  /* Grid cells */
  --wall-bg:     var(--crust);
  --clue-bg:     var(--mantle);
  --clue-border: var(--surface1);
  --clue-down:   var(--sapphire);
  --clue-across: var(--peach);
  --input-bg:    var(--text);
  --input-border:var(--surface0);
  --input-text:  var(--base);
  /* 8 revelation tint colors — one per possible cipher letter */
  --r0: var(--mauve);   --r1: var(--blue);   --r2: var(--green);
  --r3: var(--peach);   --r4: var(--teal);   --r5: var(--pink);
  --r6: var(--yellow);  --r7: var(--flamingo);
  --radius: 10px; --radius-sm: 6px;
}
/* Latte needs different input treatment — cells are light already */
[data-theme="latte"] {
  --wall-bg:    var(--crust);
  --clue-bg:    var(--surface0);
  --clue-border:var(--overlay0);
  --input-bg:   var(--base);
  --input-border:var(--surface1);
  --input-text: var(--text);
}

* { box-sizing:border-box; margin:0; padding:0; }

body {
  background: var(--ui-bg);
  color: var(--ui-text);
  font-family: 'Outfit', sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
  transition: background .3s, color .3s;
}

/* ── Header ──────────────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 11px 16px;
  background: var(--ui-surface);
  border-bottom: 1px solid var(--ui-border);
  position: sticky; top:0; z-index:100;
}
.logo {
  font-size:1.05rem; font-weight:700; letter-spacing:-0.02em;
  color: var(--ui-accent);
  display:flex; align-items:center; gap:8px;
}
.logo-icon { font-size:1.2rem; }
.logo-sub { color:var(--ui-subtext); font-weight:400; font-size:0.85rem; }
.header-right { display:flex; align-items:center; gap:8px; }

.logo-wrap {
  display:flex; align-items:center; gap:10px;
}
.app-version {
  display:inline-flex; align-items:center; justify-content:center;
  min-height:24px; padding:2px 8px;
  border-radius:999px;
  background:color-mix(in srgb,var(--ui-accent) 16%,var(--ui-raised));
  border:1px solid color-mix(in srgb,var(--ui-accent) 36%,transparent);
  color:var(--ui-accent);
  font-family:'JetBrains Mono',monospace;
  font-size:0.68rem; font-weight:700;
  letter-spacing:0.01em;
}

.build-meta {
  display:flex; align-items:center; justify-content:center; gap:8px;
  color:var(--ui-muted);
  font-size:0.72rem;
  font-family:'JetBrains Mono',monospace;
  margin-top:2px;
}
.build-meta-value {
  color:var(--ui-subtext);
}


/* Catppuccin theme dots */
.theme-switcher { display:flex; gap:5px; align-items:center; }
.theme-dot {
  width:14px; height:14px; border-radius:50%;
  border: 2px solid transparent;
  cursor:pointer;
  transition: border-color .15s, transform .15s;
  flex-shrink:0;
}
.theme-dot:hover { transform:scale(1.25); }
.theme-dot.active { border-color: var(--ui-text); }
.theme-dot[data-t="mocha"]     { background:#cba6f7; }
.theme-dot[data-t="macchiato"] { background:#c6a0f6; }
.theme-dot[data-t="frappe"]    { background:#ca9ee6; }
.theme-dot[data-t="latte"]     { background:#8839ef; }

.btn {
  background: var(--ui-raised);
  border: 1px solid var(--ui-border);
  color: var(--ui-text);
  font-family:'Outfit',sans-serif;
  font-size:0.76rem; font-weight:500;
  padding:6px 11px; border-radius:var(--radius-sm);
  cursor:pointer; transition:background .12s, border-color .12s;
  white-space:nowrap;
}
.btn:hover { background:var(--ui-border); }
.btn.danger { color:var(--ui-error); border-color:var(--ui-error); }
.btn.danger:hover { background:color-mix(in srgb, var(--ui-error) 10%, transparent); }

/* ── Main ────────────────────────────────────────────────────── */
main {
  flex:1; display:flex; flex-direction:column;
  padding:13px; gap:11px;
  max-width:620px; margin:0 auto; width:100%;
}

/* ── Status bar ──────────────────────────────────────────────── */
.status-bar {
  display:flex; align-items:center; gap:8px;
  padding:8px 12px;
  background:var(--ui-surface);
  border:1px solid var(--ui-border);
  border-radius:var(--radius);
  font-size:0.78rem;
}
.status-label { color:var(--ui-subtext); }
.status-value { font-weight:600; font-family:'JetBrains Mono',monospace; color:var(--ui-accent); }
.status-sep { color:var(--ui-border); }
.timer { margin-left:auto; font-family:'JetBrains Mono',monospace; font-size:0.83rem; color:var(--ui-info); }

/* ── Cipher panel ────────────────────────────────────────────── */
.cipher-panel {
  background:var(--ui-surface);
  border:1px solid var(--ui-border);
  border-radius:var(--radius);
  overflow:hidden;
}
.panel-header {
  display:flex; align-items:center; justify-content:space-between;
  padding:8px 12px;
  border-bottom:1px solid var(--ui-border);
  font-size:0.7rem; font-weight:600;
  text-transform:uppercase; letter-spacing:0.07em;
  color:var(--ui-subtext);
}
.cipher-grid {
  display:flex; flex-wrap:wrap;
  gap:1px; background:var(--ui-border); padding:1px;
}
.cipher-cell {
  flex:1; min-width:50px;
  background:var(--ui-raised);
  display:flex; flex-direction:column; align-items:center; justify-content:center;
  padding:8px 4px; gap:3px;
  cursor:pointer;
  transition:background .1s;
  position:relative;
}
.cipher-cell:hover { background:var(--ui-border); }
.cipher-cell.solved {
  background:color-mix(in srgb, var(--ui-correct) 12%, var(--ui-raised));
}
.cipher-letter {
  font-family:'JetBrains Mono',monospace;
  font-size:0.95rem; font-weight:700; line-height:1;
}
.cipher-digit {
  font-family:'JetBrains Mono',monospace;
  font-size:0.82rem; font-weight:700;
  color:var(--ui-correct); line-height:1; min-height:13px;
}
.cipher-digit.unset { color:var(--ui-muted); }
.cipher-cell.revealed {
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--ui-correct) 45%, transparent);
}
/* Color swatch linking letter to its grid tint */
.cipher-swatch {
  width:18px; height:3px; border-radius:2px;
  margin-top:2px; opacity:0.75;
}

/* ── Hidden message reveal ───────────────────────────────────── */
.reveal-panel {
  background:var(--ui-surface);
  border:1px solid var(--ui-border);
  border-radius:var(--radius);
  overflow:hidden;
}
.reveal-message-wrap {
  padding:14px 12px 12px;
  display:flex;
  flex-direction:column;
  gap:10px;
}
.reveal-message {
  min-height:54px;
  display:flex;
  flex-wrap:wrap;
  gap:6px 4px;
  align-items:center;
  font-family:'JetBrains Mono',monospace;
  font-size:1rem;
  line-height:1.6;
}
.reveal-char, .reveal-space {
  display:inline-flex;
  align-items:center;
  justify-content:center;
  min-width:0.8em;
}
.reveal-char {
  min-width:1.05em;
  height:1.4em;
  border-radius:6px;
  border:1px solid var(--ui-border);
  background:var(--ui-raised);
  color:var(--ui-muted);
  transition:background .22s ease, color .22s ease, border-color .22s ease, transform .18s ease;
}
.reveal-char.revealed {
  background:color-mix(in srgb, var(--ui-accent) 16%, var(--ui-raised));
  color:var(--ui-text);
  border-color:color-mix(in srgb, var(--ui-accent) 38%, transparent);
  transform:translateY(-1px);
}
.reveal-note {
  color:var(--ui-subtext);
  font-size:0.76rem;
  line-height:1.55;
}

/* ── Grid ────────────────────────────────────────────────────── */
.puzzle-wrapper {
  background:var(--ui-surface);
  border:1px solid var(--ui-border);
  border-radius:var(--radius);
  overflow:hidden;
}
.puzzle-header {
  display:flex; align-items:center; justify-content:space-between;
  padding:8px 12px;
  border-bottom:1px solid var(--ui-border);
  font-size:0.7rem; font-weight:600;
  text-transform:uppercase; letter-spacing:0.07em;
  color:var(--ui-subtext);
}
.puzzle-meta { color:var(--ui-muted); font-weight:400; font-family:'JetBrains Mono',monospace; }
.grid-container { padding:14px; display:flex; justify-content:center; }
.kakuro-grid {
  display:inline-grid;
  gap:3px;
  background:var(--wall-bg);
  border:3px solid var(--wall-bg);
  border-radius:8px;
  padding:3px;
}

/* Grid cells */
.gcell { width:52px; height:52px; border-radius:4px; position:relative; flex-shrink:0; }
@media (max-width:420px) { .gcell { width:42px; height:42px; } }

.gcell.wall { background:var(--wall-bg); border-radius:2px; }

.gcell.clue {
  background:var(--clue-bg);
  border:1px solid var(--clue-border);
  border-radius:4px; overflow:hidden; padding:0;
}
.gcell.clue svg { width:100%; height:100%; display:block; }

/* ── REVELATION LAYER ─────────────────────────────────────────
   Input cells use --cell-bg (set per-cell by JS) as background.
   Unresolved cipher letters → Catppuccin accent color at ~35% mix
   into --surface0 (a dark neutral). Resolved → plain light input-bg.
   The transition animates the background between these states.
   ─────────────────────────────────────────────────────────────*/
.gcell.input {
  background: var(--cell-bg, var(--input-bg));
  border: 1.5px solid var(--cell-border, var(--input-border));
  border-radius: 4px;
  cursor: pointer;
  position: relative; overflow: hidden;
  transition: background 0.45s ease, border-color 0.3s ease, box-shadow .15s;
}
.gcell.input:hover { filter: brightness(1.05); }
.gcell.input.selected {
  border-color: var(--ui-warn); border-width: 2px;
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--ui-warn) 35%, transparent);
  z-index: 2; filter: none;
}
.gcell.input.highlight {
  border-color: color-mix(in srgb, var(--ui-info) 70%, transparent);
}
.gcell.input.correct {
  background: color-mix(in srgb, var(--ui-correct) 30%, var(--input-bg)) !important;
  border-color: var(--ui-correct) !important;
}
.gcell.input.error {
  background: color-mix(in srgb, var(--ui-error) 30%, var(--input-bg)) !important;
  border-color: var(--ui-error) !important;
  animation: shake .25s ease;
}
.gcell.input.given {
  background: color-mix(in srgb, var(--ui-muted) 20%, var(--input-bg)) !important;
  border-color: var(--ui-muted) !important;
  cursor: default;
}
@keyframes shake {
  0%,100%{transform:translateX(0)} 25%{transform:translateX(-3px)} 75%{transform:translateX(3px)}
}
.cell-digit {
  width:100%; height:100%;
  display:flex; align-items:center; justify-content:center;
  font-family:'JetBrains Mono',monospace;
  font-size:1.28rem; font-weight:700;
  color: var(--cell-text, var(--input-text));
  user-select:none; position:relative; z-index:2;
  transition: color 0.45s ease;
}
.cell-digit.pencil {
  display:grid;
  grid-template-columns:repeat(3,1fr);
  grid-template-rows:repeat(3,1fr);
  padding:2px; align-items:start; justify-items:center;
}
.cell-digit.pencil span { font-size:0.44rem; line-height:1.4; color:var(--ui-subtext); }

/* ── Numpad ──────────────────────────────────────────────────── */
.numpad-section {
  background:var(--ui-surface);
  border:1px solid var(--ui-border);
  border-radius:var(--radius); overflow:hidden;
}
.numpad-row { display:flex; gap:1px; background:var(--ui-border); padding:1px; }
.numpad-btn {
  flex:1; background:var(--ui-raised); border:none;
  color:var(--ui-text);
  font-family:'JetBrains Mono',monospace;
  font-size:1.12rem; font-weight:700;
  padding:15px 4px; cursor:pointer;
  transition:background .1s; border-radius:3px;
}
.numpad-btn:hover { background:var(--ui-border); }
.numpad-btn:active { transform:scale(0.93); }
.numpad-btn.used { color:var(--ui-muted); }
.numpad-btn.clear-btn {
  background:color-mix(in srgb,var(--ui-error) 8%,var(--ui-raised));
  color:var(--ui-error); font-size:0.78rem; font-family:'Outfit',sans-serif;
}
.numpad-btn.pencil-btn {
  background:color-mix(in srgb,var(--ui-info) 8%,var(--ui-raised));
  color:var(--ui-info); font-size:0.68rem; font-family:'Outfit',sans-serif;
}
.numpad-btn.pencil-btn.active {
  background:color-mix(in srgb,var(--ui-info) 22%,var(--ui-raised));
}
.numpad-mode-label {
  font-size:0.66rem; color:var(--ui-muted);
  text-align:center; padding:5px; font-weight:500;
}
.numpad-mode-label span { color:var(--ui-info); font-weight:600; }

/* ── Victory overlay ─────────────────────────────────────────── */
.overlay {
  display:none; position:fixed; inset:0;
  background:color-mix(in srgb,var(--ui-bg) 82%,transparent);
  z-index:200; align-items:center; justify-content:center;
  backdrop-filter:blur(10px);
}
.overlay.show { display:flex; }
.victory-card {
  background:var(--ui-surface); border:1px solid var(--ui-border);
  border-radius:20px; padding:34px 26px;
  text-align:center; max-width:290px; width:90%;
  animation:popIn .3s cubic-bezier(.34,1.56,.64,1);
}
@keyframes popIn { from{transform:scale(.8);opacity:0} to{transform:scale(1);opacity:1} }
.victory-emoji { font-size:2.8rem; margin-bottom:10px; }
.victory-title { font-size:1.45rem; font-weight:700; color:var(--ui-accent); margin-bottom:4px; }
.victory-sub { color:var(--ui-subtext); font-size:0.82rem; margin-bottom:18px; }
.victory-time { font-family:'JetBrains Mono',monospace; font-size:1.7rem; font-weight:700; color:var(--ui-info); margin-bottom:18px; }
.victory-btn {
  width:100%; padding:12px;
  background:var(--ui-accent); color:var(--base);
  border:none; border-radius:var(--radius-sm);
  font-family:'Outfit',sans-serif; font-size:0.88rem; font-weight:700;
  cursor:pointer; transition:opacity .15s;
}
.victory-btn:hover { opacity:.88; }

/* ── Puzzle selector modal ───────────────────────────────────── */
.modal {
  display:none; position:fixed; inset:0;
  background:color-mix(in srgb,var(--ui-bg) 78%,transparent);
  z-index:200; align-items:center; justify-content:center;
  backdrop-filter:blur(8px); padding:16px;
}
.modal.show { display:flex; }
.modal-card {
  background:var(--ui-surface); border:1px solid var(--ui-border);
  border-radius:18px; padding:20px;
  max-width:460px; width:100%; max-height:88vh; overflow-y:auto;
  animation:popIn .22s ease;
}
.modal-title { font-size:1rem; font-weight:700; margin-bottom:3px; }
.modal-sub { color:var(--ui-subtext); font-size:0.78rem; margin-bottom:16px; }
.puzzle-list { display:flex; flex-direction:column; gap:7px; margin-bottom:13px; }
.puzzle-item {
  display:flex; align-items:center; gap:11px;
  padding:10px 12px;
  background:var(--ui-raised);
  border:1px solid var(--ui-border);
  border-radius:var(--radius-sm);
  cursor:pointer; transition:border-color .12s, background .12s;
}
.puzzle-item:hover { border-color:var(--ui-accent); background:color-mix(in srgb,var(--ui-accent) 6%,var(--ui-raised)); }
.puzzle-item.current { border-color:var(--ui-accent); background:color-mix(in srgb,var(--ui-accent) 10%,var(--ui-raised)); }
.pi-badge {
  background:var(--ui-surface); border:1px solid var(--ui-border);
  border-radius:5px; padding:3px 8px;
  font-family:'JetBrains Mono',monospace;
  font-size:0.7rem; font-weight:700; color:var(--ui-accent); white-space:nowrap;
}
.pi-info { flex:1; }
.pi-name { font-size:0.85rem; font-weight:600; }
.pi-meta { font-size:0.7rem; color:var(--ui-subtext); margin-top:1px; }
.pi-status { font-size:1.05rem; }

/* ── Cipher popup (bottom sheet) ─────────────────────────────── */
.cipher-popup {
  display:none; position:fixed; inset:0; z-index:300;
  align-items:flex-end; justify-content:center;
  background:color-mix(in srgb,var(--ui-bg) 52%,transparent);
}
.cipher-popup.show { display:flex; }
.cipher-popup-inner {
  background:var(--ui-surface); border:1px solid var(--ui-border);
  border-radius:20px 20px 0 0;
  padding:18px 16px 30px;
  width:100%; max-width:430px;
  animation:slideUp .22s ease;
}
@keyframes slideUp { from{transform:translateY(100%)} to{transform:translateY(0)} }
.cp-drag { width:34px; height:4px; background:var(--ui-border); border-radius:2px; margin:0 auto 14px; }
.cp-label { font-size:0.75rem; color:var(--ui-subtext); margin-bottom:5px; font-weight:500; }
.cp-letter { font-size:2rem; font-family:'JetBrains Mono',monospace; font-weight:700; margin-bottom:13px; }
.cp-numpad { display:grid; grid-template-columns:repeat(5,1fr); gap:5px; margin-bottom:9px; }
.cp-btn {
  background:var(--ui-raised); border:1px solid var(--ui-border);
  color:var(--ui-text);
  font-family:'JetBrains Mono',monospace;
  font-size:1rem; font-weight:700;
  padding:12px; border-radius:8px;
  cursor:pointer; transition:background .1s, border-color .1s; text-align:center;
}
.cp-btn:hover { background:var(--ui-border); border-color:var(--ui-accent); }
.cp-btn:active { transform:scale(0.93); }
.cp-btn.disabled { opacity:.28; cursor:default; pointer-events:none; }
.cp-btn.selected {
  background:color-mix(in srgb,var(--ui-correct) 18%,var(--ui-raised));
  border-color:var(--ui-correct); color:var(--ui-correct);
}
.cp-clear {
  width:100%;
  background:color-mix(in srgb,var(--ui-error) 8%,var(--ui-raised));
  border:1px solid color-mix(in srgb,var(--ui-error) 28%,transparent);
  color:var(--ui-error);
  font-family:'Outfit',sans-serif; font-size:0.8rem; font-weight:600;
  padding:10px; border-radius:8px; cursor:pointer;
}
.cp-hint { font-size:0.66rem; color:var(--ui-muted); text-align:center; margin-top:7px; }

/* Conflict note */
.conflict-note {
  padding:7px 11px;
  background:color-mix(in srgb,var(--ui-error) 10%,transparent);
  border:1px solid color-mix(in srgb,var(--ui-error) 24%,transparent);
  border-radius:var(--radius-sm);
  font-size:0.74rem; color:var(--ui-error); display:none;
}
.conflict-note.show { display:block; }

/* Instructions */
.instructions {
  background:var(--ui-surface); border:1px solid var(--ui-border);
  border-radius:var(--radius); overflow:hidden;
}
.instructions summary {
  padding:9px 12px; cursor:pointer;
  font-size:0.7rem; font-weight:600;
  text-transform:uppercase; letter-spacing:0.07em;
  color:var(--ui-subtext);
  list-style:none; display:flex; align-items:center; justify-content:space-between;
  user-select:none;
}
.instructions summary::after { content:"▾"; transition:transform .2s; }
.instructions[open] summary::after { transform:rotate(180deg); }
.instructions-body {
  padding:0 12px 12px;
  font-size:0.78rem; color:var(--ui-subtext);
  line-height:1.72; display:flex; flex-direction:column; gap:6px;
}
.instructions-body strong { color:var(--ui-text); }
.rule-row { display:flex; gap:7px; }
.rule-dot { color:var(--ui-accent); font-weight:700; flex-shrink:0; }
