/* ==========================================================================
   0) Design tokens & global resets
   ========================================================================== */

/* --- base tokens (light defaults) --- */
:root {
  /* mode */
  color-scheme: light;

  /* palette */
  --bg: #f6f7fb;
  --text: #0b1020;
  --card-text: #0b1020;
  --muted: #6b7280;
  --card: #fff;
  --border: #e5e7eb;
  --accent: #111827;
  --accent-bg: #e7eaf1;
  --accent-bg-hover: #dfe3ec;
  --chip: #eef2ff;
  --chip-text: #3730a3;
  --ok: #10b981;
  --no: #ef4444;
  --panel: #ffffff;
  --panel-border: #d2d6db;
  --brand: #1e40af;
  --brand-hover: #1b3a9a;
  --overlay-scrim: rgba(17, 24, 39, 0.55);
  --confirm-gradient-start: #10b981;
  --confirm-gradient-end: #059669;
  --confirm-text: #fff;
  --selected: #f0f2f6;

  /* icon buttons */
  --icon-btn-bg: #e7eaf1;
  --icon-btn-hover-bg: #dfe3ec;
  --icon-btn-border: #cfd3db;
  --icon-btn-icon: #0b1020;

  /* radii / shadows / layout */
  --radius-lg: 10px;
  --shadow-md: 0 8px 24px rgba(2, 6, 23, 0.08);
  --shadow-sm: 0 2px 10px rgba(2, 6, 23, 0.06);
  --container: 720px;
  --card-width: 560px;

  /* focus / type / anim */
  --focus: 2px solid #1e40af;
  --font-ui:
    ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, "Helvetica Neue",
    Arial, sans-serif;
  --flip-dur: 600ms;
  --fade-dur: 260ms;
  --pulse-dur: 2.5s;

  /* rails */
  --rule: #e6e8ee;
  --gutter: clamp(6px, 2.2vw, 12px);
}

/* --- OS-driven dark defaults (only when no user override) --- */
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #0b0d12;
    --text: #e8eaf1;
    --card-text: #0b1020;
    --muted: #97a0b3;
    --card: #12151d;
    --border: #202434;
    --accent: #0b0d12;
    --accent-bg: #e8eaf1;
    --accent-bg-hover: #dfe3ec;
    --chip: #182036;
    --chip-text: #a5b4fc;
    --shadow-md: 0 10px 40px rgba(0, 0, 0, 0.35);
    --shadow-sm: 0 2px 12px rgba(0, 0, 0, 0.25);
    --panel: #111827;
    --panel-border: #374151;
    --brand: #93c5fd;
    --brand-hover: #bfdbfe;
    --overlay-scrim: rgba(0, 0, 0, 0.6);
    --focus: 2px solid #93c5fd;
    --confirm-gradient-start: #059669;
    --confirm-gradient-end: #065f46;
    --confirm-text: #e8fdf3;
    --on-dark: #fff;
    --on-dark-muted-green: #cff7df;
    --on-dark-muted-red: #ffd7d7;
    --selected: #94a3b8;
    --icon-btn-bg: #1e293b;
    --icon-btn-hover-bg: #2c3649;
    --icon-btn-border: #374151;
    --icon-btn-icon: #f9fafb;
  }
}

