/* viSHual UI: "paper and ink".
   Content first: warm paper surfaces, one ink-blue accent, quiet chrome. Every colour, space,
   radius, size, shadow and duration is a token below; the rest of the file (and cards.css) only
   uses tokens. Light and dark are both real themes: variables follow prefers-color-scheme, and an
   explicit data-theme="light|dark" on <html> (set by the header toggle) wins.
   Text/background pairs are listed with their measured contrast in docs/design.md. */

:root {
  color-scheme: light;

  /* colour: surfaces */
  --bg: #f6f3ec;
  --surface: #fffdf8;
  --panel: #efebe0;
  --sunken: #e8e3d6;
  --border: #ddd6c6;
  --border-strong: #8f8672; /* controls: >= 3:1 on surfaces */
  /* colour: text */
  --text: #1d1b16;
  --text-2: #47443a;
  --text-3: #625d4f;
  /* colour: accent and status */
  --accent: #2a45b8;
  --accent-ink: #ffffff;
  --accent-soft: #e3e8fa;
  --accent-text: #1f3794;
  --amber: #f5b83d;
  --danger: #a8231b;
  --danger-soft: #fbe8e5;
  --warn-text: #7a4a00;
  --warn-soft: #fbefd0;
  --ok: #1b6535;
  --focus: #2a45b8;
  --scrim: rgb(29 27 22 / 0.45);
  /* the graph plate is white in both themes (export and print parity); everything drawn on it
     (tick labels, floating controls) uses these fixed warm inks, never the theme text colours */
  --plate: #ffffff;
  --plate-edge: #cfc8b6;
  --plate-ink: #2b2821;     /* 14.9:1 on white */
  --plate-ink-2: #57524a;   /* 7.6:1 on white: tick labels, captions */
  --plate-line: #d6cfbf;    /* control borders on the plate */
  --plate-hover: #f4f1ea;
  --plate-accent: #2a45b8;  /* 7.9:1 on white: focus rings, slice frame, pressed keys on the plate */

  /* space (4px scale) */
  --s-1: 4px; --s-2: 8px; --s-3: 12px; --s-4: 16px; --s-5: 24px; --s-6: 32px; --s-7: 48px;
  /* radius */
  --r-sm: 6px; --r-md: 10px; --r-lg: 16px; --r-pill: 999px;
  /* type */
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-display: "Iowan Old Style", Charter, "Bitstream Charter", "Palatino Linotype", Palatino, "Book Antiqua", "Noto Serif", Georgia, serif;
  /* the maths face, for slider names, window bounds and table heads (MathLive renders the rows) */
  --font-math: "STIX Two Text", "Latin Modern Roman", Cambria, "Times New Roman", serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, "Liberation Mono", monospace;
  --fs-xs: 0.75rem; --fs-sm: 0.8125rem; --fs-md: 0.9375rem; --fs-lg: 1.0625rem; --fs-xl: 1.375rem; --fs-2xl: 1.75rem;
  /* elevation */
  --shadow-1: 0 1px 2px rgb(60 48 20 / 0.08), 0 0 0 1px rgb(60 48 20 / 0.02);
  --shadow-2: 0 6px 18px rgb(60 48 20 / 0.14), 0 1px 3px rgb(60 48 20 / 0.1);
  --shadow-3: 0 24px 56px rgb(40 30 10 / 0.3);
  /* motion */
  --dur-1: 120ms; --dur-2: 220ms; --dur-3: 320ms;
  --ease: cubic-bezier(0.2, 0, 0, 1);
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1); /* entrances: popovers, trays */
  --press: scale(0.96);                      /* :active feedback on compact controls */
  /* layout */
  --header-h: 52px;
  --peek-h: 60px;
  --touch: 44px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #14151a;
    --surface: #1d1f26;
    --panel: #191b21;
    --sunken: #252832;
    --border: #2d313b;
    --border-strong: #6b7184;
    --text: #ecebe6;
    --text-2: #bcbbb3;
    --text-3: #9293a0;
    --accent: #93a6ff;
    --accent-ink: #0f1330;
    --accent-soft: #283056;
    --accent-text: #bac5ff;
    --danger: #ff9087;
    --danger-soft: #3b1f1f;
    --warn-text: #f3c872;
    --warn-soft: #3a3015;
    --ok: #7fdc9f;
    --focus: #93a6ff;
    --scrim: rgb(0 0 0 / 0.62);
    --plate-edge: #3a3f4d;
    --shadow-1: 0 1px 2px rgb(0 0 0 / 0.4);
    --shadow-2: 0 8px 22px rgb(0 0 0 / 0.5), 0 0 0 1px rgb(255 255 255 / 0.04);
    --shadow-3: 0 24px 56px rgb(0 0 0 / 0.6);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #14151a;
  --surface: #1d1f26;
  --panel: #191b21;
  --sunken: #252832;
  --border: #2d313b;
  --border-strong: #6b7184;
  --text: #ecebe6;
  --text-2: #bcbbb3;
  --text-3: #9293a0;
  --accent: #93a6ff;
  --accent-ink: #0f1330;
  --accent-soft: #283056;
  --accent-text: #bac5ff;
  --danger: #ff9087;
  --danger-soft: #3b1f1f;
  --warn-text: #f3c872;
  --warn-soft: #3a3015;
  --ok: #7fdc9f;
  --focus: #93a6ff;
  --scrim: rgb(0 0 0 / 0.62);
  --plate-edge: #3a3f4d;
  --shadow-1: 0 1px 2px rgb(0 0 0 / 0.4);
  --shadow-2: 0 8px 22px rgb(0 0 0 / 0.5), 0 0 0 1px rgb(255 255 255 / 0.04);
  --shadow-3: 0 24px 56px rgb(0 0 0 / 0.6);
}

