*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0a0e1a;
  --surface: #0f1424;
  --surface2: #141a30;
  --border: #1e2740;
  --text: #e2e8f0;
  --text-dim: #9aabc4;
  --text-muted: #6b7fa0;
  --accent-gold: #f0c040;
  --accent-cyan: #38bdf8;
  --glow-gold: 0 0 40px rgba(240,192,64,0.15);
  --glow-cyan: 0 0 40px rgba(56,189,248,0.15);
}

[data-theme="light"] {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-dim: #475569;
  --text-muted: #64748b;
  --accent-gold: #d97706;
  --accent-cyan: #0284c7;
  --glow-gold: 0 0 40px rgba(217,119,6,0.1);
  --glow-cyan: 0 0 40px rgba(2,132,199,0.1);
}

html, body { height: 100%; overflow: hidden; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Space Grotesk', sans-serif;
  display: flex;
  flex-direction: column;
  padding-top: 36px;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background-image:
    radial-gradient(1px 1px at 10% 20%, rgba(255,255,255,0.4) 0%, transparent 100%),
    radial-gradient(1px 1px at 30% 65%, rgba(255,255,255,0.3) 0%, transparent 100%),
    radial-gradient(1px 1px at 55% 15%, rgba(255,255,255,0.35) 0%, transparent 100%),
    radial-gradient(1px 1px at 70% 45%, rgba(255,255,255,0.25) 0%, transparent 100%),
    radial-gradient(1px 1px at 85% 80%, rgba(255,255,255,0.3) 0%, transparent 100%),
    radial-gradient(1.5px 1.5px at 25% 35%, rgba(240,192,64,0.3) 0%, transparent 100%),
    radial-gradient(1.5px 1.5px at 75% 60%, rgba(56,189,248,0.2) 0%, transparent 100%);
  animation: twinkle 8s ease-in-out infinite alternate;
}

[data-theme="light"] body::before { display: none; }

@keyframes twinkle {
  0% { opacity: 1; }
  100% { opacity: 0.5; }
}

/* ── Data Zone (top) ── */
.data-zone {
  position: relative;
  z-index: 1;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 220px;
  min-height: 48px;
  flex-shrink: 0;
  transition: height 0.2s;
}

.data-zone.collapsed {
  height: 38px !important;
  min-height: 38px;
  overflow: hidden;
}

.data-zone.collapsed .data-grid-row { display: none; }

.data-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 8px;
}

.data-toolbar-left, .data-toolbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.toggle-data {
  font-size: 10px;
  width: 24px;
  height: 24px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.data-zone.collapsed .toggle-data { transform: rotate(180deg); }

/* ── Data Grid ── */
.data-grid-row {
  flex: 1;
  display: flex;
  min-height: 0;
}

.data-grid-wrap {
  flex: 1;
  overflow: auto;
  min-height: 0;
}

.add-col-btn {
  width: 28px;
  min-width: 28px;
  border: none;
  border-left: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
}

.add-col-btn:hover {
  background: var(--accent-cyan);
  color: #000;
}

.data-grid-wrap::-webkit-scrollbar { width: 5px; height: 5px; }
.data-grid-wrap::-webkit-scrollbar-track { background: transparent; }
.data-grid-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.data-grid {
  border-collapse: collapse;
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  white-space: nowrap;
  width: max-content;
  min-width: 100%;
}

.data-grid th,
.data-grid td {
  padding: 3px 8px;
  border: 1px solid var(--border);
  height: 26px;
}

.data-grid th {
  background: var(--surface2);
  color: var(--text-dim);
  font-weight: 500;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  position: sticky;
  top: 0;
  z-index: 3;
}

.data-grid th.sticky-col {
  left: 0;
  z-index: 4;
}

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

.add-cat-btn {
  width: 20px;
  height: 20px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  flex-shrink: 0;
}

.add-cat-btn:hover {
  background: var(--accent-cyan);
  color: #000;
  border-color: var(--accent-cyan);
}

.data-grid td {
  background: var(--surface);
}

.data-grid td.sticky-col {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--surface);
}

