:root {
  color-scheme: dark;
  --bg-0: #07070a;
  --bg-1: #0f0f14;
  --bg-2: #16161d;
  --bg-3: #1d1d26;
  --line: rgba(255, 255, 255, 0.08);
  --line-strong: rgba(255, 255, 255, 0.16);
  --text: #f5f5f7;
  --muted: #8a8a95;
  --muted-2: #5d5d68;
  --accent: #7c5cff;
  --accent-2: #4ad7ff;
  --accent-grad: linear-gradient(135deg, #7c5cff 0%, #4ad7ff 100%);
  --ok: #34d399;
  --warn: #fbbf24;
  --err: #f87171;
  --shadow-lg: 0 24px 80px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(1200px 600px at 20% -10%, rgba(124, 92, 255, 0.18), transparent 60%),
    radial-gradient(900px 500px at 110% 10%, rgba(74, 215, 255, 0.12), transparent 55%),
    var(--bg-0);
  color: var(--text);
  font: 14px/1.5 "Inter", "SF Pro Text", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button, input, select, textarea { font: inherit; color: inherit; }

/* ============== Layout ============== */
.app {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(10, 10, 14, 0.7);
  backdrop-filter: blur(20px);
  z-index: 5;
}
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}
.brand-logo {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--accent-grad);
  position: relative;
  box-shadow: 0 6px 24px rgba(124, 92, 255, 0.45);
}
.brand-logo::after {
  content: "";
  position: absolute;
  inset: 6px;
  border-radius: 6px;
  background: var(--bg-1);
}
.brand h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.brand .subtitle {
  margin: 2px 0 0;
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.workspace {
  display: grid;
  grid-template-columns: 360px 1fr;
  min-height: 0;
}

/* ============== Sidebar ============== */
.sidebar {
  border-right: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(15, 15, 20, 0.85), rgba(11, 11, 14, 0.95));
  overflow-y: auto;
  padding: 18px 16px 24px;
  scrollbar-width: thin;
  scrollbar-color: var(--bg-3) transparent;
}
.sidebar::-webkit-scrollbar { width: 8px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--bg-3); border-radius: 8px; }

.section {
  border: 1px solid var(--line);
  background: var(--bg-1);
  border-radius: 16px;
  margin-bottom: 12px;
  overflow: hidden;
}
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  cursor: pointer;
  user-select: none;
}
.section-head h3 {
  margin: 0;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.section-head .chev {
  color: var(--muted);
  transition: transform 0.2s ease;
  font-size: 12px;
}
.section.collapsed .section-body { display: none; }
.section.collapsed .chev { transform: rotate(-90deg); }
.section-body { padding: 4px 14px 14px; }

label {
  display: block;
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 12px 0 6px;
}
label:first-child { margin-top: 4px; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row.three { grid-template-columns: 1fr 1fr 1fr; }

input[type="text"], input[type="number"], select, textarea {
  width: 100%;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 11px;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s ease, background 0.15s ease;
}
input:focus, select:focus, textarea:focus {
  border-color: rgba(124, 92, 255, 0.5);
  background: var(--bg-3);
}
textarea {
  min-height: 96px;
  resize: vertical;
  line-height: 1.45;
}
input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}
.value-pill {
  display: inline-block;
  background: var(--bg-3);
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  color: var(--text);
  margin-left: 6px;
  font-variant-numeric: tabular-nums;
}

/* Chips */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  background: var(--bg-2);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 6px 11px;
  border-radius: 999px;
  font-size: 12px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.chip:hover { border-color: var(--line-strong); background: var(--bg-3); }
.chip.active {
  background: rgba(124, 92, 255, 0.18);
  border-color: rgba(124, 92, 255, 0.5);
  color: var(--text);
}

/* Preset segmented */
.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 4px;
  gap: 4px;
}
.segmented button {
  background: transparent;
  border: 0;
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  transition: background 0.15s, color 0.15s;
}
.segmented button.active { background: var(--bg-3); color: var(--text); }

/* Reference uploader */
.dropzone {
  border: 1.5px dashed var(--line-strong);
  border-radius: 12px;
  padding: 18px 12px;
  text-align: center;
  color: var(--muted);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, color 0.15s;
}
.dropzone:hover, .dropzone.dragover {
  border-color: var(--accent);
  color: var(--text);
  background: rgba(124, 92, 255, 0.06);
}
.dropzone .icon { font-size: 22px; margin-bottom: 4px; opacity: 0.7; }
.dropzone .small { font-size: 11px; margin-top: 4px; opacity: 0.7; }

.ref-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 6px;
  margin-top: 10px;
}
.ref-tile {
  position: relative;
  aspect-ratio: 1;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: var(--bg-2);
}
.ref-tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ref-tile .rm {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  border: 0;
  cursor: pointer;
  font-size: 12px;
  line-height: 22px;
  padding: 0;
  display: grid;
  place-items: center;
}
.ref-tile .rm:hover { background: var(--err); }