/* Old variable names, kept as aliases so nothing that still says --color-* breaks. */
:root {
  --color-primary: var(--accent);
  --color-primary-light: var(--accent);
  --color-primary-lighter: var(--accent);
  --color-primary-faint: var(--accent-soft);
  --color-accent: var(--amber);
  --color-bg: var(--bg);
  --color-bg-card: var(--surface);
  --color-bg-panel: var(--panel);
  --color-bg-input: var(--surface);
  --color-border: var(--border);
  --color-border-light: var(--border);
  --color-text: var(--text);
  --color-text-secondary: var(--text-2);
  --color-text-muted: var(--text-3);
  --color-error: var(--danger);
  --color-error-bg: var(--danger-soft);
  --color-success: var(--ok);
  --on-primary: var(--accent-ink);
  --radius: var(--r-md);
}

/* ---------- Base ---------- */

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--fs-md);
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  text-rendering: optimizeLegibility;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.45; }
h1, h2, h3, p, ul, ol { margin: 0; }
ul, ol { padding: 0; list-style: none; }
a { color: var(--accent-text); }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

::selection { background: var(--accent-soft); color: var(--text); }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px; margin: -1px; padding: 0; border: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

.skip-link {
  position: absolute; left: var(--s-3); top: -48px; z-index: 100;
  padding: var(--s-2) var(--s-4); border-radius: var(--r-md);
  background: var(--accent); color: var(--accent-ink); font-weight: 600; text-decoration: none;
  transition: top var(--dur-1) var(--ease);
}
.skip-link:focus { top: var(--s-2); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---------- Shell ---------- */

.app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; background: var(--bg); }

.app-header {
  display: flex; align-items: center; gap: var(--s-3);
  height: var(--header-h); padding: 0 var(--s-4); flex-shrink: 0;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: inline-flex; align-items: center; gap: var(--s-2); color: var(--text); text-decoration: none; border-radius: var(--r-md); }
