/* Double Pendulum Simulator — design tokens and components.
   Every colour used by the interface is a token defined here; JS reads the
   same tokens via getComputedStyle so canvas drawings follow the theme. */

/* Monochrome design system. No hue anywhere: hierarchy comes from grey
   level, weight, spacing and dash patterns. The legacy hue names (--cyan,
   --pink, ...) are kept as aliases onto grey levels so older call sites stay
   valid, but new code should use --fg..--fg-4. */
:root {
  --bg: #000000;
  --bg-2: #000000;
  --panel: #000000;
  --panel-2: #0e0e0e;
  --panel-3: #161616;
  --border: #262626;
  --border-strong: #3a3a3a;
  --grid: #161616;
  --text: #f2f2f2;
  --text-2: #8a8a8a;
  --text-3: #6b6b6b;
  --fg: #ffffff;
  --fg-2: #b0b0b0;
  --fg-3: #6b6b6b;
  --fg-4: #2e2e2e;
  --accent: var(--fg);
  --accent-ink: #000000;
  --cyan: var(--fg); --green: var(--fg); --red: var(--fg);
  --pink: var(--fg-2); --gold: var(--fg-2); --purple: var(--fg-3);
  --glass: #000000;
  --shadow: none;
  --radius: 10px;
  --radius-sm: 7px;
  --gap: 16px;
  --font: "Inter", "SF Pro Text", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: "JetBrains Mono", "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  --ease: cubic-bezier(.2, .8, .2, 1);
  --t: 150ms;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #ffffff;
  --bg-2: #ffffff;
  --panel: #ffffff;
  --panel-2: #f4f4f4;
  --panel-3: #ebebeb;
  --border: #dadada;
  --border-strong: #bdbdbd;
  --grid: #efefef;
  --text: #0a0a0a;
  --text-2: #6e6e6e;
  --text-3: #8f8f8f;
  --fg: #000000;
  --fg-2: #4a4a4a;
  --fg-3: #8f8f8f;
  --fg-4: #d6d6d6;
  --accent-ink: #ffffff;
  --glass: #ffffff;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
:root.reduced-motion * { transition: none !important; animation: none !important; }
a { color: var(--accent); }
::selection { background: var(--fg); color: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--text-2); }
.faint { color: var(--text-3); }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- shell ---------- */
.app { display: grid; grid-template-columns: 228px 1fr; height: 100vh; }
.app.nav-collapsed { grid-template-columns: 64px 1fr; }
.nav {
  background: var(--bg);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 16px 14px; }
.brand-mark { width: 34px; height: 34px; flex: none; }
.brand-name { font-weight: 750; letter-spacing: .2px; line-height: 1.05; }
.brand-name small { display: block; color: var(--text-3); font-weight: 500; font-size: 11px; letter-spacing: .3px; }
.nav-list { list-style: none; margin: 4px 0; padding: 0 10px; overflow: auto; flex: 1; }
.nav-item {
  display: flex; align-items: center; gap: 11px; width: 100%;
  padding: 8px 10px; margin: 2px 0; border: 0; border-radius: 10px; background: none;
  color: var(--text-2); cursor: pointer; text-align: left; transition: background var(--t), color var(--t);
}
.nav-item svg { width: 18px; height: 18px; flex: none; }
.nav-item:hover { background: var(--panel); color: var(--text); }
.nav-item[aria-current="page"] {
  background: var(--panel-2);
  color: var(--text); box-shadow: inset 2px 0 0 var(--fg); font-weight: 600;
}
.nav-item kbd { margin-left: auto; }
.nav-foot { padding: 10px 12px 14px; border-top: 1px solid var(--border); display: flex; gap: 6px; flex-wrap: wrap; }
.app.nav-collapsed .nav-label, .app.nav-collapsed .brand-name, .app.nav-collapsed .nav-item kbd { display: none; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 10px 18px; border-bottom: 1px solid var(--border);
  background: var(--bg); min-height: 56px;
}
.topbar h1 { font-size: 16px; margin: 0; font-weight: 650; }
.topbar .subtitle { color: var(--text-3); font-size: 12.5px; }
.topbar .spacer { flex: 1; }
.view { flex: 1; min-height: 0; overflow: auto; padding: var(--gap); }
.view.fill { overflow: hidden; }

