:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --border: #e5e7eb;
  --ink: #111827;
  --muted: #6b7280;
  --accent: #2563eb;
  --ok: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%; height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 13px;
  overflow: hidden;
}

#app {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 8px;
  padding: 8px;
}

/* ── Header ─────────────────────────────── */
#header {
  display: flex; align-items: center;
  gap: 6px; padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  flex-wrap: wrap;
}

#title { font-size: 0.9rem; font-weight: 700; margin-right: 4px; }
.sep { width: 1px; height: 18px; background: var(--border); margin: 0 2px; }

.btn {
  border: 1px solid var(--border); border-radius: 7px;
  padding: 5px 10px; background: var(--surface); color: var(--ink);
  cursor: pointer; font-size: 0.78rem; font-weight: 600;
  transition: background 0.12s, box-shadow 0.12s;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { background: #f9fafb; box-shadow: 0 1px 4px rgb(0 0 0/8%); }
.btn:disabled { cursor: not-allowed; opacity: 0.4; }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.primary:hover:not(:disabled) { background: #1d4ed8; }
.btn.warning { background: var(--warn); color: #fff; border-color: var(--warn); }
.btn.warning:hover:not(:disabled) { background: #b45309; }
.btn.toggled { background: var(--ink); color: #fff; border-color: var(--ink); }

#statusBadge {
  margin-left: auto;
  font-size: 0.75rem; font-weight: 700;
  border-radius: 999px; padding: 4px 10px;
  background: #f3f4f6; color: var(--muted);
  border: 1px solid var(--border);
  white-space: nowrap;
}
#statusBadge.ok   { color: var(--ok);   background: #f0fdf4; border-color: #86efac; }
#statusBadge.warn { color: var(--warn);  background: #fffbeb; border-color: #fcd34d; }
#statusBadge.bad  { color: var(--bad);   background: #fef2f2; border-color: #fca5a5; }

/* ── Workspace ──────────────────────────── */
#workspace {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

/* ── Camera preview ─────────────────────── */
#cameraPreviewPanel {
  position: absolute; left: 12px; top: 12px; z-index: 25;
  width: min(240px, calc(100vw - 40px));
  background: rgb(255 255 255 / 92%);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 8px; backdrop-filter: blur(6px);
}

#cameraPreviewTitle {
  font-size: 0.72rem; font-weight: 700; color: var(--muted);
  margin-bottom: 5px; text-transform: uppercase; letter-spacing: 0.06em;
}

#cameraPreviewStage {
  position: relative; width: 100%; aspect-ratio: 4/3;
  border-radius: 6px; overflow: hidden; background: #111;
}

#cameraPreview {
  width: 100%; height: 100%; object-fit: cover;
  transform: scaleX(-1); position: absolute; inset: 0;
}

#cameraPreviewState { margin-top: 5px; font-size: 0.72rem; color: var(--muted); }

/* WebGazer injects its own container — override styles to fit our panel */
#webgazerVideoContainer {
  position: absolute !important; inset: 0 !important;
  width: 100% !important; height: 100% !important;
  display: block !important; z-index: 0;
}

#webgazerVideoFeed {
  width: 100% !important; height: 100% !important;
  display: block !important; position: absolute !important;
  inset: 0 !important; background: #111 !important;
  object-fit: cover; transform: scaleX(-1);
}

#webgazerVideoCanvas {
  position: absolute !important; inset: 0 !important;
  width: 100% !important; height: 100% !important;
  display: block !important; pointer-events: none !important;
  object-fit: cover; transform: scaleX(-1);
}

#webgazerFaceOverlay, #webgazerFaceFeedbackBox {
  position: absolute !important; inset: 0 !important;
  width: 100% !important; height: 100% !important;
  display: block !important; pointer-events: none !important;
  transform: scaleX(-1);
}

/* ── Metrics ────────────────────────────── */
#metrics {
  position: absolute; right: 12px; top: 12px; z-index: 20;
  min-width: 200px;
  background: rgb(255 255 255 / 92%);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; backdrop-filter: blur(6px);
  line-height: 1.6;
}
#metrics .row { display: flex; justify-content: space-between; gap: 8px; }
#metrics .label { color: var(--muted); }
#metrics .value { font-weight: 600; color: var(--ink); }

/* ── Trial AOIs ─────────────────────────── */
#trialArea { position: absolute; inset: 0; display: none; }

/* ── Gallery (face images) ──────────────── */
/* Fixed to the viewport so the image gets the full screen height — covers
 * header, footer, metrics panel, etc. on purpose for the demo.
 * z-index sits above the metrics/camera panels (20/25) but below the gaze
 * dot (40) and calibration overlay (30) so those stay readable. */
#gallery {
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: 28;
}

#galleryStage {
  position: absolute;
  inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: transparent;
  border: none;
  border-radius: 0;
  padding: 0;
  z-index: 2;
  pointer-events: auto;
}

#galleryImage {
  width: 100%; height: 100%;
  object-fit: contain;
  display: block;
  user-select: none;
  -webkit-user-drag: none;
}

#galleryEmpty {
  color: var(--muted);
  font-size: 0.85rem;
  display: none;
}

#gallery.empty #galleryImage { display: none; }
#gallery.empty #galleryEmpty { display: block; }

