/* =========================================================================
   Chain — Daily Word Chain
   Zero-dependency stylesheet. System font stack only. Mobile-first.
   ========================================================================= */

/* Visibility is toggled in JS via el.hidden. Author display rules (.streak,
   .modal-overlay, .state-screen, …) otherwise beat the UA [hidden] style, so
   force it: anything with the hidden attribute is truly hidden. */
[hidden] { display: none !important; }

:root {
  --bg: #fafafa;
  --panel: #ffffff;
  --ink: #18181b;
  --muted: #71717a;
  --faint: #d1d5db;
  --line: #e4e4e7;
  --accent-blue: #2563eb;
  --good: #0d5c1f;
  --bad: #dc2626;
  --hint: #8b4513;
  --reveal: #2563eb;
  --clue: #a16207;     /* yellow  — solved using the clue */
  --letters: #c2410c;  /* orange  — solved after revealing letters */
  --fail: #52525b;     /* black   — revealed / skipped the word */
  /* Wordle per-letter feedback fills (distinct from the tier underlines).
     Fills are darkened so white text clears WCAG 4.5:1 and the three states
     differ in luminance (not hue alone) for colorblind viewers; a redundant
     status glyph is added on the tiles/keys below. */
  --wd-correct: #15803d;  /* green  — right letter, right spot (white ~4.9:1) */
  --wd-present: #b45309;  /* amber  — right letter, wrong spot (white ~4.6:1) */
  --wd-absent:  #6b7280;  /* grey   — not in the word (white ~4.0:1) */
  --wd-on:      #ffffff;  /* text on filled tiles/keys */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --slot-base: 32px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0d0f;
    --panel: #161618;
    --ink: #f5f5f5;
    --muted: #b4bac2;
    --faint: #3f3f46;
    --line: #27272a;
    --accent-blue: #3b82f6;
    --good: #22c55e;
    --bad: #f87171;
    --hint: #f59e0b;
    --reveal: #3b82f6;
    --clue: #eab308;
    --letters: #fdba74;
    --fail: #d4d4d8;
    /* Wordle fills — tuned for contrast on the dark surface. Present is
       darkened so #f5f5f5 text clears 4.5:1; absent is lightened so an
       eliminated key separates from the idle key (~#232325) by >=3:1. */
    --wd-correct: #15803d;
    --wd-present: #8a5a0a;
    --wd-absent:  #666d79;
    --wd-on:      #f5f5f5;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  }
}
@media (max-width: 520px) {
  :root { --slot-base: 28px; }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  /* Lock to the dynamic viewport. We ship our own on-screen keyboard and never
     open the native one, so the height is stable — plain 100dvh is correct and
     consistent across browsers (a JS VisualViewport override misbehaved on
     Firefox Android and collapsed the board). overflow:hidden keeps the page
     pinned; only .chain scrolls. */
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  -webkit-font-smoothing: antialiased;
}
.app {
  width: 100%;
  max-width: 480px;
  /* App-shell: header + dock stay fixed, only .chain scrolls. Fills the body,
     which tracks the visible viewport above the keyboard. */
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

/* ---------- HEADER ---------- */
header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.wordmark {
  font-weight: 800;
  font-size: 20px;
  letter-spacing: 4px;
  text-transform: uppercase;
  line-height: 1;
}
.submeta {
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hgroup { display: flex; align-items: center; gap: var(--space-3); flex-wrap: wrap; }
.counter { font-size: 13px; font-weight: 700; color: var(--muted); white-space: nowrap; }
.dots { display: flex; gap: 3px; }
.dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--faint);
  transition: background 0.25s ease;
}
.dot.filled { background: var(--good); }

.streak {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  font-size: 13px;
  font-weight: 800;
  color: var(--ink);
  background: color-mix(in srgb, var(--hint) 14%, transparent);
  border: 1px solid color-mix(in srgb, var(--hint) 32%, transparent);
  border-radius: 999px;
  padding: 2px 8px;
  white-space: nowrap;
}
.streak .flame { font-size: 12px; }

.ctrls { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }

