:root {
  --bg: #0b0b0d;
  --panel-bg: #1a1a1e;
  --panel-border: #2c2c32;
  --text: #f2f2f4;
  --muted: #9a9aa2;
  --accent: #4da3ff;
  --accent-strong: #2f8bef;
  --danger: #ff4d4d;
  --toolbar-w: 96px;
  --toolbar-w-collapsed: 56px;
}

* { box-sizing: border-box; }

/* A plain `display: flex` (etc.) declared anywhere below on an element's
   class beats the browser's default [hidden] { display: none } rule, since
   author-normal CSS always wins over user-agent-normal CSS regardless of
   specificity. Several panels/overlays here do exactly that, so without
   this !important, toggling .hidden = true in JS silently does nothing. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

button { font-family: inherit; }

/* The raw camera feed is never shown directly — the Compositor draws it
   onto #stage — but it must stay a real rendered (just invisible) element,
   not display:none, or some browsers throttle/never start its autoplay. */
.source-video {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.app {
  position: relative;
  display: flex;
  height: 100vh;
  width: 100vw;
}

.stage-wrap {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  background: #000;
  overflow: hidden;
}

#stage {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: default;
}

.app.tool-active #stage { cursor: crosshair; }
.app.pannable #stage { cursor: grab; }

/* ---------- Right-side toolbar ---------- */

.toolbar {
  width: var(--toolbar-w);
  flex: 0 0 auto;
  background: var(--panel-bg);
  border-left: 1px solid var(--panel-border);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: 8px 6px;
  gap: 4px;
  overflow-y: auto;
  transition: width 0.15s ease;
}

.app.collapsed .toolbar { width: var(--toolbar-w-collapsed); }
.app.collapsed .tbtn .lbl { display: none; }

.tb-spacer { flex: 1 1 auto; }

