:root {
  --bg: #fafafa;
  --ink: #1c1c28;
  --muted: #8a8a99;
  --panel: #ffffff;
  --line: #e4e4ee;
  --accent: #4f46e5;
  --accent-soft: #eef;
  --free: #4f46e5;
  --derived: #0d9488;
  --shadow: 0 4px 24px rgba(20, 20, 40, .10);
  --radius: 14px;
}

* { box-sizing: border-box; margin: 0; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  font: 15px/1.4 -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}

#stage { position: fixed; inset: 0; }

#canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; cursor: crosshair; }
#canvas.tool-move { cursor: default; }
#canvas.over-draggable { cursor: grab; }
#canvas.dragging { cursor: grabbing; }

/* ---------- floating chrome ---------- */

#topbar {
  position: absolute; top: 10px; left: 10px; right: 10px;
  display: flex; align-items: center; gap: 8px;
  pointer-events: none;
}
#topbar > * { pointer-events: auto; }
#topbar .spacer { flex: 1; pointer-events: none; }

#docName {
  border: none; background: transparent; font: inherit; font-weight: 600;
  color: var(--ink); width: 11ch; max-width: 45vw;
  padding: 6px 8px; border-radius: 8px; outline: none;
  text-overflow: ellipsis;
}
#docName:focus { background: var(--panel); box-shadow: var(--shadow); }

#saveState { font-size: 12px; color: var(--muted); }

.chip {
  border: none; background: var(--panel); color: var(--ink);
  box-shadow: var(--shadow);
  width: 38px; height: 38px; border-radius: 10px;
  font-size: 17px; cursor: pointer; display: grid; place-items: center;
}
.chip:active { transform: scale(.95); }
.chip:disabled { opacity: .35; cursor: default; }
.chip svg { width: 20px; height: 20px; }

#btnMeasure.on { background: var(--accent); color: #fff; }

/* ---------- toolbar ---------- */

#toolwrap {
  position: absolute; top: 58px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  touch-action: none;
}
#toolwrap.moved { transform: none; }

#toolbar {
  position: relative;
  display: flex; flex-wrap: wrap; gap: 4px; padding: 5px; padding-right: 12px;
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  justify-content: center;
}

#toolresize {
  position: absolute; top: 6px; bottom: 6px; right: 2px; width: 8px;
  cursor: ew-resize; border-radius: 4px; opacity: 0;
  background: var(--accent-soft);
  display: grid; place-items: center; font-size: 9px; color: var(--accent);
  transition: opacity .12s;
}
#toolbar:hover #toolresize { opacity: 1; }

#toolgrip {
  align-self: stretch; display: grid; place-items: center;
  padding: 0 4px; color: var(--muted); cursor: grab; font-size: 13px;
  border-radius: 8px;
}
#toolgrip:active { cursor: grabbing; background: var(--accent-soft); }

#graphInput {
  display: flex; flex-direction: column; gap: 8px; padding: 12px;
  width: min(430px, 94vw);
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  font-size: 14px;
}
#graphInput .gi-row { display: flex; align-items: center; gap: 8px; }
#graphInput .gi-fx { font-weight: 700; font-size: 16px; white-space: nowrap; }
#graphInput input {
  flex: 1; min-width: 0;
  border: 2px solid var(--line); outline: none;
  font: inherit; font-size: 16px;
  background: var(--bg); border-radius: 9px; padding: 9px 12px;
}
#graphInput input:focus { border-color: var(--accent); }
#graphInput input.bad { border-color: #ef4444; background: #fef2f2; }
#graphInput .gi-plot {
  border: none; background: var(--accent); color: #fff; font: inherit;
  font-size: 15px; font-weight: 700;
  padding: 9px 18px; border-radius: 9px; cursor: pointer;
}
#graphInput .gi-plot:active { transform: scale(.96); }

#graphInput .gi-sug {
  display: flex; flex-direction: column; border-radius: 9px; overflow: hidden;
  border: 1px solid var(--line);
}
#graphInput .gi-sug-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  border: none; background: var(--panel); cursor: pointer;
  font: inherit; font-size: 13.5px; padding: 7px 12px; text-align: left;
}
#graphInput .gi-sug-row b { color: var(--accent); font-family: ui-monospace, monospace; }
#graphInput .gi-sug-row span { color: var(--muted); font-size: 12px; }
#graphInput .gi-sug-row.active, #graphInput .gi-sug-row:hover { background: var(--accent-soft); }

#graphInput .gi-msg { font-size: 12.5px; color: var(--muted); line-height: 1.45; }
#graphInput .gi-msg.ok { color: var(--derived); font-weight: 600; }
#graphInput .gi-msg b { color: var(--ink); }
#graphInput .gi-msg.ok b { color: var(--derived); }

