/* ---------- Tokens ---------- */
:root {
  color-scheme: light;
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-raised: #ffffff;
  --surface-sunken: #f0efec;
  --chart-surface: #fcfcfb;
  --sidebar: #f3f2ef;
  --border: rgba(11, 11, 11, 0.10);
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: #e3eefb;
  --ref-band: rgba(12, 163, 12, 0.08);

  --status-good: #006300;
  --status-warning: #8a5a00;
  --status-critical: #d03b3b;

  /* Categorical series slots (fixed order, validated palette). */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #6250d6;
  --series-8: #e34948;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04), 0 1px 1px rgba(11, 11, 11, 0.03);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-raised: #232322;
    --surface-sunken: #141413;
    --chart-surface: #1a1a19;
    --sidebar: #131312;
    --border: rgba(255, 255, 255, 0.10);
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --accent: #3987e5;
    --accent-soft: #1c2a3d;
    --ref-band: rgba(12, 163, 12, 0.09);
    --status-good: #0ca30c;
    --status-warning: #fab219;
    --status-critical: #e66767;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-raised: #232322;
  --surface-sunken: #141413;
  --chart-surface: #1a1a19;
  --sidebar: #131312;
  --border: rgba(255, 255, 255, 0.10);
  --text-primary: #f0efec;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --accent: #3987e5;
  --accent-soft: #1c2a3d;
  --ref-band: rgba(12, 163, 12, 0.09);
  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-critical: #e66767;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
  --shadow: none;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.45;
  color: var(--text-primary);
  background: var(--page);
}
h1 { font-size: 22px; margin: 0 0 2px; font-weight: 650; }
h2 { font-size: 15px; margin: 0; font-weight: 600; }
code { font-size: 0.95em; background: var(--surface-sunken); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--text-muted); }
.spacer { flex: 1; }
.nowrap { white-space: nowrap; }
[hidden] { display: none !important; }

/* ---------- Shell ---------- */
.shell { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: 100%; }
.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 20px;
  padding: 18px 12px;
  position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 15px; padding: 0 8px; color: var(--text-primary); }
.brand svg { color: var(--accent); }
.nav-title { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); padding: 0 8px 6px; }
.nav ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.nav-item {
  display: flex; flex-direction: column; padding: 8px 10px; border-radius: 8px;
  color: var(--text-secondary); text-decoration: none; font-weight: 500;
}
.nav-item:hover { background: var(--surface-sunken); color: var(--text-primary); }
.nav-item.is-active { background: var(--accent-soft); color: var(--accent); }
.nav-item.is-disabled { opacity: 0.55; pointer-events: none; }
.nav-item small { font-weight: 400; font-size: 11px; color: var(--text-muted); }
.sidebar-footer { margin-top: auto; display: grid; gap: 10px; }
.side-field { display: grid; gap: 2px; }
.side-field .native-select { margin: 0 8px; padding: 5px 8px; font-size: 13px; }
.user-box { display: grid; gap: 2px; padding-top: 10px; border-top: 1px solid var(--border); }
.user-box .side-field { margin-top: 8px; }
.user-name { padding: 0 8px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-email { padding: 0 8px; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Sign-in ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: min(380px, 100%); display: grid; gap: 14px; padding: 28px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
}
.login-card .brand { padding: 0; font-size: 17px; }
.login-card h1 { margin: 4px 0 0; }
.login-card .btn-primary { text-align: center; padding: 9px 14px; }
.role-field { border: 0; margin: 0; padding: 0; }
.role-field legend { font-size: 12px; font-weight: 600; color: var(--text-secondary); padding: 0 0 6px; }
.role-options { display: flex; gap: 18px; }

/* ---------- Folders ---------- */
.folders-layout { display: grid; grid-template-columns: minmax(220px, 300px) minmax(0, 1fr); gap: 16px; align-items: start; }
.folder-names { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 2px; }
.folder-names button {
  width: 100%; display: flex; align-items: center; gap: 8px; font: inherit; color: var(--text-primary);
  background: transparent; border: 0; border-radius: 8px; padding: 8px 10px; cursor: pointer; text-align: left;
}
.folder-names button:hover { background: var(--surface-sunken); }
.folder-names button[aria-current="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.folder-names .folder-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-names small { color: var(--text-muted); font-weight: 400; }
.new-folder { display: flex; gap: 8px; }
.new-folder input { flex: 1; }
.folder-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.file-actions { display: flex; gap: 6px; justify-content: flex-end; }
.file-link { color: var(--accent); text-decoration: none; font-weight: 500; overflow-wrap: anywhere; }
.file-link:hover { text-decoration: underline; }
progress { width: 100%; height: 8px; accent-color: var(--accent); }
@media (max-width: 900px) { .folders-layout { grid-template-columns: minmax(0, 1fr); } }

/* ---------- Sharing ---------- */
.share-form { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px 18px; }
.share-form .field { flex: 1 1 260px; }
.share-form .check { padding-bottom: 8px; }
.share-form + .folder-status { margin-top: 10px; }

.main { padding: 24px 28px 48px; display: grid; gap: 16px; align-content: start; min-width: 0; outline: none; }
.view-header { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px 16px; }
.view-header .btn { white-space: nowrap; }
.view-header p { margin: 0; }

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

/* ---------- Buttons & fields ---------- */
.btn {
  font: inherit; font-weight: 500; color: var(--text-primary);
  background: var(--surface-raised); border: 1px solid var(--border); border-radius: 8px;
  padding: 7px 14px; cursor: pointer; text-align: left;
}
a.btn { text-decoration: none; display: inline-block; }
.btn:hover:not(:disabled) { background: var(--surface-sunken); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.08); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-secondary); }
.btn-sm { padding: 5px 8px; font-size: 13px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.field { display: grid; gap: 4px; min-width: 0; }
.field > span { font-size: 12px; font-weight: 600; color: var(--text-secondary); }
input[type="text"], input[type="email"], input[type="password"], input[type="file"], .native-select {
  font: inherit; color: var(--text-primary); background: var(--surface-raised);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; min-width: 0;
}
.input-row { display: flex; gap: 8px; }
.input-row input { flex: 1; font-family: ui-monospace, Consolas, monospace; }
.inline-field { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-secondary); }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-secondary); cursor: pointer; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }

