:root {
  --font: "Avenir Next", "Segoe UI Variable Text", "Segoe UI", system-ui, sans-serif;
  --bench: #E3E7EB;
  --bench-2: #D3D9DF;
  --bench-3: #BCC5CE;
  --sheet: #FFFFFF;
  --ink: #16202B;
  --muted: #5B6875;
  --grid: #BFD0DF;
  --grid-minor: #E4EDF5;
  --accent: #1F5FBF;
  --sel: rgba(31, 95, 191, .13);
  --warn: #B3261E;
  --s1: #C8102E; --s2: #1F5FBF; --s3: #1E8A4C; --s4: #7B3FA0; --s5: #C96A00; --s6: #0E7C86;
  color-scheme: light;
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bench: #1D232A;
  --bench-2: #2A323B;
  --bench-3: #3B4652;
  --sheet: #12161B;
  --ink: #E4E9EE;
  --muted: #98A4B0;
  --grid: #33414F;
  --grid-minor: #1E2731;
  --accent: #6AA5FF;
  --sel: rgba(106, 165, 255, .16);
  --warn: #FF8A80;
  --s1: #FF5A6E; --s2: #6AA5FF; --s3: #4CC38A; --s4: #B88AE0; --s5: #F0A040; --s6: #45C1CC;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column;
  font: 13px/1.35 var(--font); color: var(--ink); background: var(--bench);
  -webkit-font-smoothing: antialiased; overflow: hidden;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ---- top chrome ---- */
#top { border-bottom: 1px solid var(--bench-3); flex: none; }
#menubar { display: flex; align-items: center; gap: 4px; padding: 0 8px; height: 30px; }
#brand { font-weight: 700; letter-spacing: -.01em; font-size: 14px; margin-right: 10px; }
#menus { display: flex; }
#menus button { border: 0; background: none; padding: 5px 9px; border-radius: 3px; }
#menus button:hover, #menus button[aria-expanded="true"] { background: var(--bench-2); }
#title {
  margin-left: auto; width: min(34vw, 320px); text-align: right;
  border: 1px solid transparent; background: transparent; padding: 3px 7px; border-radius: 3px; font-weight: 600;
}
#title:hover { border-color: var(--bench-3); }
#title:focus { background: var(--sheet); border-color: var(--accent); outline: 0; text-align: left; }

#toolbar { display: flex; align-items: center; gap: 2px; padding: 3px 6px 5px; overflow-x: auto; }
#toolbar button {
  display: inline-flex; align-items: center; gap: 5px; flex: none;
  height: 28px; min-width: 30px; padding: 0 6px; border: 1px solid transparent; border-radius: 3px; background: none;
}
#toolbar button:hover { background: var(--bench-2); }
#toolbar button[aria-pressed="true"] { background: var(--sheet); border-color: var(--bench-3); color: var(--accent); }
#toolbar button:disabled { opacity: .38; cursor: default; background: none; }
#toolbar svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
#toolbar .sep { width: 1px; height: 20px; background: var(--bench-3); margin: 0 5px; flex: none; }

/* ---- work area ---- */
#work { flex: 1; display: flex; min-height: 0; }
#tablePane { width: var(--table-w, 402px); min-width: 140px; max-width: 70vw; background: var(--sheet); display: flex; }
#split { width: 6px; cursor: col-resize; flex: none; border-left: 1px solid var(--bench-3); border-right: 1px solid var(--bench-3); }
#split:hover { background: var(--bench-2); }
#graphPane { flex: 1; min-width: 0; position: relative; background: var(--sheet); }
#graph { position: absolute; inset: 0; touch-action: none; }

/* ---- table ---- */
#table { flex: 1; overflow: auto; position: relative; outline: 0; font-variant-numeric: tabular-nums; }
#thead { position: sticky; top: 0; z-index: 2; display: flex; height: 46px; background: var(--bench); border-bottom: 1px solid var(--bench-3); }
#thead .corner { width: 48px; flex: none; border-right: 1px solid var(--bench-3); }
#thead .th {
  width: 116px; flex: none; border: 0; border-right: 1px solid var(--bench-3); background: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 0 6px; position: relative;
}
#thead .th:hover { background: var(--bench-2); }
#thead .th .nm { font-weight: 600; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#thead .th .un { color: var(--muted); font-size: 12px; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#thead .th.plotted::before { content: ""; position: absolute; left: 0; right: 0; top: 0; height: 3px; background: var(--chip); }
#thead .th.isx::before { content: ""; position: absolute; left: 0; right: 0; top: 0; height: 3px; background: var(--muted); }
#thead .th.err .nm { color: var(--warn); }
#thead .addcol { width: 34px; border: 0; background: none; font-size: 17px; color: var(--muted); }
#thead .addcol:hover { color: var(--ink); background: var(--bench-2); }
#tbody { position: relative; }
#rows .tr { position: absolute; left: 0; display: flex; height: 24px; }
#rows .rn { width: 48px; flex: none; text-align: right; padding: 3px 8px 0 0; color: var(--muted); font-size: 12px; background: var(--bench); border-right: 1px solid var(--bench-3); border-bottom: 1px solid var(--bench-2); }
#rows .td {
  width: 116px; flex: none; padding: 3px 8px 0; text-align: right; white-space: nowrap; overflow: hidden;
  border-right: 1px solid var(--bench-2); border-bottom: 1px solid var(--bench-2); cursor: cell;
}
#rows .td.ro { color: var(--muted); }
#rows .td.sel { background: var(--sel); }
#rows .td.cur { box-shadow: inset 0 0 0 2px var(--accent); }
.cellEdit {
  position: absolute; z-index: 1; border: 2px solid var(--accent); background: var(--sheet); padding: 0 6px; text-align: right;
  font-variant-numeric: tabular-nums; outline: 0;
}

