/* ─── Custom Properties ─────────────────────────────────────── */
:root {
  /* Backgrounds */
  --bg-page:    #07090f;
  --bg-panel:   #0c1018;
  --bg-card:    #101520;
  --bg-input:   #151e2d;
  --bg-hover:   #1a2538;

  /* Borders */
  --border-dim:     #141f30;
  --border-default: #1c2d44;
  --border-bright:  #263d5c;

  /* Accent — electric blue */
  --accent:        #3b82f6;
  --accent-light:  #60a5fa;
  --accent-dim:    rgba(59, 130, 246, 0.12);
  --accent-glow:   rgba(59, 130, 246, 0.25);
  --accent-border: rgba(59, 130, 246, 0.35);

  /* Text */
  --text-primary:   #eef2ff;
  --text-secondary: #a8bfd4;
  --text-muted:     #6a899e;
  --text-dimmer:    #5a7a96;

  /* Semantic */
  --positive: #f87171;
  --negative: #60a5fa;
  --trace:    #3b82f6;

  /* Layout */
  --header-h:     52px;
  --left-w:       272px;
  --panel-pad:    16px;
  --radius-sm:    4px;
  --radius-md:    8px;
  --radius-lg:    12px;

  /* Typography */
  --font-sans:  'Inter', system-ui, -apple-system, sans-serif;
  --font-mono:  'JetBrains Mono', 'Fira Mono', monospace;
}

/* ─── Reset & Base ──────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg-page);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 13px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ─── App Shell ─────────────────────────────────────────────── */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ─── Header ────────────────────────────────────────────────── */
.app-header {
  height: var(--header-h);
  min-height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-dim);
  z-index: 10;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: var(--accent-dim);
  border: 1px solid var(--accent-border);
  border-radius: var(--radius-md);
  color: var(--accent-light);
}

.brand-text {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.brand-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.01em;
}

.brand-divider {
  color: var(--text-muted);
  font-weight: 300;
}

.brand-sub {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 400;
}

.pill {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.02em;
}

.pill-blue {
  background: var(--accent-dim);
  color: var(--accent-light);
  border: 1px solid var(--accent-border);
}

/* ─── Help Button ───────────────────────────────────────────── */
.btn-help {
  -webkit-appearance: none;
  appearance: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  background: var(--bg-input);
  border: 1px solid var(--border-default);
  border-radius: 50%;
  color: var(--text-muted);
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.btn-help:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--border-bright);
}

/* ─── Help Modal ────────────────────────────────────────────── */
.help-backdrop {
  display: none;   /* hidden by default; .open makes it visible */
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  z-index: 200;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.help-backdrop.open { display: flex; }

.help-modal {
  background: var(--bg-panel);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 16px 48px rgba(0,0,0,0.6);
}

.help-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border-dim);
  flex-shrink: 0;
}

.help-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: 0.01em;
}

.btn-help-close {
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 15px;
  cursor: pointer;
  line-height: 1;
  padding: 2px 4px;
  border-radius: var(--radius-sm);
  transition: color 0.15s;
}

.btn-help-close:hover { color: var(--text-primary); }

.help-body {
  padding: 16px 18px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.help-section-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent-light);
  margin-bottom: 5px;
}

.help-section p {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.6;
  margin: 0;
}

.help-section strong {
  color: var(--text-primary);
  font-weight: 600;
}

/* ─── App Body ──────────────────────────────────────────────── */
.app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ─── Panel collapse / restore buttons ─────────────────────── */
.btn-collapse-panel {
  -webkit-appearance: none;
  appearance: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  flex-shrink: 0;
  background: transparent;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.btn-collapse-panel:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--border-bright);
}

.btn-restore-panel {
  -webkit-appearance: none;
  appearance: none;
  display: none;           /* hidden by default; shown when panel collapsed */
  align-items: center;
  justify-content: center;
  position: fixed;
  top: calc(var(--header-h) + 10px);
  left: 10px;
  width: 32px;
  height: 32px;
  background: var(--bg-panel);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  z-index: 50;
  box-shadow: 0 2px 8px rgba(0,0,0,0.4);
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.btn-restore-panel:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--border-bright);
}

/* When panel is collapsed */
.app-body.panel-hidden .btn-restore-panel { display: flex; }

/* ─── Left Panel ────────────────────────────────────────────── */
.left-panel {
  /* flex-basis drives the width; transitioning it is more reliable
     in Safari than animating width directly on a flex child */
  flex: 0 0 50%;
  min-width: 280px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-panel);
  border-right: 1px solid var(--border-dim);
  transition: flex-basis 0.25s ease, min-width 0.25s ease, border-color 0.25s ease;
}