/* ============== Stage ============== */
.stage-wrap {
  position: relative;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}
.stage {
  flex: 1;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: #050507;
  overflow: hidden;
  position: relative;
  box-shadow: var(--shadow-lg);
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  place-items: stretch;
}
/* Single pane mode (default) */
.stage .pane {
  position: relative;
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  background: #050507;
  overflow: hidden;
  min-height: 0;
  min-width: 0;
}
.stage .pane video {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  display: block;
}
/* In single mode hide the camera pane; only output is shown */
.stage:not(.split) .pane-camera { display: none; }
.stage:not(.split) .pane-label { display: none; }
/* When all panes are hidden (e.g. before stream), placeholder shows */
.stage .placeholder {
  grid-area: 1 / 1 / 2 / 2;
  align-self: center;
  justify-self: center;
  color: var(--muted);
  text-align: center;
  padding: 32px;
  max-width: 460px;
  pointer-events: none;
}
.stage .pane { grid-area: 1 / 1 / 2 / 2; }

/* Side-by-side split */
.stage.split {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr;
  gap: 2px;
  background: var(--line);
}
.stage.split .pane-camera { grid-column: 1; grid-row: 1; }
.stage.split .pane-output { grid-column: 2; grid-row: 1; }
.stage.split .placeholder { display: none; }

.stage .placeholder .big {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 8px;
}
.pane-label {
  position: absolute;
  top: 12px;
  left: 12px;
  background: rgba(0, 0, 0, 0.55);
  color: var(--text);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  backdrop-filter: blur(8px);
  pointer-events: none;
  z-index: 3;
}

/* Loader overlay (model warmup / re-compile) */
.loader {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: grid;
  place-items: center;
  background: rgba(5, 5, 7, 0.78);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  animation: fadeIn 0.18s ease;
}
.loader[hidden] { display: none !important; }
.loader-card {
  display: grid;
  justify-items: center;
  gap: 14px;
  padding: 28px 36px;
  border-radius: 20px;
  background: rgba(15, 15, 20, 0.85);
  border: 1px solid var(--line);
  max-width: 380px;
  text-align: center;
  box-shadow: var(--shadow-lg);
}
.loader-spinner {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.08);
  border-top-color: var(--accent);
  border-right-color: var(--accent-2);
  animation: spin 0.9s linear infinite;
}
.loader-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: 0.02em;
}
.loader-sub {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* Floating overlay metrics */
.overlay {
  position: absolute;
  top: 14px;
  right: 14px;
  display: flex;
  gap: 6px;
  align-items: center;
  z-index: 4;
}
.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(0, 0, 0, 0.6);
  color: var(--text);
  padding: 5px 11px;
  border-radius: 999px;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  backdrop-filter: blur(8px);
  border: 1px solid var(--line);
}
.dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--muted-2);
}
.dot.ok { background: var(--ok); box-shadow: 0 0 12px var(--ok); }
.dot.warn { background: var(--warn); }
.dot.err { background: var(--err); box-shadow: 0 0 12px var(--err); }
.dot.connecting { background: var(--accent-2); animation: pulse 1.2s infinite; }
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.7); }
}

/* Footer status bar */
.statusbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 12px;
  background: var(--bg-1);
  border: 1px solid var(--line);
  border-radius: 12px;
  font-size: 12px;
  color: var(--muted);
}
.statusbar .left { display: flex; gap: 14px; align-items: center; }
.statusbar .left strong { color: var(--text); font-weight: 600; }

/* Buttons */
.btn {
  border: 1px solid var(--line);
  background: var(--bg-2);
  color: var(--text);
  padding: 9px 16px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover:not(:disabled) { background: var(--bg-3); border-color: var(--line-strong); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary {
  background: var(--accent-grad);
  color: #0c0c12;
  border-color: transparent;
  box-shadow: 0 8px 28px rgba(124, 92, 255, 0.35);
}
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn.danger {
  background: rgba(248, 113, 113, 0.14);
  color: var(--err);
  border-color: rgba(248, 113, 113, 0.35);
}
.btn.danger:hover:not(:disabled) { background: rgba(248, 113, 113, 0.22); }
.btn.ghost { background: transparent; }
.btn.icon { padding: 8px; }

.icon-btn {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  color: var(--muted);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.15s, color 0.15s;
}
.icon-btn:hover { background: var(--bg-3); color: var(--text); }

/* Login */
.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-1);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 28px;
  box-shadow: var(--shadow-lg);
  text-align: center;
}
.login-card .logo {
  width: 56px;
  height: 56px;
  border-radius: 14px;
  background: var(--accent-grad);
  margin: 0 auto 18px;
  position: relative;
  box-shadow: 0 12px 36px rgba(124, 92, 255, 0.4);
}
.login-card .logo::after {
  content: "";
  position: absolute;
  inset: 12px;
  border-radius: 8px;
  background: var(--bg-1);
}
.login-card h1 { margin: 0; font-size: 20px; letter-spacing: -0.01em; }
.login-card p { color: var(--muted); font-size: 13px; margin: 6px 0 22px; }
.login-card input { text-align: center; letter-spacing: 0.18em; font-weight: 600; }
.login-card .btn { width: 100%; justify-content: center; margin-top: 14px; }
.login-card .err {
  color: var(--err);
  font-size: 12px;
  margin-top: 12px;
  min-height: 14px;
}