/* ---- status ---- */
#status { flex: none; display: flex; justify-content: space-between; gap: 16px; padding: 3px 10px; border-top: 1px solid var(--bench-3); color: var(--muted); font-size: 12px; min-height: 23px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; }

/* ---- menus ---- */
.menu {
  position: fixed; z-index: 50; min-width: 190px; padding: 4px; background: var(--sheet); border: 1px solid var(--bench-3);
  border-radius: 4px; box-shadow: 0 6px 18px rgba(10, 20, 30, .22); max-height: 80vh; overflow: auto;
}
.menu button { display: flex; width: 100%; gap: 6px; align-items: center; border: 0; background: none; padding: 5px 10px 5px 4px; border-radius: 3px; text-align: left; }
.menu button:hover:not(:disabled), .menu button:focus-visible { background: var(--accent); color: #fff; outline: 0; }
.menu button:disabled { opacity: .4; cursor: default; }
.menu .ck { width: 16px; text-align: center; flex: none; }
.menu .lb { flex: 1; }
.menu .kb { color: var(--muted); font-size: 12px; padding-left: 14px; }
.menu button:hover:not(:disabled) .kb { color: inherit; }
.menu hr { border: 0; border-top: 1px solid var(--bench-2); margin: 4px 0; }

/* ---- dialogs ---- */
dialog.dlg { border: 1px solid var(--bench-3); border-radius: 6px; padding: 0; background: var(--bench); color: var(--ink); width: min(520px, calc(100vw - 32px)); box-shadow: 0 18px 50px rgba(10, 20, 30, .35); }
dialog.dlg.wide { width: min(640px, calc(100vw - 32px)); }
dialog.dlg::backdrop { background: rgba(10, 20, 30, .32); }
.dlg form { padding: 16px 18px 14px; }
.dlg h2 { margin: 0 0 12px; font-size: 15px; }
.dlg label { display: grid; grid-template-columns: 96px 1fr; align-items: center; gap: 8px; margin-bottom: 8px; }
.dlg label.check { display: flex; gap: 8px; }
.dlg input:not([type=checkbox]), .dlg textarea, .dlg select { border: 1px solid var(--bench-3); background: var(--sheet); border-radius: 3px; padding: 5px 7px; width: 100%; min-width: 0; }
.dlg textarea { resize: vertical; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; }
.dlg .pair { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.dlg .chips { display: flex; flex-wrap: wrap; gap: 4px; margin: 0 0 8px 104px; }
.dlg .chips button { border: 1px solid var(--bench-3); background: var(--sheet); border-radius: 3px; padding: 2px 7px; font-size: 12px; }
.dlg .chips button:hover { border-color: var(--accent); }
.dlg .note { color: var(--muted); font-size: 12px; margin: 0 0 8px; max-width: 62ch; }
.dlg .derr { color: var(--warn); min-height: 18px; margin: 6px 0 0; }
.dlg .dbtns { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.dlg .dbtns button { border: 1px solid var(--bench-3); background: var(--sheet); border-radius: 4px; padding: 5px 14px; }
.dlg .dbtns button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.dlg .dbtns button.danger { margin-right: auto; color: var(--warn); }
.fitgrid { display: grid; grid-template-columns: 170px 1fr; gap: 14px; }
.models { display: flex; flex-direction: column; border: 1px solid var(--bench-3); background: var(--sheet); border-radius: 3px; padding: 3px; max-height: 300px; overflow: auto; }
.models button { border: 0; background: none; text-align: left; padding: 4px 8px; border-radius: 3px; }
.models button[aria-selected="true"] { background: var(--accent); color: #fff; }
.eq { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; margin-bottom: 8px; word-break: break-word; }
.fitout { margin: 8px 0 0; padding: 8px 10px; background: var(--sheet); border: 1px solid var(--bench-3); border-radius: 3px; min-height: 110px; font: 12.5px/1.5 ui-monospace, Menlo, Consolas, monospace; white-space: pre-wrap; }
.help dt { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; margin-top: 7px; }
.help dd { margin: 0 0 0 0; color: var(--muted); }
.help h3 { font-size: 13px; margin: 14px 0 2px; }
.help { max-height: 60vh; overflow: auto; padding-right: 6px; }

@media (max-width: 720px) {
  #work { flex-direction: column-reverse; }
  #tablePane { width: auto; max-width: none; height: 38%; min-height: 120px; border-top: 1px solid var(--bench-3); }
  #split { display: none; }
  #title { width: 38vw; }
  #toolbar button .lbl { display: none; }
}
@media print {
  #top, #status, #tablePane, #split { display: none; }
  body { overflow: visible; background: #fff; }
}
#coords { white-space: pre; }
#themeBtn { border: 0; background: none; padding: 4px 9px; border-radius: 3px; color: var(--muted); flex: none; }
#themeBtn:hover { background: var(--bench-2); color: var(--ink); }
