@layer reset, base, theme, components, utilities;

@layer theme {
  :root {
    color-scheme: light;
    --font:
      ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI",
      Roboto, sans-serif;
    --bg: #eaf3ff;
    --bg-glow: #fff4e6;
    --surface: #ffffff;
    --text: #13213c;
    --muted: #4a5876;
    --accent: #5b3df5;
    --accent-text: #ffffff;
    --border: #c5d3ee;
    --state-fill: #ff8a3d;
    --state-stroke: #b34f0f;
    --correct: #13783f;
    --wrong: #b8322a;
    --hot: #c2410c;
    --shadow: 0 0.5rem 1.5rem rgb(19 33 60 / 0.12);
  }

  /* app.js starts the switch from the saved choice or the device setting. */
  :root:has(#dark-mode:checked) {
    color-scheme: dark;
    --bg: #0f1630;
    --bg-glow: #24164a;
    --surface: #1a2550;
    --text: #f1f4ff;
    --muted: #b6c0e0;
    --accent: #a28dff;
    --accent-text: #0f1630;
    --border: #34437a;
    --state-fill: #ffa45c;
    --state-stroke: #ffd2ad;
    --correct: #13783f;
    --wrong: #b8322a;
    --hot: #ffa45c;
    --shadow: 0 0.5rem 1.5rem rgb(0 0 0 / 0.35);
  }
}

@layer base {
  html {
    font-family: var(--font);
    color: var(--text);
    background: var(--bg);
  }

  body {
    min-height: 100dvh;
    background:
      radial-gradient(circle at 15% 0%, var(--bg-glow), transparent 55%),
      var(--bg);
  }

  button {
    cursor: pointer;
  }

  :focus-visible {
    outline: 3px solid var(--accent);
    outline-offset: 3px;
  }
}

