/* ---------- theme tokens ---------- */
:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #f6f6f3;
  --chart-surface: #fcfcfb;
  --hover: rgba(11, 11, 11, 0.05);
  --border: rgba(11, 11, 11, 0.10);
  --border-strong: rgba(11, 11, 11, 0.20);
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --grid-minor: #efeee9;
  --axis: #c3c2b7;
  --marker: #52514e;
  --crosshair: rgba(11, 11, 11, 0.35);
  --accent: #2a78d6;
  --accent-text: #1c5cab;
  --accent-soft: rgba(42, 120, 214, 0.12);
  --accent-fill: rgba(42, 120, 214, 0.10);
  --danger: #d03b3b;
  --warn: #c98500;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.10);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #6250d6;
  --series-8: #e34948;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0d0d;
    --surface: #161615;
    --surface-2: #121211;
    --chart-surface: #1a1a19;
    --hover: rgba(255, 255, 255, 0.06);
    --border: rgba(255, 255, 255, 0.10);
    --border-strong: rgba(255, 255, 255, 0.20);
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --grid-minor: #222221;
    --axis: #4a4a46;
    --marker: #c3c2b7;
    --crosshair: rgba(255, 255, 255, 0.35);
    --accent: #3987e5;
    --accent-text: #86b6ef;
    --accent-soft: rgba(57, 135, 229, 0.18);
    --accent-fill: rgba(57, 135, 229, 0.14);
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0d0d;
  --surface: #161615;
  --surface-2: #121211;
  --chart-surface: #1a1a19;
  --hover: rgba(255, 255, 255, 0.06);
  --border: rgba(255, 255, 255, 0.10);
  --border-strong: rgba(255, 255, 255, 0.20);
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --grid-minor: #222221;
  --axis: #4a4a46;
  --marker: #c3c2b7;
  --crosshair: rgba(255, 255, 255, 0.35);
  --accent: #3987e5;
  --accent-text: #86b6ef;
  --accent-soft: rgba(57, 135, 229, 0.18);
  --accent-fill: rgba(57, 135, 229, 0.14);
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
}

/* ---------- base ---------- */
* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}

[hidden] { display: none !important; }

button, input, select { font: inherit; color: inherit; }

h1, h2, h3 { margin: 0; }

