:root {
  color-scheme: light;
  --navy: #002e5d;
  --royal: #0062b8;
  --bg: #f4f6f9;
  --surface: #ffffff;
  --text: #14202e;
  --muted: #5a6878;
  --line: #d5dce5;
  --good: #1f8a4c;
  --good-bg: #e3f4ea;
  --bad: #c0392b;
  --cell-hover: #e9f1fb;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d141d;
  --surface: #16212e;
  --text: #e8eef5;
  --muted: #9aabbd;
  --line: #2b3a4c;
  --good: #5bd08c;
  --good-bg: #173a29;
  --bad: #ff7a6b;
  --cell-hover: #1d2d41;
  --royal: #5aa8f2;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 20px 16px 40px;
}

header { text-align: center; margin-bottom: 14px; }
h1 { margin: 0; font-size: 2rem; letter-spacing: -0.02em; color: var(--navy); }
:root[data-theme="dark"] h1 { color: #fff; }
.tagline { margin: 4px 0 0; color: var(--muted); font-size: 0.95rem; }

button { font: inherit; cursor: pointer; }

.modes { display: flex; gap: 8px; justify-content: center; margin-bottom: 14px; }
.modes button {
  padding: 8px 14px;
  border: 1px solid var(--navy);
  border-radius: 999px;
  background: transparent;
  color: var(--navy);
  font-weight: 600;
  font-size: 0.9rem;
}
.modes button[aria-pressed="true"] { background: var(--navy); color: #fff; }
:root[data-theme="dark"] .modes button { border-color: var(--royal); color: var(--royal); }
:root[data-theme="dark"] .modes button[aria-pressed="true"] { background: var(--royal); color: #0d141d; }

/* difficulty: small segmented control under the mode buttons */
.difficulty { display: flex; justify-content: center; margin: -4px 0 14px; }
.difficulty button {
  padding: 5px 16px; border: 1px solid var(--line); background: var(--surface); color: var(--muted);
  font-size: 0.82rem; font-weight: 600;
}
.difficulty button:first-child { border-radius: 8px 0 0 8px; }
.difficulty button:not(:first-child) { border-left: 0; }
.difficulty button:not(:first-child):not(:last-child) { border-radius: 0; }
.difficulty button:last-child { border-radius: 0 8px 8px 0; }
.difficulty button[aria-pressed="true"] { background: var(--navy); border-color: var(--navy); color: #fff; }
body[data-difficulty="hard"] .difficulty button[aria-pressed="true"] { background: #a4262c; border-color: #a4262c; }
body[data-difficulty="insane"] .difficulty button[aria-pressed="true"] { background: #4b2a7a; border-color: #4b2a7a; }
:root[data-theme="dark"] .difficulty button[aria-pressed="true"] { background: var(--royal); border-color: var(--royal); color: #0d141d; }
:root[data-theme="dark"] body[data-difficulty="hard"] .difficulty button[aria-pressed="true"] { background: #ff7a6b; border-color: #ff7a6b; color: #0d141d; }

.status { display: flex; justify-content: space-between; margin-bottom: 8px; color: var(--muted); font-size: 0.95rem; }
.status strong { color: var(--text); }

/* ---- board ---- */
.board {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) repeat(3, minmax(0, 1fr));
  grid-template-rows: auto repeat(3, 1fr);
  gap: 6px;
}

.corner { background: transparent; }

.head {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 8px 4px;
  background: var(--navy);
  color: #fff;
  border-radius: 8px;
  min-height: 64px;
}
.head .sub { font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.04em; opacity: 0.75; }
.head .label { font-weight: 700; font-size: 0.88rem; line-height: 1.15; overflow-wrap: anywhere; }

.cell {
  position: relative;
  aspect-ratio: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  text-align: center;
  overflow: hidden;
}
.cell:not(:disabled):hover, .cell:not(:disabled):focus-visible { background: var(--cell-hover); border-color: var(--royal); outline: none; }
.cell .plus { font-size: 1.8rem; color: var(--muted); line-height: 1; }
.cell:disabled { cursor: default; }
.cell.correct { background: var(--good-bg); border-color: var(--good); }
.cell .name { font-weight: 700; font-size: 0.85rem; line-height: 1.15; overflow-wrap: anywhere; }
.cell .meta { font-size: 0.68rem; line-height: 1.2; color: var(--muted); margin-top: 2px; overflow-wrap: anywhere; }
.cell.correct .name { margin-bottom: 1px; }
.cell.missed { border-style: dashed; }
.cell.missed .plus { font-size: 0.8rem; }
.cell.shake { animation: shake 0.35s; border-color: var(--bad); }
@keyframes shake { 20%, 60% { transform: translateX(-5px); } 40%, 80% { transform: translateX(5px); } }
@media (prefers-reduced-motion: reduce) { .cell.shake { animation: none; } }

.toast { min-height: 1.4em; margin: 10px 0 0; text-align: center; color: var(--bad); font-weight: 600; font-size: 0.92rem; }

/* ---- result ---- */
.result {
  margin-top: 16px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.result h2 { margin: 0 0 8px; font-size: 1.25rem; }
.result h3 { margin: 16px 0 6px; font-size: 0.95rem; color: var(--muted); }
.result-grid { font: 1.6rem/1.3 monospace; margin: 0 0 10px; }
.result-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.result-actions button {
  padding: 8px 14px; border: none; border-radius: 8px; background: var(--navy); color: #fff; font-weight: 600;
}
:root[data-theme="dark"] .result-actions button { background: var(--royal); color: #0d141d; }
.reveal { margin: 0; padding-left: 18px; font-size: 0.88rem; }
.reveal li { margin-bottom: 6px; }
.reveal .crit { color: var(--muted); }

footer { margin-top: 20px; text-align: center; color: var(--muted); font-size: 0.85rem; display: flex; gap: 10px; justify-content: center; align-items: center; flex-wrap: wrap; }
.credits { flex-basis: 100%; margin: 8px 0 0; font-size: 0.78rem; line-height: 1.5; }
.credits a { color: var(--royal); }
.link { background: none; border: none; color: var(--royal); text-decoration: underline; padding: 0; font-size: inherit; }

/* ---- picker dialog ---- */
dialog {
  width: min(520px, calc(100vw - 24px));
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
dialog h2 { margin: 0 0 10px; font-size: 1rem; line-height: 1.3; }
#search {
  width: 100%;
  padding: 10px 12px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
#search:focus { outline: none; border-color: var(--royal); }
.suggestions { list-style: none; margin: 8px 0; padding: 0; max-height: 40vh; overflow-y: auto; }
.suggestions li {
  display: flex; justify-content: space-between; gap: 10px; align-items: baseline;
  padding: 9px 10px; border-radius: 6px; cursor: pointer;
}
.suggestions li[aria-selected="true"], .suggestions li:hover { background: var(--cell-hover); }
.suggestions .p-meta { color: var(--muted); font-size: 0.8rem; white-space: nowrap; }
.suggestions .empty { cursor: default; color: var(--muted); }

@media (max-width: 420px) {
  .head { padding: 8px 2px; }
  .head .label { font-size: 0.72rem; overflow-wrap: break-word; }
  .cell .name { font-size: 0.75rem; }
  .cell .meta { font-size: 0.6rem; }
}

/* ---- explanation tooltips ---- */
.head.has-info { cursor: help; }
.head.has-info:focus-visible { outline: 2px solid var(--royal); outline-offset: 2px; }
#tip {
  position: fixed; z-index: 30;
  max-width: min(260px, calc(100vw - 16px));
  padding: 10px 12px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  font-size: 0.82rem; line-height: 1.4; pointer-events: none;
}
/* fade in with an animation (not a transition): if it never runs, the tooltip is still fully visible */
#tip.show { animation: tip-in 0.12s ease-out; }
@keyframes tip-in { from { opacity: 0; } }
#tip[hidden] { display: none; }
/* arrow: a rotated square sharing the tooltip's background and border */
#tip::before {
  content: ""; position: absolute; left: var(--arrow-x, 50%);
  width: 10px; height: 10px; background: var(--surface);
  border: 1px solid var(--line); border-right: 0; border-bottom: 0;
  transform: translateX(-50%) rotate(45deg);
  top: -6px;
}
#tip.above::before { top: auto; bottom: -6px; border: 1px solid var(--line); border-left: 0; border-top: 0; }
@media (prefers-reduced-motion: reduce) { #tip.show { animation: none; } }

/* ---- light / dark toggle ---- */
header { position: relative; }
.theme-toggle {
  position: absolute; top: 0; right: 0;
  width: 38px; height: 38px; display: grid; place-items: center;
  border: 1px solid var(--line); border-radius: 50%;
  background: var(--surface); color: var(--text); padding: 0;
}
.theme-toggle:hover { border-color: var(--royal); }
.theme-toggle:focus-visible { outline: 2px solid var(--royal); outline-offset: 2px; }
.theme-toggle svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
/* show the icon for the mode you would switch TO */
.theme-toggle .icon-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }

/* ---- hints ---- */
.hint-box { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin: 0 0 10px; }
.hint-btn {
  padding: 5px 12px; border: 1px solid var(--royal); border-radius: 999px; background: transparent;
  color: var(--royal); font-size: 0.85rem; font-weight: 600;
}
.hint-btn:disabled { opacity: 0.45; cursor: default; }
.hint-btn[hidden] { display: none; }
.hint-text { margin: 0; font-size: 0.88rem; color: var(--text); }
.hint-text:empty { display: none; }
.cell .bulb { position: absolute; top: 3px; right: 5px; font-size: 0.7rem; }

/* ---- help popup ---- */
.help-toggle { right: auto; left: 0; font-size: 1.1rem; font-weight: 700; line-height: 1; }
.help { width: min(480px, calc(100vw - 24px)); max-height: calc(100vh - 32px); overflow-y: auto; }
.help h2 { font-size: 1.15rem; }
.help-list { margin: 0 0 14px; padding-left: 20px; font-size: 0.9rem; line-height: 1.45; }
.help-list li { margin-bottom: 8px; }
.help-close { padding: 8px 18px; }

/* ---- soft secondary buttons (copy link, cancel) ---- */
.btn-soft {
  padding: 7px 16px; border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface); color: var(--royal); font-size: 0.88rem; font-weight: 600;
  transition: border-color 0.12s, background 0.12s;
}
.btn-soft:hover { border-color: var(--royal); background: var(--cell-hover); }
.btn-soft:focus-visible { outline: 2px solid var(--royal); outline-offset: 2px; }
.btn-soft:active { transform: translateY(1px); }
.picker-actions { display: flex; justify-content: flex-end; }

:root[data-theme="dark"] body[data-difficulty="insane"] .difficulty button[aria-pressed="true"] { background: #c9a2ff; border-color: #c9a2ff; color: #0d141d; }

/* ---- expandable list of every valid answer on a missed square ---- */
.reveal details summary { cursor: pointer; }
.reveal details summary::marker { color: var(--muted); }
.reveal .more { color: var(--royal); font-weight: 600; white-space: nowrap; }
.answer-list {
  list-style: none; margin: 6px 0 2px; padding: 8px 10px; max-height: 14rem; overflow-y: auto;
  border: 1px solid var(--line); border-radius: 8px; background: var(--bg); font-size: 0.85rem;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 3px 16px; overflow-x: hidden;
}
.answer-list li { margin: 0 0 4px; line-height: 1.25; }
.answer-list .nm { display: block; }
.answer-list .yrs { display: block; color: var(--muted); font-size: 0.74rem; }

.reveal .pick { font-weight: 700; color: var(--good); }
.h3-note { font-weight: 400; font-size: 0.8rem; }

.give-up-row { display: flex; justify-content: center; margin-top: 10px; }
.give-up-row[hidden], #give-up[hidden] { display: none; }
#give-up { color: var(--muted); font-size: 0.82rem; padding: 5px 14px; }
#give-up[data-armed="1"] { color: var(--bad); border-color: var(--bad); }
