/*
 * octordlegame.org stylesheet
 * Order: tokens > base > chrome > game > dialogs > content.
 * System fonts on purpose: zero font requests, zero layout shift from font swaps.
 */

/* ---------- tokens ---------- */
:root {
  color-scheme: light;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --paper: #f1f3f8;
  --card: #ffffff;
  --ink: #1b2440;
  --muted: #525c78;
  --line: #cbd2e1;
  --empty: #e4e8f2;
  --keybg: #d9deec;
  --link: #1d4ed8;
  --focus: #2f5bff;

  --hit: #0b7f77;
  --on-hit: #ffffff;
  --near: #f0b429;
  --on-near: #1b2440;
  --miss: #636d88;
  --on-miss: #ffffff;

  --radius: 10px;
}

/* No-JS fallback: follow the system. With JS, app.js writes data-theme instead. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    --paper: #11172a; --card: #1a2138; --ink: #e9edf8; --muted: #a3adc9;
    --line: #2f3958; --empty: #252e4a; --keybg: #2b3556; --link: #8fb0ff;
    --hit: #12968b; --on-hit: #03201d; --miss: #4a5372; --on-miss: #e9edf8;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --paper: #11172a; --card: #1a2138; --ink: #e9edf8; --muted: #a3adc9;
  --line: #2f3958; --empty: #252e4a; --keybg: #2b3556; --link: #8fb0ff;
  --hit: #12968b; --on-hit: #03201d; --miss: #4a5372; --on-miss: #e9edf8;
}
/* Colorblind palette: blue for correct, orange for misplaced. Must stay last. */
:root[data-cb="on"] { --hit: #1f5fd6; --on-hit: #ffffff; --near: #f28c28; }
:root[data-cb="on"][data-theme="dark"] { --hit: #6b96ff; --on-hit: #06122e; }

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-padding-top: 4rem; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 400 1rem/1.6 var(--sans);
}
h1, h2, h3 { font-family: var(--serif); line-height: 1.2; margin: 0; }
a { color: var(--link); text-underline-offset: 0.18em; }
a:hover { text-decoration-thickness: 2px; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 4px; }

.wrap { width: min(100% - 2rem, 44rem); margin-inline: auto; }
.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.skip {
  position: absolute; left: 0.5rem; top: -4rem; z-index: 50;
  background: var(--ink); color: var(--paper); padding: 0.6rem 1rem; border-radius: 6px;
}
.skip:focus { top: 0.5rem; }

/* ---------- site chrome ---------- */
.site-head { border-bottom: 1px solid var(--line); background: var(--paper); }
.site-head .wrap {
  display: flex; align-items: center; justify-content: space-between;
  gap: 0.25rem 1rem; min-height: 3.5rem; padding-block: 0.35rem; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 0.6rem; color: var(--ink); text-decoration: none; }
.brand-name { font: 700 1.35rem/1 var(--serif); letter-spacing: 0.01em; }

/* The eight-cell mark doubles as a live progress meter on the game pages. */
.mark { display: grid; grid-template-columns: repeat(4, 1fr); gap: 2px; width: 30px; }
.mark i { aspect-ratio: 1; background: var(--line); border-radius: 2px; transition: background 0.25s; }
.mark i.on { background: var(--hit); }

.site-nav ul { display: flex; gap: 0.25rem; margin: 0; padding: 0; list-style: none; }
.site-nav a {
  display: block; padding: 0.4rem 0.65rem; border-radius: 6px;
  color: var(--ink); text-decoration: none; font-size: 0.92rem;
}
.site-nav a:hover { background: var(--empty); }
.site-nav a[aria-current="page"] { background: var(--ink); color: var(--paper); }

.site-foot { border-top: 1px solid var(--line); margin-top: 3rem; padding: 1.5rem 0 2.5rem; color: var(--muted); font-size: 0.9rem; }
.site-foot ul { display: flex; gap: 1rem; flex-wrap: wrap; margin: 0 0 0.75rem; padding: 0; list-style: none; }
.site-foot p { margin: 0; max-width: 46rem; }

/* ---------- game ---------- */
.game-bar { display: flex; align-items: flex-start; justify-content: space-between; gap: 0.75rem; padding-block: 0.8rem 0.6rem; }
.game-bar > div:first-child { min-width: 0; }
.game-bar h1 { font-size: clamp(1.15rem, 3.4vw, 1.6rem); }
.game-bar .sub { margin: 0.15rem 0 0; color: var(--muted); font-size: 0.9rem; }
.tools { display: flex; gap: 0.4rem; }
.tool {
  border: 1px solid var(--line); background: var(--card); border-radius: 8px;
  padding: 0.4rem 0.7rem; font-size: 0.88rem; cursor: pointer; min-height: 2.5rem; white-space: nowrap;
}
.tool:hover { border-color: var(--muted); }
[hidden] { display: none !important; }

.boards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.6rem; }
@media (min-width: 720px) { .boards { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0.9rem; } }

