/* ==========================================================================
   Verbraid
   --------------------------------------------------------------------------
   Layout notes:
   - Four boards sit side by side on a wide screen so the chain reads left to
     right, which is the whole point of the game. Below 1100px they fall to two
     columns, and on a phone only the active board expands while the rest stay
     as compact headers so all four remain reachable without endless scrolling.
   - Colour is never the only carrier of meaning. Every tile and key also has
     screen-reader text, and the high contrast option swaps green/yellow for
     blue/orange, which stays distinguishable with deuteranopia and protanopia.
   ========================================================================== */

:root {
  --bg: #0f1420;
  --bg-soft: #151c2b;
  --panel: #1a2233;
  --panel-2: #212b40;
  --line: #2c374e;
  --line-soft: #232d42;
  --text: #e9edf5;
  --muted: #94a1ba;
  --muted-dim: #6b7793;

  --correct: #4ade80;
  --correct-ink: #06240f;
  --present: #fbbf24;
  --present-ink: #2b1d00;
  --absent: #38425a;
  --absent-ink: #aeb8cc;

  --accent: #7dd3fc;
  --danger: #f87171;

  --heat-freezing: #3b6fd4;
  --heat-frigid: #2f8fd0;
  --heat-cold: #22a5b8;
  --heat-cool: #2fb39a;
  --heat-mild: #a3bf3f;
  --heat-warm: #e8a33d;
  --heat-hot: #f2762f;
  --heat-scorching: #ef4444;
  --heat-exact: #4ade80;
  --heat-unknown: #4a5570;

  --radius: 12px;
  --radius-sm: 7px;
  --tile-gap: 5px;

  --font: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono: "SF Mono", "Cascadia Mono", ui-monospace, Menlo, Consolas, monospace;

  --dur: 180ms;
}

