:root {
  --hud-primary: #00e5c7;
  --hud-secondary: #4fc3f7;
  --hud-warn: #ffb74d;
  --hud-alert: #ff5252;
  --hud-dim: rgba(0, 229, 199, 0.35);
  --hud-bg: rgba(0, 12, 18, 0.78);
  --hud-border: rgba(0, 229, 199, 0.45);
  --hud-text: #b2dfdb;
  --hud-glow: 0 0 12px rgba(0, 229, 199, 0.35);
  --d1: #00e5c7;
  --d2: #4fc3f7;
  --d3: #ce93d8;
  --d4: #ff8a65;
  --font-mono: "Cascadia Code", "SF Mono", "Fira Code", "Consolas", monospace;
  --font-ui: "Segoe UI", system-ui, -apple-system, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%;
  min-height: 100%;
  background: #020608;
  color: var(--hud-text);
  font-family: var(--font-ui);
}
html { height: auto; }

body { overflow: hidden; }
body.start-mode, body.error-mode {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
body.hud-mode {
  height: auto;
  min-height: 100%;
  min-height: 100dvh;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}

.hidden { display: none !important; }

.screen {
  width: 100%;
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
  background:
    radial-gradient(ellipse at 25% 15%, rgba(0, 229, 199, 0.07) 0%, transparent 50%),
    radial-gradient(ellipse at 80% 70%, rgba(255, 138, 101, 0.06) 0%, transparent 45%),
    #020608;
}

#hud-screen {
  padding: 0;
  height: auto;
  min-height: 100dvh;
  align-items: stretch;
  display: block;
}

#start-screen, #error-screen { position: relative; z-index: 100; }

.start-card {
  max-width: 520px;
  padding: 2.25rem 1.75rem;
  text-align: center;
  border: 1px solid var(--hud-border);
  background: var(--hud-bg);
  backdrop-filter: blur(12px);
  box-shadow: var(--hud-glow);
}

.start-icon { width: 68px; height: 68px; margin: 0 auto 1rem; color: var(--hud-primary); }
.start-card h1 {
  font-size: 1.5rem; font-weight: 600; color: var(--hud-primary);
  letter-spacing: 0.04em; margin-bottom: 0.35rem;
}
.subtitle {
  font-family: var(--font-mono); font-size: 0.72rem; color: var(--hud-warn);
  text-transform: uppercase; letter-spacing: 0.1em; margin-bottom: 1rem;
}
.description { font-size: 0.88rem; line-height: 1.55; opacity: 0.85; margin-bottom: 1rem; }
.feature-list { list-style: none; text-align: left; margin: 0 auto 1.25rem; max-width: 360px; }
.feature-list li {
  font-family: var(--font-mono); font-size: 0.74rem; padding: 0.3rem 0 0.3rem 1.1rem;
  position: relative; color: var(--hud-secondary);
}
.feature-list li::before { content: "▸"; position: absolute; left: 0; color: var(--hud-primary); }
.disclaimer { font-size: 0.7rem; opacity: 0.55; margin-top: 1rem; line-height: 1.4; }
.video-hint { font-size: 0.8rem; line-height: 1.5; margin-bottom: 0.85rem; opacity: 0.8; }
.video-name-row { display: flex; flex-direction: column; gap: 0.35rem; margin-bottom: 1rem; text-align: left; }
.video-name-row label {
  font-family: var(--font-mono); font-size: 0.62rem; letter-spacing: 0.08em; color: var(--hud-secondary);
}
.video-name-row input {
  width: 100%; font-family: var(--font-mono); font-size: 0.9rem; padding: 0.7rem 0.75rem;
  border: 1px solid var(--hud-border); background: rgba(0,0,0,0.35); color: var(--hud-primary); outline: none;
}
.picked-name {
  font-family: var(--font-mono); font-size: 0.7rem; color: var(--hud-secondary);
  margin: 0.6rem 0 0.2rem; word-break: break-all;
}
.start-status { font-family: var(--font-mono); font-size: 0.7rem; margin-top: 0.6rem; color: var(--hud-warn); }
.btn-row { display: flex; flex-direction: column; gap: 0.6rem; align-items: center; }

