:root {
  --bg: #0b0d12;
  --bg-2: #12151d;
  --panel: #161a24;
  --panel-2: #1c2130;
  --line: #262c3a;
  --line-2: #333b4d;
  --text: #e8ebf2;
  --text-dim: #98a1b5;
  --text-faint: #667085;
  --accent: #7c5cff;
  --accent-2: #a78bfa;
  --accent-glow: rgba(124, 92, 255, 0.35);
  --good: #34d399;
  --warn: #fbbf24;
  --bad: #f87171;
  --rec: #ff4d5e;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
  font-synthesis: none;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(1200px 700px at 70% -10%, #1a1436 0%, transparent 60%),
              radial-gradient(900px 600px at -10% 110%, #0f2036 0%, transparent 55%),
              var(--bg);
  color: var(--text);
  font-family: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.mono { font-family: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace; }
code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.85em;
  background: rgba(124, 92, 255, 0.12);
  color: var(--accent-2);
  padding: 1px 6px;
  border-radius: 6px;
}
a { color: var(--accent-2); }

.btn {
  appearance: none;
  border: 1px solid var(--line-2);
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: transform .06s ease, background .15s ease, border-color .15s ease, box-shadow .15s ease;
  line-height: 1;
}
.btn:hover { background: #232a3b; border-color: var(--line-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary {
  background: linear-gradient(180deg, var(--accent) 0%, #6a48f0 100%);
  border-color: transparent;
  box-shadow: 0 6px 20px var(--accent-glow);
}
.btn-primary:hover { background: linear-gradient(180deg, #8b6dff 0%, #7355f2 100%); }
.btn-ghost { background: transparent; }
.btn-ghost:hover { background: rgba(255,255,255,0.05); }
.btn-block { width: 100%; text-align: center; }
.btn-lg { padding: 14px 18px; font-size: 1.02rem; width: 100%; }
.btn-tiny { padding: 4px 10px; font-size: .78rem; }
.btn.is-recording {
  background: linear-gradient(180deg, var(--rec) 0%, #e03a4b 100%);
  border-color: transparent;
  box-shadow: 0 6px 20px rgba(255,77,94,.35);
}

/* ============================== ONBOARDING ============================== */
.onboarding {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 20px;
  gap: 20px;
}
.onboarding-card {
  width: 100%;
  max-width: 560px;
  background: linear-gradient(180deg, rgba(28,33,48,.9), rgba(18,21,29,.92));
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 34px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
}
.brand { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; }
.brand-mark {
  font-size: 26px;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), #4f9dff);
  width: 52px; height: 52px;
  display: grid; place-items: center;
  border-radius: 14px;
  box-shadow: 0 8px 24px var(--accent-glow);
}
.brand-mark.small { width: 30px; height: 30px; font-size: 15px; border-radius: 9px; }
.brand h1 { margin: 0; font-size: 1.5rem; letter-spacing: -0.02em; }
.brand-sub { margin: 2px 0 0; color: var(--text-dim); font-size: .92rem; }
.lede { color: var(--text-dim); line-height: 1.6; margin: 0 0 22px; }
.lede strong { color: var(--text); }

.field-label { display: block; font-size: .82rem; font-weight: 600; color: var(--text-dim); margin-bottom: 8px; }
.field-label.mt { margin-top: 16px; }
.license-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: .9rem;
  padding: 12px 14px;
  resize: vertical;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.license-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }
.remember { display: flex; gap: 10px; align-items: flex-start; margin: 14px 0 22px; color: var(--text-dim); font-size: .85rem; line-height: 1.4; }
.remember input { margin-top: 2px; accent-color: var(--accent); }

.onboarding-error {
  margin: 14px 0 0;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  background: rgba(248, 113, 113, .1);
  border: 1px solid rgba(248, 113, 113, .35);
  color: #fecaca;
  font-size: .88rem;
  line-height: 1.5;
}

.help { margin-top: 24px; border-top: 1px solid var(--line); padding-top: 18px; }
.help summary { cursor: pointer; font-weight: 600; color: var(--accent-2); }
.help ol { color: var(--text-dim); line-height: 1.7; padding-left: 20px; margin: 14px 0; }
.help-note { color: var(--text-faint); font-size: .84rem; line-height: 1.6; margin: 0; }
.onboarding-foot { color: var(--text-faint); font-size: .8rem; text-align: center; max-width: 560px; line-height: 1.6; }

/* ============================== APP SHELL ============================== */
.app { min-height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: rgba(11,13,18,.75);
  backdrop-filter: blur(10px);
  position: sticky; top: 0; z-index: 20;
}
.topbar-brand { display: flex; align-items: center; gap: 10px; }
.topbar-title { font-weight: 700; letter-spacing: -.01em; }
.badge {
  font-size: .72rem; font-weight: 700; color: var(--accent-2);
  background: rgba(124,92,255,.14); border: 1px solid rgba(124,92,255,.3);
  padding: 3px 8px; border-radius: 999px;
}
.topbar-actions { display: flex; align-items: center; gap: 12px; }
.conn-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--warn); box-shadow: 0 0 0 3px rgba(251,191,36,.18); transition: .3s; }
.conn-dot.ok { background: var(--good); box-shadow: 0 0 0 3px rgba(52,211,153,.18); }
.conn-dot.err { background: var(--bad); box-shadow: 0 0 0 3px rgba(248,113,113,.18); }
.conn-label { font-size: .82rem; color: var(--text-dim); min-width: 84px; }

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 400px;
  gap: 22px;
  padding: 22px;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
}

/* ---------- Stage ---------- */
.stage-col { display: flex; flex-direction: column; gap: 16px; position: sticky; top: 76px; align-self: start; }
.stage {
  position: relative;
  aspect-ratio: 4 / 3;
  background: #06070a;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.preview { position: absolute; inset: 0; }
.preview canvas { width: 100% !important; height: 100% !important; display: block; }

.stage-hud { position: absolute; top: 12px; left: 12px; display: flex; gap: 8px; z-index: 5; pointer-events: none; }
.hud-chip {
  font-size: .74rem; font-weight: 600;
  background: rgba(6,7,10,.62); border: 1px solid rgba(255,255,255,.1);
  color: var(--text); padding: 5px 10px; border-radius: 999px;
  backdrop-filter: blur(4px);
}
#face-chip[data-on="true"] { color: #062; background: rgba(52,211,153,.9); border-color: transparent; }
#rec-chip { color: #fff; background: rgba(255,77,94,.9); border-color: transparent; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .55; } }

.loading {
  position: absolute; inset: 0; z-index: 10;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  background: rgba(6,7,10,.9);
  text-align: center; padding: 24px;
}
.loading.hidden { display: none; }
.loading.stopped .spinner, .loading.stopped .progress { display: none; }
.loading.stopped p { color: var(--text); max-width: 340px; }
.loading p { margin: 0; color: var(--text-dim); font-size: .9rem; }
.spinner {
  width: 42px; height: 42px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,.12); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.progress { width: 220px; height: 6px; background: rgba(255,255,255,.08); border-radius: 999px; overflow: hidden; }
.progress-bar { width: 8%; height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); border-radius: 999px; transition: width .3s ease; }

/* ---------- Capture results ---------- */
.results { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.results-head { display: flex; justify-content: space-between; align-items: center; font-weight: 600; font-size: .9rem; margin-bottom: 12px; }
.results-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 12px; }
.result-item { position: relative; border: 1px solid var(--line-2); border-radius: var(--radius-sm); overflow: hidden; background: #000; }
.result-item img, .result-item video { width: 100%; display: block; aspect-ratio: 4/3; object-fit: cover; }
.result-badge { position: absolute; top: 6px; left: 6px; font-size: .66rem; font-weight: 700; padding: 2px 7px; border-radius: 999px; background: rgba(0,0,0,.7); color: #fff; }
.result-dl {
  position: absolute; bottom: 6px; right: 6px;
  font-size: .68rem; font-weight: 700; text-decoration: none;
  background: var(--accent); color: #fff; padding: 4px 9px; border-radius: 999px;
}

/* ---------- Controls ---------- */
.controls { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 16px 18px; }
.panel-title { display: flex; align-items: center; gap: 9px; margin: 0 0 3px; font-size: 1rem; letter-spacing: -.01em; }
.panel-hint { margin: 0 0 14px; font-size: .78rem; color: var(--text-faint); }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.d1 { background: #7c5cff; } .d2 { background: #4f9dff; } .d3 { background: #34d399; }
.d4 { background: #fbbf24; } .d5 { background: #f472b6; } .d6 { background: #22d3ee; } .d7 { background: #94a3b8; }
.d8 { background: #ff6fae; }
.d9 { background: #c084fc; }

/* DeepAR Beauty live panel */
.warn-txt { color: var(--warn); font-weight: 600; }
.beauty-group {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-faint); margin: 18px 0 10px; padding-top: 12px;
  border-top: 1px solid var(--line);
}
.beauty-topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.beauty-topbar .hud-chip { position: static; background: rgba(192,132,252,.16); border-color: rgba(192,132,252,.4); color: var(--accent-2); }
.beauty-actions { display: flex; align-items: center; gap: 6px; }
#beauty-compare.is-active { background: var(--accent); color: #fff; border-color: transparent; }

/* effect grid */
.effect-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.effect-card {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 12px 6px; border: 1px solid var(--line-2); border-radius: var(--radius-sm);
  background: var(--panel-2); cursor: pointer; transition: .14s ease;
}
.effect-card:hover { border-color: var(--accent); background: #232a3b; }
.effect-card.is-active { border-color: var(--accent); background: rgba(124,92,255,.15); box-shadow: 0 0 0 1px var(--accent) inset; }
.effect-emoji { font-size: 1.7rem; line-height: 1; }
.effect-name { font-size: .74rem; color: var(--text-dim); text-align: center; }
.effect-card.is-active .effect-name { color: var(--text); }

/* segmented control */
.seg { display: grid; gap: 6px; background: var(--bg); border: 1px solid var(--line-2); border-radius: var(--radius-sm); padding: 4px; }
.seg-2 { grid-template-columns: repeat(2, 1fr); }
.seg-3 { grid-template-columns: repeat(3, 1fr); }
.seg-btn {
  appearance: none; border: 0; background: transparent; color: var(--text-dim);
  font: inherit; font-weight: 600; font-size: .86rem; padding: 8px; border-radius: 7px; cursor: pointer; transition: .14s;
}
.seg-btn:hover { color: var(--text); }
.seg-btn.is-active { background: var(--accent); color: #fff; box-shadow: 0 2px 8px var(--accent-glow); }

.sub-controls { margin-top: 14px; }
.range-row { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 10px 12px; font-size: .86rem; color: var(--text-dim); margin: 6px 0; }
.range-row.mt { margin-top: 12px; }
.range-row input[type=range] { grid-column: 1 / -1; width: 100%; accent-color: var(--accent); }
.range-row span { grid-column: 1; }
.range-row output { grid-column: 2; grid-row: 1; font-variant-numeric: tabular-nums; font-weight: 700; color: var(--text); }

.bg-thumbs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 12px; }
.bg-thumb { aspect-ratio: 1; border-radius: 9px; border: 2px solid transparent; cursor: pointer; background-size: cover; background-position: center; transition: .14s; }
.bg-thumb:hover { transform: scale(1.05); }
.bg-thumb.is-active { border-color: #fff; box-shadow: 0 0 0 2px var(--accent); }

.upload-row { display: inline-flex; align-items: center; justify-content: center; font-size: .84rem; cursor: pointer; }

.field-label + .select, .select { margin-top: 6px; }
.select {
  width: 100%; background: var(--bg); border: 1px solid var(--line-2); color: var(--text);
  border-radius: var(--radius-sm); padding: 10px 12px; font: inherit; font-size: .86rem; cursor: pointer;
}
.select:focus { outline: none; border-color: var(--accent); }

/* switches */
.switch-row { margin-top: 12px; }
.switch { display: flex; align-items: center; gap: 11px; cursor: pointer; user-select: none; }
.switch input { display: none; }
.switch-track { position: relative; width: 42px; height: 24px; border-radius: 999px; background: var(--line-2); transition: .18s; flex: none; }
.switch-track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: .18s; }
.switch input:checked + .switch-track { background: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(18px); }
.switch-label { font-size: .87rem; color: var(--text-dim); }

.btn-row { margin-bottom: 10px; }

/* telemetry */
.telemetry { display: flex; flex-direction: column; gap: 12px; }
.tele-row { display: flex; justify-content: space-between; align-items: center; font-size: .84rem; }
.tele-key { color: var(--text-dim); }
.tele-val { font-weight: 700; font-variant-numeric: tabular-nums; }
.tele-val.mono { font-size: .78rem; font-weight: 500; color: var(--text-dim); }
.gauges { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.gauge { text-align: center; font-size: .74rem; color: var(--text-faint); }
.gauge label { display: block; margin-bottom: 5px; }
.gauge-bar { position: relative; height: 6px; background: var(--bg); border-radius: 999px; overflow: hidden; margin-bottom: 4px; }
.gauge-bar i { position: absolute; top: 0; bottom: 0; left: 50%; width: 3px; background: var(--accent-2); border-radius: 2px; transform: translateX(-50%); transition: left .08s linear; }
.gauge span { font-weight: 700; color: var(--text-dim); font-variant-numeric: tabular-nums; }

/* log */
.log {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: .76rem; line-height: 1.6;
  background: var(--bg); border: 1px solid var(--line-2); border-radius: var(--radius-sm);
  padding: 10px 12px; height: 168px; overflow-y: auto; color: var(--text-dim);
}
.log-line { white-space: pre-wrap; word-break: break-word; }
.log-time { color: var(--text-faint); }
.log-call { color: var(--accent-2); }
.log-err { color: var(--bad); }
.log-ok { color: var(--good); }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%) translateY(20px);
  background: var(--panel-2); border: 1px solid var(--line-2); color: var(--text);
  padding: 12px 18px; border-radius: 999px; box-shadow: var(--shadow);
  font-size: .88rem; z-index: 60; opacity: 0; transition: .25s ease; pointer-events: none; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { border-color: rgba(248,113,113,.5); }
.toast.ok { border-color: rgba(52,211,153,.5); }

/* ---------- Responsive ---------- */
@media (max-width: 940px) {
  .layout { grid-template-columns: 1fr; }
  .stage-col { position: static; }
}
@media (max-width: 520px) {
  .layout { padding: 14px; gap: 14px; }
  .onboarding-card { padding: 24px; }
  .effect-grid { grid-template-columns: repeat(3, 1fr); }
}

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--line-2); border-radius: 999px; border: 2px solid var(--bg); }
::-webkit-scrollbar-track { background: transparent; }