.brand-name { font-family: var(--font-display); font-size: var(--fs-xl); letter-spacing: 0.01em; line-height: 1; }
.brand-name b { font-weight: 700; color: var(--accent-text); }
.logo-mark { display: block; flex-shrink: 0; }
.logo-tile { fill: var(--accent); }
.logo-axes { stroke: var(--accent-ink); opacity: 0.6; }
.logo-curve { stroke: var(--accent-ink); }
.logo-dot { fill: var(--amber); }
.tagline { color: var(--text-3); font-size: var(--fs-sm); margin-left: var(--s-3); padding-left: var(--s-4); border-left: 1px solid var(--border); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.header-right { display: flex; align-items: center; gap: var(--s-1); margin-left: auto; }

.app-main { display: flex; flex: 1; min-height: 0; overflow: hidden; --panel-w: 380px; }

aside.left {
  display: flex; flex-direction: column; flex-shrink: 0;
  width: var(--panel-w); height: 100%; overflow: hidden;
  background: var(--panel);
}
aside.left.is-collapsed { display: none; }
.side-head {
  display: flex; align-items: center; justify-content: space-between; gap: var(--s-2);
  padding: var(--s-2) var(--s-3); flex-shrink: 0;
  border-bottom: 1px solid var(--border); background: var(--panel);
}
.side-tabs { display: flex; gap: 2px; padding: 3px; background: var(--sunken); border-radius: var(--r-md); }
.side-tab {
  padding: 6px 14px; border: 0; border-radius: 8px; background: transparent;
  font-size: var(--fs-sm); font-weight: 600; color: var(--text-2); white-space: nowrap;
  transition: background var(--dur-1) var(--ease), color var(--dur-1) var(--ease);
}
.side-tab:hover { color: var(--text); }
.side-tab.active { background: var(--surface); color: var(--accent-text); box-shadow: var(--shadow-1); }
.side-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.side-body > .panel { flex: 1; min-height: 0; }

.resizer {
  position: relative; flex-shrink: 0; width: 10px; margin: 0 -4px 0 -5px; z-index: 5;
  cursor: col-resize; touch-action: none; display: flex; justify-content: center;
}
.resizer-grip { width: 1px; height: 100%; background: var(--border); transition: background var(--dur-1), width var(--dur-1); }
.resizer:hover .resizer-grip, .resizer.dragging .resizer-grip { width: 3px; background: var(--accent); }
.resizer:focus-visible { outline-offset: -2px; }
.resizer:focus-visible .resizer-grip { width: 3px; background: var(--accent); }
.app.side-collapsed .resizer { display: none; }

.right { flex: 1; min-width: 0; height: 100%; overflow-y: auto; display: flex; flex-direction: column; scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
.right:focus { outline: none; }

/* ---------- Buttons and controls ---------- */

.side-head .sheet-toggle { display: none; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; padding: 0; border: 0; border-radius: var(--r-md);
  background: transparent; color: var(--text-2); line-height: 1;
  transition: background var(--dur-1) var(--ease), color var(--dur-1) var(--ease);
}
.icon-btn:hover { background: var(--sunken); color: var(--text); }
.icon-btn.danger { color: var(--danger); }
.icon-btn.danger:hover { background: var(--danger-soft); }
.icon-btn.lg { width: 40px; height: 40px; }

.link-btn {
  border: 0; background: transparent; padding: 4px 10px; border-radius: var(--r-sm);
  font-size: var(--fs-sm); font-weight: 600; color: var(--accent-text);
}
.link-btn:hover { background: var(--accent-soft); }

.btn {
  padding: 10px 16px; font-size: var(--fs-sm); font-weight: 600;
  border-radius: var(--r-md); border: 1px solid transparent;
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2);
  transition: background var(--dur-1) var(--ease), border-color var(--dur-1) var(--ease), transform var(--dur-1) var(--ease);
}
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn-sm { padding: 5px 12px; font-size: var(--fs-xs); }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn-outline { background: transparent; color: var(--accent-text); border-color: var(--border-strong); }
.btn-outline:hover:not(:disabled) { background: var(--accent-soft); }
.btn-ghost { background: transparent; color: var(--text-2); }
.btn-ghost:hover:not(:disabled) { background: var(--sunken); color: var(--text); }
.btn.wide, .wide { width: 100%; }

.input, .select {
  padding: 9px 12px; font-size: var(--fs-md);
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--r-md);
  min-width: 0;
  transition: border-color var(--dur-1) var(--ease), box-shadow var(--dur-1) var(--ease);
}
.input:focus-visible, .select:focus-visible { outline: 2px solid var(--focus); outline-offset: 0; border-color: var(--focus); }
.input { width: 100%; }
.input::placeholder { color: var(--text-3); opacity: 1; }
.select { cursor: pointer; width: auto; padding: 6px 8px; font-size: var(--fs-sm); color: var(--text-2); border-radius: var(--r-sm); }
.select.wide { width: 100%; padding: 9px 12px; font-size: var(--fs-md); border-radius: var(--r-md); }
.input.grow { flex: 1; }
.input.w-16 { width: 4.5rem; }
.input.w-14 { width: 4rem; }
.input[type="number"], .input.w-16, .input.w-14 { padding: 7px 8px; font-size: var(--fs-sm); }
.range { accent-color: var(--accent); }
.range.w-12 { width: 3.5rem; }
.color-input { width: 36px; height: 36px; padding: 0; border: 1px solid var(--border-strong); border-radius: var(--r-sm); background: none; cursor: pointer; }
.check { gap: 10px; font-size: var(--fs-md); color: var(--text-2); cursor: pointer; }
.check-input { accent-color: var(--accent); width: 18px; height: 18px; }