/* Toast */
.toast-host {
  position: fixed;
  right: 18px;
  bottom: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  pointer-events: none;
}
.toast {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 13px;
  color: var(--text);
  pointer-events: auto;
  box-shadow: var(--shadow-lg);
  animation: slideIn 0.2s ease;
  max-width: 360px;
}
.toast.err { border-color: rgba(248, 113, 113, 0.5); }
.toast.ok { border-color: rgba(52, 211, 153, 0.5); }
@keyframes slideIn {
  from { transform: translateY(8px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* ============== Responsive ============== */

/* Tablet: sidebar narrower, allow wrap of top actions */
@media (max-width: 1100px) {
  .workspace { grid-template-columns: 300px 1fr; }
  .topbar { padding: 12px 16px; }
}

/* Mobile: stage on top, sidebar collapsible drawer below */
@media (max-width: 820px) {
  .app { height: auto; min-height: 100vh; }

  /* Topbar: brand + hamburger row, actions row */
  .topbar {
    flex-wrap: wrap;
    gap: 10px;
    padding: 10px 12px;
    position: sticky;
    top: 0;
  }
  .brand h1 { font-size: 15px; }
  .brand .subtitle { font-size: 10px; }
  .top-actions {
    width: 100%;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 8px;
  }
  .top-actions .btn { padding: 10px 14px; font-size: 13px; flex: 1 1 auto; justify-content: center; min-height: 40px; }
  .top-actions .icon-btn { width: 36px; height: 36px; }

  /* Stack: stage first, sidebar second */
  .workspace {
    display: flex;
    flex-direction: column-reverse;
    min-height: 0;
  }
  .stage-wrap {
    padding: 10px;
    gap: 8px;
    min-height: 60vh;
  }
  .stage {
    border-radius: 16px;
    min-height: 50vh;
  }
  .stage .placeholder { padding: 18px; max-width: 320px; }
  .stage .placeholder .big { font-size: 17px; }

  .sidebar {
    border-right: 0;
    border-top: 1px solid var(--line);
    padding: 12px 12px 24px;
    max-height: none;
    overflow: visible;
  }

  /* Sections collapsed by default on mobile (controlled via JS too,
     but provide visual cues if all open) */
  .section { margin-bottom: 10px; border-radius: 14px; }
  .section-head { padding: 14px 14px; min-height: 48px; }

  /* Touch-friendly form controls */
  input[type="text"],
  input[type="number"],
  input[type="password"],
  select,
  textarea {
    font-size: 16px; /* prevents iOS zoom on focus */
    padding: 11px 12px;
    border-radius: 12px;
  }
  textarea { min-height: 88px; }
  .segmented button { padding: 11px 8px; font-size: 12px; min-height: 40px; }
  .chip { padding: 9px 14px; font-size: 13px; }
  .btn { min-height: 42px; }

  .ref-grid { grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); }

  /* Stage overlay pills smaller */
  .overlay { top: 8px; right: 8px; gap: 4px; }
  .pill { font-size: 10px; padding: 4px 8px; }
  .pane-label { font-size: 10px; padding: 3px 8px; top: 8px; left: 8px; }

  /* Status bar wraps */
  .statusbar {
    flex-wrap: wrap;
    gap: 6px;
    font-size: 11px;
    padding: 8px 10px;
  }
  .statusbar .left { gap: 10px; flex-wrap: wrap; }

  /* Loader scales */
  .loader-card { padding: 22px 24px; max-width: 88vw; }
  .loader-spinner { width: 44px; height: 44px; border-width: 3px; }
  .loader-title { font-size: 14px; }
  .loader-sub { font-size: 12px; }

  /* Toasts full-width-ish at bottom */
  .toast-host {
    right: 8px;
    left: 8px;
    bottom: 8px;
    align-items: stretch;
  }
  .toast { max-width: none; }

  /* Split view becomes stacked on mobile */
  .stage.split {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }
  .stage.split .pane-camera { grid-column: 1; grid-row: 1; }
  .stage.split .pane-output { grid-column: 1; grid-row: 2; }
}

/* Very small phones */
@media (max-width: 420px) {
  .brand .subtitle { display: none; }
  .top-actions .btn span,
  .top-actions .btn { font-size: 12px; }
  .stage-wrap { padding: 8px; }
  .stage { min-height: 45vh; }
}

/* Landscape phones: keep stage prominent */
@media (max-width: 820px) and (orientation: landscape) and (max-height: 500px) {
  .stage-wrap { min-height: 80vh; }
  .stage { min-height: 70vh; }
}
