/*
 * Voter pages. --bg and --fg come from the contest (set on <body>).
 * Propshaft's :app bundle loads this file in every layout, so every rule is
 * nested under body.voter and keyframes carry a voter- prefix. Keep it that way.
 */

body.voter {
  --surface: color-mix(in srgb, var(--fg) 9%, var(--bg));
  --surface-strong: color-mix(in srgb, var(--fg) 16%, var(--bg));
  --muted: color-mix(in srgb, var(--fg) 72%, var(--bg));
  --shadow: 0 1px 2px rgb(0 0 0 / 0.12), 0 12px 32px -8px rgb(0 0 0 / 0.35);
  --radius: 22px;
  --ease-out: cubic-bezier(0.2, 0.8, 0.2, 1);

  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 17px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;

  *, *::before, *::after { box-sizing: border-box; }

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

  .visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
  }

  /* Admin results preview banner */
  .preview-note {
    display: flex; flex-wrap: wrap; justify-content: space-between; gap: 6px 16px; align-items: center;
    background: var(--surface-strong); border-radius: 12px; padding: 10px 16px; margin-bottom: 20px; font-size: 0.95rem;
    p { margin: 0; }
    a { color: inherit; font-weight: 650; }
  }

  .voter-main {
    max-width: 1120px;
    margin: 0 auto;
    padding: max(28px, env(safe-area-inset-top)) 16px max(40px, env(safe-area-inset-bottom));
  }

  /* ---------- Type ---------- */

  .contest-title {
    margin: 0;
    font-size: clamp(2.6rem, 11vw, 5rem);
    font-weight: 850;
    line-height: 0.98;
    letter-spacing: -0.035em;
    text-wrap: balance;
  }

  .screen-title {
    margin: 0;
    font-size: clamp(2.2rem, 9vw, 4rem);
    font-weight: 850;
    line-height: 1.02;
    letter-spacing: -0.03em;
    text-wrap: balance;
  }

  .lede {
    margin: 0;
    font-size: 1.2rem;
    color: var(--muted);
    text-wrap: pretty;
  }

  /* ---------- Screens ---------- */

  .screen { display: flex; flex-direction: column; gap: 24px; }

  .screen--center {
    min-height: calc(100dvh - 120px);
    justify-content: center;
    align-items: center;
    text-align: center;
    max-width: 640px;
    margin: 0 auto;

    .lede { text-wrap: balance; }
  }

  .welcome { max-width: 640px; margin: 0 auto; gap: 28px; padding-top: 4vh; }

  .instructions {
    font-size: 1.2rem;

    p { margin: 0 0 0.6em; }
    p:last-child { margin-bottom: 0; }
  }

  .panel {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 22px 22px 6px;
    box-shadow: var(--shadow);

    p { margin: 0 0 16px; }
  }
  .panel-title {
    margin: 0 0 10px;
    font-size: 0.85rem;
    font-weight: 800;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
  }

  /* ---------- Results ---------- */

  .results { max-width: 760px; margin: 0 auto; gap: 28px; }
  .results-header { display: flex; flex-direction: column; gap: 10px; text-align: center; padding: 2vh 0 4px; }
  .results-header .screen-title {
    font-size: 1rem;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--muted);
    font-weight: 800;
  }

  .result-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 22px; }
  .result-card {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 12px;
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
    animation: voter-enter-up 320ms var(--ease-out) both;
  }
  .result-card--first {
    padding: 14px;
    gap: 18px;
    box-shadow: 0 0 0 3px var(--fg), var(--shadow);

    .result-title { font-size: clamp(1.7rem, 6vw, 2.4rem); letter-spacing: -0.025em; }
    .rank-badge { font-size: 1.15rem; padding: 9px 18px; }
  }
  .rank-badge {
    position: absolute;
    top: 22px;
    left: 22px;
    z-index: 2;
    padding: 7px 14px;
    border-radius: 999px;
    background: var(--fg);
    color: var(--bg);
    font-weight: 850;
    font-size: 1rem;
    letter-spacing: 0.01em;
    box-shadow: 0 2px 10px rgb(0 0 0 / 0.3);
  }
  .result-title { margin: 0; font-size: 1.5rem; font-weight: 800; line-height: 1.15; letter-spacing: -0.02em; }
  .result-caption { margin: 4px 0 0; color: var(--muted); }
  .result-confidence { margin: 8px 0 0; font-size: 0.95rem; font-weight: 700; color: var(--muted); }

  .stats { padding-bottom: 14px; }
  .stat-row { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; margin: 0 0 18px; }
  .stat { display: flex; flex-direction: column; }
  .stat-value { font-size: 2rem; font-weight: 850; line-height: 1.1; letter-spacing: -0.02em; }
  .stat-label { color: var(--muted); font-size: 0.9rem; font-weight: 600; }
  @media (min-width: 640px) { .stat-row { grid-template-columns: repeat(4, 1fr); } }

  /* ---------- Buttons ---------- */

  .big-button, .pick-button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 64px;
    padding: 14px 24px;
    border: 0;
    border-radius: 999px;
    background: var(--fg);
    color: var(--bg);
    font: inherit;
    font-size: 1.25rem;
    font-weight: 800;
    letter-spacing: -0.01em;
    text-decoration: none;
    cursor: pointer;
    box-shadow: 0 6px 18px -6px rgb(0 0 0 / 0.45);
    transition: transform 120ms var(--ease-out), box-shadow 120ms var(--ease-out), opacity 120ms;
    touch-action: manipulation;

    &:active { transform: scale(0.97); box-shadow: 0 2px 8px -4px rgb(0 0 0 / 0.45); }
  }
  .pick-button {
    margin-top: auto;

    &[disabled] { cursor: default; }
  }

  .more-form { width: 100%; max-width: 420px; margin: 8px 0 0; }

  .text-button {
    min-height: 48px;
    padding: 10px 16px;
    border: 0;
    background: none;
    color: var(--muted);
    font: inherit;
    font-weight: 600;
    text-decoration: underline;
    text-underline-offset: 4px;
    cursor: pointer;
    touch-action: manipulation;
  }
  .text-button--strong { color: var(--fg); font-weight: 800; display: inline-flex; align-items: center; }

  /* ---------- Voting header ---------- */

  .voting { gap: 18px; }

  .vote-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
    gap: 10px 16px;
  }
  .vote-title {
    margin: 0;
    font-size: clamp(1.5rem, 6vw, 2.25rem);
    font-weight: 850;
    line-height: 1.05;
    letter-spacing: -0.025em;
  }
  .counter {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;

    &.is-ticking .counter-count { animation: voter-tick 320ms var(--ease-out); }
  }
  .counter-count { font-size: 1.15rem; }
  .counter-label {
    font-size: 0.8rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
  }

  .progress {
    grid-column: 1 / -1;
    height: 6px;
    border-radius: 999px;
    background: var(--surface-strong);
    overflow: hidden;
  }
  .progress-bar {
    height: 100%;
    border-radius: inherit;
    background: var(--fg);
    transition: width 400ms var(--ease-out);
  }

  .vote-message {
    margin: 0;
    padding: 14px 18px;
    border-radius: 16px;
    background: var(--fg);
    color: var(--bg);
    font-weight: 700;
    text-align: center;

    &[hidden] { display: none; }
  }

  .vote-footer {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 8px 24px;
  }

  /* ---------- Pair and cards ---------- */

  .pair {
    display: grid;
    grid-template-columns: 1fr;
    gap: 18px;
  }

  .entry-card {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 12px;
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
    transition: transform 240ms var(--ease-out), opacity 240ms var(--ease-out);

    &.is-picked { transform: scale(1.04); z-index: 1; }
    &.is-picked .check-badge { transform: scale(1); opacity: 1; }
    &.is-dismissed { transform: scale(0.96); opacity: 0.3; }
  }

  .entry-labels { padding: 0 6px; }
  .entry-title {
    margin: 0;
    font-size: 1.3rem;
    font-weight: 800;
    line-height: 1.2;
    letter-spacing: -0.015em;
  }
  .entry-caption {
    margin: 4px 0 0;
    color: var(--muted);
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    overflow: hidden;
  }

  .check-badge {
    position: absolute;
    z-index: 2;
    top: -12px;
    right: -8px;
    display: grid;
    place-items: center;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background: var(--fg);
    color: var(--bg);
    box-shadow: 0 6px 16px -4px rgb(0 0 0 / 0.45);
    transform: scale(0);
    opacity: 0;
    transition: transform 260ms cubic-bezier(0.3, 1.6, 0.5, 1), opacity 120ms;
    pointer-events: none;
  }

  .stage {
    transition: opacity 150ms ease;

    &.is-entering .entry-card { animation: voter-enter-up 260ms var(--ease-out) both; }
    &.is-entering .entry-card[data-side="second"] { animation-delay: 60ms; }
    &.is-fading-out { opacity: 0; }
    &.is-fading-in { animation: voter-fade-in 180ms ease both; }
  }

  /* ---------- Photo grid ---------- */

  .photo-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
    border-radius: calc(var(--radius) - 8px);
    overflow: hidden;
    aspect-ratio: 4 / 3;
    background: var(--surface-strong);
  }
  .photo-grid--1 { grid-template-columns: 1fr; }
  .photo-grid--2 { grid-template-rows: 1fr; }
  .photo-grid--3 { grid-template-rows: 1fr 1fr; }
  .photo-grid--3 .photo-tile:first-child { grid-row: span 2; }
  .photo-grid--4 { grid-template-rows: 1fr 1fr; }

  .photo-tile {
    position: relative;
    display: block;
    min-width: 0;
    min-height: 0;
    padding: 0;
    border: 0;
    background: none;
    cursor: zoom-in;
    overflow: hidden;

    img {
      display: block;
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  .photo-more {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    background: rgb(0 0 0 / 0.5);
    color: #fff;
    font-size: 1.8rem;
    font-weight: 800;
  }

  /* ---------- Phones: both entries in one screen ---------- */

  @media (max-width: 759px) {
    .voter-main:has(.voting) { padding-top: max(12px, env(safe-area-inset-top)); padding-bottom: 16px; }
    .voting { gap: 10px; }

    .vote-header { gap: 8px 12px; align-items: center; }
    .vote-title {
      font-size: 1.25rem;
      line-height: 1.15;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .counter { flex-direction: row; align-items: baseline; gap: 8px; }
    .counter-count { font-size: 1rem; }
    .counter-label { font-size: 0.72rem; }

    .pair { gap: 10px; }
    .entry-card { padding: 8px; gap: 8px; border-radius: 18px; }
    .check-badge { width: 44px; height: 44px; top: -8px; right: -4px; }

    /* Photo height comes from the screen height so two cards and the skip link fit. */
    .photo-grid {
      aspect-ratio: auto;
      height: clamp(110px, calc((100vh - 310px) / 2), 75vw);
      height: clamp(110px, calc((100dvh - 310px) / 2), 75vw);
      border-radius: 12px;
    }
    .pair--labels .photo-grid {
      height: clamp(96px, calc((100vh - 450px) / 2), 75vw);
      height: clamp(96px, calc((100dvh - 450px) / 2), 75vw);
    }

    .entry-labels { padding: 0 4px; }
    .entry-title { font-size: 1.05rem; line-height: 1.25; }
    .entry-caption { margin-top: 2px; font-size: 0.9rem; line-height: 1.35; }

    .pick-button { min-height: 56px; padding: 0 20px; font-size: 1.15rem; }
    .text-button { min-height: 44px; padding: 8px 14px; }
  }

  /* Short phones (iPhone SE and the like): the skip link may scroll, so its
     space goes to the photos; captions drop to one line. */
  @media (max-width: 759px) and (max-height: 740px) {
    .voter-main:has(.voting) { padding-top: max(10px, env(safe-area-inset-top)); }
    .voting { gap: 8px; }
    .vote-header { gap: 6px 12px; }
    .vote-title { font-size: 1.1rem; }
    .progress { height: 5px; }
    .entry-card { gap: 6px; }
    .entry-caption { -webkit-line-clamp: 1; line-clamp: 1; }

    .photo-grid {
      height: clamp(80px, calc((100vh - 240px) / 2), 75vw);
      height: clamp(80px, calc((100dvh - 240px) / 2), 75vw);
    }
    .pair--labels .photo-grid {
      height: clamp(72px, calc((100vh - 336px) / 2), 75vw);
      height: clamp(72px, calc((100dvh - 336px) / 2), 75vw);
    }
  }

  /* ---------- Wide screens: side by side ---------- */

  @media (min-width: 760px) {
    .pair { grid-template-columns: 1fr 1fr; gap: 28px; }
    .stage.is-entering .entry-card[data-side="first"] { animation-name: voter-enter-left; }
    .stage.is-entering .entry-card[data-side="second"] { animation-name: voter-enter-right; }
  }

  /* ---------- Lightbox ---------- */

  .lightbox {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: grid;
    place-items: center;
    background: rgb(10 10 12 / 0.94);
    animation: voter-fade-in 160ms ease both;
    touch-action: pan-y;
  }
  .lightbox-image {
    max-width: 100vw;
    max-height: 100dvh;
    object-fit: contain;
    user-select: none;
    -webkit-user-drag: none;
  }
  .lightbox-button {
    position: absolute;
    display: grid;
    place-items: center;
    width: 52px;
    height: 52px;
    border: 0;
    border-radius: 50%;
    background: rgb(255 255 255 / 0.16);
    color: #fff;
    font: 700 1.6rem/1 system-ui, sans-serif;
    cursor: pointer;

    &:hover { background: rgb(255 255 255 / 0.28); }
    &:focus-visible { outline: 3px solid #fff; outline-offset: 2px; }
  }
  .lightbox-close { top: max(14px, env(safe-area-inset-top)); right: 14px; }
  .lightbox-prev { left: 14px; top: 50%; translate: 0 -50%; }
  .lightbox-next { right: 14px; top: 50%; translate: 0 -50%; }
  .lightbox-count {
    position: absolute;
    bottom: max(18px, env(safe-area-inset-bottom));
    left: 50%;
    translate: -50% 0;
    padding: 6px 14px;
    border-radius: 999px;
    background: rgb(255 255 255 / 0.16);
    color: #fff;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
  }
  @media (hover: none) {
    .lightbox-prev, .lightbox-next { display: none; }
  }

  /* ---------- Confetti ---------- */

  .thanks { position: relative; }
  .confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 50; }
  .confetti-piece {
    position: absolute;
    top: 38%;
    left: 50%;
    width: 10px;
    height: 16px;
    border-radius: 2px;
    animation: voter-confetti-burst var(--dur, 1.8s) cubic-bezier(0.15, 0.7, 0.4, 1) var(--delay, 0s) both;
  }

  /* ---------- Reduced motion: crossfades only ---------- */

  @media (prefers-reduced-motion: reduce) {
    .entry-card, .big-button, .pick-button, .progress-bar { transition: opacity 200ms ease; }
    .entry-card.is-picked, .entry-card.is-dismissed { transform: none; }
    .check-badge { transform: none; transition: opacity 200ms ease; }
    .big-button:active, .pick-button:active { transform: none; }
    .stage.is-entering .entry-card,
    .stage.is-entering .entry-card[data-side] { animation: voter-fade-in 200ms ease both; animation-delay: 0ms; }
    .counter.is-ticking .counter-count { animation: none; }
    .confetti { display: none; }
  }
}

/* ---------- Keyframes (prefixed; keyframes cannot be nested) ---------- */

@keyframes voter-enter-up { from { opacity: 0; transform: translateY(24px); } }
@keyframes voter-enter-left { from { opacity: 0; transform: translateX(-24px); } }
@keyframes voter-enter-right { from { opacity: 0; transform: translateX(24px); } }
@keyframes voter-fade-in { from { opacity: 0; } }
@keyframes voter-tick {
  0% { transform: translateY(0); opacity: 1; }
  40% { transform: translateY(-6px); opacity: 0; }
  41% { transform: translateY(6px); opacity: 0; }
  100% { transform: translateY(0); opacity: 1; }
}
@keyframes voter-confetti-burst {
  0% { transform: translate(0, 0) rotate(0deg); opacity: 1; }
  35% { transform: translate(var(--dx), var(--dy)) rotate(calc(var(--spin) * 0.4)); opacity: 1; }
  100% { transform: translate(calc(var(--dx) * 1.25), calc(var(--dy) + 75vh)) rotate(var(--spin)); opacity: 0; }
}