.field label, .label-sm, .label-xs {
  display: block; font-size: var(--fs-xs); font-weight: 600; margin-bottom: 6px; color: var(--text-2);
}
.label-sm { margin-bottom: 0; }
.label-xs { font-weight: 500; margin-bottom: 4px; color: var(--text-3); }
.row .label-xs, .row .label-sm { margin-bottom: 0; }
.hint { font-size: var(--fs-xs); margin-top: 6px; color: var(--text-3); }
.section-title { font-size: var(--fs-xs); font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-3); }

.muted { color: var(--text-3); }
.small { font-size: var(--fs-xs); }
.mono { font-family: var(--mono); font-size: var(--fs-xs); }
.dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; display: inline-block; box-shadow: 0 0 0 1px var(--border-strong); }

.row { display: flex; align-items: center; gap: 6px; }
.row.between { justify-content: space-between; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; }
.stack { display: flex; flex-direction: column; gap: var(--s-4); }
.stack-sm { display: flex; flex-direction: column; gap: var(--s-2); }
.mb { margin-bottom: var(--s-2); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s-2); }
.points-grid { display: grid; grid-template-columns: 1fr 1fr 32px; gap: 4px; align-items: center; }

/* chips: notes, diagnostics, quick tweaks */
.chips, .suggest { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-block; max-width: 100%; padding: 3px 10px; border-radius: var(--r-pill);
  font-size: var(--fs-xs); line-height: 1.4; overflow-wrap: anywhere;
  background: var(--sunken); color: var(--text-2); border: 1px solid transparent;
}
.chip-warn { background: var(--warn-soft); color: var(--warn-text); }
.chip-error { background: var(--danger-soft); color: var(--danger); }
.chip-btn { border: 1px solid transparent; background: var(--accent-soft); color: var(--accent-text); font-weight: 500; padding: 5px 12px; }
.chip-btn:hover:not(:disabled) { border-color: var(--accent); }

/* ---------- Panels (manual form, shared chrome) ---------- */

.panel { display: flex; flex-direction: column; height: 100%; background: var(--panel); }
.panel-scroll { position: relative; flex: 1; min-height: 0; overflow-y: auto; padding: var(--s-4); scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
.panel-foot { padding: var(--s-3); background: var(--surface); border-top: 1px solid var(--border); }

.tabs { display: flex; background: var(--panel); border-bottom: 1px solid var(--border); padding: 0 var(--s-2); }
.tab {
  flex: 1; padding: 10px 12px; font-size: var(--fs-sm); font-weight: 600;
  border: 0; border-bottom: 2px solid transparent; background: transparent; color: var(--text-2);
  transition: color var(--dur-1) var(--ease), border-color var(--dur-1) var(--ease);
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent-text); border-bottom-color: var(--accent); }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md); }
.eq-card { padding: var(--s-3); display: flex; flex-direction: column; gap: var(--s-2); }
.style-row { padding-top: 4px; }

/* ---------- Chat ---------- */

