/* Layout concept: a sheet of white dot-grid paper in the middle, softly tinted instrument panels at
   the sides; phenolphthalein magenta as the one accent; aqueous blue / CH2Cl2 ochre mark the layers.
   Calm palette (learned from Fastwork, 31 Oct 2026): every neutral leans the same cool slate hue, no
   pure black text, hairline borders, diffuse tinted shadows, small radii. Muted text ≥ 4.5:1. */
:root {
  --bench: #ffffff;
  --dot: #e3e8ee;
  --panel: #f7f9fb;
  --panel-2: #eef2f6;
  --ink: #27303b;
  --ink-2: #4f5d6e;
  --muted: #5f6d81;
  --line: #e1e6ec;
  --accent: #b8306f;
  --accent-ink: #ffffff;
  --accent-soft: #f9e6ef;
  --aqueous: #2f74c8;
  --aqueous-soft: #e3eefb;
  --organic: #a26d14;
  --organic-soft: #f7ecd6;
  --solid: #5b6774;
  --solid-soft: #e8ebee;
  --warn: #df5a12;
  --warn-soft: #fdeadf;
  --error: #d42c2c;
  --error-text: #a51f1f; /* error text on soft backgrounds: ≥ 4.5:1 */
  --ok: #23874a;
  --ok-soft: #e1f3e7;
  --halo: rgba(223, 90, 18, 0.26);
  --select-halo: rgba(184, 48, 111, 0.14);
  --ink-ink: #17212b;
  --ink-blue: #1f5fbf;
  --ink-red: #c62828;
  --ink-green: #2e7d32;
  --ink-yellow: #f2b800;
  --laser: #ff2d55;
  --q-neg: #e03131;
  --q-pos: #1c7ed6;
  --tlc-spot: #6a4fd6;
  --tlc-uv: #3f3a52;
  --shadow: 0 1px 2px rgba(25, 52, 92, 0.06), 0 0 24px rgba(25, 72, 142, 0.10);
  --font-display: 'Bai Jamjuree', 'IBM Plex Sans Thai', Tahoma, sans-serif;
  --font-body: 'IBM Plex Sans Thai', Tahoma, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, Menlo, monospace;
  --radius: 8px;
  --touch: 44px;
  --duration: 160ms;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bench: #11171d; --dot: #222b35; --panel: #1a222a; --panel-2: #212b34; --ink: #e7edf2; --ink-2: #b4bfca;
    --muted: #8d99a5; --line: #2f3a45; --accent: #e0669f; --accent-ink: #1a0a12; --accent-soft: #3a1a2b;
    --aqueous: #6ea8ef; --aqueous-soft: #182c44; --organic: #e0a748; --organic-soft: #3a2c14; --solid: #9aa6b2;
    --solid-soft: #26303a; --warn: #ff8a4c; --warn-soft: #3b2216; --error: #ff6b6b; --error-text: #ff8787; --ok: #5fd08a; --ok-soft: #173325;
    --halo: rgba(255, 138, 76, 0.3); --select-halo: rgba(224, 102, 159, 0.22); --ink-ink: #e7edf2; --ink-blue: #6ea8ef; --ink-red: #ff6b6b; --ink-green: #5fd08a; --ink-yellow: #ffd54a; --laser: #ff4d6d; --q-neg: #ff6b6b; --q-pos: #4dabf7; --tlc-spot: #a78bfa; --tlc-uv: #c3bcdf; --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bench: #11171d; --dot: #222b35; --panel: #1a222a; --panel-2: #212b34; --ink: #e7edf2; --ink-2: #b4bfca;
  --muted: #8d99a5; --line: #2f3a45; --accent: #e0669f; --accent-ink: #1a0a12; --accent-soft: #3a1a2b;
  --aqueous: #6ea8ef; --aqueous-soft: #182c44; --organic: #e0a748; --organic-soft: #3a2c14; --solid: #9aa6b2;
  --solid-soft: #26303a; --warn: #ff8a4c; --warn-soft: #3b2216; --error: #ff6b6b; --error-text: #ff8787; --ok: #5fd08a; --ok-soft: #173325;
  --halo: rgba(255, 138, 76, 0.3); --select-halo: rgba(224, 102, 159, 0.22); --ink-ink: #e7edf2; --ink-blue: #6ea8ef; --ink-red: #ff6b6b; --ink-green: #5fd08a; --ink-yellow: #ffd54a; --laser: #ff4d6d; --q-neg: #ff6b6b; --q-pos: #4dabf7; --tlc-spot: #a78bfa; --tlc-uv: #c3bcdf; --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

html, body { height: 100%; }
body { margin: 0; }
[hidden] { display: none !important; }
body {
  background: var(--bench);
  color: var(--ink);
  font: 15px/1.5 var(--font-body);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
body::before {
  content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px); z-index: 10; background: var(--panel);
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
h2, h3 { text-wrap: balance; margin: 0; }
.muted { color: var(--muted); }
.icon { display: inline-grid; place-items: center; line-height: 0; }

.app { height: 100%; display: grid; grid-template-rows: auto 1fr; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px; min-height: 60px;
  padding-block: 8px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--panel); border-bottom: 1px solid var(--line);
  position: relative; z-index: 3;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center;
  font: 700 15px var(--font-display); color: var(--accent-ink); background: var(--accent);
  letter-spacing: 0.02em;
}
.brand-name { font: 600 19px var(--font-display); letter-spacing: 0.01em; }
.doc { display: flex; flex-direction: column; min-width: 0; padding-left: 16px; border-left: 1px solid var(--line); }
.doc-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.summary { font-size: 13px; color: var(--muted); }
.summary[data-level="warn"] { color: var(--warn); }
.summary[data-level="ok"] { color: var(--ok); }
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.btn {
  white-space: nowrap;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--touch); padding: 0 16px; border-radius: 10px; border: 1px solid transparent;
  font-weight: 600; transition: transform var(--duration) ease, background-color var(--duration) ease;
}
.btn:active { transform: translateY(1px) scale(0.98); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.ghost:hover { background: var(--panel-2); }
.btn.small { min-height: 38px; padding: 0 12px; font-size: 14px; }
.btn.danger { background: var(--error); color: #fff; }
.btn.danger-text { color: var(--error); }
.btn:disabled { opacity: 0.45; cursor: default; }
.icon-btn {
  width: var(--touch); height: var(--touch); display: inline-grid; place-items: center;
  border-radius: 10px; border: 1px solid transparent; background: transparent; font-size: 20px;
}
.icon-btn:hover:not(:disabled) { background: var(--panel-2); }
.icon-btn:disabled { opacity: 0.35; cursor: default; }
.icon-btn.danger:hover { color: var(--error); }

.switch { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 500; cursor: pointer; padding-right: 6px; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch-track {
  width: 40px; height: 24px; border-radius: 12px; background: var(--line); position: relative;
  transition: background-color var(--duration) ease;
}
.switch-track::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%;
  background: var(--panel); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25); transition: transform var(--duration) ease;
}
.switch input:checked + .switch-track { background: var(--ok); }
.switch input:checked + .switch-track::after { transform: translateX(16px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- workspace ---------- */
.workspace {
  display: grid; grid-template-columns: 76px auto 1fr 340px; min-height: 0; position: relative;
}
.rail { grid-column: 1; }
.drawer { grid-column: 2; }
.board, .draw-host { grid-column: 3; grid-row: 1; }
.inspector { grid-column: 4; }
.workspace[data-mode="draw"] .draw-host { grid-column: 3 / 5; }
.draw-host { position: relative; min-width: 0; min-height: 0; }
.rail-spacer { flex: 1; }
.rail {
  background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 6px; padding: 10px 8px;
  padding-left: max(8px, env(safe-area-inset-left, 0px));
}
.rail-btn {
  display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 9px 2px;
  border: 0; border-radius: 10px; background: transparent; color: var(--ink-2); font-size: 12px; font-weight: 500;
}
.rail-btn:hover { background: var(--panel-2); }
.rail-btn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }

.drawer {
  width: 284px; background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto; min-height: 0;
}
.panel-body { padding: 18px 16px 28px; display: flex; flex-direction: column; gap: 12px; }
.panel-title { font: 600 18px var(--font-display); }
.panel-note { margin: 0; font-size: 13px; color: var(--muted); }
.template-list, .compound-grid, .mol-list, .hint-list { list-style: none; margin: 0; padding: 0; }
.template-list { display: flex; flex-direction: column; gap: 10px; }
.template-card {
  width: 100%; text-align: left; display: grid; gap: 2px; padding: 12px 14px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--panel);
  transition: border-color var(--duration) ease, transform var(--duration) ease;
}
.template-card:hover { border-color: var(--accent); transform: translateY(-1px); }
.template-card.is-current { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.template-card .tag {
  justify-self: start; font-size: 11px; font-weight: 600; letter-spacing: 0.04em; color: var(--accent);
  background: var(--accent-soft); padding: 1px 8px; border-radius: 99px; margin-bottom: 4px;
}
.template-card .note { font-size: 13px; color: var(--muted); }
.search {
  width: 100%; min-height: var(--touch); padding: 0 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel-2);
}
.class-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.class-chip { min-height: 32px; padding: 0 10px; border-radius: 99px; border: 1px solid var(--line); background: var(--panel); font-size: 12.5px; font-weight: 500; }
.class-chip[aria-checked="true"] { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); font-weight: 600; }
.compound-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.compound {
  width: 100%; height: 100%; display: grid; grid-template-rows: 78px auto auto; gap: 2px; padding: 8px;
  border-radius: 10px; border: 1px solid var(--line); background: var(--panel); text-align: left;
}
.compound:hover { border-color: var(--accent); }
.compound-img { display: grid; place-items: center; overflow: hidden; color: var(--ink); }
.compound-img svg { max-width: 100%; max-height: 78px; }
.compound-name { font-size: 13px; font-weight: 600; line-height: 1.25; }
.compound[aria-pressed="true"] { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.course-ask { margin-top: 18px; padding-top: 12px; border-top: 1px solid var(--line); display: grid; gap: 8px; justify-items: start; }

/* ---------- board ---------- */
.board {
  position: relative; min-width: 0; min-height: 0; overflow: hidden;
  background-color: var(--bench);
  background-image: radial-gradient(var(--dot) 1.2px, transparent 1.3px);
  background-size: 22px 22px;
}
.board-svg { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; -webkit-user-select: none; }
.zoom-controls {
  position: absolute; left: 14px; bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  display: flex; align-items: center; gap: 2px; padding: 3px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.zoom-label { min-width: 46px; text-align: center; font: 500 13px var(--font-mono); font-variant-numeric: tabular-nums; }

/* flowchart (SVG) */
.card { cursor: pointer; outline: none; }
.card-bg { fill: var(--panel); stroke: var(--line); stroke-width: 1; filter: drop-shadow(0 2px 6px rgba(23, 33, 43, 0.08)); }
.card:hover .card-bg { stroke: var(--ink-2); }
.card.is-selected .card-bg, .card:focus-visible .card-bg { stroke: var(--accent); stroke-width: 2.5; }
.card-head { fill: var(--panel-2); }
.phase-aqueous .card-head { fill: var(--aqueous-soft); }
.phase-organic .card-head { fill: var(--organic-soft); }
.phase-solid .card-head { fill: var(--solid-soft); }
.glyph-line { fill: none; stroke: var(--ink-2); stroke-width: 1.4; stroke-linejoin: round; stroke-linecap: round; }
.glyph-empty { fill: none; }
.glyph-fill { fill: var(--ink-2); }
.phase-aqueous .glyph-fill { fill: var(--aqueous); }
.phase-organic .glyph-fill { fill: var(--organic); }
.card-title { font: 600 13.5px var(--font-body); fill: var(--ink); }
.card-sub { font: 11.5px var(--font-body); fill: var(--muted); }
.card-empty { font: 13px var(--font-body); fill: var(--muted); }
.mol { color: var(--ink); }
.plus { font: 18px Arial, sans-serif; fill: var(--ink-2); }
.badge circle { stroke: var(--panel); stroke-width: 2; }
.badge text { font: 700 12px var(--font-body); fill: #fff; }
.badge-warn circle { fill: var(--warn); }
.badge-error circle { fill: var(--error); }
.badge-ok circle { fill: var(--ok); }
.badge-info circle { fill: var(--muted); }
.edge { fill: none; stroke: var(--ink-2); stroke-width: 1.6; }
.arrow { fill: var(--ink-2); }
.edge-tag { font: 500 12px var(--font-body); fill: var(--muted); }
.step rect { fill: var(--panel); stroke: var(--ink-2); stroke-width: 1.2; }
.step text { font: 600 12.5px var(--font-body); fill: var(--ink); }

/* ---------- inspector ---------- */
.inspector {
  background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; min-height: 0;
  padding-right: env(safe-area-inset-right, 0px);
}
.insp-empty { padding: 28px 22px; display: grid; gap: 4px; }
.insp-empty p { margin: 0; }
.insp { display: flex; flex-direction: column; }
.insp-head { padding: 18px 20px 14px; border-bottom: 1px solid var(--line); background: var(--panel-2); display: grid; gap: 4px; }
.insp.phase-aqueous .insp-head { background: var(--aqueous-soft); }
.insp.phase-organic .insp-head { background: var(--organic-soft); }
.insp.phase-solid .insp-head { background: var(--solid-soft); }
.eyebrow { font-size: 12px; font-weight: 600; letter-spacing: 0.03em; color: var(--ink-2); }
.title-input {
  font: 600 19px var(--font-display); border: 1px solid transparent; border-radius: 8px;
  padding: 2px 6px; margin-left: -6px; background: transparent; width: 100%;
}
.title-input:hover { border-color: var(--line); }
.title-input:focus { border-color: var(--accent); background: var(--panel); outline: none; }
.insp-section { padding: 16px 20px; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px; }
.insp-section h3 { font: 600 14px var(--font-body); color: var(--ink-2); display: flex; align-items: center; gap: 8px; }
.count { font-size: 12px; min-width: 22px; height: 22px; border-radius: 11px; display: inline-grid; place-items: center; background: var(--warn); color: #fff; padding: 0 6px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; }
.mol-list { display: flex; flex-direction: column; gap: 8px; }
.mol-row { display: grid; grid-template-columns: 76px minmax(0, 1fr) auto; gap: 10px; align-items: center; }
.mol-thumb {
  height: 64px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel);
  display: grid; place-items: center; overflow: hidden; padding: 2px; color: var(--ink);
}
.mol-thumb:hover { border-color: var(--accent); }
.mol-thumb svg { max-width: 100%; max-height: 60px; }
.mol-meta { display: grid; min-width: 0; }
.mol-meta strong { font-size: 14px; line-height: 1.3; overflow-wrap: anywhere; }
.formula { font: 500 13px var(--font-mono); color: var(--muted); }
.row-actions { display: flex; }
.hint-list { display: flex; flex-direction: column; gap: 8px; }
.hint { border-radius: 10px; padding: 10px 12px; background: var(--panel-2); border: 1px solid var(--line); }
.hint p { margin: 4px 0 0; font-size: 13.5px; color: var(--ink-2); }
.hint-head { display: flex; flex-direction: column; gap: 2px; }
.hint-head strong { font-size: 14px; line-height: 1.35; }
.hint-level { font-size: 11px; font-weight: 700; letter-spacing: 0.05em; color: var(--muted); }
.hint-warn { background: var(--warn-soft); border-color: transparent; }
.hint-warn .hint-level { color: var(--warn); }
.hint-error { background: var(--warn-soft); border-color: var(--error); }
.hint-error .hint-level { color: var(--error); }
.hint-ok { background: var(--ok-soft); border-color: transparent; }
.hint-ok .hint-level { color: var(--ok); }
.rubric {
  font-size: 12.5px !important; color: var(--ink) !important; padding-top: 6px; margin-top: 8px !important;
  border-top: 1px dashed var(--line);
}
.step-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.step-btn {
  display: grid; gap: 0; text-align: left; padding: 9px 11px; min-height: 56px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--panel); transition: border-color var(--duration) ease;
}
.step-btn span { font-size: 12px; color: var(--muted); }
.step-btn strong { font: 500 14px var(--font-mono); }
.step-btn:hover { border-color: var(--ink-2); }
.step-btn.ph-acid strong { color: var(--error); }
.step-btn.ph-base strong { color: var(--aqueous); }
.step-done { margin: 0; font-weight: 600; }
.confirm { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: 14px; }
.insp-foot { padding: 16px 20px 28px; }

/* ---------- mode switch, toast ---------- */
.segmented { display: inline-flex; padding: 3px; gap: 2px; border-radius: 11px; background: var(--panel-2); border: 1px solid var(--line); }
.seg-btn { white-space: nowrap; min-height: 36px; padding: 0 14px; border: 0; border-radius: 8px; background: transparent; font-weight: 600; font-size: 14px; color: var(--ink-2); }
.seg-btn[aria-pressed="true"] { background: var(--panel); color: var(--ink); box-shadow: 0 1px 3px rgba(23, 33, 43, 0.14); }
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); z-index: 40;
  max-width: min(520px, calc(100% - 32px)); padding: 12px 18px; border-radius: 12px; background: var(--ink); color: var(--panel);
  font-size: 14px; box-shadow: var(--shadow);
}
.toast[data-level="error"] { background: var(--error); color: #fff; }
.export-tip { margin: 0; font-size: 13px; }

/* ---------- editor ---------- */
.editor {
  position: fixed; inset: 0; z-index: 20; background: var(--bench);
  display: grid; grid-template-rows: auto 1fr auto;
  padding-top: env(safe-area-inset-top, 0px); padding-bottom: env(safe-area-inset-bottom, 0px);
}
.editor.embedded { position: absolute; inset: 0; z-index: auto; padding: 0; }
.editor-bar {
  display: flex; align-items: center; gap: 12px; padding-block: 8px; background: var(--panel); border-bottom: 1px solid var(--line);
  padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.editor-title { display: grid; min-width: 0; }
.id-box { flex: 1 1 200px; min-width: 140px; max-width: 320px; height: 34px; padding: 0 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel-2); color: var(--ink); font: 14px var(--font-body); }
.id-box:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; background: var(--panel); }
.id-box:disabled { opacity: 0.6; }
.editor-title strong { font: 600 16px var(--font-display); }
.editor-title span { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* The action row scrolls sideways when it doesn't fit, at every width: the header must never push
   the editor wider than the screen (that shifted the canvas off-screen). */
.editor-actions { margin-left: auto; display: flex; align-items: center; gap: 6px; min-width: 0; overflow-x: auto; scrollbar-width: thin; }
.editor-actions > * { flex-shrink: 0; }
.editor-bar { min-width: 0; }
.editor-title { flex-shrink: 1; }
.editor-body { display: grid; grid-template-columns: 252px 1fr; min-height: 0; }
.tool-rail {
  background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto; padding: 12px;
  padding-left: max(12px, env(safe-area-inset-left, 0px)); display: flex; flex-direction: column; gap: 14px;
}
.tool-group { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.tool-group.elements { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.tool {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 54px; padding: 4px 2px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel); font-size: 12px; font-weight: 500;
}
.tool:hover { border-color: var(--ink-2); }
.tool[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.tool.el { min-height: 46px; font: 600 17px Arial, sans-serif; }
.tool.el-N span { color: var(--aqueous); }
.tool.el-O span { color: var(--error); }
.tool.el-Cl span, .tool.el-Br span, .tool.el-F span, .tool.el-I span { color: var(--ok); }
.tool.el-P span { color: #d9822b; }
.tool.frag { font: 600 13px Arial, sans-serif; min-height: 44px; }
.editor-stage { position: relative; min-width: 0; min-height: 0; }
.stage-svg { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; -webkit-user-select: none; color: var(--ink); cursor: crosshair; }
/* Shapes inside the drawing never take pointer events: hit-testing is geometric (editor.js hitTest),
   and the drawing is redrawn on every zoom (innerHTML). Safari aims a trackpad pinch at the element
   under the fingers; a shape replaced mid-pinch restarted the gesture every frame and the pinch barely
   zoomed. With this, the target is the svg itself, which outlives every redraw. */
.stage-svg * { pointer-events: none; }
.stage-svg .dot { fill: var(--dot); }
.stage-svg .hover { fill: var(--accent); fill-opacity: 0.16; stroke: var(--accent); stroke-width: 1.5; }
.stage-svg .hover-bond { stroke: var(--accent); stroke-opacity: 0.35; stroke-width: 12; stroke-linecap: round; }
.stage-svg .preview { stroke: var(--accent); stroke-width: 2.5; stroke-dasharray: 5 4; stroke-linecap: round; }
.stage-svg.panning, .stage-svg[data-tool="pan"] { cursor: grab; }
.stage-svg.panning:active, .stage-svg[data-tool="pan"]:active { cursor: grabbing; }
.zoom-float .hand-btn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.stage-svg[data-tool="pen"], .stage-svg[data-tool="highlighter"], .stage-svg[data-tool="smart"], .stage-svg[data-tool="laser"] { cursor: crosshair; }
.stage-svg .grid-line { stroke: var(--dot); stroke-width: 1; }
.stage-svg .laser-glow { fill: none; stroke: var(--laser); stroke-opacity: 0.25; stroke-width: 12; stroke-linecap: round; stroke-linejoin: round; }
.stage-svg .laser { fill: none; stroke: var(--laser); stroke-width: 3.5; stroke-linecap: round; stroke-linejoin: round; }
.swatches { grid-column: 1 / -1; display: flex; gap: 8px; padding: 4px 2px 0; }
.swatch { width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--panel); box-shadow: 0 0 0 1px var(--line); padding: 0; }
.swatch[aria-checked="true"] { box-shadow: 0 0 0 2px var(--accent); }
.swatch.ink-ink { background: var(--ink-ink); } .swatch.ink-blue { background: var(--ink-blue); }
.swatch.ink-red { background: var(--ink-red); } .swatch.ink-green { background: var(--ink-green); }
.swatch.ink-yellow { background: var(--ink-yellow); }
.chip-btn {
  min-height: 34px; padding: 0 12px; border-radius: 99px; border: 1px solid var(--line); background: var(--panel);
  font-size: 13px; font-weight: 600; white-space: nowrap;
}
.chip-btn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.check-count:not(:empty) { margin-left: 6px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--error); color: #fff; font: 600 11px/18px var(--font-mono); text-align: center; }
.check-panel { position: absolute; top: 12px; right: 12px; z-index: 6; width: min(340px, calc(100% - 24px)); max-height: 60%; overflow: auto; display: flex; flex-direction: column; gap: 6px; padding: 10px; border: 1px solid var(--line); border-radius: 14px; background: var(--panel); box-shadow: var(--shadow); }
.check-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 14px; }
.check-item { text-align: left; padding: 8px 10px; border: 1px solid var(--line); border-left: 3px solid var(--error); border-radius: 10px; background: var(--panel-2); color: var(--ink); font-size: 13px; line-height: 1.45; cursor: pointer; }
.check-item.overlap, .check-item.stereo { border-left-color: var(--warn, #d08a00); }
.check-item.open-answer { border-left-color: var(--accent); }
.check-note { margin: 2px 2px 0; color: var(--muted); font-size: 12px; line-height: 1.45; }
.check-item:hover, .check-item:focus-visible { border-color: var(--accent); outline: none; }
.chip-ink { background: var(--panel); border-style: dashed; }
.stage-svg .q-neg, .chip-charge .q-neg { fill: var(--q-neg); background: var(--q-neg); }
.stage-svg .q-pos, .chip-charge .q-pos { fill: var(--q-pos); background: var(--q-pos); }
.chip-charge i { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin: 0 4px 0 2px; vertical-align: middle; }
.stage-svg .preview-dot { fill: var(--accent); }
.stage-svg .stereo-label { fill: var(--accent); font-family: Helvetica, Arial, sans-serif; }
.stage-svg .object-layer { color: var(--ink); }
.text-pop {
  position: absolute; left: 0; top: 0; z-index: 3; width: 320px; display: flex; flex-direction: column; gap: 8px;
  padding: 14px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.text-pop strong { font-size: 14px; }
.text-pop-field { display: grid; gap: 3px; font-size: 12px; color: var(--muted); }
.text-pop-input { min-height: 38px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel-2); padding: 6px 10px; font-size: 15px; color: var(--ink); resize: vertical; }
.text-pop-preview { position: relative; min-height: 34px; color: var(--ink); border-radius: 8px; background: var(--panel-2); }
.text-pop-preview::before { content: attr(data-label); position: absolute; top: 3px; right: 8px; font-size: 10px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.text-pop-hint { margin: 0; font-size: 12px; color: var(--muted); }
.tlc-sheet { width: min(900px, 100%); }
.tlc-body { display: flex; flex-direction: column; gap: 14px; }
.tlc-dims { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.tlc-lanes { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 10px; }
.tlc-lane { display: flex; flex-direction: column; gap: 8px; padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--panel-2); }
.tlc-lane-head, .tlc-spot-row { display: flex; gap: 6px; align-items: flex-end; }
.tlc-spot { display: flex; flex-direction: column; gap: 6px; padding-bottom: 8px; border-bottom: 1px dashed var(--line); }
.tlc-spot select { min-width: 0; }
.tlc-captions { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.tlc-toggle { display: inline-flex; gap: 6px; align-items: center; font-size: 14px; }
.tlc-matches { color: var(--accent); }
@media (max-width: 640px) { .tlc-captions, .tlc-dims { grid-template-columns: 1fr; } }
.tlc-lane-head .v3-field, .tlc-spot-row .v3-field { flex: 1; }
.tlc-add-lane { align-self: center; }
.tlc-working { border-top: 1px solid var(--line); padding-top: 10px; }
.tlc-working h3 { font: 600 15px var(--font-body); margin-bottom: 6px; }
.tlc-working ul { margin: 0; padding-left: 18px; font: 14px/1.7 var(--font-mono); }
.v3-sheet { width: min(1080px, 100%); }
.v3-body { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 18px; }
.v3-stage { position: relative; min-height: 380px; border-radius: 12px; border: 1px solid var(--line); background: radial-gradient(circle at 40% 35%, var(--panel), var(--panel-2)); overflow: hidden; }
.v3-svg { width: 100%; height: 100%; min-height: 380px; display: block; touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none; }
.v3-svg .v3-bond { stroke: #b7bcc4; stroke-linecap: round; }
.v3-svg .v3-bond-shine { stroke: #fff; stroke-opacity: 0.55; stroke-linecap: round; }
.v3-svg .v3-atom { stroke: none; }
/* Dark: explicit choice, or "system" while the OS is dark (same pattern as the tokens above). */
:root[data-theme="dark"] .v3-svg .v3-bond { stroke: #6f7782; }
:root[data-theme="dark"] .v3-svg .v3-bond-shine { stroke-opacity: 0.25; }
:root[data-theme="dark"] .v3-svg .v3-ground { opacity: 0.6; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .v3-svg .v3-bond { stroke: #6f7782; }
  :root:not([data-theme="light"]) .v3-svg .v3-bond-shine { stroke-opacity: 0.25; }
  :root:not([data-theme="light"]) .v3-svg .v3-ground { opacity: 0.6; }
}
.v3-spin { position: absolute; right: 10px; bottom: 10px; }
.v3-setup { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.v3-setup h3 { font: 600 15px var(--font-body); }
.v3-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.v3-field { display: grid; gap: 3px; font-size: 12px; color: var(--muted); }
.v3-field select, .v3-field input { min-height: 38px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); padding: 0 8px; font-size: 14px; color: var(--ink); }
.v3-preview { margin: 0; max-height: 220px; overflow: auto; padding: 10px 12px; border-radius: 10px; background: var(--panel-2); border: 1px solid var(--line); font: 12px/1.45 var(--font-mono); white-space: pre; }
.v3-note { margin: 0; font-size: 12px; }
@media (max-width: 760px) { .v3-body { grid-template-columns: 1fr; } .v3-stage, .v3-svg { min-height: 280px; } }
.stage-tip.flash { color: var(--accent); font-weight: 600; }
.tool-group.pens { padding-bottom: 12px; border-bottom: 1px solid var(--line); }
.stage-svg .sel-box { fill: none; stroke: var(--accent); stroke-width: 1.2; stroke-dasharray: 5 4; }
.stage-svg .sel-stem { stroke: var(--accent); stroke-width: 1.2; }
.stage-svg .rotate-handle circle { fill: var(--panel); stroke: var(--accent); stroke-width: 1.6; cursor: grab; }
.stage-svg .rotate-handle path { stroke: var(--accent); stroke-width: 1.6; }
.stage-svg .marquee { fill: var(--select-halo); stroke: var(--accent); stroke-width: 1; stroke-dasharray: 4 3; }
.stage-svg .measure rect { fill: var(--ink); }
.stage-svg .measure text { fill: var(--panel); font: 600 12px var(--font-mono); }
.action-bar {
  position: absolute; left: 0; top: 0; z-index: 2; display: flex; align-items: center; gap: 2px; padding: 4px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
  white-space: nowrap; transition: transform 90ms ease-out;
}
.action-bar button {
  display: inline-flex; align-items: center; gap: 5px; min-height: 40px; padding: 0 10px; border: 0; border-radius: 8px;
  background: transparent; font-size: 13px; font-weight: 600;
}
.action-bar button:hover { background: var(--panel-2); }
.action-bar button.danger { color: var(--error); }
.sel-count { padding: 0 8px 0 6px; font: 500 12px var(--font-mono); color: var(--muted); border-right: 1px solid var(--line); margin-right: 2px; }
.zoom-inline { display: inline-flex; align-items: center; gap: 0; padding-right: 6px; margin-right: 4px; border-right: 1px solid var(--line); }
.zoom-inline .icon-btn { width: 38px; font-size: 20px; }
.cheat-btn { margin-top: auto; }
.cheat-backdrop { position: fixed; inset: 0; z-index: 35; background: rgba(10, 16, 22, 0.45); display: grid; place-items: center; padding: 16px; }
.cheat {
  width: min(880px, 100%); max-height: calc(100% - 32px); overflow-y: auto; background: var(--panel);
  border-radius: 16px; box-shadow: var(--shadow); padding: 20px; display: flex; flex-direction: column; gap: 14px;
}
.cheat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 18px 28px; }
.cheat h3 { font: 600 14px var(--font-body); color: var(--accent); margin-bottom: 8px; }
.cheat dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; align-items: baseline; }
.cheat dt { white-space: nowrap; color: var(--muted); font-size: 12px; }
.cheat dd { margin: 0; font-size: 14px; }
kbd {
  font: 600 12px var(--font-mono); padding: 2px 6px; border-radius: 6px; background: var(--panel-2);
  border: 1px solid var(--line); border-bottom-width: 2px; color: var(--ink);
}
.stage-tip {
  position: absolute; left: 16px; right: 16px; bottom: 12px; margin: 0; pointer-events: none;
  font-size: 13px; color: var(--ink-2); text-align: center;
}
.editor-info {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center; min-height: 52px; padding-block: 8px;
  padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--panel); border-top: 1px solid var(--line); font-size: 14px;
}
.chip { padding: 4px 12px; border-radius: 99px; background: var(--panel-2); border: 1px solid var(--line); }
.chip b { font-family: var(--font-mono); font-weight: 500; }
.chip-error { background: var(--warn-soft); color: var(--error-text); border-color: var(--error); }

/* ---------- export sheet ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 30; background: rgba(10, 16, 22, 0.45);
  display: grid; place-items: center; padding: 16px;
}
.sheet {
  width: min(920px, 100%); max-height: calc(100% - 32px); overflow-y: auto; background: var(--panel);
  border-radius: 16px; box-shadow: var(--shadow); padding: 20px; display: flex; flex-direction: column; gap: 14px;
}
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.sheet-head h2 { font: 600 20px var(--font-display); }
.sheet-head p { margin: 2px 0 0; font-size: 13px; }
.export-figure {
  margin: 0; overflow: auto; max-height: 56vh; border-radius: 10px; border: 1px solid var(--line);
  background: #fff; display: grid; place-items: center; padding: 8px;
}
.export-figure img { max-width: 100%; height: auto; }
.export-status { margin: 0; font-size: 14px; color: var(--ink-2); }
.svg-code { width: 100%; min-height: 120px; font: 12px var(--font-mono); border-radius: 10px; border: 1px solid var(--line); background: var(--panel-2); }

.noscript { padding: 24px; }

/* ---------- feedback sheet ---------- */
.sheet.feedback { width: min(560px, 100%); }
.feedback-form { display: flex; flex-direction: column; gap: 12px; }
.feedback-field, .form-field { display: flex; flex-direction: column; gap: 4px; }
.feedback-field label, .form-field > label, .form-field legend { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.feedback-field input, .feedback-field textarea, .form-field textarea, .form-field select {
  font: inherit; font-size: 15px; padding: 9px 11px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--ink); resize: vertical; min-width: 0;
}
.feedback-field input:focus-visible, .feedback-field textarea:focus-visible, .form-field textarea:focus-visible, .form-field select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.feedback-note { margin: 0; font-size: 12.5px; }
.feedback-actions { justify-content: flex-end; gap: 8px; }
.feedback-status { margin: 0; min-height: 1.4em; font-size: 14px; }
.feedback-status[data-level="ok"] { color: var(--ok); }
.feedback-status[data-level="error"] { color: var(--error-text); }

/* ---------- worksheet sheet (#11) ---------- */
.sheet.worksheet { width: min(560px, 100%); }
.worksheet-form { display: flex; flex-direction: column; gap: 12px; }
.worksheet-form textarea { font-family: var(--font-mono); font-size: 14px; }
.worksheet-options { display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-start; }
.worksheet-options fieldset { border: 0; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.worksheet-options .choice { display: flex; align-items: center; gap: 6px; font-size: 14px; }
.worksheet-status { margin: 0; min-height: 1.4em; font-size: 14px; }
.worksheet-status[data-level="error"] { color: var(--error-text); }

/* ---------- iPad portrait / small laptops ---------- */
@media (max-width: 1180px) {
  .topbar { gap: 12px; }
  .brand-name { display: none; }
  .switch-text { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .doc { padding-left: 12px; }
  .workspace { grid-template-columns: 76px 1fr 320px; }
  .drawer {
    position: absolute; left: 76px; top: 0; bottom: 0; z-index: 4; box-shadow: var(--shadow);
  }
  .board, .draw-host { grid-column: 2; }
  .workspace[data-mode="draw"] .draw-host { grid-column: 2 / 4; }
  .inspector { grid-column: 3; }
}

/* ---------- phones ---------- */
@media (max-width: 760px) {
  .topbar { gap: 8px; }
  .brand-name, .doc, .btn-text { display: none; }
  .seg-btn { padding: 0 10px; font-size: 13px; }
  .workspace[data-mode="draw"] .draw-host { grid-column: 1; }
  .draw-host { grid-row: 1; }
  .rail-spacer { flex: 0; }
  .rail-btn span:last-child { font-size: 12px; }
  .switch-text { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .top-actions { gap: 2px; }
  .top-actions .btn.primary { padding: 0 12px; }
  .switch { font-size: 13px; }
  .workspace { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .board { grid-column: 1; grid-row: 1; }
  .rail { grid-column: 1; }
  .drawer, .inspector { grid-column: 1; grid-row: 1; }
  .rail {
    grid-row: 2; flex-direction: row; justify-content: center; border-right: 0; border-top: 1px solid var(--line);
    padding-bottom: max(8px, env(safe-area-inset-bottom, 0px));
  }
  .rail-btn { flex-direction: column; gap: 2px; padding: 6px 10px; font-size: 12px; }
  .drawer { left: 0; right: 0; width: auto; top: auto; bottom: 62px; z-index: 6; max-height: 60%; border-right: 0; border-top: 1px solid var(--line); border-radius: 16px 16px 0 0; }
  .inspector {
    position: absolute; left: 0; right: 0; bottom: 62px; max-height: 50%; z-index: 3;
    border-left: 0; border-top: 1px solid var(--line); border-radius: 16px 16px 0 0; box-shadow: var(--shadow);
    padding-left: env(safe-area-inset-left, 0px);
  }
  .inspector:not(.has-node) { display: none; }
  .zoom-controls { bottom: 12px; }
  .editor-body { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr; }
  .editor-bar { min-width: 0; gap: 8px; }
  .editor.embedded .editor-title { display: none; }
  .editor-actions { min-width: 0; flex: 1; overflow-x: auto; justify-content: flex-start; scrollbar-width: none; }
  .editor-actions > * { flex-shrink: 0; }
  .tool-rail { min-width: 0; }
  .tool-rail { flex-direction: row; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--line); padding: 8px; gap: 10px; }
  .tool-group, .tool-group.elements { display: flex; gap: 6px; }
  .tool-rail > *, .swatches, .tool { flex-shrink: 0; }
  .tool-group.pens { padding-bottom: 0; border-bottom: 0; padding-right: 10px; border-right: 1px solid var(--line); }
  .tool { min-width: 58px; min-height: 52px; }
  .editor-actions .btn.primary { padding: 0 10px; }
  .action-bar .ab-label, .action-bar .sel-count { display: none; }
  .editor-actions .chip-btn { padding: 0 8px; font-size: 12px; }
  .swatches { padding: 0 4px; align-items: center; }
  .action-bar button { padding: 0 12px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0ms !important; animation-duration: 0ms !important; }
}

/* iPad portrait (768–900 px): tools in one scrolling row on top so the canvas gets the full width,
   as on phones. Landscape tablets keep the side rail, narrower. */
@media (min-width: 761px) and (max-width: 900px) {
  .editor-body { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr; }
  .tool-rail { min-width: 0; flex-direction: row; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--line); padding: 8px; gap: 10px; }
  .tool-group, .tool-group.elements { display: flex; gap: 6px; }
  .tool-rail > *, .swatches, .tool { flex-shrink: 0; }
  .tool-group.pens { padding-bottom: 0; border-bottom: 0; padding-right: 10px; border-right: 1px solid var(--line); }
  .tool { min-width: 58px; min-height: 52px; }
  .swatches { padding: 0 4px; align-items: center; }
}
@media (min-width: 901px) and (max-width: 1180px) {
  .editor-body { grid-template-columns: 196px 1fr; }
  .tool-group, .tool-group.elements { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .tool { min-height: 50px; font-size: 11px; }
}

/* pKa vs pH panel (flowchart inspector) */
.pka-ph { margin: 0 0 6px; font-weight: 600; }
.pka-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.pka-row { padding: 8px 10px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); }
.pka-numbers { margin: 4px 0; font-variant-numeric: tabular-nums; }
.pka-src { font-size: 12px; padding: 0 6px; border-radius: 99px; background: var(--panel-2); color: var(--ink-2); }
.pka-src.literature { background: var(--ok-soft); color: var(--ok); }
.pka-reveal summary { cursor: pointer; color: var(--accent); font-weight: 600; font-size: 13px; }
.pka-reveal p { margin: 4px 0 0; }
.pka-rule { font-size: 12px; margin-top: 8px; }

/* Properties sheet */
.props-sheet { width: min(760px, 100%); }
.props-body { display: grid; gap: 14px; }
.props-card { display: grid; gap: 8px; padding: 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); }
.props-card h3 { margin: 0; font: 600 15px var(--font-body); display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.props-card h3 .props-pubchem { margin-left: auto; text-decoration: none; color: inherit; }
.props-ids { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px 12px; margin: 0; align-items: center; }
.props-ids dd { margin: 0; display: flex; gap: 8px; align-items: center; min-width: 0; }
.props-code { font: 500 12.5px var(--font-mono); background: var(--panel-2); padding: 4px 6px; border-radius: 6px; overflow-wrap: anywhere; min-width: 0; flex: 1; user-select: all; }
.props-grid { display: grid; grid-template-columns: repeat(2, max-content minmax(0, 1fr)); gap: 4px 12px; margin: 0; font-variant-numeric: tabular-nums; }
.props-grid dt, .props-ids dt { color: var(--muted); font-size: 13px; }
.props-grid dd { margin: 0; }
@media (max-width: 640px) { .props-grid { grid-template-columns: max-content minmax(0, 1fr); } }

/* =====================================================================
   Mac-first layout (3 Nov 2026): Keynote toolbar · Canva sidebar · Freeform zoom
   ===================================================================== */
:root { --toolbar-h: 58px; --side-tabs-w: 68px; --side-panel-w: 232px; }

/* Drawing mode: the editor owns the whole window; its toolbar is the only top bar. */
.workspace[data-mode="draw"] { grid-template-columns: minmax(0, 1fr); }
.workspace[data-mode="draw"] .draw-host { grid-column: 1 / -1; }
.app > .workspace { grid-row: 2; }

/* ---------- Keynote-style toolbar: icon above a short label, grouped ---------- */
.editor-bar.toolbar {
  min-height: var(--toolbar-h); gap: 6px; padding-block: 4px; overflow-x: auto; scrollbar-width: none;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.editor-bar.toolbar::-webkit-scrollbar { display: none; }
.editor-bar.toolbar > * { flex-shrink: 0; }
.editor-bar.toolbar .id-box { flex: 1 1 220px; flex-shrink: 1; min-width: 160px; max-width: 360px; margin-inline: 6px; }
.tb-group { display: flex; align-items: center; gap: 2px; }
.tb-center { margin-left: auto; }
.tb-trailing { margin-left: 10px; padding-left: 10px; border-left: 1px solid var(--line); gap: 6px; }
.tb-btn {
  position: relative; display: inline-flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-width: 54px; height: 48px; padding: 3px 8px; border: 0; border-radius: 8px; background: transparent; color: var(--ink-2);
  transition: background-color var(--duration) ease, color var(--duration) ease;
}
.tb-btn:hover { background: var(--panel-2); color: var(--ink); }
.tb-btn:active { background: var(--line); }
.tb-btn[aria-pressed="true"], .tb-btn[aria-expanded="true"] { background: var(--accent-soft); color: var(--accent); }
.tb-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.tb-label { display: inline-flex; align-items: center; gap: 2px; font-size: 11px; font-weight: 500; line-height: 1; white-space: nowrap; }
.tb-btn .check-count:not(:empty) { position: absolute; top: 2px; right: 6px; margin: 0; }
.tb-group .icon-btn { width: 38px; height: 38px; }
.brand-mini { margin-right: 4px; border-radius: 10px; transition: transform 200ms ease, box-shadow 200ms ease; }
a.brand-mini:hover { transform: scale(1.06); }
a.brand-mini:active { transform: scale(0.97); }
a.brand-mini:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
a.side-tab { text-decoration: none; color: inherit; }
.brand-mini .brand-mark { width: 30px; height: 30px; font-size: 13px; border-radius: 8px; }
.brand-logo { display: block; object-fit: contain; }
.editor-bar.toolbar .btn.primary { min-height: 36px; padding: 0 14px; }
.editor-bar.toolbar .lang-btn { min-height: 32px; padding: 0 10px; }

/* ---------- pop-up menus (macOS menu look) ---------- */
.pop-menu {
  position: fixed; z-index: 40; min-width: 240px; max-width: 300px; padding: 5px; border-radius: 10px;
  background: var(--panel); border: 1px solid var(--line); box-shadow: 0 10px 34px rgba(15, 30, 55, 0.18), 0 1px 3px rgba(15, 30, 55, 0.1);
  animation: pop-in 120ms ease-out;
}
@keyframes pop-in { from { opacity: 0; } to { opacity: 1; } } /* fade only: items never move under the pointer */
@media (prefers-reduced-motion: reduce) { .pop-menu { animation: none; } }
.pop-item {
  display: grid; grid-template-columns: 16px 1fr auto; align-items: center; gap: 6px; width: 100%;
  min-height: 30px; padding: 4px 10px 4px 6px; border: 0; border-radius: 6px; background: transparent; color: var(--ink);
  font-size: 13.5px; text-align: left;
}
.pop-item:hover, .pop-item:focus-visible { background: var(--accent); color: var(--accent-ink); outline: none; }
.pop-check { font-size: 12px; text-align: center; }
.pop-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pop-keys { font-size: 12px; color: var(--muted); letter-spacing: 0.04em; }
.pop-item:hover .pop-keys, .pop-item:focus-visible .pop-keys { color: inherit; opacity: 0.85; }
.pop-sep { height: 1px; margin: 4px 8px; background: var(--line); }

/* ---------- Canva-style sidebar: category tabs + one open panel ---------- */
.editor-body { grid-template-columns: auto minmax(0, 1fr); }
.side { display: flex; min-height: 0; background: var(--panel); border-right: 1px solid var(--line); }
.side-tabs {
  width: var(--side-tabs-w); display: flex; flex-direction: column; align-items: stretch; gap: 2px; padding: 8px 6px;
  padding-left: max(6px, env(safe-area-inset-left, 0px)); overflow-y: auto; scrollbar-width: none;
}
.side.collapsed .side-tabs { border-right: 0; }
.side:not(.collapsed) .side-tabs { border-right: 1px solid var(--line); }
.side-tab {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 8px 2px 7px;
  border: 0; border-radius: 10px; background: transparent; color: var(--ink-2); font-size: 11px; font-weight: 500;
}
.side-tab span { max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-tab:hover { background: var(--panel-2); color: var(--ink); }
.side-tab[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.side-tab.has-active[aria-pressed="false"]::after {
  content: ""; position: absolute; top: 7px; right: 10px; width: 6px; height: 6px; border-radius: 50%; background: var(--accent);
}
.side-tab:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.side-panel { width: var(--side-panel-w); overflow-y: auto; min-height: 0; }
.side-panel:has(> [data-category="compounds"]:not([hidden])) { width: 300px; }
.side-panel-body { padding: 14px 12px 24px; display: flex; flex-direction: column; gap: 8px; }
.side-title { font: 600 15px var(--font-display); margin: 2px 4px 4px; }
.side-sub { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; margin: 8px 4px 0; }
.side-note { font-size: 12px; line-height: 1.45; color: var(--muted); margin: 6px 4px 2px; }
.course-name { margin: 2px 4px 8px; font-size: 13px; line-height: 1.4; }
.course-summary { list-style: none; cursor: pointer; display: grid; grid-template-columns: 1fr auto; column-gap: 8px; align-items: center; border-radius: 10px; padding: 2px 4px; }
.course-summary::-webkit-details-marker { display: none; }
.course-summary .side-sub, .course-summary .course-name { grid-column: 1; display: block; }
.course-summary::after { content: '›'; grid-column: 2; grid-row: 1 / span 2; font-size: 20px; color: var(--muted); transition: transform 160ms; }
.course-group[open] > .course-summary::after { transform: rotate(90deg); }
.course-summary:hover { background: var(--accent-soft); }
.course-summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.course-code { color: var(--muted); font-variant-numeric: tabular-nums; }
.tool-list { display: flex; flex-direction: column; gap: 2px; }
.tool.tool-row {
  display: grid; grid-template-columns: 24px minmax(0, 1fr) auto; align-items: center; gap: 10px;
  min-height: 38px; padding: 4px 8px; border: 1px solid transparent; border-radius: 8px; background: transparent;
  font-size: 13.5px; font-weight: 500; text-align: left; color: var(--ink);
}
.tool.tool-row:hover { background: var(--panel-2); border-color: transparent; }
.tool.tool-row[aria-pressed="true"] { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.tool-icon { display: grid; place-items: center; }
.tool-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tool-key { font: 500 11px var(--font-mono); color: var(--muted); padding: 1px 5px; border: 1px solid var(--line); border-radius: 4px; background: var(--panel); }
.tile-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.tile-grid.frags { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.tool.tool-tile {
  display: grid; place-items: center; min-height: 42px; padding: 2px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--bench); font: 600 16px Arial, sans-serif;
}
.tool.tool-tile span { max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tool.tool-tile.frag { font-size: 13px; }
.side .swatches { padding: 8px 6px 0; }
.compounds-host .panel-body { padding: 14px 4px 24px; }
.side-panel-body[data-category="compounds"] { padding-inline: 8px; }

/* ---------- floating zoom (Freeform) ---------- */
.zoom-float {
  position: absolute; right: 14px; bottom: 14px; z-index: 4; display: flex; align-items: center; gap: 0;
  padding: 2px; border-radius: 10px; background: var(--panel); border: 1px solid var(--line); box-shadow: var(--shadow);
}
.zoom-float .icon-btn { width: 32px; height: 32px; font-size: 18px; }
.zoom-float .zoom-label { min-width: 46px; text-align: center; font: 500 12px var(--font-mono); color: var(--ink-2); }
.editor-stage .stage-tip { right: 190px; }

/* Narrow screens: the panel floats over the canvas instead of squeezing it. */
@media (max-width: 900px) {
  .side { position: relative; }
  .side-panel { position: absolute; left: var(--side-tabs-w); top: 0; bottom: 0; z-index: 8; background: var(--panel); border-right: 1px solid var(--line); box-shadow: var(--shadow); }
  .tb-center { margin-left: 0; }
  .editor-stage .stage-tip { right: 16px; bottom: 60px; }
}

/* Phones: one row of formula chips that scrolls sideways, so a lab page's list doesn't take half the screen. */
@media (max-width: 600px) {
  .editor-info { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .editor-info .chip { flex: none; white-space: nowrap; }
  /* The toolbar scrolls sideways here; Check (what a lab page asks for) comes first so it is on screen. */
  .tb-center .check-btn { order: -1; }
}

/* Export sheet: quick copies on the left, one "Save as" control on the right */
.export-actions { align-items: center; flex-wrap: wrap; gap: 8px; }
.save-as { display: inline-flex; align-items: center; gap: 8px; margin-left: auto; }
.save-as label { font-size: 13px; color: var(--muted); }
.export-format { height: var(--touch); padding: 0 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); color: var(--ink); font: 500 14px var(--font-body); }
.export-format:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* iPad portrait and narrower: the toolbar fits without scrolling sideways */
@media (max-width: 900px) {
  .editor-bar.toolbar { gap: 2px; }
  .editor-bar.toolbar .id-box { min-width: 110px; flex-basis: 140px; margin-inline: 4px; }
  .tb-btn { min-width: 44px; padding-inline: 5px; }
  .tb-trailing { margin-left: 4px; padding-left: 6px; }
}

/* Live 3D pane (View ▸ Show 3D Model), beside the canvas */
.editor-body { grid-template-columns: auto minmax(0, 1fr) auto; position: relative; }
.live3d { width: 320px; display: flex; flex-direction: column; min-height: 0; background: var(--panel); border-left: 1px solid var(--line); }
.live3d-head { display: flex; align-items: center; gap: 2px; padding: 8px 8px 8px 14px; border-bottom: 1px solid var(--line); }
.live3d-head strong { flex: 1; font: 600 14px var(--font-display); }
.icon-btn.small { width: 32px; height: 32px; }
.icon-btn.small[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.live3d-stage { position: relative; flex: 1; min-height: 0; background: radial-gradient(circle at 40% 35%, var(--panel), var(--panel-2)); }
.live3d-svg { position: absolute; inset: 0; width: 100%; height: 100%; min-height: 0; }
.live3d-status { margin: 0; padding: 8px 14px 10px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); min-height: 34px; }
@media (max-width: 900px) {
  .live3d { position: absolute; right: 0; top: 0; bottom: 0; z-index: 7; width: min(300px, 70vw); box-shadow: var(--shadow); }
}
.pop-swatch { display: inline-block; width: 12px; height: 12px; border-radius: 50%; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15); vertical-align: middle; }
/* Highlights: pastel fills read well under black bonds; under white bonds (dark theme) tone them down.
   Exports always use the full print colours. */
:root[data-theme="dark"] .stage-svg .highlight-layer { opacity: 0.42; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .stage-svg .highlight-layer { opacity: 0.42; } }

/* ---------- first-visit welcome (ui/welcome.js) ---------- */
.welcome-backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(10, 16, 22, 0.4); display: grid; place-items: center; padding: 16px; animation: welcome-fade 220ms ease-out both; }
.welcome-card {
  position: relative; width: min(480px, 100%); max-height: calc(100% - 32px); overflow-y: auto; background: var(--panel);
  border-radius: 18px; box-shadow: var(--shadow); padding: 28px 24px 20px; display: flex; flex-direction: column; align-items: center; gap: 12px; text-align: center;
  animation: welcome-rise 320ms cubic-bezier(0.16, 1, 0.3, 1) both;
}
.welcome-close { position: absolute; top: 10px; right: 10px; }
.welcome-logo { width: 56px; height: 56px; }
.welcome-card h2 { margin: 0; font: 700 22px var(--font-display); color: var(--ink); }
.welcome-lead { margin: 0; font-size: 14px; line-height: 1.55; color: var(--muted); }
.welcome-langs { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; padding: 3px; gap: 2px; }
.welcome-lang { border: 0; background: none; color: var(--ink-2); font: 600 13px var(--font-body); padding: 6px 14px; border-radius: 999px; cursor: pointer; }
.welcome-lang[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.welcome-tries { width: 100%; display: grid; gap: 8px; margin-top: 4px; }
.welcome-try {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; padding: 12px 14px; cursor: pointer;
  border: 1px solid var(--line); border-radius: 12px; background: var(--bench); color: var(--ink); font: inherit;
  transition: border-color 150ms, transform 150ms;
}
.welcome-try:hover, .welcome-try:focus-visible { border-color: var(--accent); }
.welcome-try:active { transform: scale(0.99); }
.welcome-try .icon { color: var(--accent); flex: none; }
.welcome-try strong { display: block; font-size: 14px; }
.welcome-try small { display: block; font-size: 12px; color: var(--muted); }
.welcome-foot { margin: 6px 0 0; font-size: 13px; color: var(--muted); }
.welcome-foot a { color: var(--accent); }
.welcome-foot .link-btn { border: 0; background: none; padding: 0; font: inherit; color: var(--accent); cursor: pointer; text-decoration: underline; }
@keyframes welcome-fade { from { opacity: 0; } }
@keyframes welcome-rise { from { opacity: 0; transform: translateY(12px) scale(0.98); } }
@media (prefers-reduced-motion: reduce) { .welcome-backdrop, .welcome-card { animation: none; } }