.app-body.panel-hidden .left-panel {
  flex-basis: 0;
  min-width: 0;
  border-right-color: transparent;
}

/* Head-map area: fixed, non-scrolling */
.left-upper {
  flex-shrink: 0;
}

/* Settings area: scrollable */
.left-lower {
  flex: 1;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border-default) transparent;
}

.left-lower::-webkit-scrollbar { width: 4px; }
.left-lower::-webkit-scrollbar-track { background: transparent; }
.left-lower::-webkit-scrollbar-thumb {
  background: var(--border-default);
  border-radius: 2px;
}

.panel-section {
  padding: var(--panel-pad);
  border-bottom: 1px solid var(--border-dim);
}

.panel-section:last-child { border-bottom: none; }

/* ─── Section Headers ───────────────────────────────────────── */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.section-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.section-hint {
  font-size: 10px;
  color: var(--text-dimmer);
}

.coming-soon {
  font-size: 9px;
  font-weight: 500;
  padding: 2px 7px;
  background: rgba(255, 200, 50, 0.07);
  color: rgba(255, 200, 90, 0.5);
  border: 1px solid rgba(255, 200, 50, 0.15);
  border-radius: 999px;
  letter-spacing: 0.05em;
}

/* ─── Head Map ──────────────────────────────────────────────── */
.headmap-wrap {
  width: min(100%, calc(50vh - 90px));
  aspect-ratio: 1;
  background: var(--bg-card);
  border: 1px solid var(--border-dim);
  border-radius: var(--radius-lg);
  overflow: hidden;
  position: relative;
  margin: 0 auto;
}

.headmap-wrap svg {
  width: 100%;
  height: 100%;
  display: block;
  cursor: crosshair;
}

/* Hint text below head map */
.headmap-hint {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  font-size: 10px;
  color: var(--text-dimmer);
  line-height: 1.4;
}

.legend-row {
  display: flex;
  gap: 16px;
  margin-top: 10px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--text-muted);
}

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

.legend-electrode {
  background: var(--bg-input);
  border: 1.5px solid var(--accent);
  box-shadow: 0 0 4px var(--accent-glow);
}

.legend-mastoid {
  background: var(--bg-input);
  border: 1.5px solid var(--text-muted);
}

.legend-source {
  background: #f59e0b;
  box-shadow: 0 0 5px rgba(245, 158, 11, 0.5);
}

/* ─── Controls ──────────────────────────────────────────────── */
.controls-stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

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

.ctrl-label {
  font-size: 12px;
  color: var(--text-secondary);
  width: 72px;
  flex-shrink: 0;
}

.ctrl-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}

.ctrl-value {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  min-width: 48px;
  text-align: right;
}

/* ─── Slider ────────────────────────────────────────────────── */
.slider {
  -webkit-appearance: none;
  appearance: none;
  flex: 1;
  height: 3px;
  background: var(--border-default);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
  transition: background 0.2s;
}

.slider:not(:disabled)::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-card);
  box-shadow: 0 0 6px var(--accent-glow);
  cursor: pointer;
  transition: background 0.2s, box-shadow 0.2s;
}

.slider:not(:disabled)::-moz-range-thumb {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-card);
  box-shadow: 0 0 6px var(--accent-glow);
  cursor: pointer;
}

.slider:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

/* ─── Button Group ──────────────────────────────────────────── */
.btn-group {
  display: flex;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  overflow: hidden;
  flex: 1;
}

.btn-toggle {
  flex: 1;
  padding: 5px 8px;
  background: var(--bg-input);
  color: var(--text-muted);
  border: none;
  font-family: var(--font-sans);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.btn-toggle + .btn-toggle {
  border-left: 1px solid var(--border-default);
}

.btn-toggle.active {
  background: var(--accent-dim);
  color: var(--accent-light);
}

.btn-toggle:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

/* ─── Select ────────────────────────────────────────────────── */
.select-input {
  flex: 1;
  padding: 6px 8px;
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  font-family: var(--font-sans);
  font-size: 12px;
  outline: none;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%236a899e' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 24px;
  transition: border-color 0.15s;
}

.select-input:focus {
  border-color: var(--accent-border);
  box-shadow: 0 0 0 2px var(--accent-dim);
}

/* ─── Right Panel ───────────────────────────────────────────── */
.right-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-page);
}

/* ─── Traces Toolbar ────────────────────────────────────────── */
.traces-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 46px;
  min-height: 46px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-dim);
}

.toolbar-left { display: flex; align-items: center; gap: 12px; }
.toolbar-right { display: flex; align-items: center; gap: 20px; }

