/* Rattleback simulator: workbench layout */
:root {
  color-scheme: light;
  --chrome: #e7ebed;      /* toolbar, status bar, panel headers */
  --panel: #f4f6f7;       /* side panels, dock */
  --field: #ffffff;       /* inputs, charts, cards */
  --canvas: #dce3e1;      /* 3D viewport */
  --ink: #172028;
  --ink-2: #47555e;
  --muted: #6b7880;
  --line: #cfd6da;
  --line-2: #b4bfc5;
  --accent: #1d5fa8;
  --accent-ink: #ffffff;
  --accent-soft: rgba(29, 95, 168, 0.10);
  --good: #0b7a0b; --warn: #a86a00; --bad: #c0392b;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4; --s6: #6250d6;
  --grid: #e3e8ea; --axis: #b4bfc5;
  --r: 3px; --r2: 6px;
  --font: "Barlow", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-head: "Barlow Semi Condensed", "Barlow", system-ui, sans-serif;
  --left: 304px; --right: 300px; --dock: 38vh;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --chrome: #171e22; --panel: #1d252a; --field: #141a1e; --canvas: #10161a;
    --ink: #e3e9eb; --ink-2: #aab6bb; --muted: #84929a; --line: #2c373e; --line-2: #3c4951;
    --accent: #4c8fe0; --accent-ink: #0b1218; --accent-soft: rgba(76, 143, 224, 0.16);
    --good: #3cc23c; --warn: #e0a030; --bad: #ef6b6b;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #9085e9;
    --grid: #232d33; --axis: #3c4951;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --chrome: #171e22; --panel: #1d252a; --field: #141a1e; --canvas: #10161a;
  --ink: #e3e9eb; --ink-2: #aab6bb; --muted: #84929a; --line: #2c373e; --line-2: #3c4951;
  --accent: #4c8fe0; --accent-ink: #0b1218; --accent-soft: rgba(76, 143, 224, 0.16);
  --good: #3cc23c; --warn: #e0a030; --bad: #ef6b6b;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #9085e9;
  --grid: #232d33; --axis: #3c4951;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
html, body { height: 100%; }
body { margin: 0; background: var(--panel); color: var(--ink); font: 400 14px/1.45 var(--font); -webkit-text-size-adjust: 100%; }
a { color: inherit; text-underline-offset: 3px; text-decoration-color: var(--line-2); }
a:hover { text-decoration-color: currentColor; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button, input, select { font: inherit; color: inherit; }
h1, h2, h3 { font-family: var(--font-head); font-weight: 600; margin: 0; letter-spacing: 0.005em; }
.num { font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------- shell */
.wb { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; height: 100vh; }
.workspace { display: grid; grid-template-columns: var(--left) minmax(0, 1fr) var(--right); min-height: 0; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 30px; padding: 0 11px; border-radius: var(--r); border: 1px solid var(--line-2);
  background: var(--field); color: var(--ink); cursor: pointer; font-weight: 500; white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--ink-2); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); border-color: var(--accent); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn.ghost:hover:not(:disabled) { background: var(--accent-soft); border-color: transparent; color: var(--ink); }
.btn.icon { width: 30px; padding: 0; }
.btn svg { width: 16px; height: 16px; flex: none; }
.btn[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--ink); }
kbd { font: 500 11px var(--font); border: 1px solid var(--line-2); border-bottom-width: 2px; border-radius: 3px; padding: 0 4px; color: var(--ink-2); background: var(--field); }
.btn.primary kbd { color: var(--accent-ink); border-color: rgba(255,255,255,0.45); background: transparent; }
select.sel { height: 30px; border: 1px solid var(--line-2); border-radius: var(--r); background: var(--field); padding: 0 6px; }

/* ------------------------------------------------------------- toolbar */
.toolbar {
  display: flex; align-items: center; gap: 8px; padding: 6px 12px; min-height: 46px;
  background: var(--chrome); border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 9px; margin-right: 6px; }
.brand svg { width: 30px; height: 18px; }
.brand h1 { font-size: 18px; line-height: 1; white-space: nowrap; }
.brand small { display: block; font: 400 12px var(--font); color: var(--muted); margin-top: 2px; }
.tb-sep { width: 1px; align-self: stretch; background: var(--line); margin: 2px 4px; }
.tb-label { color: var(--muted); font-size: 13px; }
.toolbar .spacer { flex: 1; }
.toolbar #preset { max-width: 280px; }

/* dropdown menu */
.menu { position: relative; }
.menu > summary { list-style: none; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-list {
  position: absolute; z-index: 30; top: calc(100% + 4px); left: 0; min-width: 240px; padding: 4px;
  background: var(--field); border: 1px solid var(--line-2); border-radius: var(--r2);
  box-shadow: 0 6px 20px rgba(10, 20, 30, 0.16);
}
.menu-list button { display: flex; width: 100%; justify-content: space-between; gap: 18px; text-align: left; padding: 7px 10px; border: 0; background: none; border-radius: var(--r); cursor: pointer; }
.menu-list button:hover:not(:disabled), .menu-list button:focus-visible { background: var(--accent-soft); }
.menu-list button:disabled { opacity: 0.45; cursor: default; }
.menu-list .meta { color: var(--muted); font-size: 12px; }

/* ------------------------------------------------------------- side panels */
.panel { background: var(--panel); min-height: 0; overflow: auto; }
.panel.left { border-right: 1px solid var(--line); }
.panel.right { border-left: 1px solid var(--line); }
.panel-head {
  position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 8px;
  padding: 9px 14px; background: var(--chrome); border-bottom: 1px solid var(--line);
}
.panel-head h2 { font-size: 15px; }
.panel-head .badge { margin-left: auto; }
.badge { font-size: 12px; padding: 1px 7px; border-radius: 10px; border: 1px solid var(--line-2); color: var(--ink-2); white-space: nowrap; }
.badge.dirty { border-color: var(--warn); color: var(--warn); }

.sec { border-bottom: 1px solid var(--line); }
.sec > summary {
  display: flex; align-items: center; gap: 8px; padding: 9px 14px; cursor: pointer; list-style: none;
  font-family: var(--font-head); font-weight: 600; font-size: 15px;
}
.sec > summary::-webkit-details-marker { display: none; }
.sec > summary::before { content: ""; width: 7px; height: 7px; border-right: 1.5px solid var(--muted); border-bottom: 1.5px solid var(--muted); transform: rotate(-45deg); transition: transform 0.12s; margin-right: 2px; }
.sec[open] > summary::before { transform: rotate(45deg); }
.sec > summary .sum { margin-left: auto; font: 400 12px var(--font); color: var(--muted); }
.sec-body { padding: 2px 14px 14px; }

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; border: 1px solid var(--line-2); border-radius: var(--r); overflow: hidden; margin: 2px 0 12px; background: var(--field); }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label { text-align: center; padding: 5px 4px; cursor: pointer; color: var(--ink-2); font-weight: 500; border-left: 1px solid var(--line-2); }
.seg label:first-of-type { border-left: 0; }
.seg input:checked + label { background: var(--accent); color: var(--accent-ink); }
.seg input:focus-visible + label { outline: 2px solid var(--accent); outline-offset: -3px; }

.f { display: grid; grid-template-columns: minmax(0, 1fr) 92px; align-items: center; gap: 3px 10px; margin: 0 0 9px; }
.f > label { color: var(--ink-2); }
.f .u { color: var(--muted); font-size: 12px; }
.f input[type=number], .f select {
  width: 100%; height: 28px; padding: 0 7px; border: 1px solid var(--line-2); border-radius: var(--r);
  background: var(--field); font-variant-numeric: tabular-nums; text-align: right;
}
.f select { text-align: left; }
.f input[type=range] { grid-column: 1 / -1; width: 100%; margin: 0; accent-color: var(--accent); height: 18px; }
.f .hint { grid-column: 1 / -1; color: var(--muted); font-size: 12px; line-height: 1.35; }
.f .err { grid-column: 1 / -1; color: var(--bad); font-size: 12px; }
.f.invalid input { border-color: var(--bad); box-shadow: 0 0 0 1px var(--bad); }
.chk { display: flex; align-items: center; gap: 8px; margin: 0 0 10px; color: var(--ink-2); cursor: pointer; }
.chk input { accent-color: var(--accent); width: 15px; height: 15px; margin: 0; }
.derived { margin: 4px 0 0; padding: 8px 10px; border-radius: var(--r); background: var(--field); border: 1px solid var(--line); color: var(--ink-2); font-size: 13px; }
.derived b { color: var(--ink); font-weight: 600; }

/* ------------------------------------------------------------- center */
.center { display: grid; grid-template-rows: minmax(180px, 1fr) auto 6px var(--dock); min-width: 0; min-height: 0; }
.viewport { position: relative; background: var(--canvas); overflow: hidden; min-height: 0; }
.viewport canvas { display: block; width: 100%; height: 100%; }
.hud { position: absolute; left: 12px; top: 10px; pointer-events: none; display: grid; gap: 1px; font-variant-numeric: tabular-nums; }
.hud .big { font-family: var(--font-head); font-size: 24px; font-weight: 600; line-height: 1.1; }
.hud .small { color: var(--ink-2); font-size: 13px; }
.hud .tag { color: var(--warn); font-size: 12px; }
.vp-tools { position: absolute; right: 10px; top: 10px; display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; max-width: 70%; }
.vp-group { display: flex; background: var(--field); border: 1px solid var(--line-2); border-radius: var(--r); overflow: hidden; }
.vp-group button { height: 28px; padding: 0 9px; border: 0; border-left: 1px solid var(--line); background: transparent; cursor: pointer; color: var(--ink-2); font-size: 13px; font-weight: 500; }
.vp-group button:first-child { border-left: 0; }
.vp-group button:hover { color: var(--ink); background: var(--accent-soft); }
.vp-group button[aria-pressed="true"] { color: var(--ink); background: var(--accent-soft); box-shadow: inset 0 -2px 0 var(--accent); }
.vp-group select { height: 28px; border: 0; background: transparent; padding: 0 6px; font-size: 13px; color: var(--ink-2); }
.vp-empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--ink-2); text-align: center; padding: 20px; pointer-events: none; }
.axes-key { position: absolute; left: 12px; bottom: 10px; display: flex; gap: 12px; color: var(--ink-2); font-size: 12px; pointer-events: none; }
.axes-key i { display: inline-block; width: 10px; height: 10px; border-radius: 50%; vertical-align: -1px; margin-right: 4px; }