.btn {
  font-family: inherit;
  font-size: 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  padding: 6px 8px;
  cursor: pointer;
  min-height: 44px;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.05s ease, box-shadow 0.1s ease;
}
.btn:hover { border-color: var(--muted); background: color-mix(in srgb, var(--ink) 5%, var(--panel)); }
.btn:active { transform: scale(0.96); box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.18); }
.btn:focus-visible { outline: 2px solid var(--accent-blue); outline-offset: 1px; }
/* 44x44 hit area; glyph stays its visual size, centered. */
.btn.icon {
  width: 44px;
  height: 44px;
  min-height: 44px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-weight: 700;
}
.btn.primary {
  background: var(--accent-blue);
  color: #fff;
  border-color: var(--accent-blue);
  font-weight: 700;
  font-size: 14px;
  padding: 0 16px;
  min-height: 44px;
}
.btn.primary:hover {
  background: color-mix(in srgb, #fff 12%, var(--accent-blue));
  border-color: color-mix(in srgb, #fff 12%, var(--accent-blue));
}
.btn.primary:active { box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.28); }
.btn.ghost { background: transparent; }
.btn.ghost:hover { background: color-mix(in srgb, var(--ink) 6%, transparent); }
.btn.danger { background: var(--bad); border-color: var(--bad); color: #fff; font-weight: 700; }
.btn.danger:hover {
  background: color-mix(in srgb, #fff 12%, var(--bad));
  border-color: color-mix(in srgb, #fff 12%, var(--bad));
}
.btn.danger:active { box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.28); }

/* ---------- BANNER ---------- */
.banner {
  margin: var(--space-2) var(--space-4) 0;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink);
  background: color-mix(in srgb, var(--accent-blue) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent-blue) 28%, transparent);
  border-radius: 8px;
}

/* ---------- LEGEND ---------- */
.legend-wrap { padding: 0 var(--space-4); }
details.legend { margin: var(--space-2) 0 0; }
details.legend summary {
  cursor: pointer;
  font-size: 11px;
  color: var(--muted);
  list-style: none;
  user-select: none;
}
details.legend summary::-webkit-details-marker { display: none; }
details.legend summary::before { content: "▸ "; }
details.legend[open] summary::before { content: "▾ "; }
.legend-body {
  display: flex; flex-wrap: wrap; gap: var(--space-3);
  font-size: 11px; color: var(--muted);
  padding: var(--space-2) 0;
}
.legend-body span { display: inline-flex; align-items: center; gap: 4px; }
.swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; flex: 0 0 auto; }
.sw-green { background: var(--good); }
.sw-yellow { background: var(--clue); }
.sw-orange { background: var(--letters); }
.sw-black { background: var(--fail); }

/* ---------- 🎨 LEGEND POPOVER (opened by the header button; replaces the inline legend) ---------- */
.legend-popover {
  position: fixed; z-index: 50; min-width: 190px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: var(--shadow); padding: 10px; font-size: 12px; color: var(--ink);
}
.legend-popover:focus { outline: none; }
.legend-pop-title { font-weight: 700; margin-bottom: 6px; }
.legend-pop-row { display: flex; align-items: center; gap: 8px; margin: 5px 0; }
.legend-pop-sw { width: 13px; height: 13px; border-radius: 3px; display: inline-block; flex: 0 0 auto; }

/* ---------- 💬 FEEDBACK-TEXT TOGGLE ---------- */
body.hide-feedback #status { display: none; }
.btn.icon.is-off { opacity: 0.5; position: relative; }
.btn.icon.is-off::after {
  content: ""; position: absolute; left: 9px; right: 9px; top: 50%;
  height: 2px; background: currentColor; transform: rotate(-18deg);
}

/* ---------- FIRST-RUN COACH-MARKS (spotlight on the top menu) ---------- */
.coach-overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6); z-index: 60; }
body.coach-active .ctrls { position: relative; z-index: 65; }
.coach-tip {
  position: fixed; left: 12px; right: 12px; top: 64px; z-index: 62;
  max-width: 460px; margin: 0 auto;
  background: var(--accent-blue); color: #fff; border-radius: 12px;
  padding: 14px 16px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 10px; align-items: flex-start;
}
.coach-tip-text { margin: 0; font-size: 14px; font-weight: 600; line-height: 1.4; }
.coach-tip .btn.primary { align-self: flex-end; }

/* ---------- CHAIN ---------- */
.chain {
  flex: 1;
  min-height: 0; /* allow the scroll area to shrink inside the flex column */
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  /* Top-align the board so the first word sits right under the legend (no floating
     gap on tall iPad / desktop screens). It still scrolls when the board grows past
     the viewport, so nothing is ever clipped. */
  justify-content: flex-start;
  padding: var(--space-3) var(--space-4) var(--space-3);
}
.connector {
  height: 0;
  opacity: 0;
  overflow: hidden;
  transition: height 0.3s ease, opacity 0.3s ease;
  padding-left: 28px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11.5px;
  color: var(--muted);
}
.connector.open {
  height: auto;
  min-height: 22px;
  opacity: 1;
  padding-top: 2px;
  padding-bottom: 2px;
}
.connector .cicon { font-size: 12px; opacity: 0.85; }
.connector .clabel { line-height: 1.3; }

