:root {
  --bg: #f5f3ee;
  --paper-mark: rgba(60, 60, 67, 0.16);
  --panel: rgba(255, 255, 255, 0.86);
  --panel-solid: #ffffff;
  --border: rgba(60, 60, 67, 0.14);
  --text: #1c1c1e;
  --muted: #8a8a8e;
  --accent: #0a84ff;
  --answer: #e8930c;
  --ink: #1c1c1e;
  --grid: rgba(60, 60, 67, 0.1);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12), 0 1px 3px rgba(0, 0, 0, 0.08);
  --danger: #ff3b30;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1c1e;
    --paper-mark: rgba(235, 235, 245, 0.12);
    --panel: rgba(44, 44, 46, 0.86);
    --panel-solid: #2c2c2e;
    --border: rgba(235, 235, 245, 0.12);
    --text: #f2f2f7;
    --muted: #98989f;
    --answer: #ffb340;
    --ink: #f2f2f7;
    --grid: rgba(235, 235, 245, 0.1);
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1c1c1e; --paper-mark: rgba(235, 235, 245, 0.12); --panel: rgba(44, 44, 46, 0.86);
  --panel-solid: #2c2c2e; --border: rgba(235, 235, 245, 0.12); --text: #f2f2f7; --muted: #98989f;
  --answer: #ffb340; --ink: #f2f2f7; --grid: rgba(235, 235, 245, 0.1); --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; overscroll-behavior: none; }
button { font: inherit; color: inherit; }
#app { display: flex; height: 100%; }

/* ── sidebar ── */
#sidebar { width: 260px; flex: none; display: flex; flex-direction: column; background: var(--panel-solid);
  border-right: 1px solid var(--border); transition: margin-left .25s ease; z-index: 5; }
#app.sidebar-hidden #sidebar { margin-left: -260px; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 10px 18px; }
.brand { font-size: 20px; letter-spacing: -0.02em; }
.brand b { color: var(--answer); }
#noteList { list-style: none; margin: 0; padding: 4px 8px; overflow-y: auto; flex: 1; }
#noteList li { display: flex; align-items: center; gap: 6px; padding: 9px 10px; border-radius: 10px; cursor: pointer; }
#noteList li:hover { background: var(--grid); }
#noteList li.active { background: var(--accent); color: #fff; }
#noteList li .meta { flex: 1; min-width: 0; }
#noteList li .title { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
#noteList li .sub { font-size: 12px; opacity: .7; }
#noteList li .del { opacity: 0; border: 0; background: none; cursor: pointer; padding: 4px; border-radius: 6px; }
#noteList li:hover .del, #noteList li.active .del { opacity: .8; }
#noteList li input { width: 100%; font: inherit; font-weight: 600; border: 1px solid var(--border); border-radius: 6px; padding: 2px 4px; }
.side-foot { padding: 10px 18px 16px; font-size: 12px; color: var(--muted); }

/* ── stage ── */
#stage { position: relative; flex: 1; min-width: 0; overflow: hidden; }
#board { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: crosshair; display: block; }
#board.tool-hand, #board.panning { cursor: grab; }
#board.tool-eraser { cursor: cell; }
#overlay { position: absolute; inset: 0; pointer-events: none; }
.graph-chip { position: absolute; transform: translate(-100%, -50%); pointer-events: auto; border: 1px solid var(--border); background: var(--panel);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-radius: 999px; padding: 3px 9px 3px 7px;
  display: flex; align-items: center; gap: 4px; font-size: 12px; cursor: pointer; box-shadow: var(--shadow); color: var(--text); }
.graph-chip svg { width: 15px; height: 15px; fill: none; stroke: var(--answer); stroke-width: 2; stroke-linecap: round; }

#emptyHint { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -40%); max-width: 440px; width: calc(100% - 32px);
  color: var(--muted); text-align: center; pointer-events: none; }
