:root {
  --bg: #141517;
  --panel: #1b1c1f;
  --panel-2: #222327;
  --line: #2e3035;
  --text: #e7e5e1;
  --muted: #9a978f;
  --accent: #f2a541;
  --accent-ink: #1b1306;
  --danger: #e5484d;
  --radius: 8px;
  color-scheme: dark;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 13px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { display: flex; flex-direction: column; overflow: hidden; }

button, .button {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 10px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover, .button:hover { border-color: #4a4d55; background: #2a2b30; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.primary:hover { background: #f7b45a; }
button.active { border-color: var(--accent); color: var(--accent); }

/* --- top bar ------------------------------------------------------------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 14px; border-bottom: 1px solid var(--line); background: var(--panel);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 15px; }
.brand svg { width: 30px; height: 20px; fill: none; stroke: var(--accent); stroke-width: 1.6; stroke-linejoin: round;
  filter: drop-shadow(0 0 4px rgba(242,165,65,.6)); }
.brand small { color: var(--muted); font-weight: 400; font-size: 12px; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* --- layout -------------------------------------------------------------- */
.app { flex: 1; display: grid; grid-template-columns: 320px 1fr; min-height: 0; }
.sidebar { overflow-y: auto; border-right: 1px solid var(--line); background: var(--panel); padding: 6px 0 40px; }
.main { display: grid; grid-template-rows: minmax(260px, 42%) 1fr; min-width: 0; min-height: 0; }

/* --- sidebar sections ----------------------------------------------------- */
details.section { border-bottom: 1px solid var(--line); }
details.section > summary {
  list-style: none; cursor: pointer; padding: 10px 14px; font-weight: 600; letter-spacing: .02em;
  display: flex; justify-content: space-between; align-items: center; user-select: none;
}
details.section > summary::-webkit-details-marker { display: none; }
details.section > summary::after { content: "▸"; color: var(--muted); transition: transform .15s; }
details.section[open] > summary::after { transform: rotate(90deg); }
.section-body { padding: 0 14px 12px; display: grid; gap: 9px; }

.field { display: grid; gap: 3px; }
.field .row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.field label { color: #cfccc6; }
.field .val { display: flex; align-items: baseline; gap: 3px; color: var(--muted); font-variant-numeric: tabular-nums; }
.field .val input {
  width: 64px; text-align: right; background: transparent; border: 1px solid transparent; color: var(--text);
  font: inherit; padding: 1px 3px; border-radius: 4px;
}
.field .val input:hover, .field .val input:focus { border-color: var(--line); background: var(--bg); outline: none; }
.field .hint { color: var(--muted); font-size: 11px; }
.field select, .field input[type=number].full {
  width: 100%; font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line);
  border-radius: 6px; padding: 5px 6px;
}
.field.check { grid-template-columns: auto 1fr; align-items: center; gap: 8px; }
.seed-row { display: flex; gap: 6px; }
.seed-row input { flex: 1; }

input[type=range] { width: 100%; accent-color: var(--accent); margin: 2px 0; }
input[type=checkbox] { accent-color: var(--accent); }

.presets { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; }
.presets button { padding: 6px 4px; font-size: 12px; }

/* --- panels --------------------------------------------------------------- */
.panel { display: flex; flex-direction: column; min-height: 0; border-bottom: 1px solid var(--line); }
.panel-head {
  display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 6px 12px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.panel-head .title { font-weight: 600; }
.panel-head .hint { color: var(--muted); font-weight: 400; margin-left: 8px; font-size: 12px; }
.tabs, .views { display: flex; gap: 4px; }
.tab { background: transparent; border-color: transparent; color: var(--muted); }
.tab.active { color: var(--text); background: var(--panel-2); border-color: var(--line); }
.toggles { display: flex; gap: 12px; color: var(--muted); }
.toggles label { display: flex; gap: 5px; align-items: center; cursor: pointer; }

.stats { display: flex; flex-wrap: wrap; gap: 6px; padding: 7px 12px 0; }
.stat { background: var(--panel-2); border: 1px solid var(--line); border-radius: 999px; padding: 2px 9px; color: var(--muted); }
.stat b { color: var(--text); font-weight: 600; }
.stat.led b { color: var(--accent); }
.warnings { padding: 4px 12px 0; display: grid; gap: 3px; }
.warnings div { color: #f5c26b; font-size: 12px; }
.warnings div::before { content: "⚠ "; }

.panel-body { flex: 1; min-height: 0; position: relative; }
.tabpane { display: none; position: absolute; inset: 0; overflow: auto; }
.tabpane.active { display: block; }

#view2d { width: 100%; height: 100%; display: block; }
#view2d .seg { stroke: #0e0b09; stroke-width: 1; vector-effect: non-scaling-stroke; }
#view2d .seg:hover { filter: brightness(1.25); }
#view2d .seam { fill: none; stroke: #ff6b5b; stroke-width: 1.5; vector-effect: non-scaling-stroke; }
#view2d .led { fill: none; stroke: #ffc56b; stroke-linejoin: round; stroke-linecap: round; opacity: .9; }
#view2d .led-glow { fill: none; stroke: #ffb347; opacity: .45; }
#view2d .splice { fill: rgba(120, 190, 255, .12); stroke: #78beff; stroke-dasharray: 4 3; stroke-width: 1; vector-effect: non-scaling-stroke; }
#view2d .standoff { fill: rgba(80, 220, 200, .18); stroke: #50dcc8; stroke-width: 1.2; vector-effect: non-scaling-stroke; }
#view2d .standoff-mark { stroke: #50dcc8; stroke-width: 2.5; vector-effect: non-scaling-stroke; }
#view2d .spacer { fill: rgba(180, 230, 90, .2); stroke: #b4e65a; stroke-width: 1.2; vector-effect: non-scaling-stroke; }
#view2d .seg-label { fill: #fff; text-anchor: middle; font-weight: 600; paint-order: stroke; stroke: #000; stroke-width: 3px; }
#view2d .dim path { stroke: var(--muted); fill: none; stroke-width: 1; vector-effect: non-scaling-stroke; }
#view2d .dim text { fill: var(--muted); text-anchor: middle; }

/* --- sheets --------------------------------------------------------------- */
.sheets { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; padding: 12px; }
.sheet-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px; display: grid; gap: 6px; }
.sheet-card .head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.sheet-card .head span { color: var(--muted); }
.sheet-card svg { width: 100%; height: auto; display: block; }
.sheet-card .sheet-bg { fill: #2a2622; stroke: #4a443d; stroke-width: 0.8; vector-effect: non-scaling-stroke; }
.sheets-note { grid-column: 1 / -1; color: var(--muted); line-height: 1.5; }
.sheets-note b { color: var(--text); }
.legend-cut { color: #ff6b6b; } .legend-score { color: #6ea8ff; }

/* --- build list ------------------------------------------------------------ */
.build { padding: 12px 16px 30px; display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 18px; line-height: 1.5; }
.build h3 { margin: 0 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.build table { border-collapse: collapse; width: 100%; }
.build td { padding: 3px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
.build td.n { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--accent); }
.build ol { margin: 0; padding-left: 18px; }
.build li { margin-bottom: 4px; }

/* --- 3D --------------------------------------------------------------------- */
.panel-3d { border-bottom: 0; }
.viewport { flex: 1; min-height: 0; position: relative; }
.viewport canvas { display: block; }

@media (max-width: 900px) {
  body { overflow: auto; }
  .app { grid-template-columns: 1fr; }
  .sidebar { max-height: none; border-right: 0; order: 2; }
  .main { grid-template-rows: 340px 420px; }
}

#view2d .bend { fill: none; stroke: #c8a2ff; stroke-width: 1.2; stroke-dasharray: 6 4; vector-effect: non-scaling-stroke; opacity: 0.8; }
#view2d .band { fill: rgba(120, 190, 255, .10); stroke: #78beff; stroke-width: 1; stroke-dasharray: 3 2; vector-effect: non-scaling-stroke; }
.field[hidden] { display: none; }
#view2d .standoff-mark.visible { stroke: #ff6b5b; }

.rgba-row { display: flex; align-items: center; gap: 8px; }
.rgba-row input[type="color"] { width: 36px; height: 24px; padding: 0; border: 1px solid var(--line, #444); border-radius: 4px; background: none; cursor: pointer; }
.rgba-row input[type="range"] { flex: 1; }
.rgba-swatch {
  width: 24px; height: 24px; border-radius: 4px; flex: none; border: 1px solid var(--line, #444);
  background-image: linear-gradient(45deg, #888 25%, transparent 25%, transparent 75%, #888 75%), linear-gradient(45deg, #888 25%, transparent 25%, transparent 75%, #888 75%);
  background-size: 8px 8px; background-position: 0 0, 4px 4px; position: relative;
}

/* --- quickstart ---------------------------------------------------------- */
.qs-overlay {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
  background: rgba(8, 8, 10, .72); padding: 16px;
}
.qs-dialog {
  width: min(980px, 100%); max-height: 100%; overflow: auto; background: var(--panel);
  border: 1px solid var(--line); border-radius: 12px; padding: 18px 20px 14px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .5);
}
.qs-head { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.qs-head h2 { margin: 0; font-size: 18px; font-weight: 600; }
.qs-dots { display: flex; gap: 6px; flex: 1; }
.qs-dots span { width: 22px; height: 4px; border-radius: 2px; background: var(--line); }
.qs-dots span.done { background: #7a5a2a; }
.qs-dots span.on { background: var(--accent); }
.qs-grid { display: grid; gap: 12px; grid-template-columns: repeat(2, 1fr); }
.qs-grid.n3, .qs-grid.n6 { grid-template-columns: repeat(3, 1fr); }
.qs-card {
  display: flex; flex-direction: column; align-items: stretch; gap: 6px; padding: 8px 8px 10px;
  border-radius: 10px; background: var(--panel-2); white-space: normal; text-align: center;
}
.qs-card:hover, .qs-card:focus-visible { border-color: var(--accent); background: #2a2b30; outline: none; }
.qs-card b { font-size: 14px; font-weight: 600; }
.qs-card small { color: var(--muted); font-size: 12px; margin-top: -3px; }
.qs-pic { aspect-ratio: 2.6; border-radius: 6px; overflow: hidden; background: #2b2824; }
.qs-pic svg, .qs-final-pic svg { display: block; width: 100%; height: 100%; }
.qs-foot { display: flex; justify-content: flex-start; margin-top: 12px; min-height: 30px; }
.qs-final { display: flex; flex-direction: column; align-items: center; gap: 14px; padding: 4px 0 6px; }
.qs-final-pic { width: min(640px, 100%); aspect-ratio: 2.6; border-radius: 8px; overflow: hidden; }
.qs-final p { margin: 0; color: var(--muted); font-size: 14px; }
@media (max-width: 640px) {
  .qs-grid, .qs-grid.n3, .qs-grid.n6 { grid-template-columns: 1fr 1fr; }
  .qs-dialog { padding: 14px; }
}