.board {
  container-type: inline-size;
  position: relative; display: grid; gap: 2px; padding: 0.4rem;
  background: var(--card); border: 2px solid var(--line); border-radius: var(--radius);
  transition: border-color 0.25s;
}
.board.solved { border-color: var(--hit); }
.board.solved .row.blank { opacity: 0.35; }
.row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 2px; }
.tile {
  aspect-ratio: 1; display: grid; place-items: center;
  background: var(--empty); color: var(--ink); border-radius: 3px;
  font: 700 0.8rem/1 var(--sans); font-size: 9cqw; text-transform: uppercase;
}
.tile[data-state="hit"]  { background: var(--hit);  color: var(--on-hit); }
.tile[data-state="near"] { background: var(--near); color: var(--on-near); }
.tile[data-state="miss"] { background: var(--miss); color: var(--on-miss); }
.tile[data-flip] { animation: flip 0.42s ease both; animation-delay: calc(var(--i, 0) * 70ms); }
.row[data-shake] { animation: shake 0.3s; }
.answer {
  position: absolute; inset: auto 0.4rem 0.4rem; text-align: center;
  background: var(--ink); color: var(--paper); border-radius: 4px;
  padding: 0.2rem; font: 700 0.85rem/1.2 var(--sans); text-transform: uppercase;
}