#graphInput .gi-chips { display: flex; flex-wrap: wrap; gap: 5px; }
#graphInput .gi-chips button {
  border: none; background: var(--accent-soft); color: var(--accent);
  font: inherit; font-size: 13px; font-weight: 600;
  padding: 5px 11px; border-radius: 999px; cursor: pointer;
}
#graphInput .gi-chips button:hover { background: var(--accent); color: #fff; }

#graphInput .gi-ops button {
  background: var(--bg); color: var(--ink);
  min-width: 40px; border-radius: 9px;
  font-family: ui-monospace, monospace; font-size: 14px;
}
#graphInput .gi-ops button:hover { background: var(--accent); color: #fff; }

#graphInput .gi-lims {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  font-size: 13px; color: var(--muted);
}
#graphInput .gi-lims span { font-weight: 700; color: var(--ink); margin-right: 2px; }
#graphInput .gi-lims em { font-style: normal; opacity: .5; }
#graphInput .gi-lim {
  width: 6ch; border: 1.5px solid var(--line); outline: none;
  font: inherit; font-size: 13px; text-align: center;
  background: var(--bg); border-radius: 7px; padding: 4px 4px;
}
#graphInput .gi-lim:focus { border-color: var(--accent); }

#shapeMenu {
  position: absolute; z-index: 15;
  display: flex; gap: 3px; padding: 5px; align-items: center;
  background: var(--panel); border-radius: 12px; box-shadow: var(--shadow);
}
#shapeMenu button {
  border: none; background: var(--accent-soft); color: var(--accent);
  font: inherit; font-size: 15px; font-weight: 600;
  width: 38px; height: 34px; border-radius: 8px; cursor: pointer;
}
#shapeMenu button:hover { background: var(--accent); color: #fff; }
#shapeMenu input {
  width: 4ch; border: none; outline: none; font: inherit; text-align: center;
  background: var(--bg); border-radius: 7px; padding: 7px 2px;
}

#helpPanel {
  position: absolute; inset: 0; background: rgba(20,20,40,.35);
  display: grid; place-items: center; z-index: 20;
}
.help-card {
  background: var(--panel); border-radius: 20px; box-shadow: var(--shadow);
  max-width: min(560px, 94vw); max-height: 86vh; overflow-y: auto;
}
.help-hero {
  position: relative; padding: 26px 26px 6px;
  background: linear-gradient(135deg, #eef 0%, #f6f0ff 100%);
}
.help-hero h2 { font-size: 22px; letter-spacing: -.3px; }
.help-hero p { color: var(--muted); font-size: 14px; margin: 4px 0 14px; }
.help-hero .chip { position: absolute; top: 14px; right: 14px; }
.help-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 14px;
}
.help-item {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 10px 11px; border-radius: 12px; background: var(--bg);
  font-size: 12.5px; line-height: 1.45; color: var(--muted);
}
.help-item span { font-size: 19px; line-height: 1.2; }
.help-item b { display: block; color: var(--ink); font-size: 13px; margin-bottom: 1px; }
.help-item code { background: var(--accent-soft); padding: 0 4px; border-radius: 4px; color: var(--accent); }
.help-keys {
  padding: 4px 20px 18px; font-size: 11.5px; color: var(--muted); line-height: 1.8; text-align: center;
}
.help-keys b {
  color: var(--ink); background: var(--bg); border: 1px solid var(--line);
  border-radius: 5px; padding: 1px 5px; font-size: 11px;
}
@media (max-width: 520px) { .help-grid { grid-template-columns: 1fr; } }

#btnMeasure.on { background: var(--accent); }

#toolbar button {
  border: none; background: transparent; cursor: pointer;
  border-radius: 10px; padding: 7px 12px 5px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--ink); font-size: 11px; min-width: 56px;
}
#toolbar button svg { width: 22px; height: 22px; }
/* Graph opens a panel rather than switching modes — set it slightly apart */
#toolbar button[data-tool="graph"] {
  margin-left: 5px; position: relative;
}
#toolbar button[data-tool="graph"]::before {
  content: ''; position: absolute; left: -3px; top: 8px; bottom: 8px;
  width: 1px; background: var(--line);
}
#toolbar button.active { background: var(--accent); color: #fff; }
#toolbar button.active svg * { stroke: #fff; }

#subtools {
  display: flex; gap: 4px; padding: 4px;
  background: var(--panel); border-radius: 10px; box-shadow: var(--shadow);
}
#subtools button {
  border: none; background: transparent; cursor: pointer; font: inherit; font-size: 12px;
  padding: 5px 10px; border-radius: 7px; color: var(--ink);
}
#subtools button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* ---------- hint ---------- */