/* ---------- controls ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  background: var(--surface);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.small { height: 26px; padding: 0 9px; font-size: 12px; }
.btn.icon-only { width: 32px; padding: 0; justify-content: center; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn.wide { width: auto; padding: 0 5px; font-size: 11px; font-weight: 600; }
.icon-btn.active { background: var(--accent-soft); color: var(--accent-text); }

.link-btn {
  border: 0;
  padding: 0;
  background: none;
  color: var(--accent-text);
  font-size: 12px;
  cursor: pointer;
}
.link-btn:hover { text-decoration: underline; }

.select, .input {
  height: 30px;
  min-width: 0;
  padding: 0 8px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface);
}
.select { width: 100%; }
.input:focus, .select:focus, .btn:focus-visible, .icon-btn:focus-visible, .cell:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.unit { color: var(--muted); font-size: 12px; }

.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 7px; overflow: hidden; }
.seg button {
  height: 30px;
  padding: 0 10px;
  border: 0;
  background: var(--surface);
  cursor: pointer;
}
.seg button + button { border-left: 1px solid var(--border-strong); }
.seg button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent-text); font-weight: 600; }

.check { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.hint { margin: 6px 0 0; color: var(--muted); font-size: 12px; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 16px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand h1 { font-size: 16px; font-weight: 650; }
.brand-sub { color: var(--muted); font-size: 12px; }
.top-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }

/* ---------- layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; }

.sidebar {
  width: 300px;
  flex-shrink: 0;
  overflow-y: auto;
  padding: 14px;
  border-right: 1px solid var(--border);
  background: var(--surface-2);
}
.side-section + .side-section { margin-top: 20px; }
.side-section h2 {
  margin-bottom: 8px;
  color: var(--text-2);
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.side-head { display: flex; align-items: baseline; justify-content: space-between; }

.dropzone {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 100%;
  padding: 14px 12px;
  border: 1.5px dashed var(--border-strong);
  border-radius: 9px;
  background: var(--surface);
  text-align: center;
  cursor: pointer;
}
.dropzone:hover { border-color: var(--accent); background: var(--accent-fill); }
.dropzone span { color: var(--muted); font-size: 12px; }

.file-list, .marker-list { list-style: none; margin: 8px 0 0; padding: 0; }

.file-item {
  padding: 7px 8px 7px 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}
.file-item + .file-item { margin-top: 6px; }
.file-item.off .f-name, .file-item.off .f-meta { opacity: 0.5; }
.file-row { display: flex; align-items: center; gap: 6px; }
.f-vis { margin: 0 2px; accent-color: var(--accent); cursor: pointer; }
.swatch {
  position: relative;
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px var(--border);
  cursor: pointer;
}
.swatch input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }
.f-name { flex: 1; min-width: 0; overflow: hidden; font-weight: 550; text-overflow: ellipsis; white-space: nowrap; }
.f-meta { margin: 3px 0 0 28px; color: var(--muted); font-size: 11.5px; }
.f-badge { color: var(--accent-text); font-weight: 600; }
.f-edit { margin: 10px 0 0 28px; font-size: 12px; }
.f-edit h4 { margin: 0 0 2px; color: var(--text-2); font-size: 12px; font-weight: 650; }
.f-edit .hint { margin: 0 0 6px; }
.port-grid { display: flex; flex-wrap: wrap; gap: 4px 10px; margin-bottom: 4px; }
.port-map { display: flex; align-items: center; gap: 4px; color: var(--text-2); font-variant-numeric: tabular-nums; }
.port-map .select { width: auto; height: 24px; padding: 0 4px; font-size: 12px; }
.off-grid { display: grid; gap: 4px; }
.off-cell { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.off-cell span { color: var(--muted); font-size: 10.5px; }
.off-cell .input, .off-actions .input {
  width: 100%;
  height: 24px;
  padding: 0 5px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.off-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 8px; }
.off-actions label { display: flex; align-items: center; gap: 4px; color: var(--text-2); }
.off-actions .input { width: 52px; }
.input.invalid { border-color: var(--danger); }
.f-details { margin: 8px 0 0 28px; font-size: 12px; }
.f-details dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 0; }
.f-details dt { color: var(--muted); }
.f-details dd { margin: 0; overflow-wrap: anywhere; }
.f-details pre {
  max-height: 160px;
  overflow: auto;
  margin: 8px 0 0;
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text-2);
  font: 11px/1.45 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  white-space: pre-wrap;
}

.marker-form { display: flex; align-items: center; gap: 6px; }
.marker-form .input { flex: 1; }
.marker-list li { display: flex; align-items: center; gap: 6px; margin-top: 6px; }
.m-tag {
  min-width: 30px;
  padding: 2px 0;
  border-radius: 4px;
  background: var(--marker);
  color: var(--surface);
  font-size: 11px;
  font-weight: 650;
  text-align: center;
}
.m-freq {
  flex: 1;
  min-width: 0;
  height: 26px;
  padding: 0 6px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface);
  font-variant-numeric: tabular-nums;
}

.help dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; margin: 0; font-size: 12px; }
.help dt { color: var(--text); font-weight: 550; white-space: nowrap; }
.help dd { margin: 0; color: var(--text-2); }

.panels {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-auto-rows: max-content;
  align-content: start;
  gap: 16px;
  min-width: 0;
  overflow-y: auto;
  padding: 16px;
}
.panels.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.panels-empty { margin: 40px auto; color: var(--muted); }

/* ---------- panel ---------- */
.panel {
  container-type: inline-size;
  min-width: 0;
  overflow: clip;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}
.panel-body { display: grid; grid-template-columns: 186px minmax(0, 1fr); }
.panel-side {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 12px;
  border-right: 1px solid var(--border);
  background: var(--surface-2);
}
.field { display: flex; flex-direction: column; gap: 5px; }
.field-label { color: var(--text-2); font-size: 11px; font-weight: 650; letter-spacing: 0.04em; text-transform: uppercase; }