.chat-panel { background: var(--panel); }
.chat-scroll { display: flex; flex-direction: column; gap: var(--s-3); padding: var(--s-4); }

.chat-empty { display: flex; flex-direction: column; align-items: center; text-align: center; padding: var(--s-5) var(--s-2) var(--s-2); }
.empty-art { display: block; margin-bottom: var(--s-3); max-width: 100%; height: auto; }
.art-paper { fill: var(--surface); stroke: var(--border); stroke-width: 1.5; }
.art-grid { stroke: var(--border); }
.art-axes { stroke: var(--text-3); }
.art-curve { stroke: var(--accent); }
.art-dot { fill: var(--amber); stroke: var(--accent); stroke-width: 2; }
.chat-empty-title { font-family: var(--font-display); font-size: var(--fs-xl); font-weight: 600; letter-spacing: 0.005em; }
.chat-empty-sub { margin-top: var(--s-1); color: var(--text-2); font-size: var(--fs-sm); max-width: 30ch; text-wrap: balance; }
.chat-empty-label { margin: var(--s-5) 0 var(--s-2); align-self: flex-start; }
.examples { display: flex; flex-direction: column; gap: var(--s-2); width: 100%; }
.example {
  display: flex; flex-direction: column; gap: 2px; width: 100%; text-align: left; padding: 10px 14px;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: var(--r-md);
  box-shadow: var(--shadow-1);
  transition: border-color var(--dur-1) var(--ease), transform var(--dur-1) var(--ease), box-shadow var(--dur-1) var(--ease);
}
.example:hover { border-color: var(--accent); box-shadow: var(--shadow-2); transform: translateY(-1px); }
.example-label { font-size: var(--fs-xs); font-weight: 700; color: var(--accent-text); letter-spacing: 0.04em; text-transform: uppercase; }
.example-text { font-size: var(--fs-sm); color: var(--text-2); }

.messages { display: flex; flex-direction: column; gap: var(--s-3); }
.msg-row { display: flex; justify-content: flex-start; }
.msg-row.end { justify-content: flex-end; }
.msg {
  max-width: 88%; padding: 10px 14px; font-size: var(--fs-md); border-radius: 16px 16px 16px 4px;
  white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.45;
}
.msg-user { background: var(--accent); color: var(--accent-ink); border-radius: 16px 16px 4px 16px; }
.msg-assistant { background: var(--surface); color: var(--text); border: 1px solid var(--border); box-shadow: var(--shadow-1); }
.msg-info { background: transparent; border: 0; box-shadow: none; color: var(--text-3); font-size: var(--fs-sm); padding: 2px 4px; font-style: italic; }
.msg-error { background: var(--danger-soft); border: 1px solid var(--danger); color: var(--danger); box-shadow: none; }
.msg-notes { margin-top: var(--s-2); white-space: normal; }
.msg-meta { margin-top: 6px; font-size: var(--fs-xs); color: var(--text-3); }
.msg-user .msg-meta { color: var(--accent-ink); opacity: 0.8; }
.retry-btn { margin-top: var(--s-2); display: flex; }

.generating { display: flex; flex-direction: column; gap: var(--s-2); min-width: 220px; white-space: normal; }
.generating-line { display: flex; align-items: center; gap: var(--s-2); color: var(--text-2); }
.thinking { color: var(--text-2); }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: bounce 1s infinite; }
.dots i:nth-child(2) { animation-delay: 0.15s; }
.dots i:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-30%); } }
.progress { display: block; height: 3px; border-radius: 2px; background: var(--sunken); overflow: hidden; }
.progress i { display: block; height: 100%; width: 40%; border-radius: 2px; background: var(--accent); animation: slide 1.4s var(--ease) infinite; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(250%); } }
.generating .hint { margin-top: 0; }
.generating .cancel-btn { align-self: flex-start; }
@media (prefers-reduced-motion: reduce) { .progress i { width: 100%; opacity: 0.35; transform: none; } }

