/* =======================================================================
   Design Tokens / CSS Variables
   All color values centralized for fast theming & brand adjustments.
   GROUPS:
     Brand Core, Text, Neutrals/Overlays, Accent Tints, States, Panels,
     Borders, Inputs, Shadows, Utilities
======================================================================= */
:root {
  /* ===== Base System & Layout ===== */
  color-scheme: dark light;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --top-bar-height: 3.25rem; /* unified header height */
  --status-chip-width: 12.5rem; /* keep chip width stable (adjust as needed) */
  --max-width: min(1200px, 100rem); /* max width for centered content */

  /* ===== Brand Core (Household Range palette) ===== */
  --brand-bg: #2b261f;                 /* Primary app body background (deep) */
  --brand-bg-darker: #2b261f;           /* Darker background for deeper layers */
  --brand-surface: #2b261f;            /* Canvas / panels deepest surface */
  --brand-accent: #5a5244;             /* Warm primary accent (buttons, highlights) c8a05b */
  --brand-accent-hover: #887350;       /* Lighter hover accent  ebc882*/
  --brand-accent-focus: #6b6253;       /* Focus border accent (neutral mid) */
  --brand-accent-rgb: 200,160,91;      /* RGB helper for dynamic accent tints */
  --brand-slate: #494135;              /* Hover / interactive neutral (replaces slate) */
  --brand-accent-focus-ring: rgba(200,160,91,0.35); /* Focus ring subtle glow */
  
  /* Unused for now */
  --brand-accent-soft: #f7ebd8;        /* Soft background accent */
  --brand-accent-soft-rgb: 247,235,216;/* Soft accent RGB if needed */
  --glass-highlight: rgba(255,255,255,0.18); /* Glass panel top gradient stop */
  --glass-border: rgba(255,255,255,0.32);    /* Glass border */
  --glass-shadow: rgba(0,0,0,0.35);          /* Glass shadow base */

  /* ===== Brand Gradients ===== */
  /* Page background gradient (matches landing HTML feel) */
  --brand-bg-gradient: radial-gradient(circle at top left, #5a5244 0%, #2b261f 55%, #18130d 100%);
  /* Canvas/surface gradient - subtle depth from bg to deepest surface */
  --brand-surface-gradient: linear-gradient(135deg, #2b261f 0%, #18130d 100%);

  /* ===== Text Colors ===== */
  --text-primary: #f8f8ff;             /* Primary foreground text */
  --text-secondary: #ddd2c2;           /* Secondary headings / subtle labels */
  --text-muted: #c5c5f5;               /* Notes / muted info */
  --text-light: #e0e0e0;               /* Light neutral (labels) */
  --text-warn-alt: #f1c40f;            /* Status warning (list item) */
  --text-error-alt: #ff6b6b;           /* Status error highlight */

  /* ===== Neutral Overlays (translucent white on dark) ===== */
  --overlay-04: rgba(255,255,255,0.04);
  --overlay-05: rgba(255,255,255,0.05);
  --overlay-06: rgba(255,255,255,0.06);
  --overlay-08: rgba(255,255,255,0.08);
  --overlay-10: rgba(255,255,255,0.10);
  --overlay-12: rgba(255,255,255,0.12); /* Standard tile border */
  --overlay-15: rgba(255,255,255,0.15);
  --overlay-16: rgba(255,255,255,0.16);
  --overlay-18: rgba(255,255,255,0.18);
  --overlay-25: rgba(255,255,255,0.25); /* Scrollbar thumb base */
  --overlay-40: rgba(255,255,255,0.40); /* Scrollbar thumb hover */

  /* ===== Accent (Dynamic from --brand-accent) Tints ===== */
  --accent-tint-15: rgba(var(--brand-accent-rgb),0.15);
  --accent-tint-18: rgba(var(--brand-accent-rgb),0.18);
  --accent-tint-20: rgba(var(--brand-accent-rgb),0.20);
  --accent-tint-30: rgba(var(--brand-accent-rgb),0.30);
  --accent-tint-35: rgba(var(--brand-accent-rgb),0.35);
  --accent-tint-40: rgba(var(--brand-accent-rgb),0.40);
  --accent-tint-45: rgba(var(--brand-accent-rgb),0.45);

  /* ===== State Colors (Success / Warn / Error) ===== */
  --state-success: #86efac;
  --state-success-bg: rgba(34,197,94,0.15);
  --state-success-border: rgba(34,197,94,0.4);
  --state-warn: #facc15;
  --state-warn-bg: rgba(245,158,11,0.15);
  --state-warn-border: rgba(245,158,11,0.4);
  --state-error: #f87171;
  --state-error-bg: rgba(239,68,68,0.15);
  --state-error-border: rgba(239,68,68,0.4);

  /* ===== Panels & Layered Backgrounds ===== */
  --panel-overlay: rgba(15,23,42,0.25); /* Floating panel shadow color */
  --panel-bg-deep: rgba(15,23,42,0.92); /* Dropdown / overlay body */

  /* ===== Borders ===== */
  --border-default: var(--overlay-12);
  --border-strong: var(--overlay-18);
  --border-subtle: var(--overlay-10);

  /* ===== Inputs ===== */
  --input-bg: #2a2a2a;                 /* Numeric & text field bg */
  --input-border: #555;                /* Input default border */
  --input-focus-border: var(--brand-accent-focus);

  /* ===== Shadows & Backdrops ===== */
  --shadow-20: rgba(0,0,0,0.2);
  --shadow-30: rgba(0,0,0,0.3);
  --shadow-35: rgba(0,0,0,0.35);
  --shadow-40: rgba(0,0,0,0.4);
  --shadow-50: rgba(0,0,0,0.5);
  --backdrop-dim: rgba(0,0,0,0.6);

  /* ===== Utilities ===== */
  --focus-ring: var(--accent-tint-35);

  /* Apply body colors (solid fallback, then gradient) */
  background: var(--brand-bg);
  background: var(--brand-bg-gradient);
  color: var(--text-primary);
}

body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  background: var(--overlay-05);
  border-bottom: 1px solid var(--border-default);
  backdrop-filter: blur(18px);
  position: relative;
  z-index: 10; /* keep overlays above workspace */
}

.header-inner {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 0 1rem; /* remove vertical padding so tiles can touch header top/bottom */
  height: var(--top-bar-height);
  display: flex;
  align-items: stretch;
  gap: 0; /* brand occupies left half, buttons right half */
}

.brand {
  display: flex;
  align-items: center;
  height: 100%;
  box-sizing: border-box;
  flex: 1 0; /* stretch to half width */
  /*padding-right: 0rem;  visual breathing room before midpoint */
}

/* Logo inside header */
.brand-logo {
  height: calc(var(--top-bar-height) - 0.5rem);
  width: auto;
  display: block;
  margin-right: 0.75rem;
  object-fit: contain;
  filter: drop-shadow(0 4px 10px var(--shadow-35));
}

.brand h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
}