#galleryHud {
  position: absolute;
  left: 50%; bottom: 8px;
  transform: translateX(-50%);
  display: flex; align-items: center; gap: 10px;
  padding: 5px 12px;
  background: rgb(255 255 255 / 88%);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: 0 2px 10px rgb(0 0 0 / 8%);
  backdrop-filter: blur(6px);
  z-index: 5;
  pointer-events: auto;
  max-width: calc(100% - 24px);
  flex-wrap: wrap; justify-content: center;
}

#galleryHud .btn { padding: 5px 12px; }

#galleryInfo {
  display: flex; align-items: center; gap: 10px;
  font-size: 0.78rem; color: var(--muted);
}

#gallerySet {
  border: 1px solid var(--border); border-radius: 6px;
  padding: 4px 6px; font-size: 0.78rem;
  background: var(--surface); color: var(--ink);
  cursor: pointer; font-weight: 600;
}

#galleryCounter { font-weight: 700; color: var(--ink); font-variant-numeric: tabular-nums; }
#galleryName    { font-family: "Cascadia Mono","Consolas",monospace; font-size: 0.74rem; color: var(--muted); }

.aoi {
  position: absolute;
  border: 1.5px dashed rgb(0 0 0 / 20%);
  background: rgb(0 0 0 / 3%);
  border-radius: 8px;
  font-weight: 600; font-size: 0.82rem; color: var(--muted);
  display: flex; align-items: center; justify-content: center;
  user-select: none; pointer-events: none; text-align: center;
  padding: 8px;
  transition: background 0.15s, border-color 0.15s;
}
.aoi.active { border-color: var(--ok); background: rgb(22 163 74 / 10%); color: var(--ok); }

#aoi-center {
  left: 50%; top: 50%;
  width: min(42vw, 440px); height: min(30vh, 240px);
  transform: translate(-50%, -50%);
  font-size: clamp(0.9rem, 1.8vw, 1.3rem);
}
#aoi-tl { left: 4%; top: 8%; width: 18%; height: 15%; }
#aoi-tr { right: 4%; top: 8%; width: 18%; height: 15%; }
#aoi-bl { left: 4%; bottom: 8%; width: 18%; height: 15%; }
#aoi-br { right: 4%; bottom: 8%; width: 18%; height: 15%; }

/* ── Calibration / Validation layers ───── */
#calibrationLayer, #validationLayer {
  position: absolute; inset: 0;
  z-index: 30; display: none; pointer-events: none;
}

.Calibration {
  position: absolute;
  width: 22px; height: 22px;
  margin-left: -11px; margin-top: -11px;
  border-radius: 999px;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1.5px rgb(0 0 0/20%), 0 3px 10px rgb(0 0 0/20%);
  pointer-events: auto; cursor: pointer;
  background: red;
  opacity: 0.2;
  transition: transform 0.1s, opacity 0.15s;
}

.point {
  position: absolute;
  width: 22px; height: 22px;
  margin-left: -11px; margin-top: -11px;
  border-radius: 999px;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1.5px rgb(0 0 0/20%), 0 3px 10px rgb(0 0 0/20%);
  pointer-events: auto; cursor: pointer;
  background: var(--accent);
  transition: transform 0.1s, opacity 0.15s;
}

.point.validation {
  background: var(--warn); cursor: default;
  animation: valPulse 1s ease-in-out infinite;
}

@keyframes valPulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 1.5px rgb(0 0 0/20%), 0 0 0 0 rgb(217 119 6/40%); }
  50%       { transform: scale(1.25); box-shadow: 0 0 0 1.5px rgb(0 0 0/20%), 0 0 0 8px rgb(217 119 6/0%); }
}

.point.hit { transform: scale(1.5); opacity: 0.3; }

/* ── Gaze dot ───────────────────────────── */
#gazeDot {
  position: absolute;
  width: 10px; height: 10px;
  border-radius: 999px;
  left: 0; top: 0;
  background: rgb(220 38 38 / 80%);
  box-shadow: 0 0 0 2px rgb(220 38 38 / 25%), 0 0 10px rgb(220 38 38 / 40%);
  z-index: 40; pointer-events: none; display: none;
  will-change: transform;
  transition: transform 80ms linear;
}

/* ── Heatmap canvas ─────────────────────── */
#heatmapCanvas {
  position: absolute; inset: 0;
  z-index: 10; pointer-events: none;
  width: 100%; height: 100%;
  display: none; border-radius: 10px;
}

/* ── Plotting canvas (for accuracy measurement) */
#plotting_canvas {
  cursor: crosshair;
  position: fixed;
  top: 0; left: 0;
  pointer-events: none;
  z-index: 35;
}

/* ── Footer ─────────────────────────────── */
#footer {
  display: grid; grid-template-columns: 1fr auto;
  gap: 8px; align-items: center; padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px;
}

#log {
  height: 48px; overflow: auto;
  background: #f9fafb;
  border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 8px;
  font-family: "Cascadia Mono", "Consolas", monospace;
  font-size: 0.72rem; white-space: pre-wrap; color: var(--muted);
}

#footerRight { font-size: 0.72rem; color: var(--muted); white-space: nowrap; }

@media (max-width: 860px) {
  #footer { grid-template-columns: 1fr; }
  #metrics { min-width: 170px; font-size: 0.8rem; }
}
