:root {
  --bg: #0b1020;
  --surface: #121a2e;
  --surface-2: #1a2440;
  --border: #26324f;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #facc15;
  --pos: #ff4d5e;
  --neg: #3b82f6;
  --error: #f87171;
  --cut: #22d3ee;
  --gauss: #c084fc;
  --ok: #4ade80;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #d5dce6;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #ca8a04;
  --pos: #e11d48;
  --neg: #2563eb;
  --error: #dc2626;
  --cut: #0891b2;
  --gauss: #7c3aed;
  --ok: #16a34a;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; } /* rules like .row { display: flex } must not show hidden elements */
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
}
button, select, input, textarea { font: inherit; color: inherit; }
button {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 5px 11px;
  cursor: pointer;
}
button:hover { border-color: var(--muted); }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button.primary { background: var(--accent); color: #111; border-color: transparent; font-weight: 600; }
select, input[type="number"], input[type="text"] {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 6px;
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--muted); }
.spacer { flex: 1; }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 8px; margin-right: 8px; white-space: nowrap; }
.logo {
  width: 22px; height: 14px; display: inline-block;
  background: radial-gradient(circle at 25% 50%, var(--pos) 0 5px, transparent 6px),
              radial-gradient(circle at 75% 50%, var(--neg) 0 5px, transparent 6px);
}
#scene-select { min-width: 230px; max-width: 46vw; }

/* Main layout */
.layout {
  display: grid;
  grid-template-columns: 64px 1fr 300px;
  min-height: 0;
}
.tools {
  display: flex; flex-direction: column; gap: 4px;
  padding: 8px 6px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}
.tools button {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 6px 2px; font-size: 11px; background: transparent; border-color: transparent;
}
.tools button svg { width: 22px; height: 22px; fill: currentColor; stroke: currentColor; }
.tools button.active { background: var(--surface-2); border-color: var(--accent); color: var(--accent); }
.tools .tool-sep { height: 1px; background: var(--border); margin: 4px 2px; }
.tools .sign b { font-size: 20px; line-height: 22px; }
.tools .sign.pos b { color: var(--pos); }
.tools .sign.neg b { color: var(--neg); }

.stage { position: relative; min-width: 0; min-height: 0; overflow: hidden; }
#canvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
#canvas.grab { cursor: grab; }
#canvas.grabbing { cursor: grabbing; }
#canvas.move { cursor: move; }