/* ---------- Filters ---------- */
.filters { display: grid; gap: 14px; }
.filter-grid { display: grid; gap: 12px; grid-template-columns: repeat(5, minmax(0, 1fr)); }
.field-metrics { grid-column: 1 / -1; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 18px; padding-top: 12px; border-top: 1px solid var(--border); }

.segmented { display: inline-flex; background: var(--surface-sunken); border-radius: 8px; padding: 3px; gap: 2px; }
.segmented button {
  font: inherit; font-size: 13px; border: 0; background: transparent; color: var(--text-secondary);
  padding: 5px 12px; border-radius: 6px; cursor: pointer;
}
.segmented button[aria-checked="true"] { background: var(--surface-raised); color: var(--text-primary); box-shadow: 0 1px 2px rgba(0,0,0,.12); font-weight: 600; }

/* Tom Select theming */
.ts-wrapper .ts-control, .ts-wrapper.single .ts-control, .ts-dropdown {
  background: var(--surface-raised); color: var(--text-primary); border-color: var(--border);
}
.ts-wrapper .ts-control { border-radius: 8px; min-height: 36px; padding: 5px 8px; box-shadow: none; }
.ts-wrapper.focus .ts-control { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.ts-control input { color: var(--text-primary); }
.ts-control input::placeholder { color: var(--text-muted); }
.ts-wrapper.multi .ts-control > div.item {
  background: var(--surface-sunken); color: var(--text-primary); border: 1px solid var(--border);
  border-radius: 6px; padding: 1px 4px 1px 6px; margin: 2px 4px 2px 0;
}
.ts-wrapper.plugin-remove_button .item .remove { border-left-color: var(--border); color: var(--text-muted); }
.ts-wrapper.plugin-remove_button .item .remove:hover { background: transparent; color: var(--text-primary); }
.ts-dropdown { border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.15); margin-top: 4px; z-index: 50; }
.ts-dropdown .option, .ts-dropdown .no-results { color: var(--text-primary); padding: 6px 10px; }
.ts-dropdown .active { background: var(--accent-soft); color: var(--text-primary); }
.ts-dropdown .optgroup-header {
  background: var(--surface-raised); color: var(--text-muted); font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em; padding: 8px 10px 4px;
}
.ts-dropdown .optgroup:before { background: var(--border); }
.metric-option { display: flex; justify-content: space-between; gap: 12px; }
.metric-option small { color: var(--text-muted); white-space: nowrap; }
.metric-item { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 10px; height: 10px; border-radius: 3px; background: var(--c); flex: none; }
.cat-icon { color: var(--text-muted); font-size: 12px; }
.metric-item.is-category { font-weight: 600; }
.metric-item.is-category small { font-weight: 400; color: var(--text-muted); }
.is-single { opacity: 0.85; }

/* ---------- Chart ---------- */
.chart-card { padding: 12px 12px 4px; transition: opacity .15s; min-height: 120px; }
.chart-card.is-loading { opacity: 0.6; }
.chart { width: 100%; }
.chart-notes:empty { display: none; }
.chart-notes { display: grid; gap: 4px; padding: 0 4px 6px; }
.note { margin: 0; font-size: 13px; color: var(--text-secondary); }
.note.error { color: var(--status-critical); }
.empty-state { padding: 48px 16px; text-align: center; color: var(--text-muted); display: grid; gap: 12px; justify-items: center; }
.empty-state p { margin: 0; }
.empty-state.error { color: var(--status-critical); }

