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

  .app {
    --bg: var(--bg);
    --surface: var(--bg-card-2);
    --border: var(--line);
    --text: var(--text);
    --muted: var(--text-soft);
    --accent: var(--purple-2);
    --accent-dim: var(--purple);
    --danger: #ff9ab5;
    --ok: var(--success);
    --warn: #fbbf24;
    --radius: var(--radius-md);
    font-family: "Figtree", ui-sans-serif, system-ui, sans-serif;
    width: 100%;
    max-width: 680px;
    margin: 0 auto;
    padding: 0.75rem clamp(0.25rem, 1.5vw, 0.75rem) 2rem;
  }

  /* Analyzing view: no extra app padding around the progress card. */
  .app:has(#panel-result:not(.hidden)) {
    padding: 0;
    max-width: none;
  }

  /* Let the portrait stage grow with tablets and desktops instead of locking
     every non-phone viewport to the same small fixed size. */
  @media (min-width: 520px) {
    .app {
      width: 100%;
      max-width: 720px;
      padding: 0.75rem 0 1.5rem;
    }

    .scan-viewport .video-wrap {
      width: min(100%, clamp(400px, 60vw, 460px));
      max-height: none;
      aspect-ratio: 16 / 21;
    }
  }

  .header {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.75rem;
    min-height: 0;
    margin-bottom: 0.55rem;
  }

  .header:not(:has(.face-scan-info-btn:not([hidden]):not(.hidden))) {
    display: none;
  }

  .header #face-scan-title {
    display: none !important;
  }

  .header .face-scan-info-btn[hidden],
  .header .face-scan-info-btn.hidden {
    display: none !important;
  }

  .header .face-scan-context[hidden] {
    display: none !important;
  }

  .face-scan-info-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.1rem;
    height: 2.1rem;
    flex-shrink: 0;
    border-radius: 999px;
    border: 1px solid rgba(255, 255, 255, 0.16);
    background: rgba(255, 255, 255, 0.04);
    color: var(--muted);
    cursor: pointer;
    transition: border-color 150ms ease, background-color 150ms ease, color 150ms ease;
  }

  .face-scan-info-btn:hover {
    border-color: rgba(76, 227, 255, 0.4);
    background: rgba(76, 227, 255, 0.1);
    color: var(--text);
  }

  .face-scan-info-btn svg {
    width: 18px;
    height: 18px;
  }

  .step-label {
    display: none;
  }
  
  .muted.block {
    margin: 0;
    font-size: 0.92rem;
    color: var(--muted);
  }
  
  @keyframes spin {
    to {
      transform: rotate(360deg);
    }
  }
  
  .panel {
    margin-top: 0.35rem;
    padding: 1.25rem 1.35rem;
    background:
      linear-gradient(145deg, var(--bg-card-2), rgba(129, 74, 200, 0.1)),
      radial-gradient(
        ellipse 120% 80% at 100% 0%,
        rgba(223, 122, 254, 0.12),
        transparent 58%
      );
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 14px 38px rgba(0, 0, 0, 0.24);
  }

  /* Drop nested panel chrome so only the wizard shell + one content card remain. */
  .panel.scan,
  .panel.result {
    margin-top: 0;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: 0;
    box-shadow: none;
  }

  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.65rem 1.1rem;
    border-radius: 10px;
    border: 1px solid rgba(255, 255, 255, 0.14);
    background: rgba(255, 255, 255, 0.05);
    color: var(--text);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition:
      transform 160ms ease,
      border-color 160ms ease,
      box-shadow 160ms ease,
      background-color 160ms ease;
  }
  
  .btn:hover:not(:disabled) {
    transform: translateY(-2px);
    border-color: rgba(223, 122, 254, 0.35);
    background: rgba(255, 255, 255, 0.08);
  }
  
  .btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
  }
  
  .btn.primary {
    background: linear-gradient(
      135deg,
      rgba(129, 74, 200, 0.95),
      rgba(223, 122, 254, 0.95)
    );
    border-color: rgba(255, 255, 255, 0.12);
    color: #fff;
    box-shadow: 0 10px 28px rgba(129, 74, 200, 0.35);
  }
  
  .btn.primary:hover:not(:disabled) {
    border-color: rgba(255, 255, 255, 0.22);
    filter: none;
  }
  
  .hint {
    margin: 0.85rem 0 0;
    font-size: 0.8rem;
    color: var(--muted);
  }

  .hint:empty {
    display: none;
  }
  
  .hidden {
    display: none !important;
  }
  
  .video-wrap {
    position: relative;
    border-radius: calc(var(--radius) + 2px);
    overflow: hidden;
    background:
      radial-gradient(ellipse 90% 70% at 50% 0%, rgba(223, 122, 254, 0.08), transparent 58%),
      #050508;
    border: 1px solid rgba(255, 255, 255, 0.1);
    width: min(100%, 440px);
    aspect-ratio: 16 / 21;
    max-height: none;
    margin: 0 auto;
    box-shadow:
      0 22px 56px rgba(0, 0, 0, 0.42),
      0 0 0 1px rgba(255, 255, 255, 0.04) inset,
      0 0 48px rgba(129, 74, 200, 0.12);
    isolation: isolate;
    transition:
      border-color 280ms ease,
      box-shadow 280ms ease;
  }

  .scan-viewport {
    position: relative;
    width: 100%;
    margin: 0 auto;
  }

  .video-wrap::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: 5;
    pointer-events: none;
    background:
      radial-gradient(
        ellipse 78% 72% at 50% 40%,
        transparent 48%,
        rgba(0, 0, 0, 0.34) 100%
      ),
      linear-gradient(
        180deg,
        rgba(0, 0, 0, 0.28) 0%,
        transparent 16%,
        transparent 80%,
        rgba(0, 0, 0, 0.42) 100%
      );
    opacity: 0.95;
  }

  .video-wrap::after {
    content: "";
    position: absolute;
    inset: 0;
    z-index: 6;
    pointer-events: none;
    border-radius: inherit;
    box-shadow:
      inset 0 0 0 1px rgba(255, 255, 255, 0.08),
      inset 0 1px 0 rgba(255, 255, 255, 0.06);
  }

  .video-wrap:has(.placement-status.state-good)::before {
    background:
      radial-gradient(
        ellipse 72% 68% at 50% 40%,
        transparent 50%,
        rgba(0, 0, 0, 0.28) 100%
      ),
      linear-gradient(
        180deg,
        rgba(0, 0, 0, 0.2) 0%,
        transparent 16%,
        transparent 80%,
        rgba(0, 0, 0, 0.34) 100%
      );
  }

  .video-wrap:has(.placement-status.state-good) {
    border-color: rgba(118, 240, 191, 0.28);
    box-shadow:
      0 22px 56px rgba(0, 0, 0, 0.42),
      0 0 0 1px rgba(118, 240, 191, 0.1) inset,
      0 0 42px rgba(118, 240, 191, 0.1);
  }

  .video-wrap:has(#overlay-countdown:not(.hidden)) {
    border-color: rgba(223, 122, 254, 0.34);
    box-shadow:
      0 22px 56px rgba(0, 0, 0, 0.42),
      0 0 52px rgba(223, 122, 254, 0.16);
  }

  .scan-viewport.is-recording .video-wrap {
    border-color: rgba(76, 227, 255, 0.22);
    box-shadow:
      0 22px 56px rgba(0, 0, 0, 0.42),
      0 0 40px rgba(76, 227, 255, 0.1);
  }
  
  .scan-overlay {
    position: absolute;
    inset: 0;
    z-index: 12;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 1.25rem;
    background:
      radial-gradient(
        ellipse 120% 85% at 50% 0%,
        rgba(223, 122, 254, 0.18),
        transparent 62%
      ),
      radial-gradient(
        ellipse 90% 70% at 50% 100%,
        rgba(76, 227, 255, 0.08),
        transparent 58%
      ),
      rgba(6, 8, 14, 0.88);
    text-align: center;
    pointer-events: auto;
    backdrop-filter: blur(12px) saturate(130%);
    -webkit-backdrop-filter: blur(12px) saturate(130%);
    animation: scan-overlay-in 320ms ease-out both;
  }

  @keyframes scan-overlay-in {
    from {
      opacity: 0;
      transform: scale(1.02);
    }
    to {
      opacity: 1;
      transform: scale(1);
    }
  }
  
  .scan-overlay.overlay-error {
    background: rgba(24, 12, 12, 0.88);
  }
  
  .scan-overlay.hidden {
    display: none !important;
  }
  
  .scan-overlay-title {
    margin: 0 0 0.35rem;
    font-size: 1rem;
    font-weight: 600;
  }
  
  .scan-overlay-body {
    margin: 0;
    font-size: 0.88rem;
    color: rgba(232, 236, 241, 0.88);
    max-width: 22rem;
    line-height: 1.45;
  }

  .scan-overlay-progress {
    display: inline-flex;
    gap: 0.35rem;
    margin-top: 0.75rem;
  }

  .scan-overlay-progress span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: rgba(223, 122, 254, 0.9);
    animation: scan-overlay-dot 1.15s ease-in-out infinite;
  }

  .scan-overlay-progress span:nth-child(2) {
    animation-delay: 0.15s;
  }

  .scan-overlay-progress span:nth-child(3) {
    animation-delay: 0.3s;
  }

  .scan-overlay-tip {
    margin: 0.55rem 0 0;
    font-size: 0.78rem;
    color: rgba(219, 223, 238, 0.78);
    max-width: 26rem;
  }

  .scan-overlay-stages {
    margin-top: 0.55rem;
    display: flex;
    gap: 0.45rem;
    flex-wrap: wrap;
    justify-content: center;
  }

  .scan-stage {
    border-radius: 999px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    background: rgba(11, 15, 28, 0.62);
    color: rgba(226, 232, 248, 0.78);
    padding: 0.22rem 0.58rem;
    font-size: 0.72rem;
    letter-spacing: 0.02em;
    transition: all 180ms ease;
  }

  .scan-stage.is-active {
    border-color: rgba(223, 122, 254, 0.55);
    color: #f0e6ff;
    box-shadow: 0 0 0 2px rgba(223, 122, 254, 0.16);
  }

  .scan-stage.is-done {
    border-color: rgba(116, 247, 181, 0.42);
    color: rgba(159, 252, 206, 0.9);
  }
  
  .scan-overlay-spinner {
    margin-top: 1rem;
    width: 2.35rem;
    height: 2.35rem;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.1);
    border-top-color: var(--accent);
    border-right-color: rgba(76, 227, 255, 0.55);
    animation: spin 0.9s linear infinite;
    box-shadow: 0 0 18px rgba(223, 122, 254, 0.22);
  }

  @keyframes scan-overlay-dot {
    0%,
    100% {
      opacity: 0.35;
      transform: translateY(0);
    }
    50% {
      opacity: 1;
      transform: translateY(-2px);
    }
  }
  
  .scan-overlay-actions {
    margin-top: 1.25rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.65rem;
    justify-content: center;
  }
  
  .chrome-overlay {
    position: absolute;
    inset: 0;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: center;
    z-index: 15;
  }

  /* Shen-style bottom scan progress bar, bound to --record-progress (set in
     recording_controller.js). Small side insets match the reference (~2%).
     A compositor-friendly scale transform follows the display refresh while
     the underlying elapsed time remains tied to the recording budget. */
  .scan-progress-bar {
    position: absolute;
    left: 2%;
    right: 2%;
    bottom: 0;
    z-index: 8;
    height: 4px;
    background: rgba(255, 255, 255, 0.22);
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
    transition: opacity 180ms ease;
  }

  /* Visible only while recording (Shen-style bottom fill). */
  .scan-viewport.is-recording .scan-progress-bar {
    opacity: 1;
  }

  .scan-progress-bar-fill {
    height: 100%;
    width: 100%;
    background: #4ee06b;
    transform: scaleX(var(--record-progress, 0));
    transform-origin: left center;
    will-change: transform;
  }

  .scan-hud {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 3;
    display: block;
  }

  .scan-hud-bottom {
    display: none;
  }

  /* Shen-style: a centered "MEASUREMENT CONDITIONS" kicker + 5-star quality rating at the top. */
  .scan-hud-top {
    position: absolute;
    top: max(0.85rem, env(safe-area-inset-top));
    left: 0;
    right: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.3rem;
    text-align: center;
    padding: 0 1rem;
  }

  .scan-hud-kicker {
    margin: 0;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: rgba(255, 255, 255, 0.88);
    text-shadow: 0 1px 6px rgba(0, 0, 0, 0.45);
  }

  .scan-hud-text {
    display: none;
  }

  .scan-hud-rating {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.55rem;
  }

  /* Signal-strength bars: this is a live conditions readout (delivered fps),
     not a rating, so ascending bars read better than stars. Level is encoded
     by bar count AND height, so colour isn't the only cue. */
  .scan-hud-signal {
    display: flex;
    align-items: flex-end;
    gap: 3px;
    height: 18px;
    filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.4));
  }

  .scan-hud-signal span {
    display: block;
    width: 4px;
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.28);
  }

  .scan-hud-signal span:nth-child(1) {
    height: 6px;
  }
  .scan-hud-signal span:nth-child(2) {
    height: 9px;
  }
  .scan-hud-signal span:nth-child(3) {
    height: 12px;
  }
  .scan-hud-signal span:nth-child(4) {
    height: 15px;
  }
  .scan-hud-signal span:nth-child(5) {
    height: 18px;
  }

  /* No transition: the reference swaps fill in a single frame (hard cut).
     Colour is semantic — a poor reading should look like a warning, not just
     "fewer green bars". Levels come from delivered fps (computeQualityStarLevel):
     1 = poor, 2-3 = fair, 4-5 = good. Level 0 (unknown) stays dim. */
  .scan-hud-signal[data-level="1"] span:nth-child(-n + 1) {
    background: #ff5c5c;
  }

  .scan-hud-signal[data-level="2"] span:nth-child(-n + 2),
  .scan-hud-signal[data-level="2.5"] span:nth-child(-n + 2),
  .scan-hud-signal[data-level="3"] span:nth-child(-n + 3) {
    background: #ffb347;
  }

  .scan-hud-signal[data-level="4"] span:nth-child(-n + 4),
  .scan-hud-signal[data-level="5"] span:nth-child(-n + 5) {
    background: #4ee06b;
  }

  .scan-face-guide {
    position: absolute;
    /* Shen alignment frame: generous inset so the face sits inside the brackets. */
    inset: 12% 16%;
    pointer-events: none;
    display: block;
    z-index: 4;
    opacity: 1;
    transition: opacity 220ms ease;
  }

  /* Hide ring + oval; keep L-corners as the alignment guide. */
  .scan-ring,
  .scan-face-oval {
    display: none !important;
  }

  /* Hide brackets only on camera/denied overlays — keep during align + record. */
  .video-wrap:has(#scan-overlay-camera:not(.hidden)) .scan-face-guide,
  .video-wrap:has(#scan-overlay-denied:not(.hidden)) .scan-face-guide {
    opacity: 0;
  }

  /* ---------------------------------------------------------------------
     Alignment guide state machine.

     `data-state` on .scan-face-guide drives colour + motion:
       idle     — informational blue, waiting for the next face sample
       scanning — success green; face well framed; soft breathing glow
       drift    — warning amber; misframed; `data-dir` names the direction the USER SHOULD
                  MOVE (see getFaceFramingGuidance), so we light that edge and
                  push chevrons inward toward centre
       error    — error red; face present but a non-directional hard check failed
       lost     — error red; no face; shake and dimmed backdrop
     Semantic palette: blue info / green success / amber warning / red error.
     --------------------------------------------------------------------- */
  .scan-face-guide {
    --guide-rgb: 59, 130, 246;
    --guide-thickness: 3px;
    --guide-alpha: 0.92;
  }

  .scan-face-guide[data-state="scanning"] {
    --guide-rgb: 78, 224, 107;
    --guide-alpha: 1;
  }

  .scan-face-guide[data-state="drift"] {
    --guide-rgb: 255, 179, 71;
    --guide-alpha: 1;
  }

  .scan-face-guide[data-state="error"],
  .scan-face-guide[data-state="lost"] {
    --guide-rgb: 255, 92, 92;
    --guide-alpha: 1;
  }

  .scan-face-guide[data-state="lost"] {
    animation: scan-guide-shake 620ms cubic-bezier(0.36, 0.07, 0.19, 0.97) infinite;
  }

  .scan-corner {
    position: absolute;
    width: clamp(1.55rem, 7vw, 2.1rem);
    height: clamp(1.55rem, 7vw, 2.1rem);
    border: 0 solid transparent;
    border-color: rgba(var(--guide-rgb), var(--guide-alpha));
    opacity: 0.92;
    filter: drop-shadow(0 0 6px rgba(var(--guide-rgb), 0.55));
    transition:
      opacity 260ms ease,
      border-color 260ms ease,
      filter 260ms ease;
  }

  /* Sleek L-brackets: soft outer radius reads more premium than a hard 90°. */
  .scan-corner--tl {
    top: 0;
    left: 0;
    border-top-width: var(--guide-thickness);
    border-left-width: var(--guide-thickness);
    border-style: solid;
    border-top-left-radius: 10px;
  }

  .scan-corner--tr {
    top: 0;
    right: 0;
    border-top-width: var(--guide-thickness);
    border-right-width: var(--guide-thickness);
    border-style: solid;
    border-top-right-radius: 10px;
  }

  .scan-corner--bl {
    bottom: 0;
    left: 0;
    border-bottom-width: var(--guide-thickness);
    border-left-width: var(--guide-thickness);
    border-style: solid;
    border-bottom-left-radius: 10px;
  }

  .scan-corner--br {
    bottom: 0;
    right: 0;
    border-bottom-width: var(--guide-thickness);
    border-right-width: var(--guide-thickness);
    border-style: solid;
    border-bottom-right-radius: 10px;
  }

  /* Locked on: slow breathing pulse confirms active tracking. */
  .scan-face-guide[data-state="scanning"] .scan-corner {
    animation: scan-corner-breathe 2.4s ease-in-out infinite;
  }

  /* Drifting: corners recede so the lit edge owns attention. */
  .scan-face-guide[data-state="drift"] .scan-corner {
    opacity: 0.55;
  }

  @keyframes scan-corner-breathe {
    0%,
    100% {
      opacity: 0.72;
      filter: drop-shadow(0 0 4px rgba(var(--guide-rgb), 0.35));
    }
    50% {
      opacity: 1;
      filter: drop-shadow(0 0 12px rgba(var(--guide-rgb), 0.7));
    }
  }

  @keyframes scan-guide-shake {
    0%,
    100% {
      transform: translateX(0);
    }
    20% {
      transform: translateX(-4px);
    }
    40% {
      transform: translateX(4px);
    }
    60% {
      transform: translateX(-2px);
    }
    80% {
      transform: translateX(2px);
    }
  }

  /* --- Directional edges -------------------------------------------------
     One glowing bar per side, hidden until that direction is the correction
     the user needs. ::after is a chevron that sweeps inward (toward centre),
     so the motion itself says "come this way". */
  .scan-edge {
    position: absolute;
    opacity: 0;
    border-radius: 999px;
    background: rgba(var(--guide-rgb), 0.95);
    box-shadow: 0 0 14px 2px rgba(var(--guide-rgb), 0.55);
    transition: opacity 240ms ease;
  }

  .scan-edge--left,
  .scan-edge--right {
    top: 22%;
    bottom: 22%;
    width: var(--guide-thickness);
  }

  .scan-edge--left {
    left: -1px;
  }
  .scan-edge--right {
    right: -1px;
  }

  .scan-edge--top,
  .scan-edge--bottom {
    left: 22%;
    right: 22%;
    height: var(--guide-thickness);
  }

  .scan-edge--top {
    top: -1px;
  }
  .scan-edge--bottom {
    bottom: -1px;
  }

  .scan-edge::after {
    content: "";
    position: absolute;
    width: 13px;
    height: 13px;
    border-style: solid;
    border-color: rgba(var(--guide-rgb), 0.95);
    border-width: 0;
    filter: drop-shadow(0 0 6px rgba(var(--guide-rgb), 0.8));
  }

  /* Chevron points toward frame centre on each side. */
  .scan-edge--left::after {
    top: 50%;
    left: 9px;
    border-top-width: 3px;
    border-right-width: 3px;
    transform: translateY(-50%) rotate(45deg);
  }

  .scan-edge--right::after {
    top: 50%;
    right: 9px;
    border-bottom-width: 3px;
    border-left-width: 3px;
    transform: translateY(-50%) rotate(45deg);
  }

  .scan-edge--top::after {
    left: 50%;
    top: 9px;
    border-bottom-width: 3px;
    border-right-width: 3px;
    transform: translateX(-50%) rotate(45deg);
  }

  .scan-edge--bottom::after {
    left: 50%;
    bottom: 9px;
    border-top-width: 3px;
    border-left-width: 3px;
    transform: translateX(-50%) rotate(45deg);
  }

  /* Reveal only the edge matching the needed correction, and thicken it. */
  .scan-face-guide[data-state="drift"][data-dir="left"] .scan-edge--left,
  .scan-face-guide[data-state="drift"][data-dir="right"] .scan-edge--right,
  .scan-face-guide[data-state="drift"][data-dir="up"] .scan-edge--top,
  .scan-face-guide[data-state="drift"][data-dir="down"] .scan-edge--bottom {
    opacity: 1;
  }

  .scan-face-guide[data-state="drift"][data-dir="left"] .scan-edge--left::after,
  .scan-face-guide[data-state="drift"][data-dir="right"] .scan-edge--right::after {
    animation: scan-chevron-x 1.15s ease-in-out infinite;
  }

  .scan-face-guide[data-state="drift"][data-dir="up"] .scan-edge--top::after,
  .scan-face-guide[data-state="drift"][data-dir="down"] .scan-edge--bottom::after {
    animation: scan-chevron-y 1.15s ease-in-out infinite;
  }

  /* Distance cues light all four edges; the whole frame breathes in/out. */
  .scan-face-guide[data-state="drift"][data-dir="near"] .scan-edge,
  .scan-face-guide[data-state="drift"][data-dir="far"] .scan-edge {
    opacity: 0.9;
  }

  /* "Move closer" must not reuse the inward chevrons — inward reads as
     "shrink", the opposite of the instruction. Flip them to point outward so
     the cue is "expand to fill the frame"; "move back" keeps them inward. */
  .scan-face-guide[data-state="drift"][data-dir="near"] .scan-edge--left::after {
    transform: translateY(-50%) rotate(225deg);
  }

  .scan-face-guide[data-state="drift"][data-dir="near"] .scan-edge--right::after {
    transform: translateY(-50%) rotate(225deg);
  }

  .scan-face-guide[data-state="drift"][data-dir="near"] .scan-edge--top::after {
    transform: translateX(-50%) rotate(225deg);
  }

  .scan-face-guide[data-state="drift"][data-dir="near"] .scan-edge--bottom::after {
    transform: translateX(-50%) rotate(225deg);
  }

  .scan-face-guide[data-state="drift"][data-dir="near"] .scan-edge::after,
  .scan-face-guide[data-state="drift"][data-dir="far"] .scan-edge::after {
    animation: scan-chevron-fade 1.5s ease-in-out infinite;
  }

  @keyframes scan-chevron-fade {
    0%,
    100% {
      opacity: 0.3;
    }
    50% {
      opacity: 1;
    }
  }

  .scan-face-guide[data-state="drift"][data-dir="near"] {
    animation: scan-guide-closer 1.5s ease-in-out infinite;
  }

  .scan-face-guide[data-state="drift"][data-dir="far"] {
    animation: scan-guide-back 1.5s ease-in-out infinite;
  }

  @keyframes scan-chevron-x {
    0%,
    100% {
      opacity: 0.25;
      margin-left: 0;
      margin-right: 0;
    }
    50% {
      opacity: 1;
      margin-left: 9px;
      margin-right: 9px;
    }
  }

  @keyframes scan-chevron-y {
    0%,
    100% {
      opacity: 0.25;
      margin-top: 0;
      margin-bottom: 0;
    }
    50% {
      opacity: 1;
      margin-top: 9px;
      margin-bottom: 9px;
    }
  }

  /* "Move closer" → frame expands, inviting the face to fill it. */
  @keyframes scan-guide-closer {
    0%,
    100% {
      transform: scale(1);
    }
    50% {
      transform: scale(1.045);
    }
  }

  /* "Move back" → frame contracts. */
  @keyframes scan-guide-back {
    0%,
    100% {
      transform: scale(1);
    }
    50% {
      transform: scale(0.955);
    }
  }

  /* Lost tracking: dim the camera so the error owns the screen. */
  .video-wrap:has(.scan-face-guide[data-state="lost"])::after {
    content: "";
    position: absolute;
    inset: 0;
    background: rgba(6, 10, 14, 0.42);
    pointer-events: none;
    z-index: 6;
    animation: scan-dim-in 260ms ease both;
  }

  @keyframes scan-dim-in {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .scan-face-guide,
    .scan-face-guide[data-state="lost"],
    .scan-face-guide[data-state="drift"][data-dir="near"],
    .scan-face-guide[data-state="drift"][data-dir="far"],
    .scan-face-guide .scan-corner,
    .scan-edge::after {
      animation: none !important;
    }
  }

  .video-wrap:has(#scan-overlay-camera:not(.hidden)) .scan-hud,
  .video-wrap:has(#scan-overlay-denied:not(.hidden)) .scan-hud,
  .video-wrap:has(#scan-overlay-camera:not(.hidden)) .placement-status,
  .video-wrap:has(#scan-overlay-denied:not(.hidden)) .placement-status {
    opacity: 0;
  }

  .placement-status {
    position: absolute;
    left: 50%;
    bottom: 14px;
    transform: translateX(-50%);
    margin: 0;
    padding: 0.58rem 1rem;
    border-radius: 999px;
    background:
      linear-gradient(
        135deg,
        rgba(18, 20, 32, 0.82) 0%,
        rgba(10, 12, 22, 0.88) 100%
      );
    border: 1px solid rgba(255, 255, 255, 0.14);
    font-size: 0.86rem;
    font-weight: 600;
    letter-spacing: -0.01em;
    text-align: center;
    box-sizing: border-box;
    max-width: calc(100% - 28px);
    width: fit-content;
    min-width: 0;
    z-index: 18;
    opacity: 1;
    pointer-events: none;
    white-space: normal;
    overflow-wrap: break-word;
    line-height: 1.28;
    backdrop-filter: blur(16px) saturate(140%);
    -webkit-backdrop-filter: blur(16px) saturate(140%);
    box-shadow:
      0 12px 28px rgba(4, 6, 14, 0.48),
      0 0 0 1px rgba(255, 255, 255, 0.05) inset;
    transition:
      color 180ms ease,
      border-color 180ms ease,
      opacity 180ms ease,
      box-shadow 220ms ease,
      transform 220ms ease;
  }

  /* Wide pill + slightly smaller type so status stays ~2 lines inside the frame. */
  @media (max-width: 540px) {
    .fps-skip-banner {
      bottom: calc(10px + 4.1rem);
      width: calc(100% - 24px);
      max-width: calc(100% - 24px);
    }

    .placement-status {
      width: calc(100% - 24px);
      max-width: calc(100% - 24px);
      font-size: 0.8rem;
      font-weight: 500;
      padding: 0.42rem 0.75rem;
      line-height: 1.22;
    }
  }
  
  .placement-status.state-wait {
    color: rgba(255, 255, 255, 0.94);
    border-color: rgba(255, 255, 255, 0.18);
  }
  
  .placement-status.state-good {
    color: #d8ffee;
    border-color: rgba(118, 240, 191, 0.48);
    box-shadow:
      0 12px 28px rgba(4, 6, 14, 0.48),
      0 0 24px rgba(118, 240, 191, 0.14),
      0 0 0 1px rgba(118, 240, 191, 0.12) inset;
  }
  
  .placement-status.state-bad {
    color: #ffe8b8;
    border-color: rgba(251, 191, 36, 0.52);
    background:
      linear-gradient(
        135deg,
        rgba(32, 18, 4, 0.9) 0%,
        rgba(18, 10, 4, 0.92) 100%
      );
    box-shadow:
      0 12px 28px rgba(4, 6, 14, 0.48),
      0 0 20px rgba(251, 191, 36, 0.12);
  }

  .video-wrap.is-recovery::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: 14;
    pointer-events: none;
    background:
      radial-gradient(
        ellipse 90% 70% at 50% 38%,
        rgba(76, 227, 255, 0.14),
        transparent 70%
      ),
      linear-gradient(
        180deg,
        rgba(8, 10, 18, 0.2) 0%,
        rgba(8, 10, 18, 0.46) 55%,
        rgba(8, 10, 18, 0.6) 100%
      );
  }

  .video-wrap.is-recovery .placement-status {
    display: none;
  }

  .scan-recovery {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    z-index: 22;
    width: min(calc(100% - 2rem), 17.5rem);
    margin: 0;
    padding: 1.1rem 1.15rem 1rem;
    border-radius: 16px;
    text-align: center;
    border: 1px solid rgba(255, 255, 255, 0.16);
    background:
      radial-gradient(
        ellipse 110% 85% at 50% -10%,
        rgba(76, 227, 255, 0.1),
        transparent 58%
      ),
      linear-gradient(
        165deg,
        rgba(24, 28, 40, 0.94) 0%,
        rgba(12, 14, 24, 0.96) 100%
      );
    backdrop-filter: blur(24px) saturate(140%);
    -webkit-backdrop-filter: blur(24px) saturate(140%);
    box-shadow:
      0 24px 56px rgba(0, 0, 0, 0.45),
      0 0 0 1px rgba(255, 255, 255, 0.07) inset,
      0 0 36px rgba(76, 227, 255, 0.1);
    animation: scan-recovery-in 280ms ease-out both;
  }

  @keyframes scan-recovery-in {
    from {
      opacity: 0;
      transform: translate(-50%, -46%);
    }
    to {
      opacity: 1;
      transform: translate(-50%, -50%);
    }
  }

  .scan-recovery-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.75rem;
    height: 2.75rem;
    margin: 0 auto 0.65rem;
    border-radius: 50%;
    color: var(--cyan);
    background: rgba(76, 227, 255, 0.1);
    border: 1px solid rgba(76, 227, 255, 0.32);
    box-shadow: 0 0 20px rgba(76, 227, 255, 0.14);
  }

  .scan-recovery-eyebrow {
    margin: 0 0 0.2rem;
    font-size: 0.68rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--cyan);
  }

  .scan-recovery-title {
    margin: 0 0 0.75rem;
    font-size: 0.88rem;
    font-weight: 500;
    line-height: 1.4;
    letter-spacing: -0.01em;
    color: rgba(255, 255, 255, 0.72);
  }

  .scan-recovery-message {
    margin: 0.1rem 0 0.5rem;
    padding: 0.62rem 0.78rem;
    font-size: 0.98rem;
    font-weight: 600;
    line-height: 1.38;
    letter-spacing: -0.01em;
    color: #fff;
    border-radius: 10px;
    background:
      linear-gradient(
        135deg,
        rgba(76, 227, 255, 0.16),
        rgba(223, 122, 254, 0.08)
      );
    border: 1px solid rgba(76, 227, 255, 0.34);
    box-shadow:
      0 0 0 1px rgba(255, 255, 255, 0.07) inset,
      0 8px 22px rgba(76, 227, 255, 0.12);
  }

  .scan-recovery-message:empty {
    display: none;
    margin: 0;
    padding: 0;
    border: 0;
    box-shadow: none;
  }

  .scan-recovery-checklist {
    margin: 0;
    padding: 0.72rem 0.82rem;
    list-style: none;
    text-align: left;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid rgba(255, 255, 255, 0.14);
    display: grid;
    gap: 0.42rem;
  }

  .scan-recovery-checklist li {
    position: relative;
    padding-left: 1.15rem;
    font-size: 0.8rem;
    font-weight: 500;
    line-height: 1.38;
    color: rgba(255, 255, 255, 0.9);
  }

  .scan-recovery-checklist li::before {
    content: "";
    position: absolute;
    left: 0;
    top: 0.42em;
    width: 0.42rem;
    height: 0.42rem;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--cyan), var(--accent));
    box-shadow: 0 0 8px rgba(76, 227, 255, 0.35);
  }
  
  .countdown-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.85rem;
    background:
      radial-gradient(
        ellipse 70% 58% at 50% 42%,
        rgba(223, 122, 254, 0.16),
        transparent 68%
      ),
      rgba(0, 0, 0, 0.62);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    z-index: 20;
    animation: countdown-overlay-in 260ms ease-out both;
  }

  @keyframes countdown-overlay-in {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }

  .countdown-visual {
    position: relative;
    width: clamp(6.5rem, 34vw, 8.5rem);
    height: clamp(6.5rem, 34vw, 8.5rem);
    display: grid;
    place-items: center;
  }

  .countdown-ring {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    animation: countdown-ring-spin 2.4s linear infinite;
  }

  .countdown-ring-track {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.1);
    box-shadow:
      inset 0 0 24px rgba(223, 122, 254, 0.08),
      0 0 28px rgba(223, 122, 254, 0.12);
  }

  .countdown-ring-glow {
    position: absolute;
    inset: -4%;
    border-radius: 50%;
    background: conic-gradient(
      from 0deg,
      rgba(223, 122, 254, 0.95),
      rgba(76, 227, 255, 0.9),
      rgba(223, 122, 254, 0.95)
    );
    -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 4px), #000 calc(100% - 3px));
    mask: radial-gradient(farthest-side, transparent calc(100% - 4px), #000 calc(100% - 3px));
    opacity: 0.88;
    filter: drop-shadow(0 0 14px rgba(223, 122, 254, 0.35));
  }

  @keyframes countdown-ring-spin {
    to {
      transform: rotate(360deg);
    }
  }

  .countdown-number {
    position: relative;
    z-index: 1;
    font-size: clamp(3.2rem, 14vw, 4.4rem);
    font-weight: 800;
    line-height: 1;
    color: #fff;
    letter-spacing: -0.04em;
    text-shadow:
      0 0 24px rgba(223, 122, 254, 0.45),
      0 0 48px rgba(76, 227, 255, 0.18);
    font-variant-numeric: tabular-nums;
  }

  .countdown-number.is-pop {
    animation: countdown-pop 420ms cubic-bezier(0.22, 1, 0.36, 1) both;
  }

  @keyframes countdown-pop {
    0% {
      opacity: 0.35;
      transform: scale(0.72);
    }
    55% {
      opacity: 1;
      transform: scale(1.08);
    }
    100% {
      opacity: 1;
      transform: scale(1);
    }
  }

  .countdown-caption {
    margin: 0;
    font-size: 0.82rem;
    font-weight: 600;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: rgba(255, 255, 255, 0.72);
  }
  
  .scan .actions,
  .scan-actions {
    margin-top: 1rem;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 0.55rem;
  }

  .scan-actions:not(:has(.btn:not(.hidden))) {
    display: none;
  }

  .scan-actions--recovery {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
    margin-top: 1.1rem;
    margin-left: auto;
    margin-right: auto;
    padding-top: 0.15rem;
    /* Match the largest responsive camera frame width. */
    width: min(100%, 460px);
  }

  .scan-actions--recovery .btn {
    width: 100%;
    min-height: 2.75rem;
  }

  .scan-actions--recovery .btn-restart {
    order: -1;
    padding: 0.72rem 1.15rem;
    font-size: 1rem;
    letter-spacing: 0.01em;
  }
  
  .success {
    margin: 0 0 1rem;
    font-weight: 500;
  }
  
  .result-actions {
    margin-top: 0.85rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.65rem;
  }

  .result-actions .btn:not(.hidden) {
    flex: 1 1 auto;
    min-width: 10rem;
  }

  .upload-preview-card {
    margin-top: 0;
    padding: 1.35rem 1.15rem 1.25rem;
    border-radius: 20px;
    border: 1px solid rgba(255, 255, 255, 0.14);
    background:
      radial-gradient(
        ellipse 120% 90% at 50% 0%,
        rgba(76, 227, 255, 0.08),
        transparent 62%
      ),
      rgba(255, 255, 255, 0.04);
    display: grid;
    gap: 0.85rem;
    box-shadow: 0 14px 38px rgba(0, 0, 0, 0.24);
    position: relative;
    overflow: hidden;
  }

  /* Lightweight "alive" state while waiting on server work. */
  .upload-preview-card.is-waiting::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    background: linear-gradient(
      100deg,
      rgba(255, 255, 255, 0) 22%,
      rgba(255, 255, 255, 0.085) 50%,
      rgba(255, 255, 255, 0) 78%
    );
    transform: translateX(-120%);
    opacity: 0.62;
    animation: upload-wait-shimmer 1.9s ease-in-out infinite;
    will-change: transform, opacity;
  }

  .upload-processor {
    display: grid;
    place-items: center;
    min-height: 11.5rem;
    padding: 0.35rem 0 0.15rem;
  }

  .upload-processor__stage {
    position: relative;
    width: 8.75rem;
    height: 8.75rem;
  }

  .upload-processor__ring {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    border: 2px solid transparent;
    opacity: 0.95;
  }

  .upload-processor__ring--outer {
    border-top-color: var(--cyan);
    border-right-color: rgba(76, 227, 255, 0.22);
    filter: drop-shadow(0 0 10px rgba(76, 227, 255, 0.35));
    animation: upload-ring-spin 2.6s linear infinite;
  }

  .upload-processor__ring--mid {
    inset: 13%;
    border-bottom-color: var(--accent);
    border-left-color: rgba(223, 122, 254, 0.24);
    filter: drop-shadow(0 0 8px rgba(223, 122, 254, 0.28));
    animation: upload-ring-spin 1.9s linear infinite reverse;
  }

  .upload-processor__ring--inner {
    inset: 26%;
    border-top-color: rgba(255, 255, 255, 0.55);
    border-right-color: rgba(255, 255, 255, 0.08);
    animation: upload-ring-spin 1.35s linear infinite;
  }

  .upload-processor__core {
    position: absolute;
    inset: 34%;
    border-radius: 50%;
    background:
      radial-gradient(
        circle at 32% 28%,
        rgba(255, 255, 255, 0.22),
        rgba(76, 227, 255, 0.14) 42%,
        rgba(129, 74, 200, 0.18) 100%
      );
    box-shadow:
      inset 0 0 18px rgba(76, 227, 255, 0.18),
      0 0 28px rgba(76, 227, 255, 0.16);
    display: grid;
    place-items: center;
  }

  .upload-processor__pulse {
    width: 58%;
    height: 58%;
    border-radius: 50%;
    background: linear-gradient(145deg, var(--cyan), var(--accent));
    box-shadow:
      0 0 22px rgba(76, 227, 255, 0.5),
      0 0 36px rgba(223, 122, 254, 0.28);
    animation: upload-core-pulse 1.55s ease-in-out infinite;
  }

  .upload-processor__orbit {
    position: absolute;
    inset: 0;
    animation: upload-orbit-spin 4.2s linear infinite;
  }

  .upload-processor__orbit span {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0.42rem;
    height: 0.42rem;
    margin: -0.21rem;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 0 12px rgba(76, 227, 255, 0.85);
  }

  .upload-processor__orbit span:nth-child(1) {
    transform: rotate(0deg) translateY(-4.15rem);
  }

  .upload-processor__orbit span:nth-child(2) {
    transform: rotate(120deg) translateY(-4.15rem);
    opacity: 0.82;
    box-shadow: 0 0 10px rgba(223, 122, 254, 0.75);
  }

  .upload-processor__orbit span:nth-child(3) {
    transform: rotate(240deg) translateY(-4.15rem);
    opacity: 0.68;
  }

  .upload-processor.is-success .upload-processor__ring,
  .upload-processor.is-success .upload-processor__orbit {
    animation-play-state: paused;
    opacity: 0.35;
  }

  .upload-processor.is-success .upload-processor__pulse {
    background: linear-gradient(145deg, var(--ok), #3dd68c);
    box-shadow: 0 0 28px rgba(118, 240, 191, 0.55);
    animation: upload-success-pop 520ms cubic-bezier(0.22, 1, 0.36, 1) both;
  }

  .upload-processor.is-error .upload-processor__ring,
  .upload-processor.is-error .upload-processor__orbit {
    animation-play-state: paused;
    opacity: 0.28;
  }

  .upload-processor.is-error .upload-processor__pulse {
    background: linear-gradient(145deg, #ff9ab5, #e86a8a);
    box-shadow: 0 0 20px rgba(255, 154, 181, 0.4);
    animation: none;
    transform: scale(0.92);
  }

  @keyframes upload-ring-spin {
    to {
      transform: rotate(360deg);
    }
  }

  @keyframes upload-core-pulse {
    0%,
    100% {
      transform: scale(0.88);
      opacity: 0.88;
    }
    50% {
      transform: scale(1);
      opacity: 1;
    }
  }

  @keyframes upload-orbit-spin {
    to {
      transform: rotate(360deg);
    }
  }

  @keyframes upload-success-pop {
    0% {
      transform: scale(0.7);
      opacity: 0.5;
    }
    60% {
      transform: scale(1.08);
      opacity: 1;
    }
    100% {
      transform: scale(1);
      opacity: 1;
    }
  }

  @keyframes upload-wait-shimmer {
    0% {
      transform: translateX(-120%);
      opacity: 0;
    }
    18% {
      opacity: 0.64;
    }
    55% {
      opacity: 0.64;
    }
    100% {
      transform: translateX(120%);
      opacity: 0;
    }
  }

  @keyframes upload-status-breathe {
    0%,
    100% {
      opacity: 0.82;
      transform: translateY(0) scale(0.992);
    }
    50% {
      opacity: 1;
      transform: translateY(-1px) scale(1);
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .upload-processor__ring,
    .upload-processor__orbit,
    .upload-processor__pulse,
    .upload-preview-card.is-waiting::after,
    .upload-status-card.is-uploading .upload-status-label {
      animation: none !important;
    }

    .upload-processor__pulse {
      transform: scale(1);
      opacity: 1;
    }
  }

  .recorded-preview {
    display: none !important;
  }

  .upload-status-card {
    display: grid;
    gap: 0.45rem;
  }

  .upload-status-head {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 1.2rem;
    text-align: center;
  }

  .upload-status-label {
    font-size: clamp(1.05rem, 1.6vw + 0.55rem, 1.35rem);
    font-weight: 700;
    letter-spacing: -0.02em;
    line-height: 1.25;
    color: var(--text);
  }

  .upload-status-card.is-uploading .upload-status-label {
    color: #ded2ff;
    animation: upload-status-breathe 1.3s ease-in-out infinite;
    transform-origin: 50% 50%;
  }

  .upload-status-card.is-success .upload-status-label {
    color: var(--ok);
  }

  .upload-status-card.is-error .upload-status-label {
    color: var(--danger);
  }

  @media (prefers-reduced-motion: reduce) {
    .scan-overlay,
    .countdown-overlay,
    .countdown-ring,
    .countdown-number.is-pop,
    .scan-corner,
    .scan-recovery {
      animation: none !important;
    }

    .countdown-number.is-pop {
      transform: none;
      opacity: 1;
    }

    .scan-progress-bar-fill {
      transition: none;
    }
  }
  
  .error {
    margin-top: 1rem;
    padding: 0.75rem 1rem;
    border-radius: 10px;
    background: rgba(255, 126, 234, 0.11);
    border: 1px solid rgba(255, 154, 181, 0.42);
    color: #ffc2de;
    font-size: 0.9rem;
  }

  .fps-skip-banner {
    position: absolute;
    left: 50%;
    bottom: calc(10px + 3.6rem);
    transform: translateX(-50%);
    z-index: 19;
    margin: 0;
    padding: 0;
    border: none;
    background: transparent;
    display: flex;
    justify-content: center;
    pointer-events: none;
    max-width: calc(100% - 28px);
    width: calc(100% - 28px);
  }

  .btn-fps-skip {
    pointer-events: auto;
    font-size: 0.82rem;
    padding: 0.45rem 1.05rem;
    border-radius: 999px;
    background: rgba(26, 14, 4, 0.88);
    border: 1px solid rgba(255, 195, 80, 0.55);
    color: rgba(255, 220, 120, 0.98);
    cursor: pointer;
    white-space: nowrap;
    box-shadow: 0 10px 22px rgba(4, 6, 14, 0.45);
  }

  .btn-fps-skip:hover {
    background: rgba(255, 195, 80, 0.22);
  }