#emptyHint h2 { color: var(--text); font-weight: 600; margin: 0 0 10px; opacity: .7; }
#emptyHint p { margin: 6px 0; }
#emptyHint code { background: var(--grid); padding: 1px 5px; border-radius: 4px; }

/* ── toolbar ── */
.toolbar { position: absolute; top: max(12px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 2px; padding: 5px 6px; border-radius: 16px; background: var(--panel);
  backdrop-filter: blur(20px) saturate(1.6); -webkit-backdrop-filter: blur(20px) saturate(1.6);
  border: 1px solid var(--border); box-shadow: var(--shadow); max-width: calc(100% - 24px); overflow-x: auto; scrollbar-width: none; z-index: 4; }
.toolbar::-webkit-scrollbar { display: none; }
.sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; flex: none; }
.icon-btn { flex: none; width: 36px; height: 36px; border: 0; border-radius: 10px; background: transparent; cursor: pointer;
  display: inline-grid; place-items: center; color: var(--text); }
.icon-btn:hover { background: var(--grid); }
.icon-btn.small { width: 26px; height: 26px; border-radius: 7px; font-size: 13px; }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn circle { fill: currentColor; stroke: none; }
.icon-btn.active { background: var(--accent); color: #fff; }
.icon-btn:disabled { opacity: .35; cursor: default; background: transparent; }
.tools { display: flex; gap: 2px; }
.segmented { display: flex; background: var(--grid); border-radius: 10px; padding: 2px; flex: none; }
.segmented button { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 13px; color: var(--muted); }
.segmented button.active { background: var(--panel-solid); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.segmented button[data-mode="math"].active { color: var(--answer); }
.colors { display: flex; gap: 4px; padding: 0 4px; }
.swatch { width: 24px; height: 24px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; padding: 0; flex: none;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.15); }
.swatch.active { border-color: var(--panel-solid); box-shadow: 0 0 0 2px var(--accent); }
.width-dot { display: block; border-radius: 50%; background: var(--text); }
.menu-wrap { position: relative; }
.menu { position: fixed; min-width: 170px; background: var(--panel-solid); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); padding: 5px; z-index: 20; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.menu button:hover { background: var(--grid); }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 4px 0; }

.selection-bar { position: absolute; bottom: max(20px, env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%); display: flex; gap: 2px;
  background: var(--panel); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px); border: 1px solid var(--border);
  border-radius: 12px; padding: 4px; box-shadow: var(--shadow); z-index: 4; }
.selection-bar button { border: 0; background: none; padding: 7px 12px; border-radius: 8px; cursor: pointer; font-weight: 500; }
.selection-bar button:hover { background: var(--grid); }
.selection-bar .danger { color: var(--danger); }
.zoom-ind { position: absolute; right: 14px; bottom: 14px; font-size: 12px; color: var(--muted); background: var(--panel);
  border: 1px solid var(--border); padding: 3px 8px; border-radius: 8px; cursor: pointer; z-index: 3; }
.toast { position: absolute; left: 50%; bottom: 70px; transform: translateX(-50%) translateY(10px); background: var(--text); color: var(--bg);
  padding: 8px 14px; border-radius: 10px; opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none; z-index: 30; max-width: calc(100% - 32px); }
.toast.show { opacity: .92; transform: translateX(-50%); }

/* ── graph window ── */
.graph-window { position: absolute; min-width: 260px; min-height: 220px; display: flex; flex-direction: column; resize: both; overflow: hidden;
  background: var(--panel-solid); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); z-index: 6; }