.legend {
  position: absolute; left: 10px; bottom: 26px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 9px; font-size: 12px; pointer-events: none;
  display: flex; flex-direction: column; gap: 4px; max-width: 280px;
}
.legend:empty { display: none; }
.legend .bar { height: 9px; border-radius: 3px; }
.legend .ends { display: flex; justify-content: space-between; color: var(--muted); font-size: 11px; }
.legend .note { color: var(--muted); font-size: 11px; margin: -2px 0 0 24px; }
.legend .swatch { display: inline-block; width: 18px; height: 0; border-top: 2px solid; vertical-align: middle; margin-right: 6px; }
/* Cut-line plot (top right of the canvas) and Gauss card (bottom right) */
.cutplot, .gausscard {
  position: absolute; right: 10px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 9px; font-size: 12px;
  display: flex; flex-direction: column; gap: 3px;
}
.cutplot { top: 10px; width: min(390px, calc(100% - 20px)); }
.gausscard { bottom: 26px; width: min(300px, calc(100% - 20px)); }
.cutplot[hidden], .gausscard[hidden] { display: none; }
.gausscard .cutplot-head b { color: var(--gauss); }
.seg { display: inline-flex; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.seg button.active { border-color: var(--gauss); color: var(--gauss); position: relative; }
.gauss-table { border-collapse: collapse; font-variant-numeric: tabular-nums; }
.gauss-table td { padding: 1px 0; }
.gauss-table td + td { text-align: right; font-weight: 600; padding-left: 10px; }
.gauss-verdict { font-weight: 600; }
.gauss-verdict.ok { color: var(--ok); }
.gauss-verdict.warn { color: var(--error); font-weight: 500; }
.gausscard .row { margin: 2px 0; font-size: 12px; }
.gausscard .row input { width: 80px; }
.cutplot-head { display: flex; align-items: center; gap: 8px; }
.cutplot-head b { color: var(--cut); }
.cutplot-head button { padding: 0 7px; font-size: 12px; line-height: 20px; }
#cutplot-canvas { display: block; width: 100%; height: 165px; cursor: crosshair; touch-action: none; }
.cutplot-read { font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cutplot-note { color: var(--muted); font-size: 11px; }
#cutplot.folded > :not(.cutplot-head) { display: none; }

.hint {
  position: absolute; top: 40%; left: 50%; transform: translate(-50%, -50%);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 18px; text-align: center; color: var(--muted); pointer-events: none; max-width: 340px;
}

/* Side panel */
.panel {
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 8px;
  display: flex; flex-direction: column; gap: 8px;
}
.card { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 9px 11px; }
.card h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 6px; }
.card p { margin: 0; }
.row { display: flex; align-items: center; gap: 8px; margin: 5px 0; justify-content: space-between; }
.row:has(input[type="checkbox"]) { justify-content: flex-start; }
.row.slider { flex-wrap: wrap; }
.row.slider input { width: 100%; }
.row output { margin-left: auto; color: var(--accent); font-variant-numeric: tabular-nums; }
input[type="range"] { accent-color: var(--accent); }
input[type="checkbox"] { accent-color: var(--accent); }

.props { display: grid; grid-template-columns: auto 1fr; gap: 5px 8px; align-items: center; }
.props label { color: var(--muted); font-size: 12px; }
.props input[type="number"], .props input[type="text"] { width: 100%; min-width: 0; }
.props .wide { grid-column: 1 / -1; }
.props .type { grid-column: 1 / -1; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.props .chip { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.prop-buttons { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.prop-buttons button { flex: 1; padding: 4px 6px; font-size: 12px; }
.prop-buttons .danger { color: var(--error); }

/* Status bar */
.status {
  display: flex; gap: 16px; justify-content: space-between; align-items: center;
  padding: 5px 12px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  font-size: 12.5px; font-variant-numeric: tabular-nums;
  white-space: nowrap; overflow: hidden;
}
#status-probe { overflow: hidden; text-overflow: ellipsis; }
.status .warn { color: var(--error); }

/* Dialogs */
dialog {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px;
  padding: 0; width: min(760px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(0, 0, 0, .5); }
.dlg { padding: 16px 18px; display: flex; flex-direction: column; gap: 10px; }
.dlg h2 { margin: 0; font-size: 17px; }
.dlg p { margin: 0; }
#json-text {
  width: 100%; resize: vertical;
  font: 12.5px/1.45 ui-monospace, "Cascadia Code", Consolas, monospace;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 10px;
}
.error { color: var(--error); min-height: 1.2em; white-space: pre-wrap; }
.dlg-buttons { display: flex; gap: 8px; align-items: center; }
code, kbd {
  font: 12px ui-monospace, Consolas, monospace;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 4px; padding: 0 4px;
}
.help { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 6px; }
.keys { border-collapse: collapse; font-size: 13px; }
.keys td { padding: 3px 10px 3px 0; vertical-align: top; }

/* Narrow screens: tools on top, panel below the canvas */
@media (max-width: 860px) {
  body { overflow: auto; grid-template-rows: auto auto auto; }
  .layout { grid-template-columns: 1fr; grid-template-rows: auto 62vh auto; }
  .tools { flex-direction: row; border-right: 0; border-bottom: 1px solid var(--border); overflow-x: auto; }
  .tools .tool-sep { width: 1px; height: auto; margin: 2px 4px; }
  .tools button { min-width: 50px; }
  .panel { border-left: 0; border-top: 1px solid var(--border); overflow: visible; }
  .topbar { flex-wrap: wrap; }
  #scene-select { min-width: 0; max-width: 60vw; }
  .status { flex-direction: column; align-items: flex-start; gap: 2px; white-space: normal; }
}

/* Chinese site navigation and responsive integration. */
.site-nav {display:flex;align-items:center;gap:12px;white-space:nowrap;font-size:12px;}
.site-nav a {color:var(--muted);text-underline-offset:3px;}
.site-nav a:hover {color:var(--text);}
.dlg-buttons {flex-wrap:wrap;}
@media (max-width:860px) {
  body {height:auto;min-height:100dvh;display:block;overflow-x:hidden;overflow-y:auto;}
  .tools {min-height:72px;overflow-y:hidden;}
  .topbar .site-nav {order:2;width:100%;padding-top:3px;}
  .topbar > .spacer {display:none;}
}
@media (max-width:480px) {
  .topbar {gap:6px;padding:8px;}
  .brand {margin-right:0;}
  .scene-pick {flex:1 1 150px;min-width:140px;}
  #scene-select {width:100%;max-width:100%;}
  .topbar button {padding:5px 8px;}
  .legend {font-size:11px;left:7px;bottom:10px;max-width:calc(100% - 14px);}
  .dlg {padding:14px;}
  #json-text {min-height:180px;}
  .cutplot-head {flex-wrap:wrap;gap:5px;}
}
@media (max-width:860px) {
  .layout {grid-template-rows:auto auto auto;}
  .stage {display:grid;grid-template-columns:minmax(0,1fr);grid-template-rows:62vh;grid-auto-rows:auto;align-content:start;}
  #canvas {grid-column:1;grid-row:1;}
  .stage > .legend {position:relative;grid-column:1;grid-row:1;align-self:end;justify-self:start;left:auto;bottom:auto;margin:8px;}
  .stage > .hint {position:relative;grid-column:1;grid-row:1;align-self:center;justify-self:center;left:auto;top:auto;transform:none;margin:12px;}
  .stage > .cutplot, .stage > .gausscard {position:relative;grid-column:1;right:auto;top:auto;bottom:auto;width:auto;margin:8px;}
}
