/* ============ tokens ============ */
:root {
  --bg: #f4f5f7;
  --bg-grad: radial-gradient(1200px 600px at 15% -10%, #eceefb 0%, transparent 60%);
  --panel: #ffffff;
  --panel-2: #fafbfc;
  --border: #e4e7ec;
  --border-strong: #d3d8e0;
  --text: #14161a;
  --text-2: #565e6b;
  --text-3: #8b929e;
  --accent: #5850ec;
  --accent-2: #7c74ff;
  --accent-soft: rgba(88, 80, 236, .1);
  --ref: #e0870b;
  --ref-soft: rgba(224, 135, 11, .12);
  --grid: #e9ebf0;
  --grid-strong: #d7dbe3;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 8px 24px -12px rgba(16, 24, 40, .12);
  --radius: 14px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

:root[data-theme="dark"] {
  --bg: #0d0f13;
  --bg-grad: radial-gradient(1200px 600px at 15% -10%, #1a1c2e 0%, transparent 60%);
  --panel: #15181d;
  --panel-2: #1a1e24;
  --border: #262b33;
  --border-strong: #363d47;
  --text: #e9ebef;
  --text-2: #a2a9b5;
  --text-3: #717986;
  --accent: #8b84ff;
  --accent-2: #a29cff;
  --accent-soft: rgba(139, 132, 255, .14);
  --ref: #f0b429;
  --ref-soft: rgba(240, 180, 41, .14);
  --grid: #21262e;
  --grid-strong: #2e343e;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 12px 28px -14px rgba(0, 0, 0, .6);
}

* { box-sizing: border-box; }

/* The gradient is viewport-anchored and never repeats: a body-propagated
   background tiles down the page and shows a seam one screen down. */
html {
  min-height: 100%;
  background-color: var(--bg);
}

body {
  margin: 0;
  min-height: 100vh;
  background-color: var(--bg);
  background-image: var(--bg-grad);
  background-repeat: no-repeat;
  background-attachment: fixed;
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h2 { font-size: 13px; font-weight: 650; letter-spacing: .01em; margin: 0; }

code { font-family: var(--mono); font-variant-ligatures: none; }

.muted { color: var(--text-3); font-size: 12px; }

/* ============ topbar ============ */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px clamp(16px, 3vw, 34px);
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.brand-mark {
  display: grid;
  place-items: center;
  width: 30px; height: 30px;
  border-radius: 9px;
  color: #fff;
  background: linear-gradient(140deg, var(--accent), var(--accent-2));
  flex: none;
}

.brand-text { margin: 0; font-size: 14px; font-weight: 550; letter-spacing: -.01em; white-space: nowrap; }
.brand-text b { font-weight: 650; color: var(--accent); }

.topbar-actions { display: flex; align-items: center; gap: 8px; }

.seg {
  display: flex;
  padding: 2px;
  gap: 2px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.seg-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-2);
  font: inherit;
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 8px;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background .15s, color .15s;
}
.seg-btn.icon-only { padding: 5px 8px; }
.seg-btn:hover { color: var(--text); background: var(--accent-soft); }
.seg-btn[aria-pressed="true"] {
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(16, 24, 40, .1);
}
:root[data-theme="dark"] .seg-btn[aria-pressed="true"] { background: #262b33; }

/* ============ layout ============ */
.layout {
  display: grid;
  grid-template-columns: minmax(280px, 352px) minmax(0, 1fr);
  align-items: start;
  gap: 16px;
  padding: 16px clamp(16px, 3vw, 34px) 48px;
  max-width: 1360px;
  margin: 0 auto;
}

.column { display: flex; flex-direction: column; gap: 16px; min-width: 0; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px 16px;
}

.panel-editor { padding-bottom: 14px; }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 26px;
  margin-bottom: 12px;
}

.head-tools { display: flex; align-items: center; gap: 8px; }

/* ============ buttons ============ */
.ghost-btn, .icon-btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font: inherit;
  font-size: 12px;
  color: var(--text-2);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 5px 9px;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
}
.ghost-btn:hover, .icon-btn:hover { color: var(--text); border-color: var(--border-strong); }
.ghost-btn.wide { width: 100%; justify-content: center; padding: 7px 9px; }
.icon-btn { padding: 6px; }
.icon-btn.ok { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }

button:focus-visible, input:focus-visible, select:focus-visible, .handle:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ============ graph ============ */
.curve-wrap { display: flex; justify-content: center; }

#graph {
  /* intrinsic 322x676 ratio; preserveAspectRatio letterboxes if max-height clamps */
  width: 100%;
  max-width: 340px;
  height: auto;
  max-height: 68vh;
  touch-action: none;
  overflow: visible;
}

.grid-line { stroke: var(--grid); stroke-width: 1; }
.grid-line.major { stroke: var(--grid-strong); }
.unit-box { fill: none; stroke: var(--grid-strong); stroke-width: 1; rx: 2; }
.axis-label { fill: var(--text-3); font-size: 9.5px; font-family: var(--mono); }
.axis-title { fill: var(--text-3); font-size: 9.5px; letter-spacing: .06em; text-transform: uppercase; }

.curve-path {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.6;
  stroke-linecap: round;
}

.ref-curve {
  fill: none;
  stroke: var(--ref);
  stroke-width: 2;
  stroke-dasharray: 5 5;
  stroke-linecap: round;
  opacity: .85;
}
.ref-curve.hidden { display: none; }

.handle-leg { stroke: var(--accent); stroke-width: 1.4; opacity: .45; }
.anchor { fill: var(--panel); stroke: var(--text-3); stroke-width: 1.6; }

.handle { cursor: grab; }
.handle.dragging { cursor: grabbing; }
.handle-hit { fill: transparent; }
.handle-dot {
  fill: var(--accent);
  stroke: var(--panel);
  stroke-width: 2.5;
  transition: r .12s ease;
}
.handle:hover .handle-dot, .handle.dragging .handle-dot { r: 9.5; }
.handle-tag {
  fill: #fff;
  font-size: 8.5px;
  font-weight: 700;
  font-family: var(--mono);
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}

.prog-line { stroke: var(--text-3); stroke-width: 1; stroke-dasharray: 3 4; opacity: .7; }
.prog-dot { fill: var(--panel); stroke: var(--accent); stroke-width: 2.5; }

.hint {
  margin: 12px 0 0;
  font-size: 11.5px;
  color: var(--text-3);
  line-height: 1.6;
}

/* ============ value + numbers ============ */
.value-block {
  margin-top: 14px;
  padding: 9px 11px 11px;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 32%, transparent);
  border-radius: 13px;
}