/* --- USER OVERRIDES --- */
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f6f7fb;
  --text: #0b1020;
  --card-text: #0b1020;
  --muted: #6b7280;
  --card: #fff;
  --border: #e5e7eb;
  --accent: #111827;
  --accent-bg: #e7eaf1;
  --accent-bg-hover: #dfe3ec;
  --chip: #eef2ff;
  --chip-text: #3730a3;
  --ok: #10b981;
  --no: #ef4444;
  --shadow-md: 0 8px 24px rgba(2, 6, 23, 0.08);
  --shadow-sm: 0 2px 10px rgba(2, 6, 23, 0.06);
  --panel: #ffffff;
  --panel-border: #d2d6db;
  --brand: #1e40af;
  --brand-hover: #1b3a9a;
  --overlay-scrim: rgba(17, 24, 39, 0.55);
  --focus: 2px solid #1e40af;
  --confirm-gradient-start: #10b981;
  --confirm-gradient-end: #059669;
  --confirm-text: #fff;
  --selected: #f0f2f6;
  --icon-btn-bg: #e7eaf1;
  --icon-btn-hover-bg: #dfe3ec;
  --icon-btn-border: #cfd3db;
  --icon-btn-icon: #0b1020;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b0d12;
  --text: #e8eaf1;
  --card-text: #0b1020;
  --muted: #97a0b3;
  --card: #12151d;
  --border: #202434;
  --accent: #0b0d12;
  --accent-bg: #e8eaf1;
  --accent-bg-hover: #dfe3ec;
  --chip: #182036;
  --chip-text: #a5b4fc;
  --shadow-md: 0 10px 40px rgba(0, 0, 0, 0.35);
  --shadow-sm: 0 2px 12px rgba(0, 0, 0, 0.25);
  --panel: #111827;
  --panel-border: #374151;
  --brand: #93c5fd;
  --brand-hover: #bfdbfe;
  --overlay-scrim: rgba(0, 0, 0, 0.6);
  --focus: 2px solid #93c5fd;
  --confirm-gradient-start: #059669;
  --confirm-gradient-end: #065f46;
  --confirm-text: #e8fdf3;
  --on-dark: #fff;
  --on-dark-muted-green: #cff7df;
  --on-dark-muted-red: #ffd7d7;
  --selected: #334155;
  --icon-btn-bg: #1e293b;
  --icon-btn-hover-bg: #2c3649;
  --icon-btn-border: #374151;
  --icon-btn-icon: #f9fafb;
}

/* --- Global reset --- */
* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font: 16px/1.5 var(--font-ui);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.hidden {
  display: none !important;
}

/* ==========================================================================
   1) Shell & layout
   ========================================================================== */

.shell {
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}

main {
  display: grid;
  place-items: start center;
  padding: 24px;
}

.container {
  width: min(100%, var(--container));
  display: grid;
  place-items: center;
}

.game-board {
  width: min(100%, var(--card-width));
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 20px 20px 16px;
}

/* ==========================================================================
   2) Header & navigation
   ========================================================================== */

header.site {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  height: 64px;
  border-bottom: 1px solid var(--border);
  padding-inline: 16px;
  gap: 12px;
}

header.site::before {
  content: "";
}

header .brand {
  font-weight: 700;
  letter-spacing: 0.2px;
  font-size: 40px;
}

header.site .brand {
  grid-column: 2;
  justify-self: center;
}

header.site .nav-button-container {
  grid-column: 3;
  justify-self: end;
  display: inline-flex;
  gap: 8px;
}

header .brand small {
  color: var(--muted);
  font-weight: 500;
  margin-left: 0.5ch;
}

.nav-icon {
  padding: 0;
  border: 1px solid var(--icon-btn-border);
  line-height: 0;
  vertical-align: middle;
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  background: var(--icon-btn-bg);
  border-radius: 8px;
  cursor: pointer;
  transition:
    background-color 0.15s ease,
    border-color 0.15s ease;
}

.nav-icon svg {
  width: 20px;
  height: 20px;
  stroke: var(--icon-btn-icon);
  stroke-width: 2;
}

.nav-icon:hover {
  background: var(--icon-btn-hover-bg);
}

.nav-icon:focus-visible {
  outline: var(--focus);
  outline-offset: 2px;
}

/* ==========================================================================
   3) Rows, rails, and question/answer grid
   ========================================================================== */

.toprow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

/* structure */
.questions-container {
  position: relative;
  padding-left: var(--gutter);
  box-sizing: border-box;
}

/* vertical rail */
.questions-container::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 1px;
  background: var(--rule, var(--border));
  opacity: 0;
  transition: opacity 300ms ease;
}

.row {
  position: relative;
  display: flex;
  align-items: stretch;
  justify-content: flex-start;
  gap: clamp(1px, 3vw, 20px);
  padding: 10px 0;
  --row-delay: 0ms;
}

.row::after {
  --gutter: 6px;
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--rule, var(--border));
  height: 1px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 300ms ease;
}

.row:last-child::after {
  display: none;
}

.row > * {
  opacity: 0;
  transition: opacity var(--fade-dur) ease;
}

.trivia-container {
  flex: 0 1 160px;
  min-width: 140px;
  max-width: 200px;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 8px;
  padding-left: 0;
  padding-right: 5px;
}