.status-bar {
  display: flex;
  flex: 1 0; /* occupy right half */
  height: 100%;
  align-items: stretch;
  justify-content: space-evenly; /* evenly space tiles across right half */
  gap: 0;
}

@media (max-width: 800px) {
  .brand { flex: 1 1 100%; padding-right: 0.75rem; }
  .status-bar { flex: 1 1 100%; justify-content: flex-start; gap: 0.75rem; }
}

.status-entry {
  position: relative;
  background: var(--overlay-06);
  border: 1px solid var(--border-default);
  /* border-radius: 10px; */
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 100%;
  box-sizing: border-box;
  padding: 0; /* no vertical padding so summary fills */
  transition: border-color 0.2s ease, background 0.2s ease, box-shadow 0.2s ease;
}

.status-entry:hover,
.status-entry:focus-within {
  border-color: var(--accent-tint-40);
  background: var(--accent-tint-18);
  box-shadow: 0 8px 24px var(--accent-tint-20);
}

.status-entry:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--focus-ring);
}

.status-entry[open] {
  border-color: var(--accent-tint-45);
  background: var(--accent-tint-15);
  box-shadow: 0 10px 30px var(--panel-overlay);
}

.status-summary {
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  height: 100%;
  padding: 0 1rem;
  box-sizing: border-box;
  line-height: 1; /* remove extra line height vertical space */
}