.transport { display: flex; align-items: center; gap: 6px; padding: 6px 10px; background: var(--chrome); border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.timeline { position: relative; flex: 1; min-width: 160px; height: 30px; display: flex; align-items: center; }
.timeline input { width: 100%; margin: 0; accent-color: var(--accent); }
.timeline .marker { position: absolute; top: 2px; bottom: 2px; width: 2px; background: var(--s3); border-radius: 1px; pointer-events: none; }
.timeline .marker::after { content: attr(data-label); position: absolute; top: -2px; left: 5px; font-size: 11px; color: var(--ink-2); white-space: nowrap; }
.clock { min-width: 112px; text-align: right; color: var(--ink-2); font-variant-numeric: tabular-nums; }

.splitter { cursor: row-resize; background: var(--chrome); border-bottom: 1px solid var(--line); position: relative; touch-action: none; }
.splitter::after { content: ""; position: absolute; left: 50%; top: 2px; width: 36px; height: 2px; margin-left: -18px; border-radius: 1px; background: var(--line-2); }
.splitter:hover::after, .splitter:focus-visible::after { background: var(--accent); }

/* dock */
.dock { display: grid; grid-template-rows: auto minmax(0, 1fr); min-height: 0; background: var(--panel); }
.tabs { display: flex; gap: 2px; padding: 0 8px; background: var(--chrome); border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs button { position: relative; padding: 8px 12px 7px; border: 0; background: none; cursor: pointer; color: var(--ink-2); font-weight: 500; white-space: nowrap; }
.tabs button[aria-selected="true"] { color: var(--ink); box-shadow: inset 0 -2px 0 var(--accent); }
.tabs button .count { margin-left: 5px; font-size: 11px; color: var(--muted); }
.tab-panel { overflow: auto; padding: 12px; min-height: 0; }

.charts { display: grid; grid-template-columns: repeat(auto-fill, minmax(380px, 1fr)); gap: 12px; }
.chart { background: var(--field); border: 1px solid var(--line); border-radius: var(--r2); padding: 8px 8px 2px; min-width: 0; position: relative; }
.chart h3 { font-size: 14px; margin: 0 0 0 4px; }
.chart .sub { color: var(--muted); font-size: 12px; margin: 0 0 2px 4px; }
.chart .plot { cursor: crosshair; }
.dock-note { color: var(--muted); margin: 0 0 10px; font-size: 13px; }

.split2 { display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr); gap: 12px; align-items: start; }
.tbl-wrap { background: var(--field); border: 1px solid var(--line); border-radius: var(--r2); overflow: auto; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl th, .tbl td { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.tbl th { position: sticky; top: 0; background: var(--chrome); color: var(--ink-2); font-weight: 600; z-index: 1; }
.tbl td.n, .tbl th.n { text-align: right; font-variant-numeric: tabular-nums; }
.tbl tr.current td { background: var(--accent-soft); }
.tbl .sw { display: inline-block; width: 12px; height: 3px; border-radius: 2px; vertical-align: middle; }
.tbl button.link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0 4px; }
.tbl input[type=checkbox] { accent-color: var(--accent); }
.tbl td.label { max-width: 260px; overflow: hidden; text-overflow: ellipsis; }

.sweep-form { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: end; margin-bottom: 12px; }
.sweep-form label { display: grid; gap: 3px; color: var(--ink-2); font-size: 13px; }
.sweep-form input, .sweep-form select { height: 30px; border: 1px solid var(--line-2); border-radius: var(--r); background: var(--field); padding: 0 7px; min-width: 0; }
.sweep-form input[type=number] { width: 92px; text-align: right; font-variant-numeric: tabular-nums; }

.log { margin: 0; padding: 10px 12px; background: var(--field); border: 1px solid var(--line); border-radius: var(--r2); font: 13px/1.6 var(--font); color: var(--ink-2); list-style: none; }
.log li { display: grid; grid-template-columns: 70px 1fr; gap: 10px; }
.log time { color: var(--muted); font-variant-numeric: tabular-nums; }
.log .e { color: var(--bad); }

.doc { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 24px; max-width: 1200px; }
.doc section { max-width: 68ch; }
.doc h2 { font-size: 18px; margin-bottom: 6px; }
.doc h3 { font-size: 15px; margin: 14px 0 4px; }
.doc p, .doc li { color: var(--ink-2); margin: 0 0 8px; }
.doc ul { padding-left: 18px; margin: 0 0 8px; }
.eq { font-family: "Cambria Math", "STIX Two Math", "Latin Modern Math", Georgia, serif; font-size: 16px; color: var(--ink); background: var(--field); border: 1px solid var(--line); border-radius: var(--r); padding: 8px 12px; margin: 8px 0; overflow-x: auto; white-space: nowrap; }

/* ------------------------------------------------------------- inspector */
.ins-sec { padding: 12px 14px; border-bottom: 1px solid var(--line); }
.ins-sec h3 { font-size: 14px; margin-bottom: 6px; display: flex; align-items: baseline; gap: 8px; }
.ins-sec h3 .sum { margin-left: auto; font: 400 12px var(--font); color: var(--muted); }
.run-label { color: var(--muted); font-size: 12px; margin: -2px 0 8px; }
.kv { display: grid; grid-template-columns: minmax(0, 1fr) auto; margin: 0; }
.kv dt { color: var(--ink-2); padding: 5px 0; border-top: 1px solid var(--line); }
.kv dd { margin: 0; padding: 5px 0; border-top: 1px solid var(--line); text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.kv dd small { font-weight: 400; color: var(--muted); font-size: 12px; margin-left: 2px; }
.kv.live dd { font-weight: 500; }
.verdict { margin: 10px 0 0; padding: 8px 10px; border-left: 3px solid var(--accent); background: var(--field); color: var(--ink); line-height: 1.4; }
.checks { list-style: none; margin: 0; padding: 0; display: grid; gap: 5px; }
.checks li { display: grid; grid-template-columns: 16px minmax(0, 1fr); column-gap: 6px; align-items: baseline; color: var(--ink-2); }
.checks .ok { color: var(--good); font-weight: 700; }
.checks .no { color: var(--bad); font-weight: 700; }
.checks .val { grid-column: 2; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------- status bar */
.statusbar { display: flex; align-items: center; gap: 14px; padding: 3px 12px; min-height: 26px; background: var(--chrome); border-top: 1px solid var(--line); font-size: 12px; color: var(--ink-2); overflow: hidden; white-space: nowrap; }
.statusbar .state { display: flex; align-items: center; gap: 6px; font-weight: 600; color: var(--ink); }
.statusbar .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--good); }
.statusbar .dot.busy { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
.statusbar .dot.err { background: var(--bad); }
.statusbar .bar { width: 120px; height: 4px; border-radius: 2px; background: var(--line); overflow: hidden; }
.statusbar .bar > div { height: 100%; width: 0; background: var(--accent); }
.statusbar .grow { flex: 1; overflow: hidden; text-overflow: ellipsis; }
@keyframes pulse { 50% { opacity: 0.35; } }

/* dialog */
dialog { border: 1px solid var(--line-2); border-radius: var(--r2); background: var(--field); color: var(--ink); padding: 18px 20px; max-width: 520px; width: calc(100% - 32px); }
dialog::backdrop { background: rgba(10, 20, 30, 0.35); }
dialog h2 { font-size: 18px; margin-bottom: 10px; }
dialog table { width: 100%; border-collapse: collapse; }
dialog td { padding: 5px 0; border-bottom: 1px solid var(--line); color: var(--ink-2); }
dialog td:first-child { white-space: nowrap; padding-right: 16px; color: var(--ink); }
dialog .close { float: right; }

/* toast */
.toast { position: fixed; left: 50%; bottom: 40px; transform: translateX(-50%); background: var(--ink); color: var(--panel); padding: 8px 14px; border-radius: var(--r2); font-size: 13px; z-index: 50; }

/* uPlot */
.uplot { font-family: var(--font); }
.u-legend { font-size: 12px; color: var(--ink-2); text-align: left; padding-left: 2px; }
.u-legend .u-marker { border-radius: 2px; }
.u-legend td { font-variant-numeric: tabular-nums; }
.u-select { background: var(--accent-soft); }

/* ------------------------------------------------------------- responsive */
@media (max-width: 1280px) { :root { --left: 280px; --right: 270px; } .charts { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); } }
@media (max-width: 1060px) {
  .wb { height: auto; display: block; }
  .toolbar { position: sticky; top: 0; z-index: 20; }
  .workspace { display: flex; flex-direction: column; }
  .center { display: contents; }
  .viewport { order: 1; } .transport { order: 2; } .panel.right { order: 3; } .panel.left { order: 4; } .dock { order: 5; }
  .panel { overflow: visible; }
  .panel.left, .panel.right { border: 0; border-bottom: 1px solid var(--line); }
  .panel-head { position: static; }
  .viewport { height: 58vw; min-height: 260px; max-height: 70vh; }
  .splitter { display: none; }
  .dock { display: block; }
  .tab-panel { overflow: visible; }
  .split2 { grid-template-columns: 1fr; }
  .statusbar { position: sticky; bottom: 0; }
  .toolbar #preset { max-width: none; flex: 1 1 200px; }
}
@media (max-width: 560px) {
  .brand small, .tb-label, .toolbar .hide-sm, .toolbar .tb-sep, #helpBtn { display: none; }
  .toolbar { gap: 6px; padding: 6px 10px; }
  .brand { order: 0; margin-right: auto; } .brand h1 { font-size: 16px; } .hide-xs { display: none; }
  #exportMenu { order: 1; } #themeBtn { order: 2; }
  .toolbar #preset { order: 3; flex: 1 1 170px; min-width: 0; }
  #runBtn { order: 4; } #stopBtn { order: 5; }
  .toolbar .spacer { display: none; }
  .menu-list { left: auto; right: 0; }
  .transport { flex-wrap: wrap; }
  .transport .timeline { order: 10; flex: 1 1 100%; }
  .charts { grid-template-columns: 1fr; }
  .clock { min-width: 0; }
  .vp-tools { max-width: 100%; left: 10px; top: auto; bottom: 10px; justify-content: flex-start; }
  .axes-key { display: none; }
}
@media (prefers-reduced-motion: reduce) { .statusbar .dot.busy { animation: none; } .sec > summary::before { transition: none; } }