/* ---------- Table ---------- */
.table-card { padding: 0; overflow: hidden; }
.table-header { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.table-wrap { overflow: auto; max-height: 560px; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th {
  position: sticky; top: 0; background: var(--surface); text-align: left; font-weight: 600;
  color: var(--text-secondary); font-size: 12px; padding: 8px 12px; border-bottom: 1px solid var(--border);
}
.data-table td { padding: 7px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.data-table tbody tr:hover { background: var(--surface-sunken); }
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.empty-row { text-align: center; color: var(--text-muted); padding: 24px !important; }
.cell-note { cursor: help; }
.status { white-space: nowrap; font-weight: 500; }
.status-low, .status-high { color: var(--status-critical); }
.status-normal { color: var(--status-good); }
.status-unknown { color: var(--text-muted); }
.source-link { color: var(--accent); text-decoration: none; font-weight: 500; }
.source-link:hover { text-decoration: underline; }
.source-missing { cursor: help; }

/* ---------- EEG ---------- */
.eeg-file { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 18px; padding: 12px 16px; }
.eeg-file .inline-field { flex: 1 1 280px; max-width: 560px; }
.eeg-file .native-select { flex: 1; }
.eeg-grid { grid-template-columns: repeat(6, minmax(0, 1fr)); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font: inherit; font-size: 12.5px; color: var(--text-muted); background: transparent;
  border: 1px solid var(--border); border-radius: 999px; padding: 3px 10px; cursor: pointer;
}
.chip:hover { color: var(--text-primary); }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--text-primary); font-weight: 500; }
.chip.is-bad { border-style: dashed; }
.chip.is-bad span { color: var(--status-warning); }
.eeg-nav { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 0 4px 10px; }
.eeg-window { margin-left: 8px; font-variant-numeric: tabular-nums; white-space: pre; }
.eeg-overview {
  position: relative; height: 22px; margin: 0 4px; border-radius: 6px; background: var(--surface-sunken);
  overflow: hidden; cursor: pointer;
}
.eeg-span, .eeg-tick, .eeg-view { position: absolute; top: 0; bottom: 0; }
.eeg-span { background: var(--accent); opacity: 0.35; }
.eeg-tick { width: 1px; background: var(--text-muted); }
.eeg-view { border: 2px solid var(--accent); border-radius: 3px; min-width: 5px; pointer-events: none; }
.eeg-slider { display: block; width: calc(100% - 8px); margin: 4px 4px 8px; accent-color: var(--accent); }
.eeg-events { max-height: 340px; }
.eeg-events tr[data-t] { cursor: pointer; }
.eeg-events tr.is-current { background: var(--accent-soft); }

/* ---------- Folder dialog ---------- */
.dialog {
  width: min(560px, calc(100vw - 32px)); border: 1px solid var(--border); border-radius: 14px;
  background: var(--surface); color: var(--text-primary); padding: 22px; box-shadow: 0 24px 64px rgba(0,0,0,.25);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.dialog h2 { font-size: 18px; }
.dialog > form { display: grid; gap: 12px; }
.dialog p { margin: 0; }
.folder-status { font-size: 13px; color: var(--text-secondary); min-height: 1.4em; }
.folder-status.ok { color: var(--status-good); }
.folder-status.warn { color: var(--status-warning); }
.folder-status.error { color: var(--status-critical); }
.folder-list {
  list-style: none; margin: 0; padding: 4px; border: 1px solid var(--border); border-radius: 8px;
  max-height: 240px; overflow: auto; background: var(--surface-sunken);
}
.folder-list button {
  width: 100%; display: flex; align-items: center; gap: 8px; font: inherit; color: var(--text-primary);
  background: transparent; border: 0; border-radius: 6px; padding: 7px 8px; cursor: pointer; text-align: left;
}
.folder-list button:hover { background: var(--surface-raised); }
.folder-name { flex: 1; }
.folder-none { padding: 8px; font-size: 13px; }
.badge { font-size: 11px; background: var(--accent-soft); color: var(--accent); padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.dialog-actions { display: flex; gap: 8px; align-items: center; padding-top: 4px; }
.upload-result {
  display: grid; gap: 8px; font-size: 13px; max-height: 280px; overflow: auto;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-sunken);
}
.upload-result summary { cursor: pointer; font-weight: 600; color: var(--text-secondary); }
.upload-result ul { margin: 6px 0 0; padding-left: 18px; }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) { .filter-grid, .eeg-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 760px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; gap: 12px; padding: 10px 16px; }
  .nav { flex: 1; }
  .nav-title { display: none; }
  .nav ul { display: flex; gap: 4px; }
  .sidebar-footer { margin: 0; display: flex; flex-wrap: wrap; align-items: center; }
  .user-box { border-top: 0; padding-top: 0; display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
  .user-box .side-field { margin-top: 0; }
  .user-email { display: none; }
  .main { padding: 16px; }
  .filter-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