.row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) 0 var(--space-1) var(--space-2);
  border-left: 4px solid transparent;
  border-radius: 4px;
  position: relative;
  min-height: 40px;
  transition: opacity 0.25s ease, background 0.2s ease;
}
.row .idx {
  width: 18px;
  text-align: right;
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  flex: 0 0 auto;
  font-variant-numeric: tabular-nums;
}
.row .glyph {
  width: 16px;
  flex: 0 0 auto;
  font-size: 13px;
  text-align: center;
  color: var(--muted);
}
.slots {
  display: flex;
  align-items: flex-end;
  flex: 1 1 auto;
  min-width: 0;
  flex-wrap: nowrap;
}
.wordgroup { display: flex; gap: 4px; }
.wordgap { width: 14px; flex: 0 0 auto; }
.slot {
  width: var(--slotSize, var(--slot-base));
  height: var(--slotSize, var(--slot-base));
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 2px solid var(--faint);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-weight: 700;
  font-size: calc(var(--slotSize, var(--slot-base)) * 0.52);
  color: var(--ink);
  position: relative;
  transition: transform 0.18s ease, border-color 0.2s ease, color 0.2s ease;
}
.row .gutter {
  width: 28px;
  flex: 0 0 auto;
  text-align: center;
  color: var(--accent-blue);
  font-size: 14px;
}

/* caret */
.slot.caret::after {
  content: "";
  position: absolute;
  bottom: 4px;
  width: 60%;
  height: 2px;
  background: var(--accent-blue);
  animation: blink 1s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* tile tints
   NOTE: "green" carries two distinct meanings, intentionally differentiated by
   TREATMENT, not just hue, so they don't conflate:
     - SOLVED-earned (here): thin underline + the row-level ✓ glyph, no fill.
     - Frozen Wordle "correct" (.g-correct below): solid fill + corner ✓ glyph.
   Keep this split if these greens are ever retuned. */
.slot.t-earned { border-bottom-color: var(--good); color: var(--ink); }
.slot.t-hinted { border-bottom-color: var(--hint); color: var(--hint); }     /* active-play letter reveal */
.slot.t-given { border-bottom-color: var(--muted); color: var(--ink); }      /* the given starter word */
.slot.t-clue { border-bottom-color: var(--clue); color: var(--ink); }        /* solved via clue */
.slot.t-letters { border-bottom-color: var(--letters); color: var(--letters); } /* solved after revealing letters */
.slot.t-revealed { border-bottom-color: var(--fail); color: var(--fail); }   /* revealed / skipped */

/* Wordle per-letter feedback: FILLED tiles (frozen after a wrong full guess).
   These intentionally fill the whole tile to read as Wordle squares, distinct
   from the thin tier underlines above. */
.slot.g-correct,
.slot.g-present,
.slot.g-absent {
  color: var(--wd-on);
  border-bottom-color: transparent;
  border-radius: 4px;
}
.slot.g-correct { background: var(--wd-correct); }
.slot.g-present { background: var(--wd-present); }
.slot.g-absent  { background: var(--wd-absent); }
/* The active row tints its slots blue; override that for frozen feedback. */
.row.active .slot.g-correct,
.row.active .slot.g-present,
.row.active .slot.g-absent { border-bottom-color: transparent; }

/* Redundant NON-HUE channel (colorblind safety): a tiny status glyph in the
   bottom-right corner so the three states differ by shape, not color alone.
   check = correct, ring = present/elsewhere, dash = absent. Distinct from the solved
   tier states, which use a row-level ✓/👁/• glyph + an underline (no fill). */
.slot.g-correct, .slot.g-present, .slot.g-absent { overflow: hidden; }
.slot.g-correct::after,
.slot.g-present::after,
.slot.g-absent::after {
  position: absolute;
  bottom: 1px;
  right: 2px;
  font-size: 9px;
  line-height: 1;
  font-weight: 700;
  color: var(--wd-on);
  opacity: 0.95;
}
.slot.g-correct::after { content: "\2713"; }   /* ✓ check */
.slot.g-present::after { content: "\2218"; }   /* ∘ ring  */
.slot.g-absent::after  { content: "\2013"; }   /* – dash  */

/* row states */
.row.locked { opacity: 0.55; }
.row.locked .slot { border-bottom-color: var(--faint); }
.row.given .idx { opacity: 0.45; font-weight: 600; }

.row.active {
  border-left-color: var(--accent-blue);
  background: color-mix(in srgb, var(--accent-blue) 11%, transparent);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--accent-blue) 22%, transparent);
}
.row.active:focus-within {
  outline: 2px solid var(--accent-blue);
  outline-offset: 1px;
}
.row.active .slot {
  --slotSize: var(--slotActive, var(--slot-base));
  border-bottom-color: var(--accent-blue);
}
.row.active .idx { color: var(--accent-blue); }

.row.solved .slot { font-weight: 800; }

.row.wrong { animation: shake 0.38s ease; }
/* Don't paint the red "wrong" underline beneath frozen Wordle feedback tiles,
   which would read as a contradictory red line under a green/amber fill. */
.row.wrong .slot:not(.g-correct):not(.g-present):not(.g-absent) { border-bottom-color: var(--bad) !important; }
@keyframes shake {
  0%,100% { transform: translateX(0); }
  15% { transform: translateX(-7px); }
  30% { transform: translateX(6px); }
  45% { transform: translateX(-5px); }
  60% { transform: translateX(4px); }
  75% { transform: translateX(-2px); }
}

