/* Styles for the QSRSU question archive.
 *
 * Kept in its own file instead of a <style> block so the browser can cache it:
 * the HTML changes whenever the question list changes, while this stylesheet
 * rarely does. Repeat visits then re-use the cached CSS instead of
 * re-downloading it inside every HTML response. It also keeps presentation
 * separate from the template structure.
 */

/* ---------------------------------------------------------------------
     WHY ONLY transform + opacity ARE ANIMATED
     ---------------------------------------------------------------------
     The browser renders a page in three stages: layout (where every box
     goes), paint (filling in pixels), and composite (putting the painted
     layers on screen, usually on the GPU).

     `transform` and `opacity` can be handled entirely at the composite
     stage: the element is painted once, then the GPU slides or fades that
     already-painted layer. No box changes size, no pixel is repainted, so
     the animation stays smooth even while the rest of the page is busy.

     Animating `width`, `height`, `top`, `left`, `margin`, `padding`, etc.
     changes layout on every frame: the browser must re-measure the page,
     re-paint what moved, and then composite. Animating `filter: blur()`
     forces a full repaint of the blurred area on every frame. That repeated
     layout/paint work is what makes an animation stutter.

     So: every transition below touches only `transform` and `opacity`
     (plus the card's border-color, which is a cheap one-off paint that you
     explicitly asked for). Nothing animates layout.
     --------------------------------------------------------------------- */

  :root {
    /* Palette: deep charcoal, amber/gold, cream. No gradients. */
    --bg: #1b1712;
    --panel: #221d17;
    --panel-raised: #2a231b;
    --amber: #d99a3d;
    --cream: #e9e0d2;
    --muted: #a99c88;
    --line: #3b3227;
  }

  * {
    box-sizing: border-box;
  }

  html {
    /* Reserve the scrollbar gutter so opening the modal (which locks body
       scroll) does not shift the whole page sideways. This is a one-time
       layout change, not an animation. */
    scrollbar-gutter: stable;
  }

  body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--cream);
    font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
  }

  body.modal-open {
    overflow: hidden;
  }

  /* ---------------------------------------------------------------------
     Page header + grid
     --------------------------------------------------------------------- */
  .page-head {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 1rem;
    padding: 2.5rem clamp(1rem, 4vw, 3rem) 1.5rem;
    border-bottom: 1px solid var(--line);
  }

  .eyebrow {
    margin: 0 0 0.35rem;
    font-size: 0.7rem;
    letter-spacing: 0.28em;
    text-transform: uppercase;
    color: var(--amber);
  }

  /* Magazine-style edition line. It is its own element, never part of the
     h1, so the title stays a clean, separate masthead. */
  .edition {
    margin: 0 0 0.55rem;
    font-size: 0.66rem;
    font-weight: 600;
    letter-spacing: 0.25em;
    text-transform: uppercase;
    color: var(--amber);
  }

  h1 {
    margin: 0;
    font-size: clamp(1.5rem, 3vw, 2.25rem);
    font-weight: 600;
    letter-spacing: -0.01em;
  }

  .count {
    margin: 0;
    font-size: 0.85rem;
    color: var(--muted);
  }

  .count span {
    color: var(--amber);
  }

  /* Clock + count + refresh, grouped at the top-right of the header. */
  .head-meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: 0.75rem 1rem;
  }

  .clock {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.85rem;
    letter-spacing: 0.06em;
    color: var(--cream);
    /* Tabular digits keep the clock from shifting width every second. */
    font-variant-numeric: tabular-nums;
  }

  /* Outlined pill; same hover colours as the cards (amber border + text). */
  .refresh-btn {
    appearance: none;
    padding: 0.45rem 0.95rem;
    background: transparent;
    color: var(--cream);
    border: 1px solid var(--line);
    border-radius: 999px;
    font: inherit;
    font-size: 0.78rem;
    font-weight: 600;
    letter-spacing: 0.06em;
    cursor: pointer;
    transition: border-color 0.15s ease-out, color 0.15s ease-out;
  }

  .refresh-btn:hover {
    border-color: var(--amber);
    color: var(--amber);
  }

  .refresh-btn:focus-visible {
    outline: 2px solid var(--amber);
    outline-offset: 2px;
  }

  .refresh-btn:disabled {
    opacity: 0.5;
    cursor: default;
  }

  main {
    padding: 1.75rem clamp(1rem, 4vw, 3rem) 4rem;
  }

  .grid {
    display: grid;
    /* 4 columns on desktop, 2 on mobile (see the media query below). */
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 0.85rem;
  }

  /* Compact, uniform archive card: only the question id. */
  .card {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 0.3rem;
    min-height: 78px;
    padding: 0.85rem 1rem;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 10px;
    color: var(--cream);
    text-align: left;
    cursor: pointer;
    /* Hover lift is transform-only; border-color is a cheap paint. */
    transition: transform 0.15s ease-out, border-color 0.15s ease-out;
  }

  .card:hover {
    transform: translateY(-2px);
    border-color: var(--amber);
  }

  .card:active {
    transform: translateY(0);
  }

  .card:focus-visible {
    outline: 2px solid var(--amber);
    outline-offset: 2px;
  }

  .card-id {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 1.05rem;
    font-weight: 600;
    letter-spacing: 0.04em;
  }

  .empty {
    margin: 2rem 0;
    color: var(--muted);
  }

  /* ---------------------------------------------------------------------
     Modal
     --------------------------------------------------------------------- */
  .overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2vh 2vw;
    /* Dark tint behind the modal. */
    background: rgba(8, 6, 4, 0.72);
    /* Static blur: it is applied when the overlay paints, NOT animated.
       Only the overlay's opacity is animated, which the compositor handles. */
    -webkit-backdrop-filter: blur(5px);
    backdrop-filter: blur(5px);
    /* Open: fade the backdrop from opacity 0 to 1. */
    opacity: 0;
    transition: opacity 0.2s ease-out;
  }

  .overlay[hidden] {
    display: none;
  }

  .overlay.is-open {
    opacity: 1;
  }

  .dialog {
    position: relative;
    /* 80% of the viewport, centred by the overlay's flexbox. */
    width: 80vw;
    height: 80vh;
    display: grid;
    /* The panes fill the dialog; the nav bar gets its own strip at the bottom
       (grid-template-rows is declared here so `1fr` is the leftover height). */
    grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 14px;
    overflow: hidden;
    /* Closed start state for the pop-in. */
    opacity: 0;
    transform: scale(0.96);
    /* Open: scale .96 -> 1 + fade in, .18s ease-out. */
    transition: opacity 0.18s ease-out, transform 0.18s ease-out;
  }

  .overlay.is-open .dialog {
    opacity: 1;
    transform: scale(1);
  }

  .pane {
    min-width: 0;
    min-height: 0;
  }

  .pane-left {
    display: flex;
    flex-direction: column;
    gap: 0.9rem;
    padding: 1.25rem 1.4rem 1.5rem;
    border-right: 1px solid var(--line);
    overflow-y: auto;
  }

  .pane-right {
    display: grid;
    place-items: center;
    padding: 1.5rem;
    overflow-y: auto;
  }

  .q-image {
    display: block;
    width: 100%;
    /* Scaled to fit: never stretched, never taller than the pane allows. */
    max-height: 38vh;
    object-fit: contain;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #15110d;
  }

  .q-image[hidden] {
    display: none;
  }

  .image-fallback {
    padding: 2rem 1rem;
    border: 1px dashed var(--line);
    border-radius: 8px;
    color: var(--muted);
    font-size: 0.85rem;
    text-align: center;
  }

  .image-fallback[hidden] {
    display: none;
  }

  .q-text {
    margin: 0;
    /* Keep the line breaks the transcriber stored. */
    white-space: pre-wrap;
    font-size: 0.92rem;
  }

  .options {
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
    margin: 0;
    padding: 0;
    list-style: none;
  }

  /* An empty list should not leave a gap behind. */
  .options:empty {
    display: none;
  }

  .options li {
    display: flex;
    gap: 0.6rem;
    font-size: 0.9rem;
    color: var(--muted);
  }

  .opt-letter {
    flex: none;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    color: var(--amber);
  }

  /* ---------------------------------------------------------------------
     Math (KaTeX)
     ---------------------------------------------------------------------
     KaTeX replaces only $...$/$$...$$ spans; the rest of the text is left
     untouched. Display math is its own block, so a wide formula scrolls
     inside the pane instead of widening the dialog. */
  .katex {
    font-size: 1.02em;
  }

  .katex-display {
    margin: 0.6rem 0;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: normal;
  }

  .katex-display > .katex {
    white-space: nowrap;
  }

  /* Invalid LaTeX is shown as readable source (throwOnError is off in JS)
     instead of disappearing from the panel. */
  .katex-error {
    color: var(--amber);
  }

  /* ---------------------------------------------------------------------
     Reveal button + answer
     --------------------------------------------------------------------- */
  .reveal-btn {
    appearance: none;
    background: transparent;
    color: var(--cream);
    border: 1px solid var(--amber);
    border-radius: 999px;
    padding: 0.85rem 2.2rem;
    font: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    cursor: pointer;
    /* Only the fade-out is animated (opacity). The hover colour change is
       instant: it is a tiny paint, not worth a transition. */
    transition: opacity 0.15s ease-out;
  }

  .reveal-btn:hover {
    color: var(--amber);
    border-color: var(--cream);
  }

  .reveal-btn:focus-visible {
    outline: 2px solid var(--amber);
    outline-offset: 3px;
  }

  .reveal-btn.is-leaving {
    opacity: 0;
  }

  .answer {
    width: 100%;
    /* Closed start state for the fade/slide-in. */
    opacity: 0;
    transform: translateY(6px);
    /* Reveal: opacity + translateY(6px -> 0), .2s. */
    transition: opacity 0.2s ease-out, transform 0.2s ease-out;
  }

  .answer.is-visible {
    opacity: 1;
    transform: translateY(0);
  }

  .verdict {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin-bottom: 1.1rem;
    font-size: 1.6rem;
  }

  .verdict-label {
    font-size: 0.78rem;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--muted);
  }

  .answer h3 {
    margin: 0 0 0.4rem;
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--amber);
  }

  .correct-answer {
    margin: 0 0 1.25rem;
    font-size: 1.05rem;
  }

  .explanation {
    margin: 0;
    white-space: pre-wrap;
    font-size: 0.9rem;
    color: var(--muted);
  }

  /* ---------------------------------------------------------------------
     Close button
     --------------------------------------------------------------------- */
  .close {
    position: absolute;
    top: 0.7rem;
    right: 0.7rem;
    z-index: 2;
    display: grid;
    place-items: center;
    width: 2rem;
    height: 2rem;
    padding: 0;
    background: var(--panel-raised);
    color: var(--cream);
    border: 1px solid var(--line);
    border-radius: 50%;
    font-size: 0.95rem;
    line-height: 1;
    cursor: pointer;
    /* No transition: this button only changes colour on hover, and an instant
       change is cheap. Animated properties stay limited to transform/opacity. */
  }

  .close:hover {
    border-color: var(--amber);
    color: var(--amber);
  }

  .close:focus-visible {
    outline: 2px solid var(--amber);
    outline-offset: 2px;
  }

  /* ---------------------------------------------------------------------
     Prev / next navigation (footer of the dialog)
     --------------------------------------------------------------------- */
  .nav-bar {
    grid-column: 1 / -1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.6rem 1rem;
    border-top: 1px solid var(--line);
    background: var(--panel-raised);
  }

  .nav-bar[hidden] {
    display: none;
  }

  .nav-btn {
    appearance: none;
    padding: 0.4rem 0.9rem;
    background: transparent;
    color: var(--cream);
    border: 1px solid var(--line);
    border-radius: 999px;
    font: inherit;
    font-size: 0.76rem;
    font-weight: 600;
    letter-spacing: 0.06em;
    cursor: pointer;
    transition: border-color 0.15s ease-out, color 0.15s ease-out;
  }

  .nav-btn:hover:not(:disabled) {
    border-color: var(--amber);
    color: var(--amber);
  }

  .nav-btn:focus-visible {
    outline: 2px solid var(--amber);
    outline-offset: 2px;
  }

  .nav-btn:disabled {
    opacity: 0.35;
    cursor: default;
  }

  .nav-pos {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.72rem;
    letter-spacing: 0.08em;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
  }

  /* ---------------------------------------------------------------------
     Responsive
     --------------------------------------------------------------------- */
  @media (max-width: 760px) {
    .grid {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .page-head {
      flex-direction: column;
      align-items: flex-start;
      padding-top: 1.75rem;
    }

    .head-meta {
      justify-content: flex-start;
    }
  }

  /* ---------------------------------------------------------------------
     Reduced motion
     ---------------------------------------------------------------------
     Honour the OS "reduce motion" setting: disable every transition and
     animation. The JS checks the same media query, so the reveal sequence
     becomes an instant swap instead of waiting for a transitionend event. */
  @media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
      transition-duration: 0.001ms !important;
      transition-delay: 0ms !important;
      animation-duration: 0.001ms !important;
      animation-iteration-count: 1 !important;
    }

    /* No hover lift either; the border highlight is enough feedback. */
    .card:hover {
      transform: none;
    }
  }