@keyframes flip {
  0%   { transform: rotateX(0); background: var(--empty); color: var(--ink); }
  49%  { background: var(--empty); color: var(--ink); }
  50%  { transform: rotateX(90deg); }
  100% { transform: rotateX(0); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

/* Keyboard: sticks to the bottom of the screen while the boards scroll. */
.kb-wrap { position: sticky; bottom: 0; z-index: 5; background: var(--paper); border-top: 1px solid var(--line); margin-top: 0.75rem; }
.kb { display: grid; gap: 0.35rem; padding: 0.5rem 0 calc(0.5rem + env(safe-area-inset-bottom, 0px)); }
.kb-row { display: flex; gap: 0.3rem; justify-content: center; }
.key {
  position: relative; flex: 1 1 0; max-width: 2.9rem; height: 2.85rem; padding: 0 0 0.35rem;
  border: 0; border-radius: 6px; background: var(--keybg); color: var(--ink);
  font: 700 0.95rem/1 var(--sans); text-transform: uppercase; cursor: pointer;
  touch-action: manipulation;
}
.key.wide { flex: 1.7 1 0; max-width: 4.5rem; font-size: 0.72rem; }
.key[data-dead] { opacity: 0.32; }
.pips { position: absolute; left: 3px; right: 3px; bottom: 3px; display: grid; grid-template-columns: repeat(8, 1fr); gap: 1px; height: 5px; }
.pips i { background: var(--line); border-radius: 1px; }
.pips i[data-s="hit"]  { background: var(--hit); }
.pips i[data-s="near"] { background: var(--near); }
.pips i[data-s="miss"] { background: var(--miss); }
.pips i[data-s="done"] { background: transparent; }

.toast {
  position: fixed; left: 50%; top: 4.5rem; z-index: 60; translate: -50% 0;
  background: var(--ink); color: var(--paper); border-radius: 8px;
  padding: 0.55rem 1rem; font-size: 0.95rem; box-shadow: 0 6px 24px rgb(0 0 0 / 0.25);
  opacity: 0; pointer-events: none; transition: opacity 0.15s;
}
.toast.show { opacity: 1; }
.noscript { padding: 1rem; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }

/* ---------- dialogs ---------- */
dialog {
  width: min(28rem, calc(100% - 2rem)); padding: 1.25rem;
  background: var(--card); color: var(--ink);
  border: 1px solid var(--line); border-radius: 14px;
}
dialog::backdrop { background: rgb(10 14 30 / 0.6); }
dialog h2 { font-size: 1.4rem; margin-bottom: 0.75rem; }
dialog .close { position: absolute; top: 0.6rem; right: 0.6rem; border: 0; background: transparent; font-size: 1.5rem; line-height: 1; padding: 0.25rem 0.55rem; cursor: pointer; border-radius: 6px; }
.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.5rem; text-align: center; margin-bottom: 1rem; }
.stat-row b { display: block; font: 700 1.6rem/1.1 var(--serif); }
.stat-row span { font-size: 0.8rem; color: var(--muted); }
.dist { display: grid; gap: 0.3rem; margin: 0 0 1rem; padding: 0; list-style: none; }
.dist li { display: grid; grid-template-columns: 1.6rem 1fr; gap: 0.5rem; align-items: center; font-size: 0.85rem; }
.dist .bar { background: var(--hit); color: var(--on-hit); border-radius: 3px; padding: 0.1rem 0.4rem; min-width: 1.5rem; text-align: right; font-weight: 700; }
.dist .bar.zero { background: var(--empty); color: var(--muted); }
.share-preview { background: var(--paper); border: 1px solid var(--line); border-radius: 8px; padding: 0.75rem; font: 0.95rem/1.5 var(--sans); white-space: pre-wrap; margin: 0.75rem 0; }
.btn { border: 0; border-radius: 8px; background: var(--ink); color: var(--paper); padding: 0.7rem 1.1rem; font-weight: 600; cursor: pointer; min-height: 2.75rem; }
.btn.alt { background: transparent; color: var(--ink); border: 1px solid var(--line); }
.dialog-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.muted { color: var(--muted); font-size: 0.9rem; }
fieldset { border: 0; padding: 0; margin: 0 0 1rem; min-width: 0; }
legend { font-weight: 700; margin-bottom: 0.4rem; padding: 0; }
.opt { display: flex; align-items: center; gap: 0.6rem; padding: 0.4rem 0; min-height: 2.5rem; }
.opt input { width: 1.15rem; height: 1.15rem; accent-color: var(--hit); }

/* ---------- content pages ---------- */
.prose { padding-top: 2rem; font-size: 1.05rem; }
.prose h1 { font-size: clamp(1.9rem, 5vw, 2.6rem); margin-bottom: 1rem; }
.prose h2 { font-size: 1.6rem; margin: 2.25rem 0 0.6rem; }
.prose h3 { font-size: 1.2rem; margin: 1.4rem 0 0.3rem; }
.prose p, .prose ul, .prose ol { margin: 0 0 1rem; }
.prose li { margin-bottom: 0.4rem; }
.prose .lede { font-size: 1.15rem; color: var(--ink); }
.prose .example { font-family: var(--serif); font-size: 1.1rem; letter-spacing: 0.12em; }
.game + .prose { padding-top: 1.5rem; }
.crumbs { font-size: 0.9rem; color: var(--muted); padding-top: 1rem; }
.crumbs a { color: var(--muted); }
.faq h3 { margin-top: 1.25rem; }

/* ---------- sequence, rescue, archive ---------- */
.game-bar .status { font-weight: 600; color: var(--ink); }
.boards[data-seq] { grid-template-columns: minmax(0, 1fr); max-width: 16rem; margin-inline: auto; }
.row.starter .tile { opacity: 0.85; outline: 1px dashed var(--muted); outline-offset: -2px; }
.archive-bar { display: flex; align-items: center; gap: 0.6rem; padding-bottom: 0.6rem; flex-wrap: wrap; }
.archive-bar label { font-weight: 700; }
.archive-bar select { flex: 1 1 14rem; min-height: 2.5rem; padding: 0 0.6rem; font: inherit; color: var(--ink); background: var(--card); border: 1px solid var(--line); border-radius: 8px; }

.meanings { margin: 0.75rem 0; }
.meanings summary { cursor: pointer; font-weight: 700; padding: 0.4rem 0; }
.meanings ul { margin: 0.4rem 0 0; padding-left: 1.1rem; font-size: 0.92rem; }
.meanings li { margin-bottom: 0.35rem; }
.meanings strong { text-transform: uppercase; letter-spacing: 0.05em; }
.pic { display: inline-block; margin-right: 0.35rem; font-size: 1.15em; line-height: 1; }
.meanings li { list-style: none; margin-left: -1.1rem; }

/* ---------- tables, callouts, footer lists ---------- */
.site-nav ul { flex-wrap: wrap; }
.site-foot .legal { margin-top: -0.25rem; font-size: 0.85rem; }
.site-foot a { color: var(--muted); }

.table-wrap { overflow-x: auto; margin: 0 0 1.25rem; }
.prose table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
.prose th, .prose td { text-align: left; padding: 0.55rem 0.7rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.prose th { font-size: 0.85rem; color: var(--muted); font-weight: 700; }
.prose th:first-child, .prose td:first-child { padding-left: 0; }
.prose caption { text-align: left; color: var(--muted); font-size: 0.85rem; padding-bottom: 0.4rem; caption-side: bottom; padding-top: 0.4rem; }
.callout { margin: 1.25rem 0; padding: 0.9rem 1.1rem; background: var(--card); border: 1px solid var(--line); border-left: 4px solid var(--hit); border-radius: var(--radius); }
.callout p:last-child { margin-bottom: 0; }
.prose .word { font-family: var(--sans); font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; font-size: 0.95em; }

/* ---------- tools: word finder and daily hints ---------- */
.widget { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.1rem 1.1rem; margin: 0 0 1.5rem; }
.widget legend { font-size: 0.95rem; }
.widget label { display: block; margin-bottom: 0.35rem; }
.widget .hint-text { margin: 0 0 0.5rem; font-size: 0.9rem; color: var(--muted); }
.slots { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 0.4rem; margin-bottom: 0.9rem; }
.slot, .text-input {
  width: 100%; min-width: 0; height: 3rem; padding: 0 0.4rem; text-align: center; text-transform: uppercase;
  font: 700 1.25rem var(--sans); color: var(--ink); background: var(--paper); border: 2px solid var(--line); border-radius: 8px;
}
.slot-green { border-color: var(--hit); }
.slot-yellow { border-color: var(--near); font-size: 1rem; }
.text-input { text-align: left; padding: 0 0.75rem; letter-spacing: 0.15em; }
.slot:focus-visible, .text-input:focus-visible { border-color: var(--focus); outline-offset: 1px; }
.tool-actions { display: flex; gap: 0.5rem; margin-top: 1rem; }
#finder-status, #hints-status { margin: 0 0 1rem; font-weight: 600; }
#finder-results h3 { font-size: 1.05rem; margin: 1rem 0 0.5rem; }
.chips { display: flex; flex-wrap: wrap; gap: 0.4rem; list-style: none; padding: 0; margin: 0 0 1rem; }
.prose .chips li { margin: 0; padding: 0.25rem 0.65rem; background: var(--empty); border-radius: 999px; font-weight: 600; font-size: 0.9rem; letter-spacing: 0.06em; text-transform: uppercase; }

.hint-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.75rem; margin-bottom: 1.5rem; }
@media (max-width: 30rem) { .hint-grid { grid-template-columns: 1fr; } }
.hint-card { display: flex; flex-direction: column; gap: 0.6rem; padding: 0.9rem 1rem; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
.prose .hint-card h3 { margin: 0; font-size: 1.05rem; }
.prose .hint-list { margin: 0; padding-left: 1.2rem; }
.hint-list:empty { display: none; }
.prose .hint-list li { margin-bottom: 0.3rem; }
.hint-list .revealed strong { letter-spacing: 0.1em; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* Language pages */
a.tool { display: inline-flex; align-items: center; text-decoration: none; color: inherit; }
.lang-switch { margin: 0 0 0.75rem; }
.lang-switch summary { cursor: pointer; }
.lang-switch ul { margin-top: 0.6rem; }
.lang-grid { list-style: none; margin: 1rem 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr)); gap: 0.4rem 1rem; }
.lang-grid span { color: var(--muted); font-size: 0.85rem; }
.credits { font-size: 0.85rem; overflow-x: auto; display: block; }
[dir="rtl"] dialog .close { right: auto; left: 0.6rem; }
[dir="rtl"] .prose th, [dir="rtl"] .prose td { text-align: right; }
[dir="rtl"] .dist .bar { text-align: left; }
/* Translated button labels are longer than the English ones, so the tools drop under the title instead of squeezing it. */
.game[data-switch="1"] .game-bar { flex-wrap: wrap; }
.game[data-switch="1"] .game-bar > div:first-child { flex: 1 1 100%; }
.game[data-switch="1"] .tools { flex-wrap: wrap; }