.slot.pop { animation: pop 0.12s ease; }
@keyframes pop { 50% { transform: scale(1.12); } }

.slot.flip { animation: flip 0.2s ease; }
@keyframes flip { 0% { transform: scale(1); } 40% { transform: scale(1.18); } 100% { transform: scale(1); } }

/* Cascade: ~400ms each, staggered ~70ms left-to-right across the row's slots.
   JS may also set `animation-delay` inline; these nth-of-type defaults give a
   sensible stagger out of the box. */
.slot.cascade {
  animation: cascade 0.4s ease both;
  animation-delay: calc(var(--cascade-i, 0) * 70ms);
}
.slot.cascade:nth-of-type(1) { --cascade-i: 0; }
.slot.cascade:nth-of-type(2) { --cascade-i: 1; }
.slot.cascade:nth-of-type(3) { --cascade-i: 2; }
.slot.cascade:nth-of-type(4) { --cascade-i: 3; }
.slot.cascade:nth-of-type(5) { --cascade-i: 4; }
.slot.cascade:nth-of-type(6) { --cascade-i: 5; }
.slot.cascade:nth-of-type(7) { --cascade-i: 6; }
.slot.cascade:nth-of-type(8) { --cascade-i: 7; }
.slot.cascade:nth-of-type(9) { --cascade-i: 8; }
.slot.cascade:nth-of-type(n+10) { --cascade-i: 9; }
@keyframes cascade {
  0% { transform: scale(1); }
  40% { transform: scale(1.25); background: color-mix(in srgb, var(--good) 25%, transparent); }
  100% { transform: scale(1); }
}

/* category chip above active row */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  font-weight: 600;
  color: var(--hint);
  background: color-mix(in srgb, var(--hint) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--hint) 35%, transparent);
  border-radius: 8px;
  padding: 2px 8px;
  margin: 2px 0 4px 28px;
}

/* Crossword-style clue card shown for the active word after the first hint. */
.cluecard {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 13.5px;
  color: var(--ink);
  background: color-mix(in srgb, var(--accent-blue) 17%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent-blue) 35%, transparent);
  border-left: 4px solid var(--accent-blue);
  border-radius: 10px;
  padding: 7px 11px;
  margin: 2px 0 6px 28px;
}
.cluecard .clue-tag {
  flex-shrink: 0;
  display: inline-flex;
  align-items: baseline;
  gap: 4px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: color-mix(in srgb, var(--ink) 35%, var(--accent-blue));
  transform: translateY(-1px);
}
.cluecard .clue-tag::before {
  content: "💡";
  font-size: 11px;
  -webkit-text-fill-color: initial;
}
.cluecard .clue-text { line-height: 1.35; font-weight: 500; }