.btn-primary, .btn-secondary {
  font-family: var(--font-mono); font-size: 0.95rem; min-height: 48px; min-width: 200px;
  padding: 0.8rem 1.5rem; cursor: pointer; letter-spacing: 0.05em;
  touch-action: manipulation; user-select: none; -webkit-user-select: none;
}
.btn-primary {
  border: 1px solid var(--hud-primary); background: rgba(0, 229, 199, 0.12); color: var(--hud-primary);
}
.btn-primary:active { background: rgba(0, 229, 199, 0.3); }
.btn-primary:disabled { opacity: 0.55; }
.btn-secondary {
  border: 1px solid var(--hud-secondary); background: rgba(79, 195, 247, 0.1); color: var(--hud-secondary);
}
.btn-secondary:active { background: rgba(79, 195, 247, 0.25); }
.error-card h1 { color: var(--hud-alert); }

.video-container {
  position: relative;
  width: 100%;
  min-height: 100dvh;
  overflow: visible;
  background: #000;
}
.video-container.no-video { background: #05080a; }

#background-video {
  position: sticky;
  top: 0;
  left: 0;
  width: 100%;
  height: 100dvh;
  object-fit: cover;
  z-index: 0;
  display: block;
}
#overlay-canvas {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100dvh;
  pointer-events: none;
  z-index: 3;
}

.corner {
  position: fixed; width: 40px; height: 40px; border-color: var(--hud-primary);
  border-style: solid; opacity: 0.45; pointer-events: none; z-index: 5;
}
.corner.tl { top: 10px; left: 10px; border-width: 2px 0 0 2px; }
.corner.tr { top: 10px; right: 10px; border-width: 2px 2px 0 0; }
.corner.bl { bottom: 10px; left: 10px; border-width: 0 0 2px 2px; }
.corner.br { bottom: 10px; right: 10px; border-width: 0 2px 2px 0; }

.scan-line {
  position: fixed; left: 0; right: 0; height: 2px; z-index: 4; pointer-events: none; opacity: 0.15;
  background: linear-gradient(90deg, transparent, var(--hud-primary), transparent);
  animation: scan 4.5s linear infinite;
}
@keyframes scan { 0% { top: 8%; } 100% { top: 90%; } }

.hud-top {
  position: fixed; top: 0; left: 0; right: 0; z-index: 20;
  display: flex; justify-content: space-between; align-items: center;
  padding: 0.55rem 0.85rem;
  padding-top: max(0.55rem, env(safe-area-inset-top));
  background: linear-gradient(180deg, rgba(0,12,18,0.92) 0%, transparent 100%);
  font-family: var(--font-mono); font-size: 0.7rem; pointer-events: none;
}
.hud-brand { display: flex; align-items: center; gap: 0.45rem; color: var(--hud-primary); letter-spacing: 0.08em; }
.brand-dot {
  width: 6px; height: 6px; border-radius: 50%; background: var(--hud-primary);
  animation: pulse 2s ease-in-out infinite;
}
.brand-ver { opacity: 0.45; font-size: 0.62rem; }
.hud-status { display: flex; align-items: center; gap: 0.45rem; }
.live-dot {
  display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--hud-alert);
  animation: pulse 1.2s ease-in-out infinite;
}
.demo-badge {
  color: var(--hud-warn); border: 1px solid var(--hud-warn);
  padding: 0.08rem 0.35rem; font-size: 0.58rem; letter-spacing: 0.06em;
}
.scale-badge {
  color: var(--hud-primary); border: 1px solid var(--hud-dim);
  padding: 0.08rem 0.35rem; font-size: 0.55rem; letter-spacing: 0.04em; opacity: 0.85;
}
.scale-legend {
  font-family: var(--font-mono); font-size: 0.5rem; letter-spacing: 0.06em;
  color: var(--hud-dim); margin-top: 0.3rem;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }


