/* Ported from demo/CamMonitor.dc.html — same visual tokens, real markup.
   The demo's inline styles are lifted into classes here; colours, radii and
   spacing are unchanged. */

:root {
  --bg: oklch(0.14 0.004 260);
  --surface: oklch(0.16 0.004 260);
  --card: oklch(0.2 0.005 260);
  --card-hi: oklch(0.24 0.006 260);
  --line: oklch(0.26 0.006 260);
  --line-hi: oklch(0.28 0.006 260);
  --line-hi2: oklch(0.3 0.006 260);
  --text: oklch(0.96 0 0);
  --text-dim: oklch(0.85 0.01 260);
  --muted: oklch(0.6 0.01 260);
  --muted-dim: oklch(0.55 0.01 260);
  --accent: oklch(0.72 0.15 155);
  --accent-hi: oklch(0.82 0.12 155);
  --motion: oklch(0.78 0.16 60);
  --motion-ink: oklch(0.18 0.05 60);
  --rec: oklch(0.63 0.22 25);
  --cold: oklch(0.68 0.09 250);
  --danger: oklch(0.8 0.08 25);

  /* Set from the camera's real resolution once /api/overview reports it.
     The camera in use is 1728x1728, so the default is square rather than
     the 16/9 the design mock assumed. See docs/video.md §10.
     --frame-ar is the same value as a bare number, for use in calc(). */
  --frame-ratio: 1;
  --frame-ar: 1;

  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, monospace;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  /* Stops iOS from enlarging text when the phone is turned sideways. */
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

::-webkit-scrollbar { display: none; }
input, textarea, button, select { font-family: inherit; }
button { cursor: pointer; }
a { color: var(--accent); text-decoration: none; }

@keyframes rec  { 0%, 100% { opacity: 1; } 50% { opacity: 0.25; } }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- shell ---------------------------------------------------------------
   The demo drew a 402x874 phone frame. On a real device the app is the whole
   viewport, with the safe-area insets doing the work the frame used to. */

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  background: var(--surface);
  position: relative;
  overflow: hidden;
}

.hidden { display: none !important; }

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

.hdr {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 14px) 16px 12px;
  background: var(--surface);
  border-bottom: 1px solid oklch(0.22 0.005 260);
}
.hdr h1 { margin: 0; font-size: 20px; font-weight: 700; letter-spacing: -0.02em; }

.icon-btn {
  width: 36px; height: 36px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 0;
  border: 1px solid var(--line-hi);
  border-radius: 10px;
  background: var(--card);
  color: var(--text-dim);
  font-size: 17px;
}

/* ---- live tile ------------------------------------------------------------ */

.live-wrap {
  flex-shrink: 0;
  padding: 14px 16px 12px;
  background: var(--surface);
  border-bottom: 1px solid oklch(0.22 0.005 260);
}

/* The tile is pinned above the scrolling list, so its height comes straight
   out of the history list's. A square camera at full width would claim most
   of a phone screen and leave a sliver for the clips.
   Capping the *width* (derived from the aspect ratio) rather than the height
   shrinks the whole box, so a square image is not pillarboxed inside a
   too-wide frame. --frame-ar is the numeric w/h, set from the camera's real
   resolution in app.js. */
.live-wrap .frame {
  max-width: calc(34dvh * var(--frame-ar));
  margin-inline: auto;
}

.frame {
  position: relative;
  width: 100%;
  aspect-ratio: var(--frame-ratio);
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--line);
  background:
    repeating-linear-gradient(45deg, var(--card) 0 12px, oklch(0.22 0.006 260) 12px 24px);
}
.frame img, .frame video {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: contain;
  background: #000;
}

.chip {
  position: absolute;
  display: flex; align-items: center; gap: 6px;
  padding: 5px 9px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.55);
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-dim);
  pointer-events: none;
}
.chip-tl { top: 12px; left: 12px; }
.chip-tr { top: 12px; right: 12px; }
.chip-bl { bottom: 12px; left: 12px; }

.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.dot-rec { background: var(--rec); animation: rec 1.4s infinite; }
.dot-off { background: var(--muted-dim); }

/* ---- scroll body ---------------------------------------------------------- */

.body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(env(safe-area-inset-bottom) + 24px);
}

.stack { padding: 16px; display: flex; flex-direction: column; gap: 14px; }

.card {
  padding: 16px;
  border-radius: 16px;
  background: var(--card);
  border: 1px solid var(--line);
}

