[hidden] { display: none !important; }
:root {
  --ink: #0e1726;
  --ink-2: #344054;
  --muted: #5b6577;
  --line: #dfe3ea;
  --line-2: #eceff4;
  --bg: #f4f6f9;
  --surface: #ffffff;
  --accent: #1d4ed8;
  --accent-ink: #ffffff;
  --accent-soft: #e8eefc;
  --danger: #b42318;
  --ok: #067647;
  --radius: 10px;
  --font-body: 'General Sans', 'Segoe UI', system-ui, sans-serif;
  --font-math: 'KaTeX_Main', 'Times New Roman', serif;
  --panel-w: 380px;
  --topbar-h: 52px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .08);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font-body);
  font-size: 15px;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; color: inherit; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
code { font-family: ui-monospace, 'SFMono-Regular', Menlo, Consolas, monospace; font-size: .92em; background: var(--line-2); padding: 1px 6px; border-radius: 5px; white-space: nowrap; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app { display: flex; flex-direction: column; height: 100dvh; }

/* Top bar */
.topbar {
  height: var(--topbar-h); flex: none;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 12px 0 18px;
  background: var(--ink); color: #fff;
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.brand-name { font-weight: 700; font-size: 19px; letter-spacing: -.01em; }
.brand-sub { font-size: 13px; color: #a9b4c6; white-space: nowrap; }
.top-actions { display: flex; align-items: center; gap: 4px; }
.tbtn {
  display: inline-flex; align-items: center; gap: 7px;
  height: 36px; padding: 0 12px; border-radius: 8px;
  background: transparent; border: 0; font-size: 14px; font-weight: 500;
}
.topbar .tbtn:hover { background: rgba(255,255,255,.1); }
.tbtn.icon { width: 36px; padding: 0; justify-content: center; }
.tbtn.ghost { color: var(--ink-2); }
.tbtn.ghost:hover { background: var(--line-2); }
.tbtn[disabled] { opacity: .35; cursor: default; }
.sep { width: 1px; height: 22px; background: rgba(255,255,255,.18); margin: 0 6px; }

.menu-wrap { position: relative; }
.menu {
  position: absolute; top: calc(100% + 6px); right: 0; z-index: 40;
  min-width: 240px; padding: 6px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
}
.panel-head .menu { left: 0; right: auto; min-width: 200px; }
.menu button {
  display: block; width: 100%; text-align: left;
  padding: 9px 12px; border: 0; background: none; border-radius: 6px; font-size: 14px;
}
.menu button:hover, .menu button:focus-visible { background: var(--accent-soft); }
.menu .danger { color: var(--danger); }
.menu hr { border: 0; border-top: 1px solid var(--line-2); margin: 6px 0; }

/* Workspace */
.workspace { flex: 1; display: flex; min-height: 0; }
.panel {
  width: var(--panel-w); flex: none;
  display: flex; flex-direction: column;
  background: var(--surface); border-right: 1px solid var(--line);
  min-height: 0;
}
.panel.collapsed { display: none; }
.panel-head {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 8px 8px 10px; border-bottom: 1px solid var(--line);
  background: #fafbfc;
}
.panel-head .collapse { margin-left: auto; }
.add-btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 12px 0 9px; border-radius: 8px;
  background: var(--accent); color: var(--accent-ink); border: 0; font-weight: 600; font-size: 14px;
}
.add-btn:hover { background: #1a43bd; }
.chip {
  height: 30px; padding: 0 10px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface);
  font-size: 12px; font-weight: 700; letter-spacing: .06em; color: var(--ink-2);
}
.chip:hover { border-color: var(--ink-2); }

.items { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; counter-reset: item; }
.item {
  display: grid; grid-template-columns: 44px 1fr 34px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  position: relative;
}
.item.selected { background: #f7f9ff; }
.item.selected::before { content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--accent); }
.item-gutter {
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start;
  gap: 6px; padding: 12px 0; background: #f6f7f9; border-right: 1px solid var(--line-2);
}
.item-num { font-size: 11px; color: var(--muted); font-weight: 600; }
.swatch {
  width: 22px; height: 22px; border-radius: 50%;
  border: 2px solid var(--c, #999); background: var(--c, #999); padding: 0;
  position: relative;
}
.swatch.off { background: transparent; }
.swatch.none { visibility: hidden; }
.item-main { min-width: 0; padding: 8px 6px 8px 10px; display: flex; flex-direction: column; gap: 6px; }
.expr-input {
  width: 100%; border: 0; outline: 0; background: transparent;
  font-size: 17px; padding: 6px 2px; color: var(--ink);
  font-family: ui-monospace, 'SFMono-Regular', Menlo, Consolas, monospace;
}
.expr-input::placeholder { color: #98a2b3; font-family: var(--font-body); font-size: 15px; }
.math-view {
  min-height: 36px; display: flex; align-items: center;
  font-size: 19px; padding: 2px; cursor: text; overflow-x: auto; overflow-y: hidden;
  scrollbar-width: thin;
}
.math-view .katex { font-size: 1.05em; }
.math-view.placeholder { color: #98a2b3; font-size: 15px; font-family: var(--font-body); }
.preview { font-size: 15px; color: var(--ink-2); min-height: 0; overflow-x: auto; }
.preview:empty { display: none; }
.result {
  align-self: flex-end; font-size: 16px; font-weight: 600; color: var(--ink);
  background: var(--line-2); border-radius: 6px; padding: 3px 10px;
}
.msg { font-size: 13px; line-height: 1.4; color: var(--danger); display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.msg.info { color: var(--muted); }
.msg button {
  border: 1px solid var(--accent); color: var(--accent); background: var(--surface);
  border-radius: 999px; padding: 3px 10px; font-size: 12px; font-weight: 600;
}
.msg button:hover { background: var(--accent-soft); }
.item-tools { display: flex; flex-direction: column; align-items: center; padding-top: 8px; gap: 2px; }
.del {
  width: 28px; height: 28px; border: 0; background: none; border-radius: 6px; color: #98a2b3;
  display: grid; place-items: center;
}
.del:hover { background: #fde8e7; color: var(--danger); }
.mini {
  width: 28px; height: 28px; border: 0; background: none; border-radius: 6px; color: #98a2b3;
  display: grid; place-items: center;
}
.mini:hover { background: var(--line-2); color: var(--ink); }
.mini svg, .del svg { width: 16px; height: 16px; }

/* Slider */
.slider { display: grid; grid-template-columns: 30px auto 1fr auto; align-items: center; gap: 8px; }
.slider input[type=range] { width: 100%; accent-color: var(--accent); height: 22px; }
.slider .bound {
  width: 46px; border: 1px solid transparent; border-radius: 5px; background: none;
  font-size: 12px; color: var(--muted); text-align: center; padding: 2px;
}
.slider .bound:hover, .slider .bound:focus { border-color: var(--line); color: var(--ink); outline: 0; }
.play {
  width: 30px; height: 30px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--surface); display: grid; place-items: center; color: var(--ink-2);
}
.play svg { width: 13px; height: 13px; fill: currentColor; stroke: none; }
.play.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.step-row { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.step-row input { width: 52px; border: 1px solid var(--line); border-radius: 5px; padding: 2px 4px; font-size: 12px; text-align: center; }

/* Note */
.note-input {
  width: 100%; border: 0; outline: 0; resize: none; background: transparent;
  font: inherit; font-size: 15px; line-height: 1.45; color: var(--ink-2); padding: 6px 2px; min-height: 34px;
}
.item.note .item-gutter { background: #fffbeb; }

/* Table */
.vtable { border-collapse: collapse; width: 100%; max-width: 280px; font-size: 15px; }
.vtable th { background: #f2f4f7; font-family: var(--font-math); font-style: italic; font-weight: 400; font-size: 17px; padding: 4px; border: 1px solid var(--line); }
.vtable td { border: 1px solid var(--line); padding: 0; }
.vtable input { width: 100%; border: 0; padding: 6px 8px; text-align: center; background: transparent; outline: 0; }
.vtable input:focus { background: var(--accent-soft); }
.table-opts { display: flex; gap: 12px; font-size: 13px; color: var(--muted); align-items: center; flex-wrap: wrap; }
.table-opts label { display: inline-flex; gap: 5px; align-items: center; }

.add-row {
  margin: 0; border: 0; border-top: 1px solid var(--line); background: #fafbfc; padding: 12px; text-align: left;
  color: var(--accent); font-weight: 600; font-size: 14px;
}
.add-row:hover { background: var(--accent-soft); }

/* Graph */
.graph { flex: 1; position: relative; min-width: 0; background: #fff; touch-action: none; }
#canvas { position: absolute; inset: 0; width: 100%; height: 100%; cursor: grab; display: block; }
#canvas.dragging { cursor: grabbing; }
#canvas.hover-curve { cursor: pointer; }
.show-panel {
  position: absolute; left: 10px; top: 10px; width: 36px; height: 36px; border-radius: 8px;
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); display: grid; place-items: center;
}
.zoom { position: absolute; right: 12px; top: 12px; display: flex; flex-direction: column; gap: 6px; }
.zbtn {
  width: 38px; height: 38px; border-radius: 9px; background: var(--surface);
  border: 1px solid var(--line); box-shadow: 0 1px 2px rgba(16,24,40,.08);
  display: grid; place-items: center; color: var(--ink-2);
}
.zbtn:hover { color: var(--ink); border-color: #c4cad4; }
.zbtn.on { background: var(--ink); color: #fff; border-color: var(--ink); }
#btnSettings { margin-bottom: 6px; }
.settings {
  position: absolute; right: 60px; top: 12px; width: 270px; z-index: 30;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
  padding: 14px 16px 16px;
}
.settings h3 { margin: 0 0 10px; font-size: 15px; font-weight: 600; }
.settings .row { display: flex; align-items: center; gap: 9px; font-size: 14px; padding: 5px 0; }
.settings input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--accent); }
.win { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 12px 0; padding-top: 12px; border-top: 1px solid var(--line-2); }
.win-title { grid-column: 1 / -1; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.win label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.win input { border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; font-size: 14px; width: 100%; }
.small-btn { border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 7px 12px; font-size: 13px; font-weight: 500; width: 100%; }
.small-btn:hover { background: var(--line-2); }
.tooltip {
  position: absolute; pointer-events: none; z-index: 20;
  background: var(--ink); color: #fff; font-size: 13px; font-weight: 500;
  padding: 4px 9px; border-radius: 6px; white-space: nowrap; transform: translate(-50%, calc(-100% - 12px));
}

/* Keypad */
.keypad { flex: none; background: #e9ecf1; border-top: 1px solid var(--line); padding: 8px; }
.kp-grid { display: grid; grid-template-columns: repeat(10, 1fr); gap: 6px; max-width: 980px; margin: 0 auto; }
.key {
  height: 42px; border-radius: 8px; border: 0; background: var(--surface); font-size: 16px;
  box-shadow: 0 1px 0 #c9ced8; font-family: var(--font-math);
}
.key.fn { font-family: var(--font-body); font-size: 14px; background: #f8f9fb; }
.key.act { background: #d7dce5; font-family: var(--font-body); font-size: 14px; }
.key.go { background: var(--accent); color: #fff; font-family: var(--font-body); font-weight: 600; font-size: 14px; }
.key:active { transform: translateY(1px); box-shadow: none; }

/* Modals */
.modal { position: fixed; inset: 0; background: rgba(14, 23, 38, .45); display: grid; place-items: center; z-index: 100; padding: 16px; }
.modal[hidden] { display: none; }
.modal-card { background: var(--surface); border-radius: 14px; width: 100%; max-width: 560px; max-height: calc(100dvh - 32px); overflow: auto; box-shadow: var(--shadow); padding: 20px 22px 24px; }
.modal-card.wide { max-width: 900px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.modal-head h2 { margin: 0; font-size: 20px; font-weight: 600; letter-spacing: -.01em; }
.modal-lead { color: var(--muted); margin: 6px 0 18px; font-size: 14px; }
.ex-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; }
.ex-card {
  text-align: left; border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 14px;
  display: flex; flex-direction: column; gap: 6px; transition: border-color .15s, box-shadow .15s;
}
.ex-card:hover { border-color: var(--accent); box-shadow: 0 4px 14px rgba(29,78,216,.12); }
.ex-grade { font-size: 13px; font-weight: 600; color: var(--accent); }
.ex-name { font-weight: 600; font-size: 15px; }
.ex-desc { font-size: 13px; color: var(--muted); line-height: 1.4; }
.ex-math { font-size: 15px; color: var(--ink); margin-top: 2px; overflow: hidden; }
.help-body { display: grid; grid-template-columns: 1.25fr 1fr; gap: 24px; margin-top: 16px; }
.help-table { border-collapse: collapse; width: 100%; font-size: 14px; }
.help-table th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 6px 8px; border-bottom: 2px solid var(--line); }
.help-table td { padding: 8px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
.help-tips h3 { margin: 0 0 8px; font-size: 15px; }
.help-tips ul { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 10px; font-size: 14px; line-height: 1.5; color: var(--ink-2); }
.help-tips code { white-space: normal; }

.toast {
  position: fixed; left: 50%; top: 64px; transform: translateX(-50%); z-index: 200;
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px; box-shadow: var(--shadow);
}

/* Mobile */
@media (max-width: 760px) {
  :root { --topbar-h: 50px; }
  .brand-sub { display: none; }
  .tbtn span { display: none; }
  .tbtn { padding: 0; width: 38px; justify-content: center; }
  .sep { margin: 0 2px; }
  .workspace { flex-direction: column-reverse; }
  .panel { width: 100%; height: 45%; border-right: 0; border-top: 1px solid var(--line); }
  .panel .collapse { display: none; }
  .graph { flex: 1; }
  .settings { right: 58px; width: min(270px, calc(100vw - 80px)); max-height: calc(100% - 24px); overflow: auto; }
  .keypad { padding: 6px 4px; }
  .kp-grid { gap: 4px; }
  .key { height: 36px; font-size: 15px; padding: 0; }
  .key.fn, .key.act, .key.go { font-size: 12px; }
  .help-body { grid-template-columns: 1fr; }
  .modal-card { padding: 16px; }
  .help-table code { white-space: normal; }
}
.tbtn.ghost.on { background: var(--ink); color: #fff; }
