  :root{
    --bg: #1a1b1e;
    --panel: #212327;
    --bezel-lo: #26282d;
    --bezel-hi: #34373e;
    --ring: #3a3d44;
    --ring-soft: #2c2f35;
    --brass: #b08d4f;
    --brass-dim: #6c5a37;
    --vial: #d8c752;
    --vial-dim: #6e6532;
    --text: #e9ebee;
    --text-dim: #8b929c;
    --text-faint: #5a6069;
    --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, Consolas, monospace;
    --sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  }

  * { box-sizing: border-box; }

  html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: var(--sans);
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
  }

  body {
    background-image:
      radial-gradient(120% 90% at 50% -10%, #22242a 0%, var(--bg) 55%);
  }

  #app {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  }

  /* ---------- gate screens (permission / unsupported / error) ---------- */

  .gate {
    max-width: 340px;
    width: calc(100% - 48px);
    padding: 28px 24px 30px;
    background: var(--panel);
    border: 1px solid var(--ring-soft);
    border-radius: 18px;
    text-align: center;
  }
  .gate .mark {
    width: 46px; height: 46px;
    margin: 0 auto 18px;
    border-radius: 50%;
    border: 2px solid var(--brass);
    position: relative;
  }
  .gate .mark::after{
    content: "";
    position: absolute;
    left: 50%; top: 50%;
    width: 8px; height: 8px;
    margin: -4px 0 0 -4px;
    border-radius: 50%;
    background: var(--vial);
  }
  .gate h1 {
    font-size: 17px;
    font-weight: 600;
    margin: 0 0 10px;
    letter-spacing: 0.01em;
  }
  .gate p {
    font-size: 14px;
    line-height: 1.5;
    color: var(--text-dim);
    margin: 0 0 20px;
  }
  .gate button {
    appearance: none;
    border: none;
    background: var(--brass);
    color: #201a0c;
    font: inherit;
    font-weight: 600;
    font-size: 14.5px;
    padding: 12px 22px;
    border-radius: 10px;
    cursor: pointer;
    width: 100%;
  }
  .gate button:active { background: #9a7a41; }
  .gate .hint {
    margin-top: 14px;
    font-size: 12.5px;
    color: var(--text-faint);
  }
  .gate .gate-links {
    margin-top: 16px;
    font-size: 12.5px;
  }
  .gate .gate-links a,
  .settings-link {
    color: var(--text-dim);
    text-decoration: underline;
    text-underline-offset: 3px;
  }
  .gate .gate-links a:hover,
  .settings-link:hover { color: var(--text); }
  .hidden { display: none !important; }

  /* ---------- main instrument ---------- */

  #instrument {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    height: 100%;
    justify-content: center;
    gap: 22px;
  }

  #modeLabel {
    font-size: 13px;
    color: var(--text-dim);
    letter-spacing: 0.02em;
    min-height: 16px;
    transition: opacity 0.2s;
  }
  #modeLabel .dim { color: var(--text-faint); }

  #stage {
    position: relative;
    width: min(78vw, 60vh, 340px);
    height: min(78vw, 60vh, 340px);
    will-change: transform;
  }

  .face {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transform: scale(0.97);
    transition: opacity 0.35s ease, transform 0.35s ease;
    pointer-events: none;
  }
  .face.active { opacity: 1; transform: scale(1); pointer-events: auto; }

  /* --- bullseye (flat mode) --- */

  #bullseye svg { width: 100%; height: 100%; display: block; }

  .bezel-ring { fill: none; stroke: var(--ring); stroke-width: 1; }
  .bezel-tick { stroke: var(--brass-dim); stroke-width: 1.4; }
  .crosshair { stroke: var(--ring-soft); stroke-width: 1; }
  .target-ring { fill: none; stroke: var(--ring-soft); stroke-width: 1; }
  .target-ring.home { stroke: var(--brass-dim); stroke-width: 1.3; }

  #bullseyeBubble {
    fill: var(--vial);
    filter: drop-shadow(0 0 0 rgba(0,0,0,0));
    transition: filter 0.25s;
  }
  #bullseyeBubble.home { filter: drop-shadow(0 0 7px var(--vial)); }

  /* --- tube (side mode) --- */

  #tube {
    flex-direction: column;
    gap: 26px;
  }
  .vial-track {
    position: relative;
    width: 88%;
    height: 54px;
    border-radius: 27px;
    background: linear-gradient(180deg, var(--bezel-hi), var(--bezel-lo));
    border: 1px solid var(--ring);
    box-shadow: inset 0 2px 6px rgba(0,0,0,0.45);
  }
  .vial-marks {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .vial-marks .home-line {
    position: absolute;
    top: 8px; bottom: 8px;
    width: 2px;
    background: var(--brass-dim);
    border-radius: 1px;
  }
  .vial-marks .home-line.l { left: calc(50% - 17px); }
  .vial-marks .home-line.r { left: calc(50% + 15px); }
  .vial-marks .center-line {
    position: absolute;
    top: 5px; bottom: 5px;
    left: calc(50% - 0.5px);
    width: 1px;
    background: var(--ring);
  }
  #tubeBubble {
    position: absolute;
    top: 7px; bottom: 7px;
    left: 50%;
    width: 40px;
    margin-left: -20px;
    border-radius: 20px;
    background: var(--vial-dim);
    transition: background 0.25s, box-shadow 0.25s;
  }
  #tubeBubble.home {
    background: var(--vial);
    box-shadow: 0 0 12px var(--vial);
  }

  .fb-gauge {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-faint);
    font-size: 11.5px;
  }
  .fb-track {
    position: relative;
    width: 120px;
    height: 6px;
    border-radius: 3px;
    background: var(--ring-soft);
    overflow: hidden;
  }
  .fb-dot {
    position: absolute;
    top: 0; bottom: 0;
    left: 50%;
    width: 10px;
    margin-left: -5px;
    border-radius: 3px;
    background: var(--brass-dim);
    transition: background 0.2s;
  }
  .fb-dot.home { background: var(--brass); }

  /* --- readout --- */

  #readout {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
  }
  #bigDeg {
    font-family: var(--mono);
    font-variant-numeric: tabular-nums;
    font-size: 44px;
    font-weight: 500;
    letter-spacing: 0.01em;
    color: var(--text);
    transition: color 0.25s, opacity 0.2s;
  }
  #bigDeg.home { color: var(--vial); }
  #subInfo {
    font-size: 12.5px;
    color: var(--text-faint);
    letter-spacing: 0.03em;
  }

  #cornerControls {
    position: absolute;
    top: max(12px, env(safe-area-inset-top));
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    display: flex;
    justify-content: space-between;
    z-index: 4;
  }
  .icon-btn {
    appearance: none;
    border: 1px solid var(--ring-soft);
    background: var(--panel);
    color: var(--text-dim);
    width: 38px; height: 38px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
  }
  .icon-btn svg { width: 19px; height: 19px; }
  .icon-btn:active { color: var(--text); border-color: var(--brass-dim); }

  .resume-banner {
    position: absolute;
    top: max(58px, calc(env(safe-area-inset-top) + 50px));
    left: 50%;
    transform: translateX(-50%);
    z-index: 5;
    appearance: none;
    border: 1.5px solid var(--vial);
    background: var(--vial);
    color: #201a0c;
    font: inherit;
    font-weight: 700;
    font-size: 13.5px;
    letter-spacing: 0.01em;
    padding: 10px 18px;
    border-radius: 999px;
    cursor: pointer;
    box-shadow: 0 0 0 rgba(216,199,82,0.5);
    animation: resumePulse 1.4s ease-in-out infinite;
  }
  .resume-banner:active { background: var(--brass); border-color: var(--brass); }
  @keyframes resumePulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(216,199,82,0.55); transform: translateX(-50%) scale(1); }
    50% { box-shadow: 0 0 0 10px rgba(216,199,82,0); transform: translateX(-50%) scale(1.035); }
  }

  .toast {
    position: absolute;
    top: max(58px, calc(env(safe-area-inset-top) + 50px));
    left: 50%;
    transform: translateX(-50%) translateY(-6px);
    z-index: 5;
    background: var(--panel);
    border: 1px solid var(--brass-dim);
    color: var(--text);
    font-size: 13px;
    font-weight: 600;
    padding: 9px 16px;
    border-radius: 999px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s, transform 0.2s;
  }
  .toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

  .held-badge {
    position: absolute;
    top: -6px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--brass);
    color: #201a0c;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.04em;
    padding: 4px 10px;
    border-radius: 999px;
    z-index: 3;
  }

  @media (prefers-reduced-motion: reduce) {
    .resume-banner { animation: none; }
  }

  /* ---------- settings panel ---------- */

  .settings-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    opacity: 0;
    transition: opacity 0.2s;
  }
  .settings-sheet {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    max-height: 82vh;
    overflow-y: auto;
    background: var(--panel);
    border-top: 1px solid var(--ring-soft);
    border-radius: 20px 20px 0 0;
    padding: 18px 20px calc(24px + env(safe-area-inset-bottom));
    transform: translateY(100%);
    transition: transform 0.28s ease;
  }
  #settingsPanel {
    position: fixed;
    inset: 0;
    z-index: 10;
    pointer-events: none; /* base state: inert. .open is the only thing that turns interaction on */
  }
  #settingsPanel.open { pointer-events: auto; }
  #settingsPanel.open .settings-backdrop { opacity: 1; }
  #settingsPanel.open .settings-sheet { transform: translateY(0); }

  .settings-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 14px;
  }
  .settings-header span { font-size: 15.5px; font-weight: 600; }
  .settings-header button {
    appearance: none; border: none; background: none;
    color: var(--text-dim); font-size: 20px; line-height: 1; cursor: pointer;
    padding: 4px 6px;
  }

  .settings-section { margin-bottom: 20px; }
  .settings-label {
    font-size: 12px;
    color: var(--text-faint);
    margin-bottom: 10px;
  }
  .settings-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 0;
    font-size: 14.5px;
    border-bottom: 1px solid var(--ring-soft);
  }
  .settings-row:last-child { border-bottom: none; }
  .settings-hint {
    font-size: 12px;
    color: var(--text-faint);
    line-height: 1.4;
    margin: 8px 0 0;
  }

  .toggle {
    appearance: none;
    border: none;
    width: 44px; height: 26px;
    border-radius: 13px;
    background: var(--ring);
    position: relative;
    cursor: pointer;
    flex: none;
  }
  .toggle::after {
    content: "";
    position: absolute;
    top: 3px; left: 3px;
    width: 20px; height: 20px;
    border-radius: 50%;
    background: var(--text-dim);
    transition: transform 0.18s, background 0.18s;
  }
  .toggle.on { background: var(--brass-dim); }
  .toggle.on::after { transform: translateX(18px); background: var(--vial); }

  .segmented {
    display: flex;
    border: 1px solid var(--ring);
    border-radius: 10px;
    overflow: hidden;
  }
  .segmented button {
    flex: 1;
    appearance: none;
    border: none;
    background: none;
    color: var(--text-dim);
    font: inherit;
    font-size: 13px;
    padding: 9px 4px;
    cursor: pointer;
    border-left: 1px solid var(--ring);
  }
  .segmented button:first-child { border-left: none; }
  .segmented button.selected { background: var(--brass-dim); color: var(--text); }

  .scheme-grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 10px;
  }
  .scheme-swatch {
    appearance: none;
    border: 2px solid transparent;
    border-radius: 50%;
    width: 40px; height: 40px;
    padding: 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .scheme-swatch.selected { border-color: var(--text); }
  .swatch-dot { width: 12px; height: 12px; border-radius: 50%; }

  /* ---------- camera-aim overlay ---------- */

  #aimView {
    position: fixed;
    inset: 0;
    background: #000;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  #aimView.hidden { display: none; }
  #aimVideo {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  .aim-overlay {
    position: absolute;
    inset: 0;
    pointer-events: none;
  }
  .aim-horizon {
    position: absolute;
    left: 50%; top: 50%;
    width: 70vmax;
    height: 2px;
    margin-left: -35vmax;
    background: var(--vial);
    box-shadow: 0 0 8px rgba(0,0,0,0.6);
    transform-origin: center;
  }
  .aim-horizon.home { background: var(--vial); box-shadow: 0 0 12px var(--vial); }
  .aim-center-dot {
    position: absolute;
    left: 50%; top: 50%;
    width: 6px; height: 6px;
    margin: -3px 0 0 -3px;
    border-radius: 50%;
    background: var(--text);
  }
  .aim-readout {
    position: absolute;
    top: max(24px, env(safe-area-inset-top));
    left: 50%;
    transform: translateX(-50%);
    font-family: var(--mono);
    font-variant-numeric: tabular-nums;
    font-size: 30px;
    font-weight: 500;
    color: #fff;
    text-shadow: 0 1px 4px rgba(0,0,0,0.8);
  }
  .aim-close {
    position: absolute;
    bottom: max(28px, calc(env(safe-area-inset-bottom) + 20px));
    left: 50%;
    transform: translateX(-50%);
    appearance: none;
    border: 1px solid rgba(255,255,255,0.4);
    background: rgba(0,0,0,0.4);
    color: #fff;
    font: inherit;
    font-size: 13px;
    padding: 10px 20px;
    border-radius: 999px;
    cursor: pointer;
  }
  .aim-error {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 0 32px;
    color: var(--text-dim);
    font-size: 14px;
  }

  @media (prefers-reduced-motion: reduce) {
    #stage, .face, #tubeBubble, .fb-dot, #bigDeg { transition: none !important; }
  }