.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.banner {
  margin: 0 16px;
  padding: 11px 14px;
  border-radius: 12px;
  background: oklch(0.28 0.07 25);
  border: 1px solid oklch(0.45 0.12 25);
  color: oklch(0.92 0.04 25);
  font-size: 13px;
  line-height: 1.5;
}

/* toggle switch */
.switch {
  width: 52px; height: 31px; flex-shrink: 0;
  padding: 0; border: none; border-radius: 20px;
  background: var(--line-hi2);
  position: relative;
  transition: background 0.2s;
}
.switch[aria-checked="true"] { background: var(--accent); }
.switch::after {
  content: "";
  position: absolute; top: 3px; left: 3px;
  width: 25px; height: 25px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  transition: left 0.2s;
}
.switch[aria-checked="true"]::after { left: 24px; }

/* home cards — a 2x2 grid of four cards cut to the same cloth: two toggles on
   top (motion, alerts), two counters below (segments, motion events). */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.stat, .tile {
  display: flex; flex-direction: column;
  gap: 6px; min-height: 96px;
  justify-content: space-between;
}
.stat-k, .tile-k {
  font-size: 11px; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.05em;
}
.stat-v { font-size: 24px; font-weight: 700; font-family: var(--mono); }
.stat-v.motion { color: var(--motion); }
.stat-u { font-size: 12px; color: var(--muted); }

.tile-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.tile-sub { font-size: 13px; color: var(--text-dim); font-weight: 600; }

/* ---- history -------------------------------------------------------------- */

.sec-title { display: flex; align-items: center; gap: 8px; padding: 8px 16px 4px; }
.sec-title span { font-size: 16px; font-weight: 700; }
.sec-title i { flex: 1; height: 1px; background: oklch(0.24 0.005 260); }

.strip {
  display: flex; gap: 8px;
  overflow-x: auto;
  padding: 10px 16px 6px;
  scrollbar-width: none;
}
.strip.tight { padding-top: 6px; padding-bottom: 12px; }

.day {
  flex-shrink: 0; width: 56px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 9px 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: var(--text-dim);
}
.day[aria-selected="true"] {
  border-color: var(--accent); background: var(--accent); color: var(--bg);
}
.day-dow { font-size: 11px; opacity: 0.7; }
.day-num { font-size: 17px; font-weight: 700; font-family: var(--mono); }
.day-cold { width: 5px; height: 5px; border-radius: 50%; background: var(--cold); }

.pill {
  flex-shrink: 0;
  padding: 7px 14px;
  border: 1px solid var(--line-hi);
  border-radius: 20px;
  background: transparent;
  color: oklch(0.75 0.01 260);
  font-size: 13px; font-weight: 500;
}
.pill[aria-selected="true"] {
  border-color: var(--accent); background: var(--accent);
  color: var(--bg); font-weight: 600;
}

.clips { padding: 2px 16px 24px; display: flex; flex-direction: column; gap: 10px; }

.clip {
  display: flex; gap: 12px; align-items: stretch;
  padding: 10px;
  border-radius: 14px;
  background: var(--card);
  border: 1px solid var(--line);
  text-align: left;
  color: inherit;
  width: 100%;
}
/* Square thumbnails are noticeably taller than the mock's 112x63, so they are
   held to 84px to keep roughly the original row height. */
.clip-thumb {
  position: relative; flex-shrink: 0;
  width: 84px; aspect-ratio: var(--frame-ratio);
  border-radius: 9px; overflow: hidden;
  background: repeating-linear-gradient(45deg, var(--card-hi) 0 8px, oklch(0.27 0.007 260) 8px 16px);
}
.clip-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.clip-dur {
  position: absolute; right: 4px; bottom: 4px;
  padding: 2px 5px; border-radius: 5px;
  background: rgba(0, 0, 0, 0.6);
  font-family: var(--mono); font-size: 9px;
}
.clip-flag {
  position: absolute; top: 4px; left: 4px;
  width: 8px; height: 8px; border-radius: 50%;
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.4);
}

.clip-meta {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; justify-content: center; gap: 5px;
}
.clip-time { font-size: 16px; font-weight: 600; font-family: var(--mono); }
.clip-sub {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  font-size: 12px; color: var(--muted); font-family: var(--mono);
}
.clip-sub .sep { opacity: 0.4; }

