:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1c2330;
  --muted: #5d6778;
  --border: #dde1e8;
  --accent: #1f6feb;
  --accent-soft: #e7f0ff;
  --solved: #f1f3f6;
  --error: #b42318;
  --error-soft: #fdecea;
  --warn: #8a5a00;
  --warn-soft: #fff4dc;
  --grid: #eceff3;
  --c-linear: #c2410c;
  --c-loglin: #1f6feb;
  --c-spline: #15803d;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11151c;
    --card: #1a2029;
    --text: #e6e9ef;
    --muted: #9aa4b5;
    --border: #2d3542;
    --accent: #5b9dff;
    --accent-soft: #1d2c45;
    --solved: #222a35;
    --error: #ff8a80;
    --error-soft: #3a1f1f;
    --warn: #f5c76a;
    --warn-soft: #3a2f17;
    --grid: #232a35;
    --c-linear: #fb923c;
    --c-loglin: #5b9dff;
    --c-spline: #4ade80;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
main { max-width: 1480px; margin: 0 auto; padding: 24px 16px 48px; }

.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
h1 { margin: 0; font-size: 1.6rem; }
.sub { margin: 4px 0 0; color: var(--muted); }
.formula { font-family: ui-monospace, Consolas, monospace; color: var(--text); white-space: nowrap; }
.actions { display: flex; gap: 8px; }

.btn {
  font: inherit; padding: 6px 14px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--card); color: var(--text);
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.small { font-size: .85rem; padding: 4px 10px; }

/* three columns: entry | chart | results */
.layout {
  display: grid; gap: 16px; align-items: start;
  grid-template-columns: minmax(270px, 310px) minmax(0, 1fr) minmax(330px, 380px);
}
@media (max-width: 1180px) {
  .layout { grid-template-columns: minmax(270px, 320px) minmax(0, 1fr); }
  .layout > :nth-child(3) { grid-column: 1 / -1; }
}
@media (max-width: 760px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .card { padding: 14px 12px; }
  .results { font-size: .85rem; }
}

.card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; min-width: 0;
}
h2 { margin: 0 0 12px; font-size: 1.1rem; display: flex; align-items: center; gap: 10px; }
h3 { margin: 18px 0 6px; font-size: 1rem; }
.step {
  display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%;
  background: var(--accent); color: #fff; font-size: .85rem;
}
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head h2 { margin: 0; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field > span { font-size: .85rem; color: var(--muted); }
input[type="text"], select {
  font: inherit; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--card); color: var(--text); width: 100%;
  font-variant-numeric: tabular-nums;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input.invalid { border-color: var(--error); background: var(--error-soft); }
input:disabled { background: var(--solved); color: var(--muted); }

/* CSV drop zone */
.drop {
  display: flex; flex-direction: column; align-items: center; gap: 2px; text-align: center;
  padding: 14px 12px; border: 1.5px dashed var(--border); border-radius: var(--radius);
  cursor: pointer; color: var(--muted); font-size: .9rem;
}
.drop b { color: var(--accent); }
.drop small { font-size: .78rem; }
.drop.over, .drop:hover { border-color: var(--accent); background: var(--accent-soft); }
.drop:focus-within { outline: 2px solid var(--accent); }
.drop input { position: absolute; width: 1px; height: 1px; opacity: 0; }
#file-status { margin: 6px 0 10px; }
#file-status:empty { display: none; }

/* entry table */
.entry-table { width: 100%; border-collapse: collapse; margin: 4px 0 8px; }
.entry-table th { font-size: .8rem; color: var(--muted); font-weight: 600; text-align: left; padding: 0 4px 4px; }
.entry-table td { padding: 2px 3px; }
.entry-table input { padding: 5px 8px; }
.entry-table .idx { color: var(--muted); font-size: .8rem; text-align: right; width: 1.6em; font-variant-numeric: tabular-nums; }
.del {
  font: inherit; width: 26px; height: 26px; border: none; border-radius: 6px; cursor: pointer;
  background: transparent; color: var(--muted); font-size: 1.1rem; line-height: 1;
}
.del:hover { background: var(--error-soft); color: var(--error); }
.card > .hint:last-of-type { margin-top: 10px; }

.i0 { border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 12px 10px; margin: 0; }
.i0 legend { font-size: .85rem; color: var(--muted); padding: 0 4px; }
.radio { display: flex; align-items: center; gap: 8px; font-size: .9rem; margin: 4px 0; }
.radio input[type="text"] { flex: 1; padding: 4px 8px; }

/* chart */
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button {
  font: inherit; font-size: .85rem; padding: 4px 12px; border: none; cursor: pointer;
  background: var(--card); color: var(--text);
}
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--accent); color: #fff; }