.value-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 7px;
  font-size: 10.5px;
  font-weight: 650;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--accent);
}
.value-head .muted { font-size: 10.5px; letter-spacing: 0; text-transform: none; font-weight: 400; }

.value-row { display: flex; gap: 8px; }

.value-field {
  flex: 1;
  min-width: 0;
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 500;
  letter-spacing: -.01em;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}
.value-field:hover { border-color: var(--border-strong); }
.value-field.bad { border-color: #e5484d; }

.primary-btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex: none;
  font: inherit;
  font-size: 12.5px;
  font-weight: 550;
  color: #fff;
  background: linear-gradient(140deg, var(--accent), var(--accent-2));
  border: 0;
  border-radius: 10px;
  padding: 0 14px;
  cursor: pointer;
  transition: filter .15s;
}
.primary-btn:hover { filter: brightness(1.08); }
.primary-btn:active { filter: brightness(.95); }
.primary-btn.ok { filter: saturate(.8) brightness(1.05); }
:root[data-theme="dark"] .primary-btn { color: #14161a; }

.nums { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 8px; }

.num { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.num > span { font-size: 10.5px; color: var(--text-3); font-family: var(--mono); padding-left: 2px; }
.num > input {
  width: 100%;
  min-width: 0;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 7px;
}

input[type="number"] { appearance: textfield; -moz-appearance: textfield; }
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

/* ============ controls ============ */
.ctrl-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 12px 18px;
  align-items: end;
}

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }

.field-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 11px;
  color: var(--text-3);
}
.field-label b { font-family: var(--mono); color: var(--text-2); font-weight: 500; }

.chips { display: flex; flex-wrap: wrap; gap: 4px; }

.chip {
  appearance: none;
  font: inherit;
  font-size: 11.5px;
  color: var(--text-2);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 9px;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
}
.chip:hover { color: var(--text); border-color: var(--border-strong); }
.chip[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 45%, transparent);
  color: var(--accent);
}

select {
  appearance: none;
  width: 100%;
  font: inherit;
  font-size: 12px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 7px 9px;
  cursor: pointer;
}
select:hover { border-color: var(--border-strong); }

input[type="range"] {
  appearance: none;
  width: 100%;
  height: 18px;
  background: transparent;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 3px;
  background: var(--border);
}
input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 14px; height: 14px;
  margin-top: -5px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--panel);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .2);
}
input[type="range"]::-moz-range-track { height: 4px; border-radius: 3px; background: var(--border); }
input[type="range"]::-moz-range-thumb {
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--panel);
}

/* switch */
.switch { display: inline-flex; align-items: center; gap: 7px; font-size: 11.5px; color: var(--text-2); cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-ui {
  width: 30px; height: 17px;
  border-radius: 99px;
  background: var(--border);
  position: relative;
  transition: background .18s;
  flex: none;
}
.switch-ui::after {
  content: "";
  position: absolute;
  inset: 2px auto 2px 2px;
  width: 13px;
  border-radius: 50%;
  background: var(--panel);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .25);
  transition: transform .18s;
}
.switch input:checked + .switch-ui { background: var(--accent); }
.switch input:checked + .switch-ui::after { transform: translateX(13px); }
.switch input:focus-visible + .switch-ui { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ============ preview tracks ============ */
.tracks { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }

.track-meta {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 11.5px;
  color: var(--text-2);
  margin-bottom: 5px;
}
.track-meta .track-read { margin-left: auto; color: var(--text-3); font-size: 11px; }
.track-meta code { font-size: 11px; color: var(--text-3); }

.dot-legend { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex: none; }
.dot-legend.ref { background: var(--ref); }

.rail {
  position: relative;
  height: 50px;
  border-radius: 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  overflow: hidden;
}
.rail::before {
  content: "";
  position: absolute;
  left: 12px; right: 12px; top: 50%;
  height: 1px;
  background: var(--border);
}

.rail-mark {
  position: absolute;
  top: 7px;
  bottom: 7px;
  width: 1px;
  background: var(--border-strong);
  transition: opacity .15s;
}
.rail-mark.is-target {
  background: repeating-linear-gradient(to bottom, var(--text-3) 0 3px, transparent 3px 6px);
}

.peak {
  font-size: 10.5px;
  font-family: var(--mono);
  color: var(--ref);
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--ref-soft);
}
.peak:empty { display: none; }
#peakCur { color: var(--accent); background: var(--accent-soft); }