#hint {
  position: absolute; bottom: 68px; left: 50%; transform: translateX(-50%);
  background: rgba(28, 28, 40, .82); color: #fff;
  padding: 7px 14px; border-radius: 999px; font-size: 13px;
  max-width: 90vw; text-align: center;
  transition: opacity .15s; pointer-events: none; white-space: nowrap;
}
#hint:empty { opacity: 0; }

/* ---------- context actions ---------- */

#contextbar {
  position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 0;
  background: var(--panel); border-radius: var(--radius);
  border: 1px solid var(--line);
  box-shadow: 0 8px 30px rgba(20, 20, 40, .14);
  max-width: 94vw; padding: 0;
  overflow: hidden;
}
#contextbar .cb-head {
  display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap;
  padding: 8px 14px 7px; background: var(--bg);
  border-bottom: 1px solid var(--line);
  font-size: 12.5px;
}
#contextbar .cb-title { font-size: 13px; color: var(--ink); white-space: nowrap; }
#contextbar .cb-note { color: var(--derived); font-weight: 600; white-space: nowrap; }
#contextbar .cb-tip { color: var(--muted); margin-left: auto; white-space: nowrap; }
#contextbar .cb-body {
  display: flex; align-items: center; gap: 4px; flex-wrap: wrap;
  padding: 5px 8px; justify-content: center;
}
#contextbar .cb-sec {
  display: flex; align-items: center; gap: 3px; flex-wrap: wrap; justify-content: center;
}
#contextbar .cb-sys { margin-left: 10px; padding-left: 10px; border-left: 1px solid var(--line); }
#contextbar button {
  border: none; background: transparent; color: var(--accent);
  font: inherit; font-size: 13px; font-weight: 600;
  padding: 8px 11px; border-radius: 9px; cursor: pointer; white-space: nowrap;
}
#contextbar button:hover { background: var(--accent-soft); }
#contextbar button:active { transform: scale(.96); }
#contextbar .cb-sys button { color: var(--ink); font-weight: 500; }
#contextbar .cb-sys button:hover { background: var(--bg); }
#contextbar button.danger { color: #b91c1c; font-weight: 600; }
#contextbar button.danger:hover { background: #fee2e2; }
#contextbar .stepper { display: flex; align-items: center; gap: 2px; }
#contextbar .stepper span { min-width: 3ch; text-align: center; font-weight: 700; }
#contextbar .aw-lbl { color: var(--muted); min-width: auto !important; }
#contextbar .aw-inp {
  width: 5ch; border: 1.5px solid var(--line); outline: none;
  font: inherit; font-size: 13px; font-weight: 600; text-align: center;
  background: var(--bg); border-radius: 7px; padding: 4px 2px;
}
#contextbar .aw-inp:focus { border-color: var(--accent); }

/* unlink chooser popover */
#unlinkMenu {
  position: absolute; bottom: 76px; left: 50%; transform: translateX(-50%);
  z-index: 15; width: min(400px, 92vw);
  background: var(--panel); border-radius: var(--radius);
  border: 1px solid var(--line); box-shadow: 0 12px 44px rgba(20, 20, 40, .2);
  padding: 8px; display: flex; flex-direction: column; gap: 2px;
}
#unlinkMenu .ul-head {
  font-size: 12.5px; font-weight: 700; color: var(--muted);
  padding: 4px 8px 6px; text-transform: uppercase; letter-spacing: .04em;
}
#unlinkMenu .ul-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 6px 8px; border-radius: 9px; font-size: 13.5px;
}
#unlinkMenu .ul-row:hover { background: var(--bg); }
#unlinkMenu .ul-row button {
  border: none; background: var(--accent-soft); color: var(--accent);
  font: inherit; font-size: 12.5px; font-weight: 700;
  padding: 5px 12px; border-radius: 999px; cursor: pointer; flex-shrink: 0;
}
#unlinkMenu .ul-row button:hover { background: var(--accent); color: #fff; }
#unlinkMenu .ul-all {
  border: none; background: transparent; color: var(--muted);
  font: inherit; font-size: 12.5px; font-weight: 600;
  padding: 7px; border-radius: 9px; cursor: pointer; margin-top: 2px;
}
#unlinkMenu .ul-all:hover { background: var(--bg); color: var(--ink); }

/* ---------- files panel ---------- */

#filesPanel {
  position: absolute; top: 0; left: 0; bottom: 0; width: min(320px, 92vw);
  background: var(--panel); box-shadow: var(--shadow);
  display: flex; flex-direction: column; z-index: 10;
}
.panel-head, .panel-foot {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px; gap: 8px;
}
.panel-foot { border-top: 1px solid var(--line); }
.panel-foot button {
  flex: 1; border: none; background: var(--accent-soft); color: var(--accent);
  font: inherit; font-weight: 600; font-size: 13px; padding: 9px 4px; border-radius: 9px; cursor: pointer;
}
#btnNew { background: var(--accent); color: #fff; }