.graph-window[hidden] { display: none; }
.graph-head { display: flex; align-items: center; gap: 4px; padding: 6px 6px 6px 12px; cursor: move; border-bottom: 1px solid var(--border); touch-action: none; }
.graph-title { flex: 1; font-weight: 600; }
.graph-legend { display: flex; flex-wrap: wrap; gap: 4px; padding: 6px 8px 0; font-size: 12px; }
.legend-chip { border: 1px solid var(--border); background: none; border-radius: 999px; padding: 2px 8px; cursor: pointer; display: flex; gap: 5px; align-items: center;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.legend-chip i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.legend-chip.off { opacity: .4; }
.graph-canvas { flex: 1; min-height: 0; width: 100%; touch-action: none; cursor: grab; }
.graph-sliders { padding: 0 10px; }
.slider-row { display: flex; align-items: center; gap: 8px; font-size: 12px; padding: 3px 0; }
.slider-row .var { font-style: italic; font-weight: 600; width: 14px; }
.slider-row input { flex: 1; accent-color: var(--answer); }
.slider-row .val { width: 54px; text-align: right; font-variant-numeric: tabular-nums; }
.graph-readout { font-size: 12px; color: var(--muted); padding: 2px 10px 6px; min-height: 22px; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }

/* ── settings ── */
dialog { border: 1px solid var(--border); border-radius: 16px; background: var(--panel-solid); color: var(--text); box-shadow: var(--shadow);
  padding: 0; width: min(460px, calc(100% - 32px)); -webkit-user-select: text; user-select: text; }
dialog::backdrop { background: rgba(0, 0, 0, 0.3); }
dialog form { padding: 18px 20px; }
dialog h3 { margin: 0 0 12px; }
dialog .row { display: flex; align-items: flex-start; gap: 10px; padding: 7px 0; }
dialog .row span { flex: 1; }
dialog .row small { display: block; color: var(--muted); }
dialog .row input[type=checkbox] { margin-top: 3px; width: 18px; height: 18px; accent-color: var(--accent); }
dialog .stack { display: flex; flex-direction: column; gap: 4px; margin: 8px 0; font-weight: 500; }
dialog input[type=text], dialog input[type=password], dialog select { font: inherit; padding: 7px 9px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); }
dialog fieldset { border: 1px solid var(--border); border-radius: 12px; margin: 10px 0; padding: 8px 12px; }
dialog legend { font-weight: 600; padding: 0 4px; }
dialog fieldset p { margin: 4px 0 6px; font-size: 12px; }
dialog .actions { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; }
dialog .primary { background: var(--accent); color: #fff; border: 0; padding: 8px 18px; border-radius: 10px; font-weight: 600; cursor: pointer; }
dialog .link { background: none; border: 0; color: var(--danger); cursor: pointer; padding: 0; }

@media (max-width: 760px) {
  #sidebar { position: absolute; inset: 0 auto 0 0; box-shadow: var(--shadow); }
  .toolbar { left: 12px; right: 12px; transform: none; max-width: none; top: max(10px, env(safe-area-inset-top)); }
  .segmented button { padding: 6px 9px; }
  .graph-window { max-width: calc(100% - 16px); }
}

[hidden] { display: none !important; }
#emptyHint code { white-space: nowrap; }

/* ── graphs beside equations ── */
.inline-graph { position: absolute; left: 0; top: 0; pointer-events: none; transition: opacity .15s;
  animation: ig-in .25s ease-out; background: var(--panel-solid); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: var(--shadow); overflow: hidden; touch-action: none; z-index: 2; will-change: transform; }
.ig-head { display: flex; align-items: center; gap: 6px; height: 30px; padding: 0 4px 0 10px; border-bottom: 1px solid var(--border);
  font: 12px ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--text); }
.ig-swatch { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.ig-title { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ig-btn { pointer-events: auto; border: 0; background: none; color: var(--muted); width: 24px; height: 24px; border-radius: 6px; cursor: pointer; font-size: 13px; padding: 0; }
.ig-btn:hover { background: var(--grid); color: var(--text); }
.inline-graph canvas { display: block; width: 100%; }
/* fade graphs while writing so ink underneath stays visible */
#overlay.writing .inline-graph { opacity: .18; }
@keyframes ig-in { from { opacity: 0; transform: var(--ig-pos) scale(.96); } }