.mover {
  position: absolute;
  top: 50%;
  left: 8px;
  width: 30px; height: 30px;
  margin-top: -15px;
  border-radius: 9px;
  background: linear-gradient(140deg, var(--accent), var(--accent-2));
  box-shadow: 0 2px 8px -2px var(--accent-soft);
  will-change: transform, opacity;
}
.mover.ref { background: linear-gradient(140deg, var(--ref), color-mix(in srgb, var(--ref) 70%, #fff)); }

.scrub-row { margin-top: 12px; }

/* spacing strip */
.spacing { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); }
.spacing-head { display: flex; align-items: baseline; gap: 8px; font-size: 11.5px; color: var(--text-2); margin-bottom: 6px; }
.space-svg { display: block; width: 100%; height: 24px; }
.space-tick { stroke: var(--accent); stroke-width: 2; stroke-linecap: round; opacity: .75; }
.space-svg.is-ref .space-tick { stroke: var(--ref); }
.space-base { stroke: var(--border); stroke-width: 1; }

/* ============ reference ============ */
.ref-values {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  padding: 8px 10px;
  background: var(--ref-soft);
  border: 1px solid var(--ref);
  border: 1px solid color-mix(in srgb, var(--ref) 30%, transparent);
  border-radius: 10px;
}
.ref-values code { flex: 1; min-width: 0; font-size: 12px; color: var(--text); overflow-x: auto; white-space: nowrap; }
.ref-values .icon-btn { background: transparent; flex: none; }

/* ============ presets ============ */
.preset-groups { display: flex; flex-direction: column; gap: 14px; }
.preset-group-title {
  font-size: 10.5px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-3);
  margin-bottom: 7px;
}
.preset-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(112px, 1fr));
  gap: 6px;
}
.preset {
  appearance: none;
  display: flex;
  align-items: center;
  gap: 8px;
  font: inherit;
  text-align: left;
  color: var(--text-2);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 8px;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
}
.preset:hover { color: var(--text); border-color: var(--border-strong); }
.preset[aria-pressed="true"] {
  border-color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 50%, transparent);
  background: var(--accent-soft);
  color: var(--accent);
}
.preset svg { flex: none; }
.preset .thumb-path { fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; opacity: .9; }
.preset .thumb-box { fill: none; stroke: currentColor; stroke-width: 1; opacity: .18; }
.preset-name {
  font-size: 11px;
  font-family: var(--mono);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ============ snippets ============ */
.snippets { display: flex; flex-direction: column; gap: 6px; }
.snippet {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 7px 8px 7px 11px;
}
.snippet-label {
  font-size: 10px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--text-3);
  width: 56px;
  flex: none;
}
.snippet code { flex: 1; min-width: 0; font-size: 11.5px; overflow-x: auto; white-space: nowrap; scrollbar-width: none; }
.snippet code::-webkit-scrollbar { display: none; }
.snippet .icon-btn { flex: none; background: transparent; border-color: transparent; }
.snippet:hover .icon-btn { border-color: var(--border); }

/* ============ toast ============ */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translate(-50%, 12px);
  padding: 8px 14px;
  font-size: 12px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ============ prose / SEO content ============ */
.prose {
  padding: 8px clamp(16px, 3vw, 34px) 0;
  max-width: 1360px;
  margin: 0 auto;
}

.prose-inner {
  max-width: 760px;
  padding: 22px clamp(18px, 3vw, 30px) 26px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.prose h2 {
  font-size: 15px;
  font-weight: 650;
  margin: 26px 0 8px;
  color: var(--text);
}
.prose h2:first-child { margin-top: 0; }

.prose h3 {
  font-size: 13px;
  font-weight: 600;
  margin: 18px 0 5px;
  color: var(--text);
}

.prose p {
  margin: 0 0 10px;
  font-size: 13px;
  line-height: 1.75;
  color: var(--text-2);
}

.prose code {
  font-size: 12px;
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
}

.prose-dl { margin: 10px 0 0; }
.prose-dl dt {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  margin-top: 12px;
}
.prose-dl dd {
  margin: 3px 0 0;
  font-size: 13px;
  line-height: 1.7;
  color: var(--text-2);
}

.faq h3 { position: relative; padding-left: 14px; }
.faq h3::before {
  content: "";
  position: absolute;
  left: 0;
  top: 6px;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--accent);
}
.faq p { padding-left: 14px; }

.site-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  max-width: 1360px;
  margin: 0 auto;
  padding: 26px clamp(16px, 3vw, 34px) 40px;
  font-size: 11.5px;
  color: var(--text-3);
}
.site-footer a { color: var(--text-3); text-decoration: none; }
.site-footer a:hover { color: var(--accent); text-decoration: underline; }

/* ============ responsive ============ */
@media (max-width: 1020px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .panel-editor { display: flex; flex-direction: column; }
}

@media (max-width: 620px) {
  .ctrl-row { grid-template-columns: minmax(0, 1fr); }
  .nums { grid-template-columns: repeat(2, 1fr); }
  .brand-text { font-size: 12.5px; }
  .brand-text b { display: none; }
  .prose-inner { padding: 18px 16px 20px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: .01ms !important; }
}