/* Blue / orange pair for colour vision deficiency. */
[data-contrast="high"] {
  --correct: #3b82f6;
  --correct-ink: #ffffff;
  --present: #f97316;
  --present-ink: #ffffff;
  --absent: #4b5568;
  --absent-ink: #e4e9f2;
  --heat-exact: #3b82f6;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background:
    radial-gradient(1100px 620px at 50% -10%, #1b2740 0%, transparent 62%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }

button { font: inherit; color: inherit; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: 50%; top: 0;
  transform: translate(-50%, -120%);
  z-index: 60;
  background: var(--panel-2);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  border: 1px solid var(--line);
  border-top: 0;
  text-decoration: none;
  transition: transform var(--dur);
}
.skip-link:focus { transform: translate(-50%, 0); }

:where(button, input, a, [tabindex]):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ------------------------------------------------------------------ topbar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px clamp(12px, 3vw, 24px);
  border-bottom: 1px solid var(--line-soft);
  background: rgba(15, 20, 32, 0.72);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }
.brand-mark { color: var(--correct); display: grid; place-items: center; flex: none; }
.brand-text { min-width: 0; }

.brand h1 {
  font-size: clamp(1.05rem, 1rem + 0.9vw, 1.5rem);
  letter-spacing: 0.26em;
  line-height: 1.1;
}
.brand-sub {
  margin: 2px 0 0;
  font-size: 0.75rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions { display: flex; gap: 6px; flex: none; }

.icon-btn {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  font-size: 1.05rem;
  line-height: 1;
  transition: background var(--dur), border-color var(--dur), transform var(--dur);
}
.icon-btn:hover { background: var(--panel-2); border-color: #3a4762; }
.icon-btn:active { transform: translateY(1px); }

/* -------------------------------------------------------------------- main */

main {
  flex: 1;
  width: 100%;
  max-width: 1320px;
  margin: 0 auto;
  padding: 16px clamp(10px, 2.5vw, 22px) 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* --------------------------------------------------------------- pool bar */

.pool {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  background: linear-gradient(180deg, var(--panel) 0%, var(--bg-soft) 100%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 11px 16px;
}

.pool-count { display: flex; align-items: baseline; gap: 9px; flex-wrap: wrap; }

.pool-value {
  font-size: 1.6rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  color: var(--correct);
}
.pool.is-low .pool-value { color: var(--danger); }
.pool-label { font-size: 0.78rem; color: var(--muted); }

.pool-pips { display: flex; gap: 4px; flex-wrap: wrap; }
.pip {
  width: 9px; height: 9px;
  border-radius: 50%;
  background: var(--correct);
  opacity: 0.9;
}
.pip.is-spent { background: var(--absent); opacity: 0.55; }
.pool.is-low .pip:not(.is-spent) { background: var(--danger); }

/* ---------------------------------------------------------------- the rail */

.rail-wrap { overflow-x: auto; padding-bottom: 2px; }

.rail {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  min-width: max-content;
}

.rail-node {
  --node: 34px;
  width: var(--node); height: var(--node);
  border-radius: 10px;
  display: grid; place-items: center;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--muted);
  flex: none;
  transition: background var(--dur), border-color var(--dur), color var(--dur);
}
.rail-node.is-active {
  border-color: var(--accent);
  color: var(--text);
  box-shadow: 0 0 0 3px rgba(125, 211, 252, 0.16);
}
.rail-node.is-solved {
  background: var(--correct);
  border-color: var(--correct);
  color: var(--correct-ink);
}

.rail-join {
  display: flex;
  align-items: center;
  flex: none;
}
.rail-join::before,
.rail-join::after {
  content: "";
  width: clamp(8px, 2vw, 20px);
  height: 2px;
  background: var(--line);
}
.rail-letter {
  width: 24px; height: 24px;
  border-radius: 50%;
  display: grid; place-items: center;
  background: var(--bg-soft);
  border: 1px dashed var(--line);
  font-family: var(--mono);
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--muted-dim);
}
.rail-letter.is-known {
  border-style: solid;
  border-color: var(--correct);
  color: var(--correct);
  background: rgba(74, 222, 128, 0.1);
}

/* ------------------------------------------------------------------ boards */

.boards {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  align-items: start;
}

.board {
  background: var(--bg-soft);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 9px;
  transition: border-color var(--dur), background var(--dur);
}
.board.is-active {
  border-color: var(--accent);
  background: var(--panel);
  box-shadow: 0 0 0 3px rgba(125, 211, 252, 0.11);
}
.board.is-solved { border-color: rgba(74, 222, 128, 0.4); }
.board.is-lost { border-color: rgba(248, 113, 113, 0.4); }

.board-head {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  background: none;
  border: 0;
  padding: 2px;
  cursor: pointer;
  text-align: left;
  border-radius: var(--radius-sm);
}
.board.is-solved .board-head,
.board.is-lost .board-head { cursor: default; }

.board-num {
  width: 22px; height: 22px;
  border-radius: 6px;
  display: grid; place-items: center;
  background: var(--panel-2);
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--muted);
  flex: none;
}
.board.is-active .board-num { background: var(--accent); color: #06263a; }
.board.is-solved .board-num { background: var(--correct); color: var(--correct-ink); }

.board-meta {
  font-size: 0.72rem;
  color: var(--muted);
  margin-left: auto;
  white-space: nowrap;
}
.board-tick { color: var(--correct); font-size: 0.9rem; }

/* the category clue for this link's answer */
.board-clue {
  display: flex;
  align-items: baseline;
  gap: 7px;
  margin: 0;
  padding: 6px 9px;
  border-radius: var(--radius-sm);
  background: rgba(125, 211, 252, 0.07);
  border: 1px solid rgba(125, 211, 252, 0.2);
  font-size: 0.78rem;
  line-height: 1.35;
}
.board-clue-label {
  flex: none;
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.11em;
  text-transform: uppercase;
  color: var(--accent);
  opacity: 0.85;
}
.board-clue-text { color: #d7e2f2; }
.board.is-solved .board-clue { opacity: 0.5; }

/* the free letters the chain gives you */
.hint-row {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: var(--tile-gap);
}
.hint {
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  display: grid; place-items: center;
  border: 1px dashed var(--line);
  background: rgba(255, 255, 255, 0.015);
  font-family: var(--mono);
  font-size: clamp(0.7rem, 1.6vw, 0.95rem);
  font-weight: 700;
  color: var(--muted-dim);
  text-transform: uppercase;
}
.hint.is-known {
  border-style: solid;
  border-color: rgba(74, 222, 128, 0.55);
  background: rgba(74, 222, 128, 0.1);
  color: var(--correct);
}

.guesses { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 7px; }

.guess-row { display: flex; flex-direction: column; gap: 4px; }

.tiles {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: var(--tile-gap);
}

.tile {
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  display: grid; place-items: center;
  font-family: var(--mono);
  font-size: clamp(0.78rem, 1.8vw, 1.05rem);
  font-weight: 700;
  text-transform: uppercase;
  background: var(--absent);
  color: var(--absent-ink);
  border: 1px solid transparent;
}
.tile.is-correct { background: var(--correct); color: var(--correct-ink); }
.tile.is-present { background: var(--present); color: var(--present-ink); }

/* draft row: what you are typing right now */
.tile.is-draft {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text);
}
.tile.is-draft.is-filled { border-color: #4b5a78; }
.tile.is-draft.is-cursor { border-color: var(--accent); }
.tile.is-draft.is-pinned { border-style: dashed; color: var(--muted-dim); }

/* the heat reading */
.heat {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.66rem;
  color: var(--muted);
}
.heat-track {
  flex: 1;
  height: 5px;
  border-radius: 99px;
  background: var(--panel-2);
  overflow: hidden;
  min-width: 22px;
}
.heat-fill {
  height: 100%;
  border-radius: 99px;
  background: var(--heat-unknown);
  transition: width var(--dur);
}
.heat-label { font-variant-numeric: tabular-nums; white-space: nowrap; letter-spacing: 0.02em; }

.heat[data-band="freezing"]  .heat-fill { background: var(--heat-freezing); }
.heat[data-band="frigid"]    .heat-fill { background: var(--heat-frigid); }
.heat[data-band="cold"]      .heat-fill { background: var(--heat-cold); }
.heat[data-band="cool"]      .heat-fill { background: var(--heat-cool); }
.heat[data-band="mild"]      .heat-fill { background: var(--heat-mild); }
.heat[data-band="warm"]      .heat-fill { background: var(--heat-warm); }
.heat[data-band="hot"]       .heat-fill { background: var(--heat-hot); }
.heat[data-band="scorching"] .heat-fill { background: var(--heat-scorching); }
.heat[data-band="exact"]     .heat-fill { background: var(--heat-exact); }

.heat[data-band="freezing"]  .heat-label { color: var(--heat-freezing); }
.heat[data-band="frigid"]    .heat-label { color: var(--heat-frigid); }
.heat[data-band="cold"]      .heat-label { color: var(--heat-cold); }
.heat[data-band="cool"]      .heat-label { color: var(--heat-cool); }
.heat[data-band="mild"]      .heat-label { color: var(--heat-mild); }
.heat[data-band="warm"]      .heat-label { color: var(--heat-warm); }
.heat[data-band="hot"]       .heat-label { color: var(--heat-hot); }
.heat[data-band="scorching"] .heat-label { color: var(--heat-scorching); }
.heat[data-band="exact"]     .heat-label { color: var(--heat-exact); }

.boot-error { padding: 24px; line-height: 1.6; color: var(--danger); }

.board-answer {
  margin: 0;
  font-family: var(--mono);
  font-size: 0.78rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--danger);
  text-align: center;
  padding: 3px 0 1px;
}

/* ---------------------------------------------------------------- keyboard */

.keyboard-wrap {
  position: sticky;
  bottom: 0;
  padding: 8px clamp(6px, 2vw, 16px) max(8px, env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(15, 20, 32, 0) 0%, var(--bg) 42%);
}

.keyboard {
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.kb-row { display: flex; gap: 5px; justify-content: center; }

.key {
  flex: 1 1 0;
  min-width: 0;
  height: 48px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--panel-2);
  font-size: 0.86rem;
  font-weight: 650;
  text-transform: uppercase;
  cursor: pointer;
  display: grid; place-items: center;
  user-select: none;
  transition: background var(--dur), color var(--dur), border-color var(--dur), transform 90ms;
}
.key:hover { background: #2a3550; }
.key:active { transform: translateY(1px); }
.key.is-wide { flex: 1.6 1 0; font-size: 0.7rem; letter-spacing: 0.04em; }
.key.is-correct { background: var(--correct); border-color: var(--correct); color: var(--correct-ink); }
.key.is-present { background: var(--present); border-color: var(--present); color: var(--present-ink); }
.key.is-absent { background: #202839; border-color: #202839; color: #59637a; }
.key[disabled] { opacity: 0.45; cursor: not-allowed; }

/* ------------------------------------------------------------------- toast */

.toast {
  position: fixed;
  left: 50%;
  top: 74px;
  transform: translate(-50%, -14px);
  z-index: 40;
  background: #eef2f9;
  color: #131a28;
  font-size: 0.84rem;
  font-weight: 600;
  padding: 9px 16px;
  border-radius: 99px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  opacity: 0;
  pointer-events: none;
  max-width: min(90vw, 420px);
  text-align: center;
  transition: opacity var(--dur), transform var(--dur);
}
.toast.is-up { opacity: 1; transform: translate(-50%, 0); }

/* ----------------------------------------------------------------- dialogs */

.dlg {
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--bg-soft);
  color: var(--text);
  padding: 22px clamp(16px, 4vw, 28px) 24px;
  max-width: 34rem;
  width: calc(100vw - 28px);
  max-height: 86dvh;
  overflow: auto;
  position: relative;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.6);
}
.dlg::backdrop { background: rgba(6, 9, 16, 0.72); backdrop-filter: blur(3px); }

.dlg h2 { font-size: 1.2rem; margin-bottom: 12px; padding-right: 32px; }
.dlg h3 { font-size: 0.86rem; text-transform: uppercase; letter-spacing: 0.1em;
          color: var(--muted); margin: 20px 0 7px; }
.dlg p { margin: 0 0 11px; font-size: 0.9rem; line-height: 1.6; color: #cfd7e6; }
.dlg strong { color: var(--text); }

.dlg-close-form { position: absolute; top: 12px; right: 12px; margin: 0; }
.dlg-close {
  width: 32px; height: 32px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  cursor: pointer;
  font-size: 1.15rem;
  line-height: 1;
  display: grid; place-items: center;
}
.dlg-close:hover { background: var(--panel-2); }

.help-demo {
  display: flex; align-items: center; justify-content: center;
  gap: 4px; flex-wrap: wrap;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 11px 8px;
  margin: 0 0 14px;
  font-family: var(--mono);
  font-size: 0.8rem;
  letter-spacing: 0.06em;
}
.hd-word { color: var(--muted); }
.hd-word b { color: var(--correct); }
.hd-arrow { color: var(--muted-dim); padding: 0 2px; }

.help-list { margin: 0 0 11px; padding-left: 20px; font-size: 0.9rem; line-height: 1.65; color: #cfd7e6; }
.help-list li { margin-bottom: 7px; }
.help-foot { font-size: 0.82rem; color: var(--muted); border-top: 1px solid var(--line-soft); padding-top: 13px; margin-top: 18px; }

kbd {
  font-family: var(--mono);
  font-size: 0.75em;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 5px;
}

.chip {
  display: inline-block;
  padding: 0 7px;
  border-radius: 99px;
  font-size: 0.72em;
  font-weight: 700;
  vertical-align: baseline;
}
.chip-hot { background: var(--heat-hot); color: #2b1200; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(88px, 1fr));
  gap: 10px;
  margin: 0;
}
.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 11px 8px;
  text-align: center;
}
.stat dt { font-size: 0.66rem; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); }
.stat dd { margin: 4px 0 0; font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stats-note { font-size: 0.76rem; color: var(--muted-dim); margin-top: 14px; }

.setting { padding: 13px 0; border-top: 1px solid var(--line-soft); }
.setting:first-of-type { border-top: 0; }
.setting-title { display: block; font-size: 0.9rem; }
.setting-contact { margin: 8px 0 0; font-size: 0.9rem; }
.setting-contact a,
.help-foot a { color: var(--accent); text-decoration: none; border-bottom: 1px solid rgba(125, 211, 252, 0.4); }
.setting-contact a:hover,
.help-foot a:hover { border-bottom-color: var(--accent); }
.switch { display: flex; align-items: flex-start; gap: 11px; cursor: pointer; }
.switch input { width: 20px; height: 20px; margin: 1px 0 0; accent-color: var(--accent); flex: none; cursor: pointer; }
.switch-text { display: block; font-size: 0.9rem; }
.switch-text small, .setting-note { display: block; color: var(--muted); font-size: 0.79rem; margin-top: 3px; line-height: 1.5; }

.btn {
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 9px;
  padding: 10px 17px;
  font-size: 0.88rem;
  font-weight: 650;
  cursor: pointer;
  transition: background var(--dur), transform 90ms;
}
.btn:hover { background: #2b3652; }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--correct); border-color: var(--correct); color: var(--correct-ink); }
.btn-primary:hover { background: #6ae694; }
.btn-quiet { background: var(--panel); }

.result-sub { font-size: 0.92rem; }
.result-chain { list-style: none; margin: 14px 0 18px; padding: 0; display: flex; flex-direction: column; gap: 7px; }
.result-link {
  display: flex; align-items: center; gap: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
}
.result-word { font-family: var(--mono); font-weight: 700; letter-spacing: 0.17em; text-transform: uppercase; font-size: 0.9rem; }
.result-link.is-solved .result-word { color: var(--correct); }
.result-link.is-missed .result-word { color: var(--danger); }
.result-cost { margin-left: auto; font-size: 0.74rem; color: var(--muted); }
.result-actions { display: flex; gap: 9px; flex-wrap: wrap; }

/* -------------------------------------------------------------- animations */

@media (prefers-reduced-motion: no-preference) {
  @keyframes tile-in {
    from { transform: rotateX(-88deg); opacity: 0; }
    to   { transform: rotateX(0deg); opacity: 1; }
  }
  .tile.is-new { animation: tile-in 300ms ease-out backwards; }
  .tiles .tile.is-new:nth-child(1) { animation-delay: 0ms; }
  .tiles .tile.is-new:nth-child(2) { animation-delay: 70ms; }
  .tiles .tile.is-new:nth-child(3) { animation-delay: 140ms; }
  .tiles .tile.is-new:nth-child(4) { animation-delay: 210ms; }
  .tiles .tile.is-new:nth-child(5) { animation-delay: 280ms; }

  @keyframes shake {
    0%, 100% { transform: translateX(0); }
    22% { transform: translateX(-6px); }
    44% { transform: translateX(6px); }
    66% { transform: translateX(-4px); }
    88% { transform: translateX(4px); }
  }
  .board.is-shaking { animation: shake 280ms ease-in-out; }

  @keyframes pop {
    0% { transform: scale(1); }
    45% { transform: scale(1.11); }
    100% { transform: scale(1); }
  }
  .rail-node.is-popping { animation: pop 340ms ease-out; }
}

/* ------------------------------------------------------------- responsive */

@media (max-width: 1100px) {
  .boards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 620px) {
  .boards { grid-template-columns: 1fr; gap: 8px; }

  /* Collapse the boards you are not playing so the active one stays on screen
     together with the keyboard. Headers and pinned letters stay visible, so you
     never lose sight of the chain. */
  .board:not(.is-active) .guesses,
  .board:not(.is-active) .hint-row { display: none; }
  .board:not(.is-active) { padding: 7px 10px; }

  .board.is-solved .board-head { pointer-events: none; }

  .pool { padding: 9px 12px; }
  .pool-value { font-size: 1.3rem; }
  .key { height: 44px; font-size: 0.8rem; }
  .rail { justify-content: flex-start; }
}

@media (max-width: 380px) {
  .key { height: 41px; }
  .brand h1 { letter-spacing: 0.16em; }
}
