:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #475569;
  --text-subtle: #64748b;
  --primary: #2563eb;
  --primary-soft: #eff6ff;
  --emerald: #059669;
  --emerald-soft: #ecfdf5;
  --purple: #7c3aed;
  --purple-soft: #f5f3ff;
  --amber: #d97706;
  --amber-soft: #fffbeb;
  --danger: #dc2626;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-md: 0 4px 16px rgba(15, 23, 42, 0.07);
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

/* Header */
.top-bar {
  height: 54px;
  padding: 0 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-shrink: 0;
  z-index: 20;
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 5px 11px;
  border-radius: 8px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  color: var(--text-muted);
  text-decoration: none;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}

.back-btn:hover {
  background: var(--border);
  color: var(--text);
}

.title-wrap {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}

.title-wrap h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 800;
  color: var(--text);
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.subtitle {
  font-size: 12px;
  color: var(--text-subtle);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-status {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.status-pill {
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  background: var(--emerald-soft);
  color: var(--emerald);
  border: 1px solid rgba(5, 150, 105, 0.25);
  white-space: nowrap;
}

.status-pill.warn {
  background: var(--amber-soft);
  color: var(--amber);
  border-color: rgba(217, 119, 6, 0.3);
}

.status-pill.danger {
  background: #fef2f2;
  color: var(--danger);
  border-color: rgba(220, 38, 38, 0.3);
}

/* Main Workspace */
.workspace {
  flex: 1;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

/* Left Optical Bench Stage */
.bench-column {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 12px;
  gap: 10px;
  overflow: hidden;
}

.bench-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px 10px;
  box-shadow: var(--shadow-sm);
  flex-shrink: 0;
}

.seg-group {
  display: inline-flex;
  background: var(--bg);
  padding: 3px;
  border-radius: 9px;
  border: 1px solid var(--border);
  gap: 2px;
}

.seg-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.seg-btn:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.6);
}

.seg-btn.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow-sm);
  font-weight: 700;
}

.layer-toggles {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.check-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}

.check-chip input {
  accent-color: var(--primary);
  cursor: pointer;
  margin: 0;
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn-sm {
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-sm:hover {
  background: var(--surface-alt);
}

.btn-sm.primary {
  background: var(--emerald);
  border-color: var(--emerald);
  color: #fff;
}

.btn-sm.primary:hover {
  background: #047857;
}

.canvas-stage {
  flex: 1;
  min-height: 280px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-md);
  position: relative;
  overflow: hidden;
}

#benchCanvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

/* Bottom Optical Telemetry Strip */
.telemetry-strip {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  flex-shrink: 0;
}

.tele-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 9px 12px;
  box-shadow: var(--shadow-sm);
}

.tele-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

.tele-title {
  font-size: 11px;
  font-weight: 800;
  color: var(--text-muted);
  letter-spacing: 0.02em;
}

.tele-badge {
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--surface-alt);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.tele-badge.emerald {
  background: var(--emerald-soft);
  color: var(--emerald);
  border-color: rgba(5, 150, 105, 0.25);
}

.tele-badge.purple {
  background: var(--purple-soft);
  color: var(--purple);
  border-color: rgba(124, 58, 237, 0.25);
}

.tele-badge.amber {
  background: var(--amber-soft);
  color: var(--amber);
  border-color: rgba(217, 119, 6, 0.25);
}

.tele-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 4px 10px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text);
}

.tele-kv {
  display: flex;
  justify-content: space-between;
  gap: 6px;
}

.tele-k {
  color: var(--text-subtle);
}

.tele-v {
  font-weight: 700;
}

/* Right Control Sidebar */
.control-sidebar {
  width: 372px;
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel-card {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

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

.card-title {
  margin: 0;
  font-size: 12px;
  font-weight: 800;
  color: var(--text);
}

.card-subtitle {
  font-size: 11px;
  color: var(--text-subtle);
}

/* Docked Viewfinder */
.viewfinder-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.viewfinder-ring {
  width: 224px;
  height: 224px;
  border-radius: 50%;
  padding: 4px;
  background: linear-gradient(145deg, #334155, #0f172a);
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.22), inset 0 0 0 1px rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
}

#viewfinderCanvas {
  border-radius: 50%;
  display: block;
}

.orient-hint {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 3px 10px;
  border-radius: 999px;
}

.meter-row {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.meter-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 6px 8px;
}

.meter-label {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  font-weight: 700;
  color: var(--text-subtle);
  margin-bottom: 4px;
}

.meter-label span:last-child {
  font-family: var(--font-mono);
  color: var(--text);
}

.meter-bar {
  height: 6px;
  background: var(--bg);
  border-radius: 999px;
  overflow: hidden;
}

.meter-fill {
  height: 100%;
  border-radius: 999px;
  transition: width 0.15s ease;
}

.meter-fill.bright {
  background: linear-gradient(90deg, #f59e0b, #fbbf24);
}

.meter-fill.dof {
  background: linear-gradient(90deg, #2563eb, #0d9488);
}

/* Presets */
.preset-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.preset-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 10px;
  padding: 7px 9px;
  font-family: var(--font-sans);
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
  cursor: pointer;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.15s;
}

.preset-btn:hover {
  border-color: var(--border-strong);
  color: var(--text);
}

.preset-btn.active {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.14);
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}

.dot.blue { background: var(--primary); }
.dot.emerald { background: var(--emerald); }
.dot.purple { background: var(--purple); }
.dot.amber { background: var(--amber); }

.guide-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 10px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.guide-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.guide-title {
  font-size: 12px;
  font-weight: 800;
  color: var(--text);
}

.guide-formula {
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 6px;
  background: var(--primary-soft);
  color: var(--primary);
}

.guide-text {
  margin: 0;
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--text-muted);
}

/* Controls */
.ctrl-block {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.ctrl-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.ctrl-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
}

.mono-readout {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--primary);
}

.full-seg {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

.slider-line {
  display: flex;
  align-items: center;
  gap: 6px;
}

.step-btn {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.step-btn:hover {
  background: var(--bg);
}

input[type="range"] {
  flex: 1;
  accent-color: var(--primary);
  cursor: pointer;
  margin: 0;
}

.ctrl-hint {
  margin: 0;
  font-size: 10.5px;
  color: var(--text-subtle);
  line-height: 1.4;
}

/* Responsive fallback for smaller screens */
@media (max-width: 1024px) {
  .app-shell {
    height: auto;
    min-height: 100dvh;
    overflow: visible;
  }

  .workspace {
    flex-direction: column;
    overflow: visible;
  }

  .bench-column {
    overflow: visible;
  }

  .canvas-stage {
    height: 460px;
    flex: none;
  }

  .telemetry-strip {
    grid-template-columns: 1fr;
  }

  .control-sidebar {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    overflow: visible;
  }

  .subtitle {
    display: none;
  }
}