/* ---------- layout grids ---------- */
.layout-3 { display: grid; grid-template-columns: 300px minmax(0, 1fr) 320px; gap: var(--gap); height: 100%; min-height: 0; }
.layout-2 { display: grid; grid-template-columns: 320px minmax(0, 1fr); gap: var(--gap); height: 100%; min-height: 0; }
.layout-2.wide-left { grid-template-columns: 360px minmax(0, 1fr); }
.col { display: flex; flex-direction: column; gap: var(--gap); min-height: 0; min-width: 0; }
.scroll { overflow: auto; min-height: 0; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--gap); }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--gap); }
.grid-auto { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: var(--gap); }

@media (max-width: 1280px) {
  .layout-3 { grid-template-columns: 290px minmax(0, 1fr); }
  .layout-3 > .inspector { grid-column: 1 / -1; }
  .view.fill { overflow: auto; }
  .layout-3, .layout-2 { height: auto; }
  .stage-wrap { min-height: 62vh; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .app.nav-collapsed { grid-template-columns: 1fr; }
  .nav { flex-direction: row; align-items: center; border-right: 0; border-bottom: 1px solid var(--border); overflow-x: auto; }
  .nav-list { display: flex; padding: 4px; overflow-x: auto; }
  .nav-item { width: auto; white-space: nowrap; }
  .nav-item kbd, .nav-foot, .brand-name { display: none; }
  .layout-3, .layout-2, .layout-2.wide-left, .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .view { padding: 10px; }
  body { overflow: auto; }
}

/* ---------- panels ---------- */
.panel {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); min-width: 0;
}
.panel-head { display: flex; align-items: center; gap: 8px; padding: 12px 14px 0; }
.panel-head h2, .panel-head h3 { font-size: 13px; margin: 0; font-weight: 650; letter-spacing: .2px; }
.panel-head .spacer { flex: 1; }
.panel-body { padding: 12px 14px 14px; }
.panel.flush .panel-body { padding: 0; }
.eyebrow { font-size: 11px; text-transform: uppercase; letter-spacing: 1.1px; color: var(--text-3); font-weight: 650; }

details.section { border-top: 1px solid var(--border); }
details.section:first-of-type { border-top: 0; }
details.section > summary {
  list-style: none; cursor: pointer; padding: 11px 14px; display: flex; align-items: center; gap: 8px;
  font-weight: 620; font-size: 13px; user-select: none;
}
details.section > summary::-webkit-details-marker { display: none; }
details.section > summary::after {
  content: ""; margin-left: auto; width: 7px; height: 7px; border-right: 1.6px solid var(--text-3);
  border-bottom: 1.6px solid var(--text-3); transform: rotate(-45deg); transition: transform var(--t);
}
details.section[open] > summary::after { transform: rotate(45deg); }
details.section > .section-body { padding: 2px 14px 14px; display: grid; gap: 10px; }