/* ---------- DOCK ---------- */
.dock {
  flex-shrink: 0;
  border-top: 1px solid var(--line);
  background: var(--panel);
  padding: var(--space-3) var(--space-4) calc(var(--space-3) + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
/* playArea stacks the input row and the hint row; give them real separation
   (the dock's gap only applies between #playArea and #status). */
#playArea { display: flex; flex-direction: column; gap: var(--space-3); }
.hint-row { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }
.btn.hint { font-size: 12px; min-height: 44px; padding: 0 12px; }
.hint-count { font-size: 11px; color: var(--muted); }

/* Color-feedback meter: labeled, always-visible indicator (directly above the
   Hint button) for the metered mini-Wordle letter colors. Filled dots = guesses
   that still color; faded = spent. When empty it shifts to a "try a clue" nudge. */
.feedback-meter {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-right: auto;   /* push the Hint button to the right edge of the row */
  font-size: 12px;
  color: var(--muted);
}
.feedback-meter .fm-label { font-weight: 600; letter-spacing: 0.2px; }
.feedback-meter .fm-dots { display: inline-flex; gap: 4px; font-size: 13px; line-height: 1; }
.feedback-meter .fm-dot.on { color: var(--accent-blue); }
.feedback-meter .fm-dot.off { color: var(--muted); opacity: 0.55; }
.feedback-meter.spent { color: color-mix(in srgb, var(--ink) 35%, var(--accent-blue)); font-weight: 600; }
.confirm { font-size: 12px; color: var(--bad); display: inline-flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.confirm .btn { min-height: 44px; padding: 4px 10px; }
.status {
  font-size: 12.5px;
  color: var(--muted);
  min-height: 18px;
  text-align: center;
}
.status.bad { color: var(--bad); }
.status.good { color: var(--good); }

/* ---------- ON-SCREEN KEYBOARD ----------
   Wordle-style. Replaces the native text input so the mobile keyboard never
   opens; footprint is fixed (~3 compact rows) leaving room for the board. */
.keyboard { display: flex; flex-direction: column; gap: 6px; touch-action: manipulation; }
.kb-row { display: flex; gap: 5px; justify-content: center; }
.key {
  flex: 1 1 0;
  min-width: 0;
  height: 50px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: color-mix(in srgb, var(--ink) 6%, var(--panel));
  color: var(--ink);
  font-family: inherit;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.1s ease, transform 0.04s ease;
}
.key:hover { background: color-mix(in srgb, var(--ink) 12%, var(--panel)); }
.key:active { transform: translateY(1px); background: color-mix(in srgb, var(--accent-blue) 24%, var(--panel)); }
.key:focus-visible { outline: 2px solid var(--accent-blue); outline-offset: 1px; }
.key-wide { flex: 1.5 1 0; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px; }

/* Accumulated Wordle key colors for the current word (filled keys). */
.key.key-correct,
.key.key-present,
.key.key-absent {
  color: var(--wd-on);
  border-color: transparent;
}
.key.key-correct { background: var(--wd-correct); }
.key.key-present { background: var(--wd-present); }
.key.key-absent  { background: var(--wd-absent); }
/* Keep the colored state legible on hover/active rather than washing it out. */
.key.key-correct:hover { background: color-mix(in srgb, #fff 10%, var(--wd-correct)); }
.key.key-present:hover { background: color-mix(in srgb, #fff 10%, var(--wd-present)); }
.key.key-absent:hover  { background: color-mix(in srgb, #fff 10%, var(--wd-absent)); }
/* Redundant NON-HUE channel on the colored keys, matching the tiles, so an
   eliminated/known key is distinguishable without relying on color. */
.key.key-correct, .key.key-present, .key.key-absent { position: relative; }
.key.key-correct::after,
.key.key-present::after,
.key.key-absent::after {
  position: absolute;
  top: 2px;
  right: 4px;
  font-size: 9px;
  line-height: 1;
  font-weight: 700;
  color: var(--wd-on);
  opacity: 0.9;
}
.key.key-correct::after { content: "\2713"; }  /* ✓ check */
.key.key-present::after { content: "\2218"; }  /* ∘ ring  */
.key.key-absent::after  { content: "\2013"; }  /* – dash  */
@media (max-width: 380px) {
  .key { height: 46px; font-size: 16px; }
  .kb-row { gap: 4px; }
  .keyboard { gap: 5px; }
}

/* Short viewports (landscape phones, split-screen, small windows): the dock +
   3 key rows aren't shrinkable by width alone, so also shrink keys and tiles by
   HEIGHT so the scrolling board never gets squeezed to a sliver. */
@media (max-height: 600px) {
  :root { --slot-base: 26px; }
  .key { height: 38px; font-size: 15px; }
  .keyboard { gap: 4px; }
  .kb-row { gap: 4px; }
}
@media (max-height: 460px) {
  :root { --slot-base: 22px; }
  .key { height: 30px; font-size: 14px; }
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- WIN PANEL ---------- */
.win {
  text-align: center;
  padding: var(--space-2) 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.win h2 { margin: 0; font-size: 22px; }
.win .stat { font-size: 13px; color: var(--muted); }
.win .squares {
  font-size: 18px;
  letter-spacing: 2px;
  line-height: 1.2;
  overflow-wrap: anywhere;
}
.win .btns { display: flex; gap: var(--space-2); justify-content: center; margin-top: var(--space-1); flex-wrap: wrap; }
.win .btns .btn { min-height: 44px; padding: 0 18px; font-size: 14px; }

/* ---------- SCREENS (archive / state) ---------- */
.screen {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-4);
}
.screen-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}
.screen-head h2 { margin: 0; font-size: 20px; }
.screen-sub { font-size: 12.5px; color: var(--muted); margin: var(--space-2) 0 var(--space-4); }

.archive-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.archive-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  text-align: left;
  font-family: inherit;
  font-size: 14px;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.05s ease;
}
.archive-item:hover { border-color: var(--muted); }
.archive-item:active { transform: scale(0.99); }
.archive-item:focus-visible { outline: 2px solid var(--accent-blue); outline-offset: 1px; }
.archive-item.today {
  border-color: var(--accent-blue);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--accent-blue) 30%, transparent);
}
.archive-item .a-num {
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  min-width: 34px;
}
.archive-item .a-date { color: var(--muted); font-size: 13px; flex: 1 1 auto; }
.archive-item .a-today-tag {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--accent-blue);
}
.archive-item .a-glyph { font-size: 16px; flex: 0 0 auto; width: 22px; text-align: center; }
.a-glyph.clean { color: var(--good); }
.a-glyph.help { color: var(--hint); }
.a-glyph.unplayed { color: var(--faint); }

/* ---------- STATE SCREEN (loading / error / empty) ---------- */
.state-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: var(--space-3);
  color: var(--muted);
}
.state-screen h2 { margin: 0; font-size: 18px; color: var(--ink); }
.state-screen p { margin: 0; font-size: 13px; max-width: 320px; }
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--line);
  border-top-color: var(--accent-blue);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- MODALS ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  z-index: 100;
}
.modal {
  width: 100%;
  max-width: 420px;
  max-height: 85dvh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  animation: modalIn 0.18s ease;
}
.modal.small { max-width: 340px; }
@keyframes modalIn { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4) var(--space-4) var(--space-2);
}
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-body { padding: 0 var(--space-4) var(--space-4); }
.modal-body p { font-size: 13.5px; line-height: 1.5; color: var(--ink); }
.modal-body .how-list { font-size: 13.5px; line-height: 1.5; padding-left: 18px; color: var(--ink); }
.modal-body .how-list li { margin-bottom: 6px; }
.modal-body kbd {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 11px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 1px 5px;
}
.how-legend {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12.5px;
  color: var(--muted);
}
.how-legend span { display: inline-flex; align-items: center; gap: 6px; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  margin-top: var(--space-3);
}
.modal-actions .btn { min-height: 44px; padding: 0 16px; }

/* ---------- TOAST ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(8px);
  background: var(--ink);
  color: var(--bg);
  font-size: 13px;
  font-weight: 600;
  padding: 10px 16px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  z-index: 200;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- VIEW TOGGLING ---------- */
/* When the archive or a state screen is shown, hide the game chrome. */
body.view-archive .legend-wrap,
body.view-archive .chain,
body.view-archive .dock,
body.view-archive .banner,
body.view-state .legend-wrap,
body.view-state .chain,
body.view-state .dock,
body.view-state .banner,
body.view-state header .hgroup,
body.view-state header .ctrls { display: none; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
  .slot.caret::after { animation: none; }
  /* No spinning for reduced-motion users; show a steady, slightly dimmed ring. */
  .spinner {
    animation: none !important;
    opacity: 0.6;
  }
}

/* =========================================================================
   RESPONSIVE — small screens
   ========================================================================= */

/* Stack win actions and modal/confirm actions full-width on narrow phones. */
@media (max-width: 480px) {
  .win .btns {
    flex-direction: column;
    align-items: stretch;
  }
  .win .btns .btn { width: 100%; }

  .modal-actions {
    flex-direction: column;
    align-items: stretch;
  }
  .modal-actions .btn { width: 100%; }

  .confirm {
    flex-direction: column;
    align-items: stretch;
    width: 100%;
  }
  .confirm .btn { width: 100%; }

  /* Keep the share grid on one line on small screens. */
  .win .squares {
    font-size: 14px;
    letter-spacing: 0;
  }
}

/* Squeeze the legend so its four items fit on a single line. */
@media (max-width: 400px) {
  .legend-body {
    gap: var(--space-2);
    font-size: 10px;
    flex-wrap: nowrap;
  }
  .legend-body span { gap: 3px; }
  .swatch { width: 10px; height: 10px; }
}

/* On touch-sized screens, thicken the active row's left accent so the
   currently-playing row reads clearly at a glance. */
@media (max-width: 520px) {
  .row { border-left-width: 5px; }
  .row.active {
    border-left-width: 6px;
    padding-left: var(--space-1);
  }
}

/* Tighten the wordmark on the smallest phones. */
@media (max-width: 375px) {
  .wordmark {
    font-size: 18px;
    letter-spacing: 2px;
  }
}

/* ====================== DESKTOP / LARGE SCREENS ======================
   On phones the app fills the whole viewport (correct). On a roomy screen that
   left the 480px column stretched full-height with big empty side columns and a
   gap between the centered board and the bottom-pinned keyboard. Instead, frame
   the app as a centered "card" with a sensible max height so the surrounding
   space reads as an intentional matte rather than dead air. Gated on BOTH a wide
   viewport and enough height so it never kicks in on landscape phones. */
@media (min-width: 600px) and (min-height: 640px) {
  body {
    /* Subtle backdrop so the card stands out from the matte. */
    background:
      radial-gradient(120% 80% at 50% 0%, color-mix(in srgb, var(--accent-blue) 7%, var(--bg)), var(--bg) 60%);
    padding: var(--space-4);
  }
  .app {
    height: min(820px, 100dvh - 2 * var(--space-4));
    max-height: 100%;
    border: 1px solid var(--line);
    border-radius: 20px;
    box-shadow: var(--shadow);
    overflow: hidden;             /* keep the rounded corners clean */
    background: var(--bg);
  }
  /* Round the dock's bottom corners to match the card. */
  .dock { border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; }
}

/* ====================== NATIVE-ONLY UI ======================
   Rendered only inside the iOS/Android app (injected by native.js when
   window.Capacitor is present). Harmless/unused on the web build. */
.native-settings {
  margin-top: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px solid var(--line);
}
.native-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13.5px;
  color: var(--ink);
  cursor: pointer;
}
.native-toggle input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent-blue);
  cursor: pointer;
}