.category {
  font-size: 0.65rem;
  font-weight: 500;
  letter-spacing: 0.04em;
}

.question-text {
  margin: 12px 0;
  font-size: 0.8rem;
  font-weight: 600;
  line-height: 1.35;
  color: inherit;
  overflow-wrap: anywhere;
  opacity: 0;
  transition: opacity 3000ms ease;
}

.row.is-active .question-text {
  opacity: 1;
  pointer-events: auto;
}

/* options grid */
.options-container {
  flex: 1 1 320px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(90px, 1fr));
  justify-items: center;
  gap: 12px;
  width: 100%;
  max-width: 320px;
  padding: 0 5px;
  perspective: 900px;
}

/* ==========================================================================
   4) Answer tiles (flip states, selection, results)
   ========================================================================== */

.answer {
  position: relative;
  overflow: visible;
  height: 90px;
  width: 90px; /* desktop cap; mobile overrides below */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 12px 14px;
  background: #fafafa;
  color: var(--card-text);
  border: 1px solid var(--border);
  cursor: default;
  user-select: none;
  text-align: center;
  word-break: break-word;

  /* 3D flip essentials */
  transform-style: preserve-3d;
  transform: rotateY(0deg);
  transition:
    transform var(--flip-dur) cubic-bezier(0.2, 0.7, 0.2, 1) var(--delay, 0ms),
    background-color 0.12s ease,
    border-color 0.12s ease;
  pointer-events: none; /* enabled on reveal */
}

/* Faces share common geometry */
.answer::before,
.answer .label,
.answer .result-face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  justify-content: center;
  backface-visibility: hidden;
  border-radius: inherit;
}

/* FRONT (pre-reveal) */
.answer::before {
  content: "";
  background: #fafafa;
  transform: rotateY(0deg);
  z-index: 0;
}

/* BACK (first flip shows label) */
.answer .label {
  transform: rotateY(180deg);
  opacity: 1;
  z-index: 1;
}

/* BACK (second flip shows results) */
.answer .result-face {
  transform: rotateY(0deg);
  font-weight: 600;
  line-height: 1.2;
  z-index: 2;
}

/* State progression */
.answer.is-revealed {
  transform: rotateY(180deg);
  pointer-events: auto;
}

.answer.is-result {
  transform: rotateY(360deg);
}

.answer.is-result::before {
  opacity: 0;
}

/* Interactivity gates */
.answer.is-revealed:not(:disabled) {
  cursor: pointer;
}

.answer:disabled,
.answer[aria-disabled="true"] {
  pointer-events: none;
  cursor: default;
}

/* Hover/focus (only when interactive) */
.answer:not(:disabled):hover {
  background: #f3f4f6;
}

.answer:not(:disabled):focus-visible {
  outline: var(--focus);
  outline-offset: 2px;
}

/* Selected (role-aware) */
.answer[role="radio"][aria-checked="true"]:not(:disabled),
.answer.is-selected:not(:disabled) {
  border-color: var(--text);
  box-shadow: inset 0 0 0 1px var(--text);
  background: var(--selected);
}

/* Improve label contrast on dark when selected */
:root[data-theme="dark"]
  .answer:is(.is-selected, [aria-pressed="true"], [aria-checked="true"])
  .label {
  color: var(--on-dark);
}

/* Correct/Wrong borders */
.answer.is-correct {
  border-color: var(--ok);
  border-width: 2px;
}

.answer.is-wrong {
  border-color: var(--no);
  border-width: 2px;
}

/* Results text */
.result-face {
  user-select: none;
}

.result-face .result-label {
  font-weight: 600;
}

.result-face .subtext {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 2px;
}