/* ---------- controls ---------- */
.field { display: grid; gap: 5px; }
.field-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.field label, .field .label { font-size: 12px; color: var(--text-2); display: flex; gap: 6px; align-items: center; }
.field .unit { color: var(--text-3); font-size: 11px; }
.field .value {
  width: 86px; text-align: right; background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 7px; padding: 3px 7px; font-family: var(--mono); font-size: 12px;
}
.field .value:focus { border-color: var(--accent); outline: none; }
input[type="range"] { -webkit-appearance: none; appearance: none; width: 100%; height: 22px; background: none; margin: 0; }
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px; border-radius: 4px;
  background: linear-gradient(90deg, var(--accent) var(--fill, 50%), var(--panel-3) var(--fill, 50%));
}
input[type="range"]::-moz-range-track { height: 4px; border-radius: 4px; background: var(--panel-3); }
input[type="range"]::-moz-range-progress { height: 4px; border-radius: 4px; background: var(--accent); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 14px; height: 14px; border-radius: 50%; margin-top: -5px;
  background: var(--fg); border: 0;
}
input[type="range"]::-moz-range-thumb { width: 12px; height: 12px; border-radius: 50%; background: var(--text); border: 2px solid var(--accent); }
select, input[type="text"], input[type="number"], input[type="search"], textarea {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 6px 9px; width: 100%;
  transition: border-color var(--t);
}
select:focus, input[type="text"]:focus, input[type="number"]:focus, input[type="search"]:focus, textarea:focus { border-color: var(--accent); outline: none; }
textarea { resize: vertical; min-height: 70px; }
input[type="color"] { width: 34px; height: 26px; padding: 0; border: 1px solid var(--border); border-radius: 6px; background: none; }
.check { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--text-2); cursor: pointer; }
.check input { accent-color: var(--accent); }
.seg { display: inline-flex; background: var(--panel-2); border: 1px solid var(--border); border-radius: 9px; padding: 2px; gap: 2px; flex-wrap: wrap; }
.seg button { border: 0; background: none; padding: 4px 10px; border-radius: 7px; color: var(--text-2); cursor: pointer; font-size: 12.5px; }
.seg button[aria-pressed="true"] { background: var(--panel-3); color: var(--text); box-shadow: inset 0 0 0 1px var(--border-strong); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px; padding: 7px 12px;
  border-radius: 9px; border: 1px solid var(--border-strong); background: var(--panel-2); color: var(--text);
  cursor: pointer; font-weight: 560; font-size: 13px; transition: background var(--t), border-color var(--t), transform 80ms;
  white-space: nowrap;
}
.btn:hover { background: var(--panel-3); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }
.btn.primary {
  background: var(--fg);
  border-color: transparent; color: var(--accent-ink);
}
.btn.primary:hover { background: var(--fg-2); }
.btn.ghost { background: none; border-color: transparent; color: var(--text-2); }
.btn.ghost:hover { background: var(--panel-2); color: var(--text); }
.btn.danger:hover { border-color: var(--fg); font-weight: 700; }
.btn.sm { padding: 4px 9px; font-size: 12px; border-radius: 8px; }
.btn.icon { padding: 7px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
kbd {
  font-family: var(--mono); font-size: 10.5px; color: var(--text-3); border: 1px solid var(--border);
  border-bottom-width: 2px; border-radius: 5px; padding: 0 5px; background: var(--panel-2);
}

/* ---------- stage ---------- */
.stage-wrap { position: relative; min-height: 0; flex: 1; display: flex; flex-direction: column; }
.stage {
  position: relative; flex: 1; min-height: 280px; border-radius: var(--radius); overflow: hidden;
  background: var(--bg); border: 1px solid var(--border);
}
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.stage .hud {
  position: absolute; left: 14px; top: 12px; pointer-events: none; font-family: var(--mono); font-size: 12px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 7px 10px; line-height: 1.55; min-width: 170px;
}
.stage .hud b { color: var(--text); font-weight: 600; }
.stage .hud .k { color: var(--text-3); }
.stage .toolbar { position: absolute; right: 10px; top: 10px; display: flex; gap: 6px; }
.stage .toolbar .btn { background: var(--bg); }
.stage .badge-bl { position: absolute; left: 14px; bottom: 12px; font-size: 11.5px; color: var(--text-3); pointer-events: none; }
.stage .loading {
  position: absolute; inset: 0; display: grid; place-items: center; background: color-mix(in srgb, var(--bg) 55%, transparent);
  font-size: 13px; color: var(--text-2); z-index: 3;
}
.stage:fullscreen { border-radius: 0; }
.spinner {
  width: 26px; height: 26px; border-radius: 50%; border: 2.5px solid var(--border-strong); border-top-color: var(--accent);
  animation: spin .8s linear infinite; margin: 0 auto 10px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* transport + timeline */
.transport { display: flex; align-items: center; gap: 8px; padding: 10px 2px 0; flex-wrap: wrap; }
.transport .time { font-family: var(--mono); font-size: 12.5px; min-width: 132px; color: var(--text-2); }
.timeline { position: relative; height: 38px; flex: 1; min-width: 200px; cursor: pointer; touch-action: none; }
.timeline canvas { width: 100%; height: 100%; display: block; }

/* ---------- data display ---------- */
.readouts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px 12px; font-size: 12px; }
.readouts .r { display: flex; justify-content: space-between; gap: 6px; border-bottom: 1px dashed var(--border); padding: 3px 0; }
.readouts .r span:first-child { color: var(--text-3); }
.readouts .r span:last-child { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.chart { position: relative; width: 100%; height: 150px; }
.chart.tall { height: 240px; }
.chart.xl { height: 340px; }
.chart.square { aspect-ratio: 1; height: auto; }
.chart canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.chart .tip {
  position: absolute; pointer-events: none; background: var(--bg); border: 1px solid var(--border-strong);
  border-radius: 8px; padding: 4px 8px; font: 11.5px var(--mono); white-space: nowrap; z-index: 2; display: none;
}
table.data { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.data th, table.data td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
table.data th { color: var(--text-3); font-weight: 600; font-size: 11.5px; text-transform: uppercase; letter-spacing: .6px; }
table.data td.num { font-family: var(--mono); text-align: right; font-variant-numeric: tabular-nums; }
.stat { display: grid; gap: 2px; }
.stat .v { font: 600 20px var(--mono); letter-spacing: -.3px; }
.stat .l { font-size: 11.5px; color: var(--text-3); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 12px; }
.progress { height: 2px; background: var(--panel-3); border-radius: 6px; overflow: hidden; }
.progress > div { height: 100%; width: 0; background: var(--fg); transition: width 200ms; }
.events { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; max-height: 220px; overflow: auto; font-size: 12px; }
.events li button {
  width: 100%; display: flex; gap: 8px; align-items: center; background: none; border: 0; padding: 4px 6px; border-radius: 7px;
  cursor: pointer; color: var(--text-2); text-align: left;
}
.events li button:hover { background: var(--panel-2); color: var(--text); }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.tag { display: inline-flex; align-items: center; padding: 1px 8px; border-radius: 999px; font-size: 11px; background: var(--panel-3); color: var(--text-2); border: 1px solid var(--border); }
.tag.accent { color: var(--text); border-color: var(--border-strong); }
.note {
  font-size: 12.5px; color: var(--text-2); background: var(--panel-2); border: 1px solid var(--border); border-left: 2px solid var(--fg-3);
  border-radius: 9px; padding: 9px 11px;
}
.note.warn { border-left-color: var(--fg-2); }
.note.ok { border-left-color: var(--fg); color: var(--text); }
.note.err { border-left-color: var(--fg); border-left-width: 3px; color: var(--text); font-weight: 600; }

/* cards */
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column;
  transition: border-color var(--t), transform var(--t) var(--ease);
}
.card:hover { border-color: var(--border-strong); }
.card .thumb { aspect-ratio: 16/10; background: var(--bg); display: block; width: 100%; object-fit: cover; }
.card .card-body { padding: 10px 12px 12px; display: grid; gap: 6px; }
.card h4 { margin: 0; font-size: 13.5px; }
.card p { margin: 0; font-size: 12.5px; color: var(--text-2); }
.card.selected { border-color: var(--fg); box-shadow: 0 0 0 1px var(--fg); }

/* hero / docs */
.prose { max-width: 820px; line-height: 1.65; }
.prose h2 { font-size: 22px; margin: 6px 0 8px; }
.prose h3 { font-size: 16px; margin: 22px 0 6px; }
.prose p, .prose li { color: var(--text-2); }
.prose .eq {
  font-family: "STIX Two Math", "Cambria Math", "Latin Modern Math", Georgia, serif; font-size: 16.5px; color: var(--text);
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; margin: 10px 0; overflow-x: auto;
  white-space: nowrap;
}
.lesson-list { display: grid; gap: 4px; }
.lesson-list button { text-align: left; border: 0; background: none; color: var(--text-2); padding: 7px 10px; border-radius: 8px; cursor: pointer; display: flex; gap: 10px; }
.lesson-list button[aria-current="true"] { background: var(--panel-2); color: var(--text); }
.lesson-list .n { font-family: var(--mono); color: var(--text-3); width: 18px; }

/* tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: var(--gap); flex-wrap: wrap; }
.tabs button {
  border: 0; background: none; padding: 9px 12px; color: var(--text-2); cursor: pointer; border-bottom: 2px solid transparent; font-weight: 560;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }

/* tooltip */
[data-tip] { position: relative; }
.tooltip {
  position: fixed; z-index: 1000; pointer-events: none; max-width: 280px; background: var(--panel-3); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: 8px; padding: 6px 9px; font-size: 12px; box-shadow: var(--shadow);
  opacity: 0; transform: translateY(4px); transition: opacity 120ms, transform 120ms;
}
.tooltip.show { opacity: 1; transform: none; }
.info-i { display: inline-grid; place-items: center; width: 14px; height: 14px; border-radius: 50%; border: 1px solid var(--text-3); color: var(--text-3); font-size: 9.5px; font-weight: 700; cursor: help; }

/* toasts */
.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 900; }
.toast {
  background: var(--panel-3); border: 1px solid var(--border-strong); border-radius: 11px; padding: 10px 14px; box-shadow: var(--shadow);
  max-width: 380px; font-size: 13px; animation: toast-in 220ms var(--ease);
}
.toast.err { border-color: var(--fg); font-weight: 600; }
.toast.ok { border-color: var(--border-strong); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* modal + command palette */
.overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, .72); z-index: 800; display: grid; place-items: start center; padding-top: 12vh; }
.palette { width: min(620px, 92vw); background: var(--panel); border: 1px solid var(--border-strong); border-radius: 16px; box-shadow: var(--shadow); overflow: hidden; }
.palette input { border: 0; border-bottom: 1px solid var(--border); border-radius: 0; padding: 14px 16px; font-size: 15px; background: none; }
.palette ul { list-style: none; margin: 0; padding: 6px; max-height: 50vh; overflow: auto; }
.palette li { padding: 8px 10px; border-radius: 9px; display: flex; gap: 10px; align-items: center; cursor: pointer; color: var(--text-2); }
.palette li[aria-selected="true"] { background: var(--panel-3); color: var(--text); }
.palette li .grp { margin-left: auto; font-size: 11px; color: var(--text-3); }
.modal { width: min(720px, 94vw); max-height: 80vh; overflow: auto; background: var(--panel); border: 1px solid var(--border-strong); border-radius: 16px; box-shadow: var(--shadow); padding: 18px 20px; }
.modal h3 { margin-top: 0; }

/* screensaver */
.app.screensaver .nav, .app.screensaver .topbar, .app.screensaver .hide-in-saver { display: none !important; }
.app.screensaver { grid-template-columns: 1fr; }
.app.screensaver .view { padding: 0; }
.app.screensaver .layout-3, .app.screensaver .layout-2 { grid-template-columns: 1fr; }
.app.screensaver .stage { border-radius: 0; border: 0; }

.swatch { display: inline-block; width: 100%; height: 10px; border-radius: 5px; }
.palette-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.palette-grid button { border: 1px solid var(--border); background: var(--panel-2); border-radius: 8px; padding: 6px; cursor: pointer; color: var(--text-2); font-size: 11px; display: grid; gap: 4px; }
.palette-grid button[aria-pressed="true"] { border-color: var(--accent); color: var(--text); }
.empty { text-align: center; color: var(--text-3); padding: 40px 10px; }
.split { display: grid; grid-template-columns: 1fr 1fr; gap: var(--gap); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; font-size: 12.5px; }
.kv dt { color: var(--text-3); }
.kv dd { margin: 0; font-family: var(--mono); }
hr.sep { border: 0; border-top: 1px solid var(--border); margin: 4px 0; }