@layer components {
  .app {
    max-width: 68rem;
    margin-inline: auto;
    padding: max(1rem, env(safe-area-inset-top))
      max(1rem, env(safe-area-inset-right))
      max(1.5rem, env(safe-area-inset-bottom))
      max(1rem, env(safe-area-inset-left));
    display: grid;
    gap: 1rem;
  }

  .site-header {
    text-align: center;
  }

  .toolbar {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-block-end: 0.5rem;
  }

  .pill-button,
  .theme-switch {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    min-height: 2.75rem;
    padding: 0.25rem 0.9rem 0.25rem 0.4rem;
    border: 2px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    color: var(--text);
    font-weight: 700;
    cursor: pointer;
  }

  .pill-button {
    padding-inline-start: 0.9rem;
  }

  /* The checkbox itself is drawn as a sliding switch. */
  .theme-switch input {
    appearance: none;
    position: relative;
    width: 2.75rem;
    height: 1.6rem;
    border-radius: 999px;
    background: var(--muted);
    cursor: pointer;
    transition: background-color 150ms ease;
  }

  .theme-switch input::before {
    content: "";
    position: absolute;
    inset-block-start: 0.2rem;
    inset-inline-start: 0.2rem;
    width: 1.2rem;
    height: 1.2rem;
    border-radius: 50%;
    background: var(--surface);
    transition: transform 150ms ease;
  }

  .theme-switch input:checked {
    background: var(--accent);
  }

  .theme-switch input:checked::before {
    transform: translateX(1.15rem);
  }

  .site-header h1 {
    font-size: clamp(1.5rem, 4.5vw, 2.4rem);
    line-height: 1.15;
  }

  .byline {
    color: var(--muted);
    font-weight: 600;
  }

  /* Only one screen shows at a time; body[data-screen] is set by app.js. */
  body[data-screen="quiz"] .results,
  body[data-screen="results"] .quiz {
    display: none;
  }

  .quiz {
    display: grid;
    gap: 1rem;
  }

  .hud {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
  }

  .stat {
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: 999px;
    padding: 0.3rem 0.9rem;
    font-weight: 600;
    color: var(--muted);
  }

  .stat-value {
    color: var(--text);
    font-weight: 800;
  }

  .stat[data-hot] {
    border-color: var(--hot);
    color: var(--hot);
  }

  .card {
    background: var(--surface);
    border-radius: 1.5rem;
    box-shadow: var(--shadow);
    padding: 1.25rem;
    display: grid;
    gap: 1rem;
    justify-items: center;
    text-align: center;
  }

  .state-shape {
    width: 100%;
    height: clamp(9rem, 32dvh, 22rem);
  }

  .state-shape path {
    fill: var(--state-fill);
    stroke: var(--state-stroke);
    stroke-width: 2.5;
    stroke-linejoin: round;
    vector-effect: non-scaling-stroke;
  }

  .state-shape .capital-star {
    fill: var(--surface);
    stroke: var(--text);
    stroke-width: 2;
  }

  .question {
    font-size: clamp(1.35rem, 3.5vw, 2rem);
    line-height: 1.2;
  }

  .question:focus-visible {
    outline-offset: 6px;
    border-radius: 0.5rem;
  }

  #state-name {
    color: var(--accent);
  }

  .answers {
    display: grid;
    gap: 1rem;
    align-content: start;
  }

  .choices {
    display: grid;
    gap: 0.75rem;
  }

  @media (min-width: 32rem) {
    .choices {
      grid-template-columns: 1fr 1fr;
    }
  }

  .choice {
    min-height: 4rem;
    padding: 0.75rem 1rem;
    border: 3px solid var(--border);
    border-radius: 1rem;
    background: var(--surface);
    color: var(--text);
    font-size: 1.25rem;
    font-weight: 700;
    box-shadow: var(--shadow);
    transition:
      transform 120ms ease,
      border-color 120ms ease;
  }

  @media (hover: hover) {
    .choice:not(:disabled):hover {
      border-color: var(--accent);
      transform: translateY(-2px);
    }
  }

  .choice:not(:disabled):active {
    transform: scale(0.97);
  }

  .choice:disabled {
    cursor: default;
  }

  .choice[data-result="correct"] {
    background: var(--correct);
    border-color: var(--correct);
    color: #ffffff;
    animation: pop 350ms ease;
  }

  .choice[data-result="wrong"] {
    background: var(--wrong);
    border-color: var(--wrong);
    color: #ffffff;
    animation: shake 350ms ease;
  }

  .quiz[data-answered] .choice:disabled:not([data-result]) {
    opacity: 0.6;
  }

  .feedback {
    min-height: 3em;
    text-align: center;
    font-size: 1.15rem;
    font-weight: 700;
  }

  .primary {
    justify-self: center;
    min-height: 3.25rem;
    padding: 0.75rem 2rem;
    border: none;
    border-radius: 999px;
    background: var(--accent);
    color: var(--accent-text);
    font-size: 1.2rem;
    font-weight: 800;
    box-shadow: var(--shadow);
  }

  /* Kept in the layout (no jump) but hidden and untabbable until answered. */
  .quiz:not([data-answered]) #next-button {
    visibility: hidden;
  }

  .results {
    background: var(--surface);
    border-radius: 1.5rem;
    box-shadow: var(--shadow);
    padding: 2rem 1.25rem;
    display: grid;
    gap: 1rem;
    justify-items: center;
    text-align: center;
    max-width: 36rem;
    width: 100%;
    margin-inline: auto;
  }

  .results h2 {
    font-size: clamp(1.6rem, 5vw, 2.25rem);
  }

  .final-score {
    font-size: clamp(3rem, 12vw, 5rem);
    font-weight: 900;
    line-height: 1;
    color: var(--accent);
  }

  .results-message {
    font-size: 1.25rem;
    font-weight: 700;
  }

  .new-best {
    font-weight: 800;
    color: var(--hot);
  }

  .results:not([data-new-best]) .new-best {
    display: none;
  }

  .best {
    color: var(--muted);
    font-weight: 600;
  }

  .missed {
    width: 100%;
    text-align: start;
  }

  .missed:not(:has(li)) {
    display: none;
  }

  .missed h3 {
    font-size: 1.1rem;
    margin-block-end: 0.5rem;
  }

  .missed-list {
    list-style: none;
    display: grid;
    gap: 0.4rem;
  }

  .missed-list li {
    border: 2px solid var(--border);
    border-radius: 0.75rem;
    padding: 0.4rem 0.75rem;
  }

  .save-score {
    width: 100%;
    display: grid;
    gap: 0.5rem;
    padding: 1rem;
    border: 3px solid var(--accent);
    border-radius: 1rem;
    text-align: start;
  }

  .save-prompt {
    font-weight: 800;
    font-size: 1.15rem;
    text-align: center;
  }

  .save-score label {
    font-weight: 700;
  }

  .save-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .save-row input {
    flex: 1 1 10rem;
    min-height: 3.25rem;
    padding: 0.5rem 0.9rem;
    border: 2px solid var(--muted);
    border-radius: 0.75rem;
    background: var(--surface);
    color: var(--text);
    /* 16px or more stops iOS Safari zooming in on focus. */
    font-size: 1.15rem;
  }

  .save-status {
    font-weight: 700;
  }

  .save-status:empty {
    display: none;
  }

  /* [data-scoreboard] is "loading", "offline", "ready", "can-save" or "saved". */
  .results:not([data-scoreboard="can-save"]) .save-score,
  .scoreboard-loading,
  .scoreboard-offline,
  [data-scoreboard="loading"] .scoreboard-table,
  [data-scoreboard="offline"] .scoreboard-table {
    display: none;
  }

  [data-scoreboard="loading"] .scoreboard-loading,
  [data-scoreboard="offline"] .scoreboard-offline {
    display: block;
  }

  .scoreboard {
    width: 100%;
    display: grid;
    gap: 0.5rem;
  }

  .scoreboard h3 {
    font-size: 1.1rem;
  }

  .scoreboard-note,
  .scoreboard-loading,
  .scoreboard-offline {
    color: var(--muted);
  }

  .scoreboard-table {
    width: 100%;
    border-collapse: collapse;
    font-variant-numeric: tabular-nums;
  }

  .scoreboard-table th,
  .scoreboard-table td {
    overflow-wrap: anywhere;
    padding: 0.45rem 0.5rem;
    border-block-end: 2px solid var(--border);
    text-align: start;
  }

  .scoreboard-table th {
    color: var(--muted);
    font-size: 0.9rem;
  }

  .scoreboard-table tr[data-mine] td {
    background: var(--accent);
    color: var(--accent-text);
    font-weight: 800;
  }

  .scoreboard-dialog {
    margin: auto;
    width: min(32rem, calc(100% - 2rem));
    max-height: calc(100dvh - 2rem);
    padding: 1.25rem;
    border: none;
    border-radius: 1.5rem;
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow);
  }

  .scoreboard-dialog[open] {
    display: grid;
    gap: 1rem;
    text-align: center;
  }

  .scoreboard-dialog .scoreboard-table th,
  .scoreboard-dialog .scoreboard-table td {
    text-align: start;
  }

  .scoreboard-dialog::backdrop {
    background: rgb(0 0 0 / 0.5);
  }

  .confetti {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
  }

  /* Side by side on wide screens such as an iPad in landscape. */
  @media (min-width: 52rem) and (orientation: landscape) {
    .quiz {
      grid-template-columns: 1fr 1fr;
      align-items: start;
    }

    .hud {
      grid-column: 1 / -1;
    }

    .state-shape {
      height: clamp(9rem, 42dvh, 24rem);
    }

    .choices {
      grid-template-columns: 1fr;
    }
  }

  @keyframes pop {
    50% {
      transform: scale(1.06);
    }
  }

  @keyframes shake {
    25% {
      transform: translateX(-6px);
    }
    75% {
      transform: translateX(6px);
    }
  }
}

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