:root {
  --bg: #0f1115;
  --bg-elevated: #171a21;
  --bg-sunken: #0a0c10;
  --fg: #e6e7ea;
  --fg-muted: #9ca3af;
  --border: #262a33;
  --accent: #4f8cff;
  --accent-hover: #6fa1ff;
  --danger: #ef4444;
  --warn: #f59e0b;
  --ok: #22c55e;
  --idle: #6b7280;

  --radius: 6px;
  --gap: 1rem;
  --max-width: 1400px;

  color-scheme: dark;
}

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

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

h1, h2, h3 { margin: 0 0 .5rem; font-weight: 600; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1rem; letter-spacing: .02em; text-transform: uppercase; color: var(--fg-muted); }

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); text-decoration: underline; }

code {
  font: 12px/1 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  background: var(--bg-sunken);
  padding: 1px 5px;
  border-radius: 3px;
  color: var(--fg);
}

.subtle { color: var(--fg-muted); }
.hidden { display: none !important; }
.error { color: var(--danger); }
.status { color: var(--ok); }

button {
  font: inherit;
  background: var(--bg-elevated);
  color: var(--fg);
  border: 1px solid var(--border);
  padding: .55rem 1rem;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
button:hover:not(:disabled) { background: #1f232c; border-color: #353b47; }
button:disabled { opacity: .5; cursor: not-allowed; }

button.primary { background: var(--accent); color: white; border-color: var(--accent); }
button.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }

button.danger { background: var(--danger); color: white; border-color: var(--danger); }
button.danger:hover:not(:disabled) { background: #dc2626; border-color: #dc2626; }

button.ghost { background: transparent; }

button.large { padding: .9rem 1.5rem; font-size: 1rem; font-weight: 600; }

button.link-button {
  background: transparent; border: none; color: var(--accent); padding: 0; font: inherit;
}
button.link-button:hover { text-decoration: underline; }

input, select, textarea {
  font: inherit;
  background: var(--bg-sunken);
  color: var(--fg);
  border: 1px solid var(--border);
  padding: .5rem .6rem;
  border-radius: var(--radius);
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}

label {
  display: flex;
  flex-direction: column;
  gap: .25rem;
  font-size: .85rem;
  color: var(--fg-muted);
}

/* ========= app shell ========= */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .75rem 1.25rem;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
}

.app-nav {
  display: flex;
  gap: 1.25rem;
  align-items: center;
}
.app-nav a { color: var(--fg-muted); padding: .25rem .15rem; }
.app-nav a:hover { color: var(--fg); text-decoration: none; }
.app-nav a[aria-current="page"] { color: var(--fg); border-bottom: 2px solid var(--accent); }

/* ========= login ========= */

.login-shell {
  max-width: 380px;
  margin: 6rem auto;
  padding: 2rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.login-header { text-align: center; margin-bottom: 1.5rem; }
.login-form { display: flex; flex-direction: column; gap: .9rem; }
.login-footer { margin-top: 1.5rem; text-align: center; font-size: .85rem; }

/* ========= capture ========= */

.capture-shell {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 1rem;
  display: grid;
  grid-template-columns: 1.4fr 1fr 1fr;
  gap: var(--gap);
  align-items: start;
}

@media (max-width: 1100px) {
  .capture-shell { grid-template-columns: 1fr 1fr; }
  .zone-telemetry { grid-column: 1 / -1; }
}
@media (max-width: 720px) {
  .capture-shell { grid-template-columns: 1fr; }
}

.zone {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: .75rem;
}
.zone-title { margin: 0; }

.video-wrap {
  position: relative;
  aspect-ratio: 16 / 9;
  background: black;
  border-radius: var(--radius);
  overflow: hidden;
}
.video-wrap video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.video-placeholder, .face-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--fg-muted);
  font-size: .9rem;
  padding: 1rem;
  text-align: center;
}
.face-placeholder { position: static; }

.camera-controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .5rem;
  align-items: end;
}
.camera-controls button { grid-column: span 1; }
.control { font-size: .8rem; }