.edit-target {
  display: flex; align-items: center; justify-content: space-between; gap: var(--s-2);
  padding: var(--s-2) var(--s-3); flex-shrink: 0;
  background: var(--accent-soft); color: var(--accent-text); border-top: 1px solid var(--border);
  font-size: var(--fs-sm);
}
.edit-target .btn-ghost { color: var(--accent-text); }
.edit-target-name { font-weight: 700; }
.suggest { padding: var(--s-2) var(--s-3) 0; background: var(--surface); flex-shrink: 0; }

.composer {
  display: flex; gap: var(--s-2); align-items: flex-end; padding: var(--s-3); flex-shrink: 0;
  background: var(--surface); border-top: 1px solid var(--border);
}
.composer-input { flex: 1; resize: none; max-height: 8rem; border-radius: var(--r-lg); padding: 10px 14px; }
.send-btn { width: 44px; height: 44px; padding: 0; border-radius: 50%; flex-shrink: 0; }

/* ---------- Math field (MathLive reads these custom properties) ---------- */

.mathfield-host { min-height: 44px; }
math-field, .mathfield-fallback {
  display: block; width: 100%; min-height: 44px; box-sizing: border-box;
  font-size: var(--fs-lg); font-family: var(--font); padding: 6px 12px;
  border-radius: var(--r-md); border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text);
  --caret-color: var(--accent);
  --selection-background-color: var(--accent-soft);
  --selection-color: var(--text);
  --contains-highlight-background-color: var(--accent-soft);
  --primary: var(--accent);
  --text-font-family: var(--font);
  --smart-fence-opacity: 0.6;
  --placeholder-color: var(--text-3);
  --placeholder-opacity: 1;
  --highlight-inline-shortcut: var(--accent-text);
  --ui-font-family: var(--font);
}
.mathfield-fallback { font-family: var(--mono); font-size: var(--fs-md); }
math-field:focus-within, .mathfield-fallback:focus { outline: 2px solid var(--focus); outline-offset: 1px; }
math-field::part(virtual-keyboard-toggle), math-field::part(menu-toggle) { color: var(--text-2); }
math-field::part(virtual-keyboard-toggle):hover, math-field::part(menu-toggle):hover { color: var(--accent-text); background: var(--accent-soft); }
/* the on-screen keyboard and menus MathLive mounts on <body> */
:root {
  --keyboard-accent-color: var(--accent);
  --keyboard-background: var(--panel);
  --keyboard-border: var(--border);
  --keyboard-toolbar-text: var(--text-2);
  --keyboard-toolbar-text-active: var(--accent-text);
  --keyboard-toolbar-background-hover: var(--sunken);
  --keyboard-horizontal-rule: 1px solid var(--border);
  --keycap-background: var(--surface);
  --keycap-background-hover: var(--sunken);
  --keycap-background-active: var(--accent);
  --keycap-text: var(--text);
  --keycap-text-active: var(--accent-ink);
  --keycap-border: var(--border);
  --keycap-border-bottom: var(--border-strong);
  --keycap-secondary-background: var(--sunken);
  --keycap-secondary-text: var(--text);
  --keycap-secondary-background-hover: var(--border);
  --keycap-shift-text: var(--accent-text);
  --keycap-shift-font-size: 0.9em;
  --variant-panel-background: var(--surface);
  --variant-panel-text: var(--text);
  --variant-panel-key-background-hover: var(--accent);
  --variant-panel-key-text-hover: var(--accent-ink);
  --variant-panel-border: var(--border-strong);
  --ui-font-family: var(--font);
  --ui-background: var(--surface);
  --ui-text: var(--text);
  --ui-border: var(--border);
  --ui-border-radius: var(--r-md);
  --ui-accent-color: var(--accent);
  --ui-menu-background: var(--surface);
  --ui-menu-active-background: var(--accent);
  --ui-menu-active-text: var(--accent-ink);
  --ui-menu-hover-background: var(--accent-soft);
  --ui-menu-hover-text: var(--text);
  --ui-menu-divider: var(--border);
  --ui-menu-shadow: var(--shadow-2);
  --ui-shadow: var(--shadow-2);
}

/* ---------- Graph area ---------- */