/* Make the chip visually centered with full-height buttons */
.status-summary .chip {
  height: calc(var(--top-bar-height) - 14px); /* account for borders & visual breathing */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--status-chip-width);
  box-sizing: border-box;
  white-space: nowrap;
}

.status-summary::-webkit-details-marker {
  display: none;
}

.status-title {
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.status-content {
  margin-top: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* Drop-down overlay: when a tile is open, render its content as an absolute overlay
   so the status bar height does not expand */
.status-entry[open] > .status-content {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  width: auto;
  margin-top: 0;
  background: var(--panel-bg-deep);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  box-shadow: 0 16px 40px var(--shadow-35);
  padding: 0.85rem 1rem;
  z-index: 20;
  box-sizing: border-box;
  overflow: hidden;
}

/* Prevent internal controls from forcing a wider overlay than the trigger */
.status-entry[open] .camera-toolbar select {
  min-width: 0;
  width: 100%;
}

.status-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
}

.workspace {
  flex: 1;
  width: 100%;
  padding: 2.5rem 1rem 3rem;
  display: flex;
  justify-content: center;
  box-sizing: border-box;
}

.workspace-inner {
  width: 100%;
  max-width: var(--max-width);;
}

.container {
  max-width: var(--max-width);;
  margin: 0 auto;
  padding: 2rem 1rem 3rem;
}

header p {
  max-width: 60ch;
}

.note {
  font-size: 0.9rem;
  color: var(--text-muted);
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 1.5rem;
}

button {
  padding: 0.6rem 1.2rem;
  border-radius: 6px;
  border: none;
  background: var(--brand-accent);
  color: var(--text-primary);
  cursor: pointer;
  font-weight: 600;
}

button[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Utilities */
.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

/* Cards & checklist */
.checklist {
  margin-top: 1.5rem;
}

.cards-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

@media (min-width: 920px) {
  .cards-grid { grid-template-columns: 1fr 1fr; }
}

.card {
  background: var(--overlay-06);
  border: 1px solid var(--border-default);
  border-radius: 16px;
  padding: 1rem;
  box-shadow: 0 4px 20px var(--shadow-20);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.card-sub {
  margin: 0.25rem 0 0.5rem;
  color: var(--text-secondary);
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.6rem;
  border-radius: 999px;
  font-size: 0.85rem;
  line-height: 1;
  border: 1px solid transparent;
}

.chip[data-state="ok"] {
  background: var(--state-success-bg);
  border-color: var(--state-success-border);
  color: var(--state-success);
}

.chip[data-state="warn"] {
  background: var(--state-warn-bg);
  border-color: var(--state-warn-border);
  color: var(--state-warn);
}

.chip[data-state="error"] {
  background: var(--state-error-bg);
  border-color: var(--state-error-border);
  color: var(--state-error);
}

.status ul,
.status li {
  list-style: none;
  padding: 0;
  margin: 0;
}

.status {
  margin-top: 2rem;
  background: var(--overlay-05);
  border-radius: 12px;
  padding: 1rem;
}

.status li[data-level="info"] {
  color: var(--text-primary);
}

.status li[data-level="warn"] {
  color: var(--text-warn-alt);
}

.status li[data-level="error"] {
  color: var(--text-error-alt);
}

.command-panel {
  margin-top: 2rem;
}

#command-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

#command-input {
  padding: 0.6rem 0.8rem;
  border-radius: 6px;
  border: 1px solid var(--overlay-15);
  background: var(--overlay-05);
  color: inherit;
}

.log {
  margin-top: 2rem;
}

#log-output {
  background: var(--shadow-50);
  border-radius: 10px;
  padding: 1rem;
  min-height: 240px;
  max-height: 480px;
  overflow-y: auto;
  white-space: pre-wrap;
}

.camera {
  margin-top: 0;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.camera-toolbar,
.camera-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
}

.camera-toolbar label {
  font-weight: 600;
}

#camera-select {
  min-width: 220px;
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  border: 1px solid var(--overlay-25);
  background: var(--overlay-08);
  color: var(--text-primary); /* Improve contrast so text is visible before opening */
}