.face-preview {
  aspect-ratio: 1 / 1;
  background: var(--bg-sunken);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex; align-items: center; justify-content: center;
}
.face-preview.large { aspect-ratio: 4 / 3; max-width: 480px; }
.face-preview img { width: 100%; height: 100%; object-fit: cover; }

.primary-link {
  display: inline-block;
  margin-top: .5rem;
  padding: .4rem .8rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--fg);
}
.primary-link:hover { background: var(--bg-sunken); text-decoration: none; }

/* ========= session state ========= */

.session-panel {
  display: flex;
  flex-direction: column;
  gap: .6rem;
  padding-top: .5rem;
  border-top: 1px solid var(--border);
}

.state {
  display: inline-flex;
  align-items: center;
  gap: .5rem;
  font-size: .85rem;
  font-weight: 500;
  padding: .35rem .6rem;
  border-radius: 999px;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  width: fit-content;
}
.state-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--idle);
}
.state-idle        .state-dot { background: var(--idle); }
.state-fetching    .state-dot,
.state-acquiring   .state-dot,
.state-negotiating .state-dot { background: var(--warn); animation: pulse 1.2s infinite; }
.state-connected   .state-dot { background: var(--warn); }
.state-streaming   .state-dot { background: var(--ok); }
.state-ending      .state-dot { background: var(--warn); animation: pulse 1.2s infinite; }
.state-failed      .state-dot { background: var(--danger); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .35; }
}

.session-meta {
  display: flex; flex-wrap: wrap;
  gap: .75rem;
  font-size: .8rem;
}

/* ========= telemetry ========= */

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .5rem;
  margin: 0;
}
.stat { display: flex; flex-direction: column; gap: .15rem; margin: 0; }
.stat dt { font-size: .75rem; color: var(--fg-muted); text-transform: uppercase; letter-spacing: .04em; }
.stat dd { margin: 0; font-variant-numeric: tabular-nums; font-size: 1rem; }
.stat.warn dd { color: var(--warn); }

.stats-detail {
  margin-top: .75rem;
  font-size: .8rem;
}
.stats-detail summary {
  cursor: pointer;
  color: var(--fg-muted);
  padding: .25rem 0;
}
.stats-detail pre {
  margin: .5rem 0 0;
  padding: .6rem;
  background: var(--bg-sunken);
  border-radius: var(--radius);
  max-height: 320px;
  overflow: auto;
  font-size: 11px;
  color: var(--fg-muted);
}

/* ========= face management ========= */

.face-shell {
  max-width: 900px;
  margin: 2rem auto;
  padding: 0 1rem;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
}
@media (max-width: 720px) { .face-shell { grid-template-columns: 1fr; } }

.face-current, .face-upload {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.file-drop {
  display: flex; flex-direction: column; gap: .5rem;
  padding: 1rem;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color .15s;
}
.file-drop:hover { border-color: var(--accent); }

/* ========= history ========= */

.history-shell {
  max-width: 900px;
  margin: 2rem auto;
  padding: 0 1rem;
}
.history-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .9rem;
}
.history-table th, .history-table td {
  text-align: left;
  padding: .5rem .6rem;
  border-bottom: 1px solid var(--border);
}
.history-table th {
  color: var(--fg-muted);
  font-weight: 500;
  text-transform: uppercase;
  font-size: .75rem;
  letter-spacing: .04em;
}
.history-table tr.empty td { color: var(--fg-muted); padding: 2rem; text-align: center; }

/* ========= dialog ========= */

.dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
  color: var(--fg);
  padding: 1.5rem;
  max-width: 480px;
  width: 90vw;
}
.dialog::backdrop { background: rgba(0, 0, 0, .5); }
.dialog form { display: flex; flex-direction: column; gap: .9rem; }
.dialog-actions {
  display: flex; justify-content: flex-end; gap: .5rem;
  margin: .5rem 0 0; padding: 0;
}