.graph-panel { flex: 1; min-height: 0; padding: var(--s-4) var(--s-5) var(--s-6); display: flex; flex-direction: column; }
.gc-panel-bar { display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); margin-bottom: var(--s-3); }
.gc-count { font-size: var(--fs-sm); font-weight: 600; color: var(--text-3); letter-spacing: 0.02em; }

.graph-empty { flex: 1; min-height: 280px; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: var(--s-6) var(--s-4); }
.graph-empty .empty-art { margin-bottom: var(--s-4); }
.graph-empty-title { font-family: var(--font-display); font-size: var(--fs-2xl); font-weight: 600; margin-bottom: var(--s-2); }
.graph-empty-sub { color: var(--text-2); max-width: 40ch; }

.graph-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 440px), 1fr)); gap: var(--s-5); align-items: start; }
.number-lines { display: grid; gap: var(--s-4); padding: 0 var(--s-4) var(--s-4); }

/* ---------- Settings dialog ---------- */

.modal {
  width: calc(100% - 32px); max-width: 30rem; padding: 0;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--r-lg);
  box-shadow: var(--shadow-3);
}
.modal[open] { animation: pop var(--dur-2) var(--ease); }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(0.98); } to { opacity: 1; transform: none; } }
.modal::backdrop { background: var(--scrim); }
.modal-body { padding: var(--s-5); display: flex; flex-direction: column; gap: var(--s-4); }
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal-head h2 { font-family: var(--font-display); font-size: var(--fs-xl); font-weight: 600; }
.modal-actions { display: flex; justify-content: flex-end; gap: var(--s-2); margin-top: var(--s-2); }
.modal .input { padding: 10px 12px; }
.settings-warning { color: var(--warn-text); background: var(--warn-soft); padding: var(--s-2) var(--s-3); border-radius: var(--r-sm); }

/* ---------- Responsive: the side panel becomes a bottom sheet ---------- */

@media (max-width: 899px) {
  .tagline { display: none; }
}

@media (max-width: 767px) {
  .app-header { padding: 0 var(--s-3); }
  .panel-toggle { display: none; }
  .resizer { display: none; }
  .app-main { position: relative; }
  .graph-panel { padding: var(--s-3) var(--s-3) calc(var(--peek-h) + var(--s-5)); }
  .graph-grid { gap: var(--s-4); }
  aside.left, aside.left.is-collapsed {
    display: flex; position: fixed; z-index: 30; left: 0; right: 0; bottom: 0; width: auto;
    height: min(58dvh, 520px);
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    border: 1px solid var(--border); border-bottom: 0;
    box-shadow: var(--shadow-3);
    transform: translateY(calc(100% - var(--peek-h)));
    transition: transform var(--dur-3) var(--ease);
  }
  .app.sheet-open aside.left { transform: none; }
  /* Open sheet: the graph keeps the area above it (like a split view) instead of sitting behind it. */
  .app.sheet-open .graph-panel, .app.sheet-open .ws-right { padding-bottom: calc(min(58dvh, 520px) + var(--s-3)); }
  .side-head { height: var(--peek-h); padding: 0 var(--s-3); position: relative; }
  .side-head::before {
    content: ""; position: absolute; left: 50%; top: 6px; width: 36px; height: 4px;
    margin-left: -18px; border-radius: 2px; background: var(--border-strong); opacity: 0.6;
  }
  .side-head .sheet-toggle { display: inline-flex; }
  .side-body { visibility: hidden; transition: visibility 0s linear var(--dur-3); }
  .app.sheet-open .side-body { visibility: visible; transition-delay: 0s; }
  .modal { max-width: none; }
}

/* Touch devices: every control is at least 44px. */
@media (pointer: coarse) {
  .icon-btn, .icon-btn.lg { width: var(--touch); height: var(--touch); }
  .btn, .side-tab, .tab, .link-btn, .chip-btn, .example, .select, .color-input { min-height: var(--touch); }
  .btn-sm { min-height: var(--touch); padding-inline: 14px; }
  .input[type="text"], .input[type="number"], .input[type="password"], .select.wide { min-height: var(--touch); }
  .check, .brand { min-height: var(--touch); }
  .check-input { width: 22px; height: 22px; }
  .range { min-height: var(--touch); }
}