.matrix { display: grid; gap: 3px; }
.cell {
  height: 28px;
  min-width: 0;
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  background: var(--surface);
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}
.cell.diag { background: var(--surface-2); }
.cell:hover { border-color: var(--accent); }
.cell.on { border-color: var(--accent); background: var(--accent); color: #fff; font-weight: 600; }

.quick { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 2px; }
.quick button {
  padding: 2px 6px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: transparent;
  color: var(--text-2);
  font-size: 11px;
  cursor: pointer;
}
.quick button:hover { background: var(--hover); color: var(--text); }

.axes summary { color: var(--text-2); cursor: pointer; }
.axes-grid {
  display: grid;
  grid-template-columns: 12px minmax(0, 1fr) minmax(0, 1fr) auto;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
}
.axes-grid .input { height: 26px; padding: 0 5px; font-size: 12px; font-variant-numeric: tabular-nums; }
.axes-btns { display: flex; gap: 6px; margin-top: 8px; }

.panel-main { display: flex; flex-direction: column; min-width: 0; }
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px 4px 14px;
}
.panel-title { overflow: hidden; font-size: 14px; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
.panel-actions { display: flex; align-items: center; gap: 4px; }

.plot-wrap {
  position: relative;
  height: 420px;
  min-height: 200px;
  margin: 0 6px;
  overflow: hidden;
  border-radius: 6px;
  background: var(--chart-surface);
}
.plot-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.cv-over { touch-action: none; }
.empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  color: var(--muted);
  text-align: center;
  pointer-events: none;
}
.resize-grip {
  height: 10px;
  margin: 0 6px;
  cursor: ns-resize;
  touch-action: none;
}
.resize-grip::after {
  content: "";
  display: block;
  width: 36px;
  height: 3px;
  margin: 3px auto 0;
  border-radius: 2px;
  background: var(--border-strong);
}
.resize-grip:hover::after { background: var(--accent); }

.tooltip {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  max-width: 360px;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: 12px;
  pointer-events: none;
}
.tip-h { margin-bottom: 4px; font-weight: 650; font-variant-numeric: tabular-nums; }
.tip-r { display: flex; align-items: baseline; gap: 6px; line-height: 1.5; }
.tip-r i, .mt-name i {
  display: inline-block;
  width: 10px;
  height: 3px;
  flex-shrink: 0;
  border-radius: 2px;
  transform: translateY(-3px);
}
.tip-n { flex: 1; min-width: 0; overflow: hidden; color: var(--text-2); text-overflow: ellipsis; white-space: nowrap; }
.tip-r b { font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.sub { color: var(--text-2); font-weight: 400; }

.legend { display: flex; flex-wrap: wrap; gap: 2px 4px; padding: 6px 10px 8px; }
.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  padding: 3px 7px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--text-2);
  font-size: 12px;
  cursor: pointer;
}
.legend-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend-item:hover { background: var(--hover); color: var(--text); }
.legend-item.off { opacity: 0.4; }
.legend-item.off span { text-decoration: line-through; }

.mtable { overflow-x: auto; padding: 0 10px 12px; }
.mtable table { border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
.mtable th, .mtable td { padding: 4px 12px 4px 0; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; white-space: nowrap; }
.mtable th { color: var(--text-2); font-weight: 600; }
.mtable th small { display: block; color: var(--muted); font-weight: 400; }
.mt-name { max-width: 260px; overflow: hidden; color: var(--text-2); text-overflow: ellipsis; }
.mt-name i { margin-right: 6px; }

/* ---------- overlays ---------- */
.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
  place-items: center;
  background: var(--accent-fill);
  backdrop-filter: blur(2px);
  pointer-events: none;
}
.drop-overlay.show { display: grid; }
.drop-overlay div {
  padding: 22px 34px;
  border: 2px dashed var(--accent);
  border-radius: 14px;
  background: var(--surface);
  color: var(--accent-text);
  font-size: 16px;
  font-weight: 600;
}

.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: min(440px, calc(100vw - 32px));
}
.toast {
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--danger);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  cursor: pointer;
}
.toast.warn { border-left-color: var(--warn); }

/* ---------- responsive ---------- */
@container (max-width: 620px) {
  .panel-body { grid-template-columns: minmax(0, 1fr); }
  .panel-side {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }
  .plot-wrap { height: 340px; }
}

@media (max-width: 1100px) {
  .panels.cols-2 { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 800px) {
  body { height: auto; }
  .layout { flex-direction: column; }
  .sidebar { width: auto; border-right: 0; border-bottom: 1px solid var(--border); }
  .panels { overflow: visible; padding: 12px; }
  .brand-sub { display: none; }
}