/* ====================== STATS MODAL ====================== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}
@media (max-width: 380px) { .stats-grid { grid-template-columns: repeat(2, 1fr); } }
.stat-tile {
  background: color-mix(in srgb, var(--ink) 5%, var(--panel));
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 6px;
  text-align: center;
}
.stat-value { font-size: 24px; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 10px; color: var(--muted); margin-top: 3px; text-transform: uppercase; letter-spacing: 0.4px; }
.stats-sub { font-size: 12.5px; color: var(--muted); text-align: center; margin: 0 0 var(--space-2); }
.stats-h {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.6px;
  color: var(--muted); margin: var(--space-3) 0 var(--space-2);
}
.dist { display: flex; flex-direction: column; gap: 4px; }
.dist-row { display: flex; align-items: center; gap: 8px; }
.dist-k {
  width: 14px; text-align: right; font-size: 12px; font-weight: 700;
  color: var(--muted); font-variant-numeric: tabular-nums; flex: 0 0 auto;
}
.dist-barwrap { flex: 1 1 auto; display: flex; }
.dist-bar {
  min-width: 0; height: 20px; line-height: 20px;
  background: var(--faint); color: var(--ink);
  font-size: 11px; font-weight: 700; text-align: right;
  border-radius: 5px; white-space: nowrap;
  transition: width 0.45s cubic-bezier(.2,.8,.2,1);
}
.dist-bar:not(:empty) { min-width: 24px; padding: 0 6px; }
.dist-bar.perfect { background: var(--good); color: #fff; }
.stats-tiers {
  display: flex; justify-content: space-between; gap: var(--space-2);
  margin-top: var(--space-3); padding-top: var(--space-3);
  border-top: 1px solid var(--line);
  font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums;
}
.stat-alltime { font-weight: 600; }

/* ====================== ADD-TO-HOME-SCREEN SHEET ====================== */
.install-sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 300;
  display: flex;
  justify-content: center;
  padding: 0 var(--space-3) calc(var(--space-3) + env(safe-area-inset-bottom));
  pointer-events: none;
  transform: translateY(130%);
  transition: transform 0.34s cubic-bezier(.2,.85,.25,1);
}
.install-sheet.show { transform: translateY(0); }
.install-card {
  pointer-events: auto;
  position: relative;
  width: 100%;
  max-width: 440px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.install-top { display: flex; align-items: center; gap: 12px; padding-right: 24px; }
.install-icon { width: 48px; height: 48px; border-radius: 11px; flex: 0 0 auto; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18); }
.install-body { flex: 1 1 auto; min-width: 0; }
.install-title { font-weight: 800; font-size: 15px; line-height: 1.25; }
.install-sub {
  font-size: 12.5px; color: var(--muted); margin-top: 2px; line-height: 1.35;
  display: inline-flex; align-items: center; flex-wrap: wrap; gap: 3px;
}
.install-actions { display: flex; gap: 8px; }
.install-actions .btn { flex: 1 1 0; min-height: 40px; }
.install-cta { white-space: nowrap; }
.ios-share-glyph { width: 15px; height: 15px; color: var(--accent-blue); flex: 0 0 auto; vertical-align: middle; }
.install-close {
  position: absolute; top: 8px; right: 8px;
  width: 26px; height: 26px; padding: 0;
  border: none; background: transparent; color: var(--muted);
  font-size: 13px; line-height: 1; cursor: pointer; border-radius: 7px;
}
.install-close:hover { background: color-mix(in srgb, var(--ink) 8%, transparent); color: var(--ink); }
.install-close:focus-visible { outline: 2px solid var(--accent-blue); outline-offset: 1px; }