.data-grid td[contenteditable="true"]:focus {
  outline: 2px solid var(--accent-cyan);
  outline-offset: -2px;
  background: var(--surface2);
}

.data-grid td.value-cell {
  text-align: right;
  min-width: 60px;
  cursor: text;
}

.data-grid .cat-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 100px;
}

.data-grid .cat-cell .cat-name {
  flex: 1;
}

.data-grid .cat-color-input {
  width: 18px;
  height: 18px;
  min-width: 18px;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--bg);
  cursor: pointer;
  padding: 1px;
  flex-shrink: 0;
}

.data-grid .cat-color-input::-webkit-color-swatch-wrapper { padding: 0; }
.data-grid .cat-color-input::-webkit-color-swatch { border: none; border-radius: 2px; }

.data-grid .cat-logo-btn {
  display: none; /* TODO: re-enable logo/image feature later */
}

.data-grid .cat-logo-btn.has-logo {
  opacity: 1;
  color: var(--accent-cyan);
}

.data-grid tr:hover .cat-logo-btn {
  opacity: 1;
}

.data-grid .cat-logo-btn:hover {
  color: var(--accent-gold);
}

.data-grid .cat-name {
  min-width: 40px;
}

.data-grid .col-header {
  display: flex;
  align-items: center;
  gap: 4px;
}

.data-grid .col-header span[contenteditable="true"]:focus {
  outline: 1px solid var(--accent-cyan);
  outline-offset: 1px;
  border-radius: 2px;
}

.data-grid .remove-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 10px;
  padding: 0 2px;
  line-height: 1;
  opacity: 0;
  transition: opacity 0.15s;
}

.data-grid th:hover .remove-btn,
.data-grid tr:hover .remove-btn {
  opacity: 1;
}

.data-grid .remove-btn:hover {
  color: #f87171;
}

/* ── Zone Resize Handle ── */
.zone-resize {
  position: relative;
  z-index: 1;
  height: 5px;
  cursor: row-resize;
  background: transparent;
  flex-shrink: 0;
}

.zone-resize:hover,
.zone-resize.dragging {
  background: var(--accent-cyan);
  opacity: 0.4;
}

/* ── Chart Zone (bottom) ── */
.chart-zone {
  position: relative;
  z-index: 1;
  flex: 1;
  display: flex;
  min-height: 0;
}

/* ── Sidebar ── */
.sidebar-wrap {
  width: 250px;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  flex-shrink: 0;
}
.sidebar {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}
.sidebar-export {
  padding: 10px 14px;
  border-top: 1px solid var(--border);
}

.sidebar::-webkit-scrollbar { width: 5px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.sidebar-resize {
  width: 5px;
  cursor: col-resize;
  background: transparent;
  flex-shrink: 0;
}

.sidebar-resize:hover,
.sidebar-resize.dragging {
  background: var(--accent-cyan);
  opacity: 0.4;
}

/* ── Sections ── */
.section {
  border-bottom: 1px solid var(--border);
  padding: 10px 14px;
}
.section-fixed .row { margin-bottom: 0; }
.section-fixed select { width: 100%; }

.section-title {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--accent-cyan);
  font-weight: 600;
  margin: -10px -14px 8px;
  padding: 6px 14px;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 6px;
}

.section-title::before {
  content: '\25BC';
  font-size: 7px;
  transition: transform 0.2s;
}

.section.collapsed .section-title::before { transform: rotate(-90deg); }
.section.collapsed .section-body { display: none; }

.section.auto-collapsed .section-body { display: none; }
.section.auto-collapsed .section-title::before { transform: rotate(-90deg); }

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

.row:has(input[type="range"]) {
  display: grid;
  grid-template-columns: auto 1fr 30px;
}

.row:last-child { margin-bottom: 0; }

label.row-label {
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
  min-width: 52px;
  flex-shrink: 0;
}

select, input[type="text"] {
  flex: 1;
  min-width: 0;
  font-family: 'Space Grotesk', sans-serif;
  font-size: 11px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
  outline: none;
}

select:focus, input[type="text"]:focus { border-color: var(--accent-cyan); }