/* Force dark dropdown list for the native menu */
.camera-toolbar select option,
#camera-select option {
  background-color: var(--brand-surface);
  color: var(--text-primary);
}

/* Highlight states inside the dropdown */
.camera-toolbar select option:checked,
.camera-toolbar select option:hover,
#camera-select option:checked,
#camera-select option:hover {
  background-color: var(--brand-slate);
  color: var(--text-primary);
}

/* Optional: focus ring to match dark theme */
#camera-select:focus {
  outline: none;
  border-color: var(--brand-accent);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

.camera-toolbar button {
  padding: 0.5rem 0.9rem;
}

/* Icon-only small button */
.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 8px;
  background: var(--overlay-10);
  border: 1px solid var(--overlay-16);
}
.icon-button:hover { background: var(--overlay-16); }
.icon-button svg { color: var(--text-primary); width: 20px; height: 20px; }


/* Hide the Stop Camera button but keep it in the DOM for logic */
#camera-stop { display: none !important; }

.camera-preview {
  margin-top: 1.25rem;
}

.details {
  margin: 0;
}

/* Details */
.details details {
  margin-top: 1.5rem;
  background: var(--overlay-04);
  border: 1px solid var(--border-default);
  border-radius: 12px;
  padding: 0.5rem 0.75rem;
}

.details summary {
  cursor: pointer;
  font-weight: 600;
}

/* ===== App Stage (dual canvases) ===== */
.app-stage { margin-bottom: 1.5rem; }
.stage-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}
.stage-pane {
  background: var(--overlay-04);
  border: 1px solid var(--border-default);
  border-radius: 12px;
  padding: 0.75rem;
  min-width: 0;
}
.stage-title { margin: 0 0 0.5rem; font-size: 1rem; color: var(--text-secondary); }
.stage-actions {
  margin-top: 0.75rem;
  display: flex;
  align-items: center;
  gap: 1rem;
}

.stage-actions button {
  height: 32px;
  padding: 0.25rem 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.control-group label {
  font-weight: 600;
  color: var(--text-light);
  white-space: nowrap;
  line-height: 1;
  margin-top: -5px;
}

.control-group input[type="number"] {
  height: 32px;
  width: 80px;
  padding: 0.25rem 0.5rem;
  background-color: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 4px;
  color: var(--text-light);
  font-size: 0.9rem;
  text-align: center;
  box-sizing: border-box;
}

.control-group input[type="number"]:focus {
  outline: none;
  border-color: var(--brand-accent-focus);
  box-shadow: 0 0 0 2px var(--brand-accent-focus-ring);
}
.stage-actions button {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  transition: all 0.2s ease;
  font-size: 0.9rem;
}
.stage-actions button:hover {
  background: var(--brand-accent-hover);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px var(--accent-tint-30);
}
.stage-actions button:active {
  transform: translateY(0);
}
.stage-actions button svg {
  flex-shrink: 0;
}
#left-canvas, #right-canvas, #camera-canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
  border-radius: 8px;
  border: 1px solid var(--overlay-15);
  /* Solid fallback then gradient for richer depth */
  background: var(--brand-surface);
  background: var(--brand-surface-gradient);
  opacity: 0;
  transition: opacity 0.2s ease-in;
}

#left-canvas.has-content, #right-canvas.has-content, #camera-canvas.has-content {
  opacity: 1;
}

@media (max-width: 820px) {
  .stage-grid { grid-template-columns: 1fr; }
}


/* ===== Tab Bar ===== */
.stage-header-tabs {
  display: flex;
  align-items: flex-end; /* Aligns title and tabs along the bottom */
  gap: 0.5rem;
  border-bottom: 1px solid var(--border-default);
}

.stage-header-tabs .stage-title {
  flex-shrink: 0;
  align-self: flex-start;
}

.tab-bar {
  display: flex;
  flex: 1; /* Allow tab bar to grow and fill space */
  min-width: 0; /* CRITICAL for flex-shrink to work */
  align-items: center;
}