.answer .subtext {
  white-space: pre-line;
  line-height: 1.15;
  color: var(--muted-fg, #6b7280);
  font-size: 0.9em;
}

/* Dark-surface tints for correctness  */
:root[data-theme="dark"] .answer.is-correct {
  background-color: #204a24;
}

:root[data-theme="dark"] .answer.is-wrong {
  background-color: #4a1c1c;
}

:root[data-theme="dark"] .is-correct .result-label {
  color: var(--on-dark);
}

:root[data-theme="dark"] .is-correct .subtext {
  color: var(--on-dark-muted-green);
}

:root[data-theme="dark"] .is-wrong .result-label {
  color: var(--on-dark);
}

:root[data-theme="dark"] .is-wrong .subtext {
  color: var(--on-dark-muted-red);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) .answer.is-correct {
    background-color: #204a24;
  }

  :root:not([data-theme]) .answer.is-wrong {
    background-color: #4a1c1c;
  }

  :root:not([data-theme]) .is-correct .result-label {
    color: var(--on-dark);
  }

  :root:not([data-theme]) .is-correct .subtext {
    color: var(--on-dark-muted-green);
  }

  :root:not([data-theme]) .is-wrong .result-label {
    color: var(--on-dark);
  }

  :root:not([data-theme]) .is-wrong .subtext {
    color: var(--on-dark-muted-red);
  }
}

/* Pulses */
@keyframes pulse-correct {
  0% {
    box-shadow: 0 0 0 rgba(67, 210, 126, 0.6);
  }

  70% {
    box-shadow: 0 0 12px rgba(67, 210, 126, 0.8);
  }

  100% {
    box-shadow: 0 0 0 rgba(67, 210, 126, 0);
  }
}
@keyframes pulse-wrong {
  0% {
    box-shadow: 0 0 0 rgba(255, 92, 92, 0.55);
  }

  70% {
    box-shadow: 0 0 12px rgba(255, 92, 92, 0.8);
  }

  100% {
    box-shadow: 0 0 0 rgba(255, 92, 92, 0);
  }
}

.answer.pulse-correct {
  animation: pulse-correct var(--pulse-dur) ease;
}

.answer.pulse-wrong {
  animation: pulse-wrong var(--pulse-dur) ease;
}

/* ==========================================================================
   5) Intro stages (rails/rules & staggered content)
   ========================================================================== */

/* 1) blank */
#game.intro-blank .questions-container::before,
#game.intro-blank .row::after,
#game.intro-blank .row > * {
  opacity: 0;
}

/* 2) lines only */
#game.intro-lines .questions-container::before,
#game.intro-lines .row::after {
  opacity: 1;
}

#game.intro-lines .row > * {
  opacity: 0;
}

/* 3) content (staggered) */
#game.intro-content .row > * {
  opacity: 1;
  transition-delay: var(--row-delay, 0ms);
}

/* ==========================================================================
   6) Overlays & buttons
   ========================================================================== */

.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--overlay-scrim);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  z-index: 1000;
  opacity: 1;
  transition:
    opacity 0.25s ease,
    visibility 0.25s ease;
}

.overlay.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.overlay-card {
  width: min(92vw, 520px);
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  box-shadow:
    0 18px 40px rgba(0, 0, 0, 0.18),
    0 4px 12px rgba(0, 0, 0, 0.08);
  padding: 24px 24px 20px;
  color: var(--text);
}

.overlay-header {
  font: 700 20px/1.3 var(--font-ui);
  line-height: 1.15;
  letter-spacing: -0.01em;
  margin: 0 0 10px;
}

.overlay-description {
  color: var(--muted);
  max-width: 65ch;
  font-size: 1rem;
  line-height: 1.6;
  margin: 0 0 24px;
}

.overlay-start {
  width: 100%;
  height: 44px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: var(--brand);
  color: var(--card);
  font-weight: 700;
  letter-spacing: 0.2px;
  box-shadow: 0 6px 16px rgba(30, 64, 175, 0.28);
  cursor: pointer;
  transition:
    transform 0.12s ease,
    background-color 0.12s ease,
    box-shadow 0.12s ease;
}

.info-card {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

#info-desc {
  margin-bottom: 0;
}

#info-widget {
  max-width: 90%;
}

.overlay-start:hover {
  transform: translateY(-1px);
  background: var(--brand-hover);
}

.overlay-start:active {
  transform: translateY(0);
}

.overlay-start:focus-visible {
  outline: var(--focus);
  outline-offset: 3px;
}

.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 12px;
  background: var(--accent-bg);
  color: var(--accent);
  padding: 10px 14px;
  font-weight: 700;
  cursor: pointer;
  transition:
    background-color 0.12s ease,
    transform 0.12s ease;
}