.toolbar-montage {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  letter-spacing: 0.01em;
}

.scale-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
}

.scale-label {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-muted);
}

.scale-bar-v {
  width: 2px;
  height: 24px;
  background: var(--text-dimmer);
  border-radius: 1px;
}

.scale-bar-h {
  width: 40px;
  height: 2px;
  background: var(--text-dimmer);
  border-radius: 1px;
}

.toolbar-note {
  font-size: 10px;
  color: var(--text-dimmer);
  letter-spacing: 0.02em;
}

/* ─── Play / Pause Button ───────────────────────────────────── */
.btn-play {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  background: var(--bg-input);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.btn-play:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--border-bright);
}

.btn-play[data-state="playing"] {
  background: var(--accent-dim);
  color: var(--accent-light);
  border-color: var(--accent-border);
}

.btn-play .icon-pause              { display: none; }
.btn-play[data-state="playing"] .icon-play  { display: none; }
.btn-play[data-state="playing"] .icon-pause { display: block; }

/* ─── Traces Canvas Area ────────────────────────────────────── */
.traces-wrap {
  flex: 1;
  overflow: auto;
  position: relative;
  background: #ffffff;   /* white clinical background */
  scrollbar-width: thin;
  scrollbar-color: #c8d3dd transparent;
}

.traces-wrap::-webkit-scrollbar { width: 6px; }
.traces-wrap::-webkit-scrollbar-track { background: transparent; }
.traces-wrap::-webkit-scrollbar-thumb {
  background: #c8d3dd;
  border-radius: 3px;
}

#traces-canvas {
  display: block;
}

/* ─── Scenario Preset Buttons ───────────────────────────────── */
.preset-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.preset-btn {
  padding: 7px 8px;
  background: var(--bg-input);
  color: var(--text-secondary);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  font-family: var(--font-sans);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  text-align: center;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  line-height: 1.2;
}

.preset-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--border-bright);
}

.preset-btn.active {
  background: var(--accent-dim);
  color: var(--accent-light);
  border-color: var(--accent-border);
}

/* ─── Findings Panel ────────────────────────────────────────── */
.findings-section {
  flex: 1;
}

.findings-card {
  background: var(--bg-card);
  border: 1px solid var(--border-dim);
  border-radius: var(--radius-md);
  padding: 12px;
  margin-bottom: 10px;
}

.findings-icon-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.findings-icon {
  font-size: 14px;
  line-height: 1;
  flex-shrink: 0;
  width: 20px;
  text-align: center;
  color: var(--text-muted);
  transition: color 0.2s;
}

.findings-icon[data-type="reversal"] { color: #b45309; }
.findings-icon[data-type="warning"]  { color: #d97706; }
.findings-icon[data-type="referential"] { color: var(--accent-light); }
.findings-icon[data-type="idle"]     { color: var(--text-muted); }

.findings-headline {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-primary);
  line-height: 1.3;
}

.findings-detail {
  font-size: 11px;
  color: var(--text-secondary);
  line-height: 1.5;
  margin: 0;
}

.findings-legend {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.findings-legend-item {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 10px;
  color: var(--text-muted);
}

.fl-dot {
  width: 8px;
  height: 8px;
  border-radius: 2px;
  flex-shrink: 0;
}

.fl-pr  { background: rgba(180, 83, 9, 0.5); }
.fl-eoc { background: var(--border-bright); }

/* ─── SVG Head Map Elements ─────────────────────────────────── */
.guide-line {
  stroke: var(--border-dim);
  stroke-width: 0.6;
  stroke-dasharray: 4 4;
}

.head-circle {
  fill: var(--bg-card);
  stroke: var(--border-bright);
  stroke-width: 1.5;
}

.head-nose {
  fill: none;
  stroke: var(--border-bright);
  stroke-width: 1.5;
  stroke-linecap: round;
}

.head-ear {
  fill: var(--bg-card);
  stroke: var(--border-bright);
  stroke-width: 1.5;
}

.electrode-dot {
  fill: var(--bg-input);
  stroke: var(--accent);
  stroke-width: 1.5;
  cursor: default;
  transition: r 0.2s, fill 0.2s;
}

.electrode-dot.mastoid {
  stroke: var(--text-muted);
  stroke-width: 1;
}

.electrode-label {
  font-family: var(--font-mono);
  font-size: 7px;
  fill: var(--text-muted);
  pointer-events: none;
  user-select: none;
}

.electrode-label.mastoid {
  fill: var(--text-dimmer);
}

.head-dir-label {
  font-family: var(--font-sans);
  font-size: 7.5px;
  font-weight: 500;
  fill: var(--text-dimmer);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  user-select: none;
}