.badge {
  padding: 3px 8px;
  border-radius: 20px;
  font-size: 11px; font-weight: 600;
  white-space: nowrap;
  background: var(--line); color: oklch(0.7 0.01 260);
}
.badge.on { background: var(--motion); color: var(--motion-ink); }

.tier-hot  { color: var(--accent); }
.tier-cold { color: var(--cold); }

.empty { padding: 40px 16px; text-align: center; color: var(--muted); font-size: 14px; }

/* ---- overlays ------------------------------------------------------------- */

.sheet {
  position: absolute; inset: 0; z-index: 50;
  background: var(--surface);
  display: flex; flex-direction: column;
}
.sheet-hdr {
  flex-shrink: 0;
  display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 14px) 16px 12px;
  border-bottom: 1px solid oklch(0.22 0.005 260);
}
.sheet-hdr h2 { margin: 0; font-size: 18px; font-weight: 700; }
.back {
  padding: 4px; border: none; background: none;
  color: var(--accent); font-size: 16px; font-weight: 600;
}
.sheet-body { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 22px; }

/* ---- forms ---------------------------------------------------------------- */

.group { display: flex; flex-direction: column; gap: 10px; }
.group-title {
  display: flex; align-items: center; gap: 8px;
  padding-left: 2px;
  font-size: 12px; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.06em;
}
.group-title .dot { width: 9px; height: 9px; }

.label { font-size: 13px; color: oklch(0.75 0.01 260); padding-left: 2px; }

.input {
  width: 100%;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--card);
  color: var(--text);
  /* 16px minimum: anything smaller makes iOS zoom the page on focus. */
  font-size: 16px;
  font-family: var(--mono);
  outline: none;
}
.input:focus { border-color: var(--accent); }
.input[readonly] { color: var(--muted); }

.ctrl {
  display: flex; flex-direction: column; gap: 12px;
  padding: 14px 16px;
  border-radius: 14px;
  background: var(--card);
  border: 1px solid var(--line);
}
.ctrl-top { display: flex; align-items: center; justify-content: space-between; font-size: 15px; }
.ctrl-val { font-family: var(--mono); font-weight: 700; color: var(--accent-hi); }
.ctrl-hint { font-size: 12px; color: var(--muted-dim); line-height: 1.6; }

.btn {
  width: 100%;
  padding: 16px;
  border: none; border-radius: 14px;
  background: var(--accent);
  color: var(--bg);
  font-size: 16px; font-weight: 700;
}
.btn:disabled { opacity: 0.55; }
.btn-ghost {
  background: transparent;
  border: 1px solid var(--line-hi2);
  color: var(--text-dim);
  font-weight: 600; font-size: 14px;
  padding: 13px;
}
.btn-danger { background: transparent; border: 1px solid var(--line-hi2); color: var(--danger); font-weight: 500; padding: 14px; }
.btn-ok   { background: var(--accent); border-color: var(--accent); color: var(--bg); }
.btn-bad  { background: transparent; border-color: oklch(0.5 0.12 25); color: var(--danger); }

input[type="range"] {
  -webkit-appearance: none; appearance: none;
  width: 100%; background: transparent; margin: 0;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 5px; border-radius: 3px; background: var(--line-hi2);
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 24px; height: 24px; margin-top: -9.5px;
  border-radius: 50%; background: var(--accent);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
}

.readonly-note {
  font-size: 12px; color: var(--muted-dim); line-height: 1.6; padding: 0 2px;
}

.json-box {
  padding: 14px;
  border-radius: 10px;
  background: oklch(0.12 0.004 260);
  color: oklch(0.85 0.02 155);
  font-family: var(--mono); font-size: 11px; line-height: 1.6;
  white-space: pre; overflow-x: auto;
  margin: 0;
}

/* motion tuning meter */
.meter {
  position: relative; height: 14px;
  border-radius: 7px;
  background: var(--card-hi);
  margin: 2px 1px;
  overflow: hidden;
}
.meter-fill {
  position: absolute; inset: 0 auto 0 0;
  width: 0;
  border-radius: 7px;
  background: var(--accent);
  transition: width 0.4s ease, background 0.3s;
}
.meter-fill.hot { background: var(--motion); }
.meter-mark {
  position: absolute; top: -3px; bottom: -3px;
  width: 2px; border-radius: 2px; background: oklch(0.95 0 0);
}