.btn:hover {
  background: var(--accent-bg-hover);
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* sticky confirm */
.confirm-sticky {
  position: fixed;
  padding-top: 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  height: 65px;
  border: 0;
  background: linear-gradient(
    180deg,
    var(--confirm-gradient-start),
    var(--confirm-gradient-end)
  );
  color: var(--confirm-text);
  font: 600 16px/1.2 var(--font-ui);
  display: grid;
  place-items: center;
  z-index: 9999;
  border-top: 1px solid var(--panel-border);
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.6);
  cursor: pointer;
  transform: translate(var(--tx, 0), var(--ty, 0));
}

.confirm-sticky[disabled],
.confirm-sticky[aria-disabled="true"] {
  display: none;
}

.confirm-sticky:focus-visible {
  outline: var(--focus);
  outline-offset: 3px;
}

footer.site {
  border-top: 1px solid var(--border);
  color: var(--muted);
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
}

a:link,
a:visited {
  color: var(--brand);
  text-decoration: underline;
}

/* ==========================================================================
   7) Responsive adaptations
   ========================================================================== */

/* ≤550: phone */
@media (max-width: 550px) {
  main,
  .game-board {
    padding: 8px;
  }

  .row {
    flex-direction: row;
    align-items: center;
    gap: 0;
  }

  .trivia-container,
  .options-container {
    max-width: none;
    padding: 0;
  }

  .trivia-container {
    justify-content: flex-start;
    padding-right: 2px;
  }

  .options-container {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    align-items: start;
    width: 100%;
    gap: 5px;
  }

  .answer {
    width: 100%;
  }

  .label {
    padding: 0 4px;
  }

  .question-text {
    margin: 8px 2px;
  }

  .confirm-sticky {
    left: 0;
    right: 0;
    bottom: max(0px, env(safe-area-inset-bottom));
    --tx: 0;
  }
}

/* ≤374: very small (e.g., iPhone SE) */
@media (max-width: 374px) {
  main,
  .game-board {
    padding: 12px;
  }

  .row {
    flex-direction: row;
    gap: 8px;
  }

  .trivia-container,
  .options-container {
    max-width: none;
    padding: 0;
  }

  .trivia-container {
    justify-content: flex-start;
  }

  .options-container {
    justify-items: stretch;
    grid-template-columns: 1fr;
    flex-direction: row;
  }

  .answer {
    width: 100%;
    min-width: 58px;
  }

  .question-text {
    margin-top: 16px;
  }

  .confirm-sticky {
    left: 0;
    right: 0;
    bottom: max(0px, env(safe-area-inset-bottom));
    --tx: 0;
  }
}

/* ≥551: desktop confirm bar centered to card width */
@media (min-width: 551px) {
  .confirm-sticky {
    left: 50%;
    bottom: max(0px, env(safe-area-inset-bottom));
    min-width: var(--card-width);
    --tx: -50%;
  }
}

/* ==========================================================================
   8) Accessibility: reduced motion
   ========================================================================== */

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

  #game,
  .overlay,
  .overlay-card,
  .overlay-start,
  .question-text,
  .questions-container::before,
  .row::after,
  .row > * {
    transition: none;
    opacity: 1;
  }

  /* remove 3D transforms to avoid mirrored text */
  .answer,
  .answer.is-revealed,
  .answer.is-result {
    transform: none;
  }
  .answer .label,
  .answer .result-face {
    transform: none !important;
  }

  .answer .label {
    opacity: 1;
  }
  .answer.pulse-correct,
  .answer.pulse-wrong {
    animation: none !important;
  }
}

/* ==========================================================================
   9) Game results & misc
   ========================================================================== */

.game-results {
  margin-top: 24px;
  padding: 28px 24px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--panel);
  text-align: center;
  box-shadow: var(--shadow-sm);
  scroll-margin-top: 72px;
}

.game-results h2 {
  font: 700 20px/1.3 var(--font-ui);
  color: var(--text);
  margin: 0 0 8px;
  letter-spacing: 0.1px;
}

.game-results p {
  font: 500 15px/1.6 var(--font-ui);
  color: var(--muted);
  margin: 0;
}

.game-results #midnight-countdown {
  font:
    600 15px/1.6 ui-monospace,
    SFMono-Regular,
    Menlo,
    monospace;
  color: var(--text);
  letter-spacing: 0.05em;
}

/* simple utility animation */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