/* ---- WIN PANEL PAYOFF ---- */
.win-theme {
  font-weight: 800; font-size: 17px; margin-top: 2px;
}
.win-chain {
  display: flex; flex-wrap: wrap; justify-content: center; align-items: center;
  gap: 4px 6px; margin: 6px 0 2px; font-size: 13px; font-weight: 700;
  color: var(--muted);
}
.win-chain .win-word { color: var(--ink); animation: winWordIn 0.32s ease both; }
.win-chain .win-arrow { color: var(--accent-blue); }
@keyframes winWordIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.win-time { font-weight: 700; }
.win-plain { color: var(--muted); }
@media (prefers-reduced-motion: reduce) {
  .win-chain .win-word { animation: none; }
}

/* ---- LARGE-TEXT MODE (accessibility toggle) ---- */
.large-text { --slot-base: 40px; }
.large-text .key { font-size: 20px; height: 56px; }
.large-text .status, .large-text .clue-text, .large-text .hint-count { font-size: 15px; }
.large-text .connector .clabel { font-size: 13.5px; }
@media (max-width: 520px) { .large-text { --slot-base: 34px; } }
.settings-row { margin-top: var(--space-3); padding-top: var(--space-3); border-top: 1px solid var(--line); }
.setting-toggle { display: flex; align-items: center; gap: 10px; font-size: 14px; cursor: pointer; }
.setting-toggle input { width: 18px; height: 18px; accent-color: var(--accent-blue); cursor: pointer; }

/* =========================================================================
   HOW-TO-PLAY WALKTHROUGH / HELP HUB  (the ? overlay, rendered by howto.js)
   ========================================================================= */