.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; font-size: 12px; }
.chart .grid line { stroke: var(--grid); }
.chart .frame { fill: none; stroke: var(--border); }
.chart .tick { fill: var(--muted); font-variant-numeric: tabular-nums; }
.chart .axis { fill: var(--muted); font-size: 13px; }
.chart .empty { fill: var(--muted); font-size: 13px; }
.chart .ref { stroke: var(--muted); stroke-dasharray: 5 4; opacity: .7; }
.chart .ref-label { fill: var(--muted); font-size: 11px; }
.chart .curve { fill: none; stroke-width: 2; }
.chart .err { stroke: var(--text); stroke-width: 1.2; opacity: .55; }
.chart .pt { fill: var(--text); stroke: var(--card); stroke-width: 1.5; }
.chart .marker { fill: var(--card); stroke-width: 2.2; }
.chart .cross { stroke: var(--muted); stroke-dasharray: 3 3; pointer-events: none; }
.chart .hit { fill: transparent; cursor: crosshair; }
.curve.s-linear, .marker.s-linear { stroke: var(--c-linear); }
.curve.s-loglin, .marker.s-loglin { stroke: var(--c-loglin); }
.curve.s-spline, .marker.s-spline { stroke: var(--c-spline); }

.tip {
  position: absolute; pointer-events: none; z-index: 2;
  background: var(--card); border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 10px; font-size: .8rem; white-space: nowrap;
  box-shadow: 0 4px 14px rgb(0 0 0 / .12); font-variant-numeric: tabular-nums;
}

.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: 10px 0 6px; font-size: .88rem; }
.legend label, .legend .key { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.legend .key { cursor: default; color: var(--muted); }
.legend .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text); }
.swatch { display: inline-block; width: 14px; height: 3px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }
.legend .swatch { margin-right: 0; }
.swatch.s-linear { background: var(--c-linear); }
.swatch.s-loglin { background: var(--c-loglin); }
.swatch.s-spline { background: var(--c-spline); }

/* results */
.eval { width: 100%; border-collapse: collapse; margin-top: 6px; font-variant-numeric: tabular-nums; }
.eval th, .eval td { padding: 7px 8px; border-bottom: 1px solid var(--border); text-align: right; }
.eval th:first-child, .eval tbody th { text-align: left; font-weight: normal; white-space: nowrap; }
.eval thead th { font-size: .8rem; color: var(--muted); font-weight: 600; }
.results th, .results td { padding: 7px 5px; }
.results tbody th { white-space: normal; }
.results td { font-weight: 600; }
.results tr.avg-row th, .results tr.avg-row td { border-top: 2px solid var(--border); }
.results tr.avg-row th { font-weight: 600; }
.results tr.cv-row th, .results tr.cv-row td,
.results tr.spread th, .results tr.spread td { color: var(--muted); font-weight: normal; font-size: .85rem; }
.avg { font-size: .88rem; }
.avg th, .avg td { padding: 4px 8px; }
.units { margin: 8px 0 0; }

.notes { margin: 18px 0 0; font-size: .82rem; }
.notes dt { font-weight: 600; margin-top: 6px; }
.notes dd { margin: 0 0 0 20px; color: var(--muted); }

.hint { color: var(--muted); font-size: .88rem; margin: 0 0 12px; }
.error, .warn { padding: 8px 12px; border-radius: 8px; margin: 0 0 10px; font-size: .9rem; }
.error { color: var(--error); background: var(--error-soft); }
.warn { color: var(--warn); background: var(--warn-soft); }

.foot { color: var(--muted); font-size: .8rem; text-align: center; margin-top: 16px; }

@media print {
  body { background: #fff; color: #000; }
  main { max-width: none; padding: 0; }
  .actions, .drop, #add-row, .del, .hint, .seg, .legend input { display: none !important; }
  .layout { display: block; }
  .card { break-inside: avoid; border-color: #999; margin-bottom: 12px; }
  .entry-table input { border: none; padding: 2px 4px; }
}