/* ── Scrollable content over sticky video ── */
.hud-scroll {
  position: relative;
  z-index: 10;
  margin-top: -100dvh;
  min-height: calc(100dvh + 200px);
  padding:
    max(56px, calc(env(safe-area-inset-top) + 48px))
    clamp(12px, 3.5vw, 28px)
    max(28px, calc(env(safe-area-inset-bottom) + 20px));
  display: flex;
  flex-direction: column;
  gap: 1rem;
  pointer-events: none;
}

.scroll-hint {
  font-family: var(--font-mono);
  font-size: 0.62rem;
  letter-spacing: 0.1em;
  color: rgba(0, 229, 199, 0.55);
  text-align: center;
  margin: 0.25rem 0 0.5rem;
}

/* Midway inset with clear video corridor in the center */
.domains-grid {
  position: relative;
  left: auto;
  top: auto;
  transform: none;
  z-index: 10;
  display: grid;
  grid-template-columns: minmax(0, 32%) 1fr minmax(0, 32%);
  grid-template-rows: auto auto;
  column-gap: clamp(0.75rem, 3vw, 1.5rem);
  row-gap: clamp(0.55rem, 1.4vw, 1rem);
  width: 100%;
  max-width: none;
  max-height: none;
  margin: 4vh auto 0;
  pointer-events: none;
  align-items: start;
}

.domain-facs { grid-column: 1; grid-row: 1; border-color: rgba(0, 229, 199, 0.55); }
.domain-gaze { grid-column: 3; grid-row: 1; border-color: rgba(79, 195, 247, 0.55); }
.domain-pose { grid-column: 1; grid-row: 2; border-color: rgba(206, 147, 216, 0.55); }
.domain-rppg { grid-column: 3; grid-row: 2; border-color: rgba(255, 138, 101, 0.55); }