#filesList { flex: 1; overflow-y: auto; padding: 0 10px; }
.file-row {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 8px; border: none; background: transparent; border-radius: 10px;
  cursor: pointer; font: inherit; text-align: left; color: var(--ink);
}
.file-row:hover { background: var(--accent-soft); }
.file-row img {
  width: 52px; height: 40px; object-fit: cover; border-radius: 6px;
  background: var(--bg); border: 1px solid var(--line);
}
.file-row .meta { flex: 1; min-width: 0; }
.file-row .meta b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-row .meta small { color: var(--muted); }
.file-row .del {
  border: none; background: transparent; color: var(--muted); cursor: pointer;
  font-size: 15px; padding: 6px; border-radius: 7px;
}
.file-row .del:hover { color: #b91c1c; background: #fee2e2; }

.empty-note { color: var(--muted); text-align: center; padding: 30px 10px; font-size: 13px; }

#renameInput {
  position: absolute; z-index: 16; width: 104px;
  border: 2px solid var(--accent); outline: none;
  background: var(--panel); color: var(--ink);
  font: inherit; font-size: 14px; font-weight: 600;
  padding: 5px 9px; border-radius: 9px; box-shadow: var(--shadow);
}

#zoomCtrl {
  position: absolute; right: 14px; bottom: 14px;
  display: flex; flex-direction: column; gap: 6px;
}
#zoomCtrl .chip { font-size: 15px; }

@media (max-width: 700px) {
  /* keep the context bar clear of the Shift and zoom buttons */
  #contextbar { bottom: 64px; max-width: 86vw; }
  #hint { bottom: 122px; white-space: normal; }
}

@media (max-width: 560px) {
  #toolbar button { min-width: 46px; padding: 6px 8px 4px; }
  #docName { width: 8ch; }
}

/* ---------- variable sliders ---------- */

#varsPanel {
  position: absolute; top: 58px; right: 14px; width: 224px;
  background: var(--panel); border-radius: var(--radius);
  border: 1px solid var(--line); box-shadow: var(--shadow);
  padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 8px;
  max-height: 60vh; overflow-y: auto; z-index: 5;
}
#varsPanel .vr-head {
  font-size: 11px; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: .05em;
}
#varsPanel .vr { display: flex; flex-direction: column; gap: 3px; }
#varsPanel .vr-top { display: flex; align-items: center; gap: 4px; font-size: 13px; }
#varsPanel .vr-top b { font-size: 14px; }
#varsPanel .vr-val {
  color: var(--derived); font-weight: 600; flex: 1;
  cursor: text; border-radius: 5px; padding: 1px 3px; margin: -1px -3px;
}
#varsPanel .vr-val:hover, #varsPanel .vr-val:focus-visible {
  background: var(--bg); outline: none;
}
#varsPanel .vr-val-inp {
  flex: 1; min-width: 0; width: 100%;
  border: 1.5px solid var(--accent); outline: none;
  font: inherit; font-size: 13px; font-weight: 600; color: var(--derived);
  background: var(--bg); border-radius: 6px; padding: 2px 5px;
}
#varsPanel .vr-fam, #varsPanel .vr-del {
  border: none; background: var(--bg); color: var(--muted);
  width: 26px; height: 24px; border-radius: 7px; cursor: pointer; font-size: 13px;
}
#varsPanel .vr-fam.on { background: var(--accent); color: #fff; }
#varsPanel .vr-del:hover { background: #fee2e2; color: #b91c1c; }
#varsPanel .vr-lims { display: flex; align-items: center; gap: 5px; }
#varsPanel .vr-lims input[type="range"] { flex: 1; accent-color: var(--accent); }
#varsPanel .vr-lims input:not([type="range"]) {
  width: 4.5ch; border: 1px solid var(--line); outline: none;
  font: inherit; font-size: 11.5px; text-align: center;
  background: var(--bg); border-radius: 6px; padding: 3px 1px;
}
#varsPanel .vr-add input {
  width: 100%; border: 1.5px dashed var(--line); outline: none;
  font: inherit; font-size: 12.5px; text-align: center; color: var(--ink);
  background: transparent; border-radius: 8px; padding: 5px;
}
#varsPanel .vr-add input:focus { border-color: var(--accent); border-style: solid; }

@media (max-width: 700px) {
  #varsPanel { top: auto; bottom: 150px; width: 200px; max-height: 40vh; }
}
