:root {
  color-scheme: dark;
  --bg: #0d0d12;
  --surface: #111118;
  --surface-raised: #171720;
  --surface-soft: #1d1d27;
  --line: #2c2c38;
  --line-strong: #41414f;
  --text: #f7f7f9;
  --muted: #a3a3af;
  --subtle: #72727f;
  --accent: #f4f4f6;
  --danger: #ffb4a8;
  --success: #b8f5c6;
  --radius: 18px;
  --shadow: 0 18px 54px rgba(0, 0, 0, 0.28);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

button, input, select { font: inherit; }
button, select, input[type="range"] { accent-color: #ffffff; }

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 390px;
  min-height: 100vh;
}

.workspace {
  min-width: 0;
  height: 100vh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  background:
    radial-gradient(circle at 50% 35%, rgba(255,255,255,0.035), transparent 36%),
    var(--surface);
}

.topbar {
  min-height: 86px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 18px 24px;
  border-bottom: 1px solid var(--line);
  background: rgba(17, 17, 24, 0.88);
  backdrop-filter: blur(18px);
  z-index: 2;
}

h1, h2, h3, p { margin: 0; }
h1 { font-size: clamp(22px, 2vw, 30px); line-height: 1.05; letter-spacing: -0.035em; }
h2 { font-size: 21px; letter-spacing: -0.025em; }
h3 { font-size: 14px; letter-spacing: -0.01em; }

.eyebrow {
  margin-bottom: 5px;
  color: var(--subtle);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.topbar-actions { display: flex; align-items: end; gap: 10px; }

.preset-field {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.preset-field select {
  min-width: 164px;
  height: 38px;
  padding: 0 34px 0 12px;
  color: var(--text);
  background: var(--surface-raised);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  outline: none;
}

.stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  isolation: isolate;
}

#stageSvg { width: 100%; height: 100%; display: block; }
.stage-background { fill: #111118; }

.mark-layer {
  fill: none;
  stroke: #fff;
  stroke-width: 1;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.terminal-caps { fill: #fff; stroke: none; }

.onion-layer {
  fill: none;
  stroke: #ffb15a;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.82;
  pointer-events: none;
}

.comb-layer {
  fill: none;
  stroke: #9dffb0;
  stroke-width: 0.03;
  stroke-linecap: round;
  pointer-events: none;
}

.balance-axis,
.balance-diagonal,
.centerline-path,
.construction-circle,
.counter-circle,
.dimension-extension,
.dimension-line {
  vector-effect: non-scaling-stroke;
}

.balance-axis {
  stroke: #8f8f9b;
  stroke-width: 1.1;
  stroke-dasharray: 7 8;
  opacity: 0.62;
}

.balance-diagonal {
  stroke: #5e5e69;
  stroke-width: 0.9;
  stroke-dasharray: 4 8;
  opacity: 0.45;
}

.quadrant-labels {
  fill: #777783;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.18px;
  font-weight: 700;
  letter-spacing: 0.012em;
}

.construction-circle {
  fill: none;
  stroke: #6f6f7b;
  stroke-width: 0.9;
  stroke-dasharray: 4 6;
  opacity: 0.48;
}

.counter-circle {
  fill: none;
  stroke: #8b8b97;
  stroke-width: 0.9;
  stroke-dasharray: 2 5;
  opacity: 0.68;
}

.centerline-path {
  fill: none;
  stroke: #92929d;
  stroke-width: 1;
  stroke-dasharray: 4 5;
  opacity: 0.68;
}

.dimension-layer { color: #f4f4f7; }
.dimension-extension {
  stroke: #a7a7b1;
  stroke-width: 0.9;
  stroke-dasharray: 3 4;
  opacity: 0.78;
}
.dimension-line {
  stroke: currentColor;
  stroke-width: 1.35;
  marker-start: url(#dimensionArrow);
  marker-end: url(#dimensionArrow);
}
.dimension-text {
  fill: currentColor;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.19px;
  font-weight: 800;
  paint-order: stroke;
  stroke: #111118;
  stroke-width: 0.07px;
  stroke-linejoin: round;
}

.stage-toolbar {
  position: absolute;
  left: 18px;
  bottom: 18px;
  z-index: 3;
  display: flex;
  flex-wrap: wrap;
  max-width: calc(100% - 36px);
  gap: 5px;
  padding: 5px;
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 13px;
  background: rgba(23, 23, 32, 0.88);
  backdrop-filter: blur(16px);
  box-shadow: var(--shadow);
}

.toggle { position: relative; cursor: pointer; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle span {
  display: block;
  padding: 8px 10px;
  border-radius: 9px;
  color: var(--subtle);
  font-size: 11px;
  font-weight: 700;
  transition: 140ms ease;
}
.toggle input:checked + span { color: var(--text); background: var(--surface-soft); }
.toggle input:focus-visible + span { outline: 2px solid #fff; outline-offset: 2px; }

.constraint-banner {
  position: absolute;
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 4;
  max-width: min(620px, calc(100% - 36px));
  padding: 10px 14px;
  color: #1c1110;
  background: var(--danger);
  border-radius: 10px;
  font-size: 12px;
  font-weight: 750;
  box-shadow: var(--shadow);
}

.preview-strip {
  display: flex;
  align-items: flex-end;
  gap: 22px;
  padding: 10px 18px 12px;
  border-top: 1px solid var(--line);
  background: #0e0e14;
  overflow-x: auto;
}

.preview-group { display: flex; align-items: flex-end; gap: 6px; }

.preview-caption {
  min-width: 18px;
  color: var(--subtle);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 10px;
  font-weight: 750;
}

.preview-cell {
  display: flex;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--line);
  line-height: 0;
}
.preview-cell svg { display: block; }
.preview-dark { background: #111118; }
.preview-light { background: #f3f1ec; }

.metrics-strip {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 1px;
  border-top: 1px solid var(--line);
  background: var(--line);
}

.metric-card {
  min-width: 0;
  padding: 13px 16px 14px;
  background: var(--surface);
}
.metric-label, .metric-note { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.metric-label { color: var(--subtle); font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; }
.metric-value { display: block; margin: 4px 0 2px; font-size: 18px; line-height: 1.05; font-variant-numeric: tabular-nums; letter-spacing: -0.03em; }
.metric-note { color: var(--muted); font-size: 10px; }
.metric-value.warning { color: var(--danger); }
.metric-value.success { color: var(--success); }

.controls {
  height: 100vh;
  overflow-y: auto;
  padding: 22px 20px 26px;
  border-left: 1px solid var(--line);
  background: #14141b;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}

.controls-header {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 20px;
}

.unit-chip {
  flex: none;
  margin-top: 2px;
  padding: 7px 9px;
  color: var(--text);
  background: var(--surface-soft);
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 10px;
  font-weight: 700;
}

.parameter-list { display: grid; gap: 9px; }

.parameter {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: var(--surface-raised);
  transition: border-color 150ms ease, background 150ms ease;
}
.parameter:focus-within { border-color: #666674; background: #191922; }

.parameter-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
}

.parameter-title { display: flex; align-items: baseline; gap: 7px; min-width: 0; }
.parameter-symbol {
  flex: none;
  min-width: 24px;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 14px;
  font-weight: 850;
}
.parameter-name { color: var(--muted); font-size: 12px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.multiplier-input {
  display: flex;
  align-items: center;
  gap: 5px;
  height: 31px;
  padding: 0 8px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #101016;
}
.multiplier-input input {
  width: 52px;
  padding: 0;
  color: var(--text);
  background: transparent;
  border: 0;
  outline: 0;
  text-align: right;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
  appearance: textfield;
}
.multiplier-input input::-webkit-outer-spin-button,
.multiplier-input input::-webkit-inner-spin-button { margin: 0; appearance: none; }
.multiplier-input span { color: var(--subtle); font-size: 11px; font-weight: 700; }

.parameter-range {
  width: 100%;
  height: 20px;
  margin: 9px 0 0;
  background: transparent;
  cursor: ew-resize;
}
.parameter-range::-webkit-slider-runnable-track { height: 3px; border-radius: 999px; background: linear-gradient(90deg, #fff var(--range-progress, 50%), #3a3a46 var(--range-progress, 50%)); }
.parameter-range::-webkit-slider-thumb { width: 14px; height: 14px; margin-top: -5.5px; border: 2px solid #111118; border-radius: 50%; background: #fff; box-shadow: 0 0 0 1px #8b8b95; appearance: none; }
.parameter-range::-moz-range-track { height: 3px; border-radius: 999px; background: #3a3a46; }
.parameter-range::-moz-range-progress { height: 3px; border-radius: 999px; background: #fff; }
.parameter-range::-moz-range-thumb { width: 12px; height: 12px; border: 2px solid #111118; border-radius: 50%; background: #fff; }

.parameter-footer { display: flex; justify-content: space-between; gap: 10px; margin-top: 2px; color: var(--subtle); font-size: 9px; }
.parameter-description { margin-top: 6px; color: var(--subtle); font-size: 10px; line-height: 1.35; }
.parameter.is-solved { opacity: 0.62; }
.parameter.is-solved .parameter-range { cursor: default; }

.choice-panel {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.segmented {
  display: flex;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #101016;
}
.segmented button {
  flex: 1 1 0;
  min-width: 0;
  min-height: 30px;
  padding: 0 6px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--subtle);
  font-size: 11px;
  font-weight: 750;
  cursor: pointer;
}
.segmented button[aria-pressed="true"] { color: #111118; background: #fff; }
.segmented button:focus-visible { outline: 2px solid #fff; outline-offset: 1px; }
.solve-segmented { margin-top: 8px; }

.custom-ratio {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 8px;
  padding: 7px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: #101016;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}
.custom-ratio input {
  width: 72px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  text-align: right;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  font-weight: 750;
  outline: none;
  appearance: textfield;
}
.custom-ratio input::-webkit-outer-spin-button,
.custom-ratio input::-webkit-inner-spin-button { margin: 0; appearance: none; }

.measure-readout {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin: 0 0 8px;
}
.measure-readout strong {
  font-size: 22px;
  letter-spacing: -0.04em;
  font-variant-numeric: tabular-nums;
}

.derived-panel, .formula-panel, .export-panel {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.section-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.section-heading span { color: var(--subtle); font-size: 9px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; }

.derived-values { display: grid; gap: 1px; margin: 0; overflow: hidden; border: 1px solid var(--line); border-radius: 12px; background: var(--line); }
.derived-values > div { display: flex; justify-content: space-between; gap: 14px; padding: 9px 11px; background: var(--surface-raised); }
.derived-values dt { color: var(--muted); font-size: 10px; }
.derived-values dd { margin: 0; color: var(--text); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 10px; font-weight: 700; font-variant-numeric: tabular-nums; }

.formula-panel summary { cursor: pointer; color: var(--muted); font-size: 12px; font-weight: 700; user-select: none; }
.formula-panel summary::marker { color: var(--subtle); }
.formula-body { display: grid; gap: 5px; margin-top: 10px; }
.formula-body code { padding: 8px 10px; color: #d9d9df; background: #101016; border: 1px solid var(--line); border-radius: 8px; font-size: 10px; }

.version-save { display: flex; gap: 8px; }
.version-save input {
  flex: 1;
  min-width: 0;
  height: 38px;
  padding: 0 10px;
  color: var(--text);
  background: #101016;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  outline: none;
}
.version-save input:focus { border-color: #8a8a98; }
.version-list { display: grid; gap: 6px; margin: 10px 0; padding: 0; list-style: none; }
.version-list:empty { display: none; }
.version-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 6px;
  align-items: center;
}
.version-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
  font-size: 12px;
  font-weight: 650;
}
.version-row button { min-height: 30px; padding: 0 10px; }
.version-slots { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; margin-bottom: 8px; }
.version-slots .is-stored { border-color: #d7d7de; }

.export-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
.export-wide { grid-column: 1 / -1; }

.button {
  min-height: 38px;
  padding: 0 13px;
  border: 1px solid transparent;
  border-radius: 10px;
  cursor: pointer;
  font-size: 11px;
  font-weight: 760;
  transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
}
.button:hover { transform: translateY(-1px); }
.button:active { transform: translateY(0); }
.button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
.button-primary { color: #111118; background: #fff; }
.button-primary:hover { background: #e7e7eb; }
.button-secondary { color: var(--text); background: var(--surface-raised); border-color: var(--line-strong); }
.button-secondary:hover { background: var(--surface-soft); border-color: #5a5a68; }

.export-status { min-height: 16px; margin-top: 8px; color: var(--muted); font-size: 10px; text-align: center; }
.controls-footer { margin-top: 18px; color: var(--subtle); font-size: 10px; line-height: 1.5; }

[hidden] { display: none !important; }
.layer-hidden { display: none; }

@media (max-width: 1050px) {
  body { overflow: auto; }
  .app-shell { grid-template-columns: 1fr; }
  .workspace { height: auto; min-height: 100svh; grid-template-rows: auto minmax(620px, 72svh) auto auto; }
  .controls { height: auto; border-left: 0; border-top: 1px solid var(--line); }
}

@media (max-width: 700px) {
  .topbar { align-items: flex-start; padding: 16px; }
  .topbar-actions { align-items: stretch; flex-direction: column; }
  .preset-field select { min-width: 142px; }
  .workspace { grid-template-rows: auto minmax(540px, 68svh) auto auto; }
  .preview-strip { gap: 14px; padding: 10px 12px; }
  .metrics-strip { grid-template-columns: 1fr 1fr; }
  .stage-toolbar { left: 10px; right: 10px; bottom: 10px; justify-content: center; }
  .toggle span { padding: 8px; }
  .controls { padding: 20px 14px 28px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
