:root {
  --bg: #0b1020;
  --surface: #131a2e;
  --surface-2: #1b2440;
  --border: #2a3558;
  --text: #e8ecf8;
  --muted: #98a3c4;
  --accent: #5eead4;
  --accent-ink: #052e2a;
  --danger: #f87171;
  --warn: #fbbf24;
  --ok: #4ade80;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow-x: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
h1, h2 { margin: 0; line-height: 1.2; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* Top bar and status */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 16px 6px; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { border-radius: 8px; }
.brand h1 { font-size: 1.25rem; }
.topbar-actions { display: flex; gap: 6px; }
.icon-btn {
  width: 48px; height: 48px; display: grid; place-items: center;
  background: transparent; color: var(--text); border: 1px solid transparent; border-radius: 12px; cursor: pointer;
}
.icon-btn:hover { background: var(--surface); }
.icon-btn[aria-pressed="true"] { color: var(--danger); }

.statusbar { display: flex; flex-wrap: wrap; gap: 6px 8px; padding: 0 16px 10px; }
.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px;
  font-size: 0.78rem; color: var(--muted); max-width: 100%;
}
.pill i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.pill[data-state="ok"] i { background: var(--ok); }
.pill[data-state="warn"] i { background: var(--warn); }
.pill[data-state="err"] i { background: var(--danger); }

/* Layout: single column on phones, two columns on wide screens */
.layout { display: grid; gap: 14px; padding: 0 16px 24px; max-width: 1200px; margin: 0 auto; }
.layout > * { min-width: 0; }
@media (min-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); align-items: start; }
  .stage-wrap { position: sticky; top: 12px; }
}

/* Camera stage */
.stage {
  position: relative; width: 100%; aspect-ratio: 4 / 3; max-height: 62vh;
  background: #000; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--border);
}
@media (min-width: 900px) { .stage { max-height: 78vh; } }
.stage video, .stage canvas {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain;
}
.stage.mirrored video, .stage.mirrored canvas { transform: scaleX(-1); }
.badge {
  position: absolute; left: 10px; top: 10px; padding: 4px 10px; border-radius: 999px;
  background: rgba(11, 16, 32, 0.75); font-size: 0.8rem; pointer-events: none;
}
.fab {
  position: absolute; right: 10px; bottom: 10px; width: 52px; height: 52px; display: grid; place-items: center;
  border-radius: 50%; border: 1px solid var(--border); background: rgba(11, 16, 32, 0.8); color: var(--text); cursor: pointer;
}
.card {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; padding: 20px; text-align: center; background: rgba(11, 16, 32, 0.92);
}
.card h2 { font-size: 1.15rem; }
.card p { margin: 0; color: var(--muted); max-width: 34ch; }

/* Panel, tabs */
.panel { display: flex; flex-direction: column; gap: 12px; }
.tabs { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; padding: 4px; background: var(--surface); border-radius: var(--radius); border: 1px solid var(--border); }
.tab {
  min-height: 48px; padding: 6px 4px; font: inherit; font-size: 0.88rem; font-weight: 600; color: var(--muted);
  background: transparent; border: 0; border-radius: 10px; cursor: pointer;
}
.tab[aria-selected="true"] { background: var(--accent); color: var(--accent-ink); }
.tabpanel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }

.hint { margin: 0; color: var(--muted); font-size: 0.88rem; }
.muted { color: var(--muted); }
.field-label { display: block; margin: 12px 0 6px; font-size: 0.85rem; color: var(--muted); }
.tabpanel .field-label:first-of-type { margin-top: 10px; }
input[type="text"], input[type="password"], select {
  width: 100%; min-height: 48px; padding: 8px 12px; font: inherit; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
}
input[type="range"] { width: 100%; min-height: 32px; accent-color: var(--accent); }
.row { display: flex; gap: 10px; align-items: stretch; }
.row.wrap { flex-wrap: wrap; align-items: center; margin-top: 10px; }
.row.end { justify-content: flex-end; margin-top: 14px; }
.row > input { flex: 1 1 0; min-width: 0; }

.btn {
  min-height: 48px; padding: 8px 16px; font: inherit; font-weight: 600; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.record {
  position: relative; overflow: hidden; flex: none; min-width: 150px;
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.btn.record.recording { border-color: var(--danger); }
.rec-fill { position: absolute; inset: 0 auto 0 0; width: 0; background: rgba(248, 113, 113, 0.45); pointer-events: none; }
.rec-text { position: relative; }

.sign-list { list-style: none; margin: 10px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.sign-list li { display: flex; align-items: center; gap: 8px; background: var(--surface-2); border-radius: 10px; padding: 2px 4px 2px 12px; }
.sign-list .name { flex: 1; min-width: 0; text-align: left; background: none; border: 0; color: var(--text); font: inherit; min-height: 44px; overflow-wrap: anywhere; cursor: pointer; }
.sign-list .count { color: var(--muted); font-size: 0.8rem; flex: none; }
.sign-list .del { width: 44px; height: 44px; flex: none; background: none; border: 0; color: var(--danger); font-size: 1.3rem; cursor: pointer; border-radius: 10px; }
.sign-list .empty { color: var(--muted); background: none; padding: 4px 0; }

/* Transcript */
.transcript-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.guess { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; font-size: 0.9rem; }
.guess strong { font-size: 1.6rem; line-height: 1.1; overflow-wrap: anywhere; }
.meter { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.meter > div { height: 100%; width: 0; background: var(--accent); transition: width 0.12s linear; }
.transcript {
  min-height: 96px; max-height: 220px; overflow-y: auto; padding: 12px; font-size: 1.35rem; line-height: 1.4;
  background: var(--surface-2); border-radius: 10px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.transcript:empty::before { content: attr(data-placeholder); color: var(--muted); font-size: 1rem; }
.actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 8px; }

/* Settings dialog */
dialog {
  width: min(520px, calc(100vw - 24px)); max-height: calc(100vh - 24px); overflow-y: auto;
  padding: 18px; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
.settings h2 { margin-bottom: 8px; }
.settings fieldset { border: 1px solid var(--border); border-radius: 12px; margin: 12px 0 0; padding: 4px 12px 14px; min-width: 0; }
.settings legend { padding: 0 6px; color: var(--accent); font-weight: 600; }
.check { display: flex; align-items: center; gap: 10px; min-height: 48px; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  max-width: calc(100vw - 32px); padding: 10px 16px; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 12px; opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 10; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.noscript { padding: 16px; }

@media (prefers-reduced-motion: reduce) {
  .meter > div, .toast { transition: none; }
}