.tbtn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 56px;
  padding: 6px 2px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
  color: var(--text);
  cursor: pointer;
  font-size: 11px;
  line-height: 1.1;
}
.tbtn .ic { font-size: 20px; }
.tbtn:hover { background: #26262c; }
.tbtn.active, .tbtn[aria-pressed="true"] { background: #26364a; border-color: var(--accent); }
.tbtn.primary { background: #35251f; }
.tbtn.primary .ic { color: var(--danger); }
.tbtn.recording { background: var(--danger); }
.tbtn.recording .ic { color: #fff; }
.collapse-btn { min-height: 36px; }

/* ---------- Docked panels ----------
   A panel is a real flex sibling between the stage and the toolbar (via
   `order`, regardless of DOM position) rather than an overlay floating on
   top of the canvas — so opening it shrinks the camera/grid area instead
   of covering part of it (which used to hide/obscure content, e.g. the
   upper-right comparison image, underneath the panel). */

.stage-wrap { order: 1; }
.toolbar { order: 3; }

.panel {
  order: 2;
  flex: 0 0 260px;
  width: 260px;
  overflow-y: auto;
  background: var(--panel-bg);
  border-left: 1px solid var(--panel-border);
  padding: 14px;
}
.panel h3 { margin: 0 0 10px; font-size: 14px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; font-size: 13px; }
.field select, .field input[type="text"] {
  background: #0f0f12; color: var(--text); border: 1px solid var(--panel-border);
  border-radius: 6px; padding: 6px 8px; font-size: 13px;
}
.field input[type="range"] { width: 100%; }
.field input[type="color"] { width: 100%; height: 32px; border: none; background: none; padding: 0; }
.field-row { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 10px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.btn-row { display: flex; gap: 6px; margin-bottom: 10px; }
.btn-row.wrap { flex-wrap: wrap; }

.pill-btn {
  background: #26262c; color: var(--text); border: 1px solid var(--panel-border);
  border-radius: 999px; padding: 8px 12px; font-size: 12px; cursor: pointer; flex: 1 1 auto;
  min-height: 36px;
}
.pill-btn:hover { background: #303038; }
.pill-btn.full { width: 100%; }
.pill-btn.primary { background: var(--accent-strong); border-color: var(--accent-strong); }
.pill-btn.active { background: var(--accent-strong); border-color: var(--accent-strong); }

.slot-label-editor { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--panel-border); font-size: 13px; }
.slot-label-editor input { background: #0f0f12; color: var(--text); border: 1px solid var(--panel-border); border-radius: 6px; padding: 6px 8px; }

/* ---------- Badges / HUD ---------- */

.badge {
  position: absolute; top: 14px; left: 14px; z-index: 15;
  background: rgba(0,0,0,0.6); color: #fff; padding: 6px 12px; border-radius: 999px;
  font-size: 13px; font-weight: 600; letter-spacing: 0.03em;
}
.badge-frozen { color: #bfe4ff; }
.badge-rec { display: flex; align-items: center; gap: 6px; }
.badge-rec .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1.2s infinite; }
@keyframes pulse { 0%,100%{opacity:1;} 50%{opacity:0.35;} }

.hud {
  position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,0.65); color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 14px; display: flex; align-items: center; gap: 14px; z-index: 15;
}
.link-btn { background: none; border: none; color: var(--accent); cursor: pointer; font-size: 13px; padding: 0; }

.capture-flash {
  position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; z-index: 25;
}
.capture-flash.flashing { animation: flash 0.25s ease-out; }
@keyframes flash { 0% { opacity: 0.85; } 100% { opacity: 0; } }

.last-capture {
  position: absolute; bottom: 16px; right: 16px; z-index: 15;
  width: 140px; background: var(--panel-bg); border: 1px solid var(--panel-border);
  border-radius: 10px; overflow: hidden; box-shadow: 0 6px 18px rgba(0,0,0,0.4);
}
.last-capture img { width: 100%; display: block; }
.last-capture-actions { display: flex; }
.mini-btn { flex: 1; background: #26262c; color: var(--text); border: none; border-top: 1px solid var(--panel-border); padding: 6px 0; font-size: 12px; cursor: pointer; }
.mini-btn:hover { background: #303038; }

.toast {
  position: absolute; top: 14px; left: 50%; transform: translateX(-50%); z-index: 30;
  background: rgba(20,20,24,0.92); color: #fff; padding: 8px 16px; border-radius: 8px; font-size: 13px;
}

.error-banner {
  position: absolute; top: 14px; left: 50%; transform: translateX(-50%); z-index: 40;
  background: #3a1414; border: 1px solid #6b1f1f; color: #ffd6d6; padding: 10px 18px;
  border-radius: 8px; font-size: 13px; max-width: 80%; text-align: center;
}

/* ---------- Permission gate ---------- */

.gate {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: #000; z-index: 50;
}
.gate-card { text-align: center; max-width: 420px; padding: 24px; }
.gate-card h1 { font-size: 22px; font-weight: 600; margin: 0 0 8px; }
.gate-card p { color: var(--muted); margin: 0 0 20px; }
.gate-start-btn {
  background: var(--accent-strong); color: #fff; border: none; border-radius: 999px;
  padding: 14px 28px; font-size: 16px; cursor: pointer;
}
.gate-start-btn:hover { background: var(--accent); }
.gate-error { color: #ffb3b3; margin-top: 16px; }

/* ---------- Comparison size modal ---------- */

.modal {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.6); z-index: 45;
}
.modal-card { background: var(--panel-bg); border: 1px solid var(--panel-border); border-radius: 14px; padding: 24px; text-align: center; }
.modal-card h2 { margin: 0 0 16px; font-size: 16px; }
.grid-size-options { display: grid; grid-template-columns: repeat(3, 64px); gap: 10px; margin-bottom: 16px; }
.grid-size-options button {
  background: #26262c; color: var(--text); border: 1px solid var(--panel-border); border-radius: 10px;
  height: 56px; font-size: 18px; cursor: pointer;
}
.grid-size-options button:hover { background: var(--accent-strong); }

.exit-presentation {
  position: absolute; top: 12px; right: 12px; z-index: 60;
  background: rgba(0,0,0,0.55); color: #fff; border: 1px solid rgba(255,255,255,0.3);
  border-radius: 999px; padding: 8px 14px; font-size: 13px; cursor: pointer;
  opacity: 0; transition: opacity 0.2s;
}
.app.presenting .exit-presentation { opacity: 0.15; }
.app.presenting .exit-presentation:hover,
.app.presenting .exit-presentation:focus { opacity: 1; }

.app.presenting .toolbar,
.app.presenting .panel {
  display: none !important;
}

/* ---------- Responsive ---------- */

@media (max-width: 700px) {
  :root { --toolbar-w: 76px; }
  .tbtn .lbl { font-size: 9px; }
  .panel { flex-basis: min(220px, 60vw); width: min(220px, 60vw); }
}