.tab-list {
  display: flex;
  flex: 1; /* Allow tab list to grow and shrink */
  min-width: 0; /* CRITICAL for tabs inside to shrink */
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-x: auto; /* <-- THE FIX: Allow horizontal scrolling */
  overflow-y: hidden; /* Prevent vertical scrollbar */

  /* Fades the last 40px to transparent */
  mask-image: linear-gradient(to right, black calc(100% - 40px), transparent 100%);
}

.tab {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.5rem 0.5rem;
  position: relative;
  background: var(--overlay-04);
  border: 1px solid var(--border-default);
  border-bottom: none;
  border-radius: 8px 8px 0 0;
  margin-right: -1px;
  cursor: pointer;
  transition: all 0.2s ease, flex-grow 0.15s ease-out;
  flex: 1 1 0;
  max-width: 220px;
}

.tab:not(.active):hover {
  background: var(--overlay-10);
  z-index: 1;
}

.tab.active {
  background: var(--brand-bg-darker);
  border-bottom-color: var(--brand-bg-darker);
  z-index: 2;
  font-weight: 600;
}

.tab-title {
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis; /* Add '...' when title is too long */
}

/* Make title editable */
.tab-title[contenteditable="true"] {
  background: var(--overlay-10);
  border-radius: 4px;
  outline: 2px solid var(--brand-accent);
  cursor: text;
}

.tab-close {
  background: transparent;
  border: none;
  padding: 0;
  width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  cursor: pointer;
  opacity: 0.7;
  flex-shrink: 0; /* Don't let the 'x' shrink */
  display: none;
}

/* Show 'x' and enforce min-width ONLY on the active tab */
.tab.active {
  /* This width accounts for:
     padding (16px) + gap (4px) + button (18px) = 38px
     We'll use 40px for a little breathing room.
  */
  min-width: 40px;
}

.tab.active .tab-close {
  display: flex; /* Show the close button on selected */
}

/* Show the close button on hover
.tab:not(.active):hover .tab-close {
  display: flex; 
}
*/

.tab-close:hover {
  background: var(--overlay-16);
  opacity: 1;
}

.tab-close svg {
  width: 14px;
  height: 14px;
  stroke: currentColor;
}

.tab-add {
  background: transparent;
  border: none;
  padding: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  margin-left: 0.25rem;
  cursor: pointer;
  flex-shrink: 0;
}

.tab-add:hover {
  background: var(--overlay-15);
}

.tab-add svg {
  stroke: currentColor;
}

/* Make the right canvas connect to the tab bar by removing its top border
  and adjusting its top radius.
*/
#right-canvas {
  margin-top: -1px; /* Pull canvas up to meet the tabs */
  border-top: none;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}


/* ===== Fullscreen canvas ===== */
/* Style for the canvas when it's in its normal state */
#left-canvas {
  cursor: zoom-in;
  /* We removed the 'transition: all' from here to prevent conflicts.
    All transitions are now handled by the JavaScript.
  */
}

/* This is the overlay that dims the background */
.canvas-modal-backdrop {
  position: fixed;
  inset: 0;
  background: var(--backdrop-dim);
  z-index: 999;
  opacity: 0;
  transition: opacity 0.3s ease;
}

/* --- NEW: This class triggers the fade-in --- */
.canvas-modal-backdrop.backdrop-visible {
  opacity: 1;
}

/* This class is toggled on the canvas itself */
#left-canvas.canvas-fullscreen {
  cursor: zoom-out;
  box-shadow: 0 10px 30px var(--shadow-50);
}
/* ===== End fullscreen canvas ===== */

/* ===== Tab Bar Scrollbar (Optional Styling) ===== */
.tab-list::-webkit-scrollbar {
  height: 6px; /* Height of the scrollbar */
}

.tab-list::-webkit-scrollbar-track {
  background: transparent; /* Make the track invisible */
  margin: 0 4px;
}

.tab-list::-webkit-scrollbar-thumb {
  background-color: var(--overlay-25); /* Scrollbar handle */
  border-radius: 3px;
}

.tab-list::-webkit-scrollbar-thumb:hover {
  background-color: var(--overlay-40);
}

/* Temporarily stops tabs from growing to fill space */
.tab-list.no-grow .tab {
  flex-grow: 0;
}