.how-modal { max-width: 440px; }
#howBody { padding-top: var(--space-2); }

/* Hub — two big choices */
.how-hub { display: flex; flex-direction: column; gap: var(--space-2); }
.how-option {
  display: flex; align-items: center; gap: 12px;
  justify-content: flex-start; text-align: left;
  min-height: 56px; padding: 0 14px;
  font-size: 15px; font-weight: 600;
}
.how-opt-ico { display: inline-flex; align-items: center; }
.setting-ico { display: inline-flex; align-items: center; }
.how-opt-label { flex: 1 1 auto; }
.how-opt-chev { color: var(--muted); font-size: 20px; font-weight: 700; }

/* Walkthrough card */
.how-walk { display: flex; flex-direction: column; align-items: center; text-align: center; gap: var(--space-2); }
.how-visual {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; min-height: 76px; margin-top: var(--space-2);
}
.how-headline { margin: 4px 0 0; font-size: 19px; font-weight: 800; line-height: 1.2; }
/* The headline takes programmatic focus on open (SR announces each card; keeps
   Enter from skipping). It's not in the tab order, so suppress the focus ring. */
.how-headline:focus, .how-headline:focus-visible { outline: none; }
/* One line per card. nowrap forces it (max-width:none alone wasn't enough with
   wider system fonts, where the line still wrapped). On phones we drop back to
   normal wrapping so a long-ish body can never overflow the narrow modal. */
.how-bodytext { margin: 0; max-width: none; white-space: nowrap; font-size: 14px; line-height: 1.45; color: var(--muted); }
@media (max-width: 460px) { .how-bodytext { white-space: normal; } }
.how-cap { font-size: 12px; color: var(--muted); max-width: 34ch; }
.how-bridges { font-size: 12px; color: var(--muted); }
.how-daily-line { font-size: 30px; letter-spacing: 8px; padding-left: 8px; }

/* Demo tiles (decoupled from the game board's .slot system) */
.how-tiles { display: flex; gap: 6px; }
.how-tile {
  width: 30px; height: 36px; border-radius: 7px;
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 16px;
  border: 2px solid var(--line); background: transparent; color: var(--ink);
  animation: howPop 0.3s ease both;
}
.how-tile.is-given { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.how-tile.is-correct { background: var(--wd-correct); color: var(--wd-on); border-color: transparent; }
.how-tile.is-present { background: var(--wd-present); color: var(--wd-on); border-color: transparent; }
.how-tile.is-absent  { background: var(--wd-absent);  color: var(--wd-on); border-color: transparent; }
@keyframes howPop { from { opacity: 0; transform: translateY(5px) scale(0.95); } to { opacity: 1; transform: none; } }

/* Vertical color legend on the "guess" card: one colored tile per row beside what
   its color means. Rows left-aligned; tiles a touch smaller than the demo tiles. */
.how-cat { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.how-cat-row { display: flex; align-items: center; gap: 10px; }
.how-cat-row .how-tile { width: 28px; height: 30px; font-size: 15px; }
.how-cat-text { font-size: 12.5px; color: var(--muted); text-align: left; line-height: 1.25; }

/* Progress dots */
.how-dots { display: flex; gap: 6px; margin-top: var(--space-2); }
.how-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--line); transition: background 0.15s ease; }
.how-dot.is-on { background: var(--accent-blue); }

/* Footer nav (Back on the left · Skip/Next on the right) */
.how-foot { display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); margin-top: var(--space-4); }
.how-foot-l, .how-foot-r { display: flex; align-items: center; gap: var(--space-2); }
.how-foot .btn { min-height: 44px; }

/* Settings screen */
.how-settings { display: flex; flex-direction: column; gap: var(--space-3); }
.how-settings .setting-toggle { padding: 4px 0; }

/* Buried backup/restore disclosure — deliberately understated so it isn't
   found by accident; expands to the Download / Restore buttons. */
.how-advanced { margin-top: 2px; border-top: 1px solid var(--line); padding-top: 8px; }
.how-advanced > summary {
  cursor: pointer; list-style: none; user-select: none;
  font-size: 12px; color: var(--muted); padding: 4px 0;
}
.how-advanced > summary::-webkit-details-marker { display: none; }
.how-advanced > summary::before { content: "\25B8 "; } /* ▸ */
.how-advanced[open] > summary::before { content: "\25BE "; } /* ▾ */
.how-advanced-note { margin: 6px 0 8px; font-size: 11.5px; line-height: 1.4; color: var(--muted); }
.how-advanced .btn { display: block; width: 100%; margin: 6px 0; }

@media (max-width: 480px) {
  .how-foot { flex-wrap: wrap; }
  .how-foot-r { flex: 1 1 auto; justify-content: flex-end; }
}

/* ---- LIVE HEADER TIMER ---- */
.livetimer {
  font-size: 13px;
  font-weight: 800;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
body.hide-timer .livetimer { display: none; }