.domain-panel {
  position: relative;
  width: 100%;
  max-width: 280px;
  min-width: 0;
  justify-self: stretch;
  padding: clamp(0.5rem, 1vw, 0.75rem);
  background: rgba(0, 12, 18, 0.84);
  border: 1px solid var(--hud-border);
  backdrop-filter: blur(10px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.domain-facs,
.domain-pose { justify-self: start; }

.domain-gaze,
.domain-rppg { justify-self: end; }

.domain-header {
  display: flex; align-items: center; gap: 0.5rem;
  margin-bottom: 0.55rem; padding-bottom: 0.35rem;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  flex-shrink: 0;
}
.domain-num {
  font-family: var(--font-mono); font-size: clamp(0.85rem, 2vw, 1.05rem); font-weight: 600;
  opacity: 0.9; min-width: 1.5rem;
}
.domain-facs .domain-num { color: var(--d1); }
.domain-gaze .domain-num { color: var(--d2); }
.domain-pose .domain-num { color: var(--d3); }
.domain-rppg .domain-num { color: var(--d4); }

.domain-title {
  font-family: var(--font-mono);
  font-size: clamp(0.65rem, 1.5vw, 0.78rem);
  letter-spacing: 0.05em; color: #e0f2f1;
}
.domain-sub {
  font-family: var(--font-mono); font-size: 0.55rem; opacity: 0.5; letter-spacing: 0.04em;
}

.meter { margin-bottom: 0.5rem; }
.meter-label {
  display: flex; justify-content: space-between; gap: 0.4rem;
  font-family: var(--font-mono);
  font-size: clamp(0.62rem, 1.35vw, 0.72rem);
  margin-bottom: 0.2rem; color: var(--hud-text);
}
.meter-label span:first-child {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.meter-value { color: var(--hud-primary); flex-shrink: 0; font-weight: 500; }
.domain-gaze .meter-value { color: var(--d2); }
.domain-pose .meter-value { color: var(--d3); }
.domain-rppg .meter-value { color: var(--d4); }

.meter-bar {
  height: 4px; background: rgba(255,255,255,0.08); border-radius: 2px; overflow: hidden;
}
.meter-fill {
  height: 100%; width: 0%; border-radius: 2px; transition: width 0.55s ease, background 0.35s;
  background: var(--d1); box-shadow: 0 0 6px rgba(0, 229, 199, 0.4);
}
.domain-gaze .meter-fill { background: var(--d2); box-shadow: 0 0 6px rgba(79, 195, 247, 0.4); }
.domain-pose .meter-fill { background: var(--d3); box-shadow: 0 0 6px rgba(206, 147, 216, 0.4); }
.domain-rppg .meter-fill { background: var(--d4); box-shadow: 0 0 6px rgba(255, 138, 101, 0.4); }
.meter-fill.warn { background: var(--hud-warn) !important; }
.meter-fill.alert { background: var(--hud-alert) !important; }

.au-chip-row {
  display: flex; flex-wrap: wrap; gap: 0.3rem; margin-top: 0.25rem;
}
.au-chip-row span {
  font-family: var(--font-mono); font-size: 0.58rem; padding: 0.15rem 0.35rem;
  background: rgba(0,0,0,0.35); border-left: 2px solid var(--d1); color: var(--hud-dim);
}
.au-chip-row b { color: var(--d1); font-weight: 500; }

.rppg-wave { display: block; width: 100%; height: 36px; margin-top: 0.35rem; opacity: 0.9; }
.baseline-status {
  font-family: var(--font-mono); font-size: 0.55rem; color: var(--hud-dim); margin-top: 0.25rem;
}

.face-box {
  position: absolute; border: 1px solid var(--hud-primary); z-index: 6; pointer-events: none;
  box-shadow: 0 0 8px rgba(0, 229, 199, 0.25);
  transition: left 0.8s ease, top 0.8s ease, width 0.8s ease, height 0.8s ease;
  top: 22vh; left: 36%; width: 28%; height: 32vh;
}
.face-label, .face-confidence {
  position: absolute; font-family: var(--font-mono); font-size: 0.55rem;
  background: rgba(0,12,18,0.85); padding: 0.1rem 0.3rem; white-space: nowrap;
}
.face-label { top: -1.05rem; left: 0; color: var(--hud-primary); }
.face-confidence { bottom: -1.05rem; right: 0; color: var(--hud-secondary); }

.hud-bottom {
  position: relative;
  left: auto;
  bottom: auto;
  transform: none;
  width: 100%;
  margin-top: 0.5rem;
  padding: 0.65rem 0.75rem;
  background: rgba(0, 12, 18, 0.9);
  border: 1px solid var(--hud-border);
  z-index: 11;
  pointer-events: none;
  backdrop-filter: blur(10px);
  max-height: none;
  overflow: visible;
}
.panel-header {
  font-family: var(--font-mono); font-size: 0.55rem; letter-spacing: 0.12em;
  color: var(--hud-primary); margin-bottom: 0.35rem;
}
.indicators { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.indicator {
  flex: 1; min-width: 120px; padding: 0.35rem 0.45rem;
  border: 1px solid var(--hud-dim); background: rgba(0,229,199,0.04);
}
.indicator.active-warn { border-color: var(--hud-warn); background: rgba(255,183,77,0.08); }
.indicator.active-alert { border-color: var(--hud-alert); background: rgba(255,82,82,0.08); }
.indicator-name {
  font-family: var(--font-mono); font-size: 0.52rem; letter-spacing: 0.04em;
  margin-bottom: 0.15rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.indicator-bar-track { height: 3px; background: rgba(255,255,255,0.08); border-radius: 2px; overflow: hidden; }
.indicator-bar-fill { height: 100%; width: 0%; transition: width 0.7s ease; background: var(--hud-primary); }
.indicator-meta {
  display: flex; justify-content: space-between; font-family: var(--font-mono);
  font-size: 0.48rem; margin-top: 0.12rem; opacity: 0.65;
}

.hud-watermark {
  position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
  font-family: var(--font-mono); font-size: 0.52rem; letter-spacing: 0.22em;
  color: rgba(0,229,199,0.12); pointer-events: none; z-index: 2; white-space: nowrap;
}

.hud-controls {
  position: fixed;
  top: max(44px, calc(env(safe-area-inset-top) + 40px));
  right: max(10px, env(safe-area-inset-right));
  display: flex; gap: 0.35rem; z-index: 22;
  pointer-events: auto;
}
.btn-hud {
  font-family: var(--font-mono); font-size: 0.68rem; min-height: 36px;
  padding: 0.35rem 0.6rem; border: 1px solid var(--hud-border); background: var(--hud-bg);
  color: var(--hud-primary); cursor: pointer; touch-action: manipulation;
  pointer-events: auto;
}
.btn-danger { color: var(--hud-alert); border-color: rgba(255,82,82,0.45); }

@media (min-width: 1100px) {
  .hud-scroll {
    padding-left: clamp(28px, 5vw, 64px);
    padding-right: clamp(28px, 5vw, 64px);
  }
  .domains-grid {
    grid-template-columns: minmax(0, 28%) 1fr minmax(0, 28%);
    margin-top: 6vh;
    row-gap: 1rem;
  }
  .domain-panel { max-width: 300px; }
  .domain-title { font-size: 0.82rem; }
  .meter-label { font-size: 0.75rem; }
  .meter-bar { height: 5px; }
}

@media (max-width: 900px) {
  .hud-scroll {
    min-height: calc(100dvh + 220px);
    padding-left: clamp(10px, 2.5vw, 20px);
    padding-right: clamp(10px, 2.5vw, 20px);
  }
  .domains-grid {
    grid-template-columns: minmax(0, 36%) 1fr minmax(0, 36%);
    row-gap: 0.55rem;
    margin-top: 3vh;
  }
  .domain-panel { max-width: 240px; }
  .indicators { flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .indicator { flex: 0 0 auto; min-width: 130px; }
  .hud-watermark, .scan-line { display: none; }
  .face-box { max-width: 36vw; max-height: 24vh; }
}

@media (max-width: 640px) {
  .hud-scroll {
    min-height: calc(100dvh + 260px);
    gap: 0.75rem;
    padding-left: 10px;
    padding-right: 10px;
  }
  .scroll-hint { font-size: 0.58rem; }
  .domains-grid {
    grid-template-columns: minmax(0, 40%) 1fr minmax(0, 40%);
    row-gap: 0.45rem;
    margin-top: 2vh;
  }
  .domain-panel { max-width: 190px; padding: 0.5rem 0.45rem; }
  .domain-title { font-size: 0.62rem; }
  .domain-sub { display: none; }
  .meter { margin-bottom: 0.35rem; }
  .meter-label { font-size: 0.55rem; }
  .meter-bar { height: 3px; }
  .au-chip-row span { font-size: 0.48rem; }
  .rppg-wave { height: 22px; }
  .baseline-status { font-size: 0.48rem; }
  .hud-bottom { padding: 0.5rem; }
  .hud-bottom .panel-header { display: block; font-size: 0.5rem; }
  .indicator-meta { display: none; }
  .hud-top { font-size: 0.6rem; flex-wrap: wrap; gap: 0.25rem; }
  .brand-ver { display: none; }
}

@media (max-width: 380px) {
  .hud-scroll { padding-left: 8px; padding-right: 8px; }
  .domains-grid {
    grid-template-columns: minmax(0, 42%) 1fr minmax(0, 42%);
  }
  .domain-panel { max-width: 160px; }
  .domain-num { font-size: 0.75rem; min-width: 1.2rem; }
  .meter-label { font-size: 0.5rem; }
}

@media (max-height: 480px) and (orientation: landscape) {
  .hud-scroll { min-height: calc(100dvh + 180px); }
  .domain-sub, .au-chip-row, .rppg-wave { display: none; }
  .meter { margin-bottom: 0.25rem; }
  .face-box { display: none; }
}