select option { background: var(--bg); color: var(--text); }

input[type="color"] {
  width: 28px;
  height: 22px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  cursor: pointer;
  padding: 1px;
}

input[type="color"]::-webkit-color-swatch-wrapper { padding: 1px; }
input[type="color"]::-webkit-color-swatch { border: none; border-radius: 2px; }

input[type="range"] {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--accent-cyan);
  border: 2px solid var(--accent-cyan);
  cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--accent-cyan);
  border: 2px solid var(--accent-cyan);
  cursor: pointer;
}

.range-value {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  color: var(--text-dim);
  width: 30px;
  flex-shrink: 0;
  text-align: right;
}

/* ── Buttons ── */
.btn {
  font-family: 'Space Grotesk', sans-serif;
  font-size: 11px;
  font-weight: 500;
  background: var(--bg);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 12px;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}

.btn:hover {
  background: rgba(255,255,255,0.05);
  color: var(--text);
  border-color: var(--accent-cyan);
}

.btn-export {
  width: 100%;
  font-family: 'Space Grotesk', sans-serif;
  font-size: 12px;
  font-weight: 600;
  padding: 8px;
  background: var(--accent-cyan);
  color: #fff;
  border: 1px solid var(--accent-cyan);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  letter-spacing: 0.5px;
}

.btn-export:hover {
  box-shadow: var(--glow-cyan);
  filter: brightness(1.15);
}

.btn-record {
  width: 100%;
  font-family: 'Space Grotesk', sans-serif;
  font-size: 12px;
  font-weight: 600;
  padding: 8px;
  background: transparent;
  color: #f87171;
  border: 1px solid #f87171;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  letter-spacing: 0.5px;
}

.btn-record:hover {
  background: rgba(248,113,113,0.1);
  box-shadow: 0 0 20px rgba(248,113,113,0.15);
}

.btn-reset-defaults {
  font-size: 11px !important;
  color: var(--text-muted) !important;
  border-color: var(--border) !important;
}
.btn-reset-defaults:hover {
  color: #f87171 !important;
  border-color: #f87171 !important;
}

.btn-record.recording {
  background: #f87171;
  color: #fff;
  animation: pulse-record 1s ease-in-out infinite;
  cursor: default;
}

@keyframes pulse-record {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

/* ── Checkbox ── */
.checkbox-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 8px;
}

.row input[type="checkbox"], .checkbox-row input[type="checkbox"] {
  accent-color: var(--accent-cyan);
  cursor: pointer;
  flex-shrink: 0;
}

.checkbox-row label {
  font-size: 11px;
  color: var(--text-dim);
  cursor: pointer;
}

.radio-label {
  font-size: 11px;
  color: var(--text-dim);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 3px;
}
.radio-label input[type="radio"] {
  accent-color: var(--accent-cyan);
  cursor: pointer;
  margin: 0;
}

.checkbox-inline {
  font-size: 11px;
  color: var(--text-dim);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 3px;
}
.checkbox-inline input[type="checkbox"] {
  accent-color: var(--accent-cyan);
  cursor: pointer;
  margin: 0;
}

/* ── Chart Area ── */
.chart-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
}

#chartdiv {
  flex: 1;
  min-height: 0;
}

/* ── Playback Bar ── */
.playback-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.play-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  width: 28px;
  height: 28px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  transition: all 0.15s;
  flex-shrink: 0;
}

.play-btn:hover {
  border-color: var(--accent-cyan);
  color: var(--text);
}

.play-btn-main {
  width: 32px;
  height: 32px;
  font-size: 13px;
  border-color: var(--accent-cyan);
  color: var(--accent-cyan);
}

.play-btn-main:hover {
  background: var(--accent-cyan);
  color: #fff;
}

.playback-bar input[type="range"] {
  flex: 1;
}

.play-label {
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  font-weight: 500;
  color: var(--text);
  min-width: 40px;
  text-align: right;
}

/* ── Toast / notification ── */
.toast {
  position: fixed;
  bottom: 60px;
  right: 12px;
  z-index: 2000;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.3);
  animation: toastIn 0.2s ease-out;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