/* region selector overlay */
.region {
  position: absolute;
  border: 2px dashed oklch(0.5 0.02 155);
  border-radius: 8px;
  pointer-events: none;
}
.region.hot { border-color: var(--motion); background: oklch(0.78 0.16 60 / 0.1); }
.handle {
  position: absolute;
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid var(--bg);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.55);
  touch-action: none;
  z-index: 4;
  padding: 0;
}

/* ---- player --------------------------------------------------------------- */

.player {
  position: absolute; inset: 0; z-index: 70;
  background: #000;
  display: flex; flex-direction: column;
}
.player-hdr {
  flex-shrink: 0;
  display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 14px) 16px 12px;
}
.player-title { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.player-title b { font-size: 15px; font-weight: 700; }
.player-title small { font-size: 11px; color: var(--muted); font-family: var(--mono); }

.player-stage {
  flex: 1;
  display: flex; align-items: center; justify-content: center;
  padding: 0 12px; min-height: 0;
}
.player-stage .frame { border-radius: 14px; max-height: 100%; }

.player-foot {
  flex-shrink: 0;
  padding: 12px 20px calc(env(safe-area-inset-bottom) + 24px);
  display: flex; flex-direction: column; gap: 12px;
}

.track-label {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 11px; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.06em;
  padding: 0 1px;
}
.track-label span:last-child { font-family: var(--mono); text-transform: none; }

.scrub {
  position: relative; height: 8px;
  border-radius: 4px;
  background: var(--line-hi);
  touch-action: none;
}
.scrub-motion {
  position: absolute; top: 0; bottom: 0;
  background: var(--motion);
  border-radius: 3px;
}
.scrub-fill {
  position: absolute; left: 0; top: 0; bottom: 0;
  border-radius: 3px; background: var(--accent);
}
.scrub-knob {
  position: absolute; top: 50%;
  width: 16px; height: 16px;
  margin-left: -8px;
  transform: translateY(-50%);
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
}

.times {
  display: flex; justify-content: space-between;
  font-family: var(--mono); font-size: 12px; color: oklch(0.65 0.01 260);
}

.jump { display: flex; align-items: center; gap: 10px; }
.jump button {
  flex: 1;
  padding: 13px;
  border-radius: 12px;
  border: 1px solid var(--line-hi2);
  background: var(--card);
  color: oklch(0.92 0 0);
  font-size: 14px; font-weight: 600;
}
.jump button.next { border: none; background: var(--motion); color: var(--motion-ink); font-weight: 700; }
.jump-mid {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  min-width: 96px;
}
.jump-mid small { font-size: 12px; color: oklch(0.7 0.01 260); white-space: nowrap; }

/* ---- login ---------------------------------------------------------------- */

.login {
  position: absolute; inset: 0; z-index: 60;
  background: var(--bg);
  display: flex; flex-direction: column; justify-content: center;
  gap: 28px;
  padding: 0 40px;
}
.login-brand { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.login-mark {
  width: 60px; height: 60px;
  border-radius: 16px; background: var(--accent);
  display: flex; align-items: center; justify-content: center;
}
.login-mark i {
  width: 22px; height: 22px;
  border: 3px solid var(--bg); border-radius: 50%;
}
.login h2 { margin: 8px 0 0; font-size: 24px; font-weight: 700; letter-spacing: -0.02em; }
.login p { margin: 0; font-size: 13px; color: var(--muted); }
.login-form { display: flex; flex-direction: column; gap: 12px; }
.login .input { padding: 16px; }
.check {
  display: flex; align-items: center; gap: 10px;
  padding: 2px 4px;
  font-size: 14px; color: oklch(0.7 0.01 260);
}
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.err { color: var(--danger); font-size: 13px; min-height: 18px; text-align: center; }

/* ---- toast ---------------------------------------------------------------- */

/* Anchored to the top: the bottom of the screen is occupied by the player's
   own controls, and a toast landing on the jump buttons hides them exactly
   when something has gone wrong. */
.toast {
  position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 72px);
  transform: translateX(-50%);
  z-index: 90;
  max-width: 90vw;
  padding: 12px 18px;
  border-radius: 12px;
  background: oklch(0.26 0.006 260);
  border: 1px solid var(--line-hi2);
  color: var(--text);
  font-size: 14px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.toast.show { opacity: 1; }
.toast.bad { border-color: oklch(0.5 0.12 25); color: var(--danger); }

.spinner {
  width: 16px; height: 16px;
  border: 2px solid var(--line-hi2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: inline-block;
  vertical-align: -3px;
}
