:root {
  --bg: #f6f7f8;
  --surface: #ffffff;
  --text: #1c1f23;
  --muted: #646b73;
  --border: #dde1e5;
  --track: #e6e9ec;
  --accent: #2f6fcf;
  --accent-soft: #a9c3ec;
  --neutral: #8a929b;
  --positive: #18794e;
  --negative: #c4362f;
  --warn-bg: #fdf3d8;
  --warn-text: #7a5600;
  --focus: #2f6fcf;
  --radius: 8px;
  --s1: 4px;
  --s2: 8px;
  --s3: 16px;
  --s4: 24px;
  --s5: 32px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121416;
    --surface: #1b1e21;
    --text: #e6e8ea;
    --muted: #9aa1a8;
    --border: #2d3135;
    --track: #2d3135;
    --accent: #6f9eea;
    --accent-soft: #3c5680;
    --neutral: #7c848c;
    --positive: #4cc38a;
    --negative: #f0736b;
    --warn-bg: #3a3017;
    --warn-text: #f2cf74;
    --focus: #8fb4f0;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

h1 { font-size: 20px; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 14px; margin: 0; }
h3 { font-size: 12px; margin: var(--s3) 0 var(--s2); color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
p { margin: 0; }
a { color: var(--accent); }

.muted { color: var(--muted); }
.small { font-size: 12px; font-weight: 400; text-transform: none; letter-spacing: 0; }
.num, input[type="number"], .key-value { font-variant-numeric: tabular-nums; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

button {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 12px;
  cursor: pointer;
}
button:hover { border-color: var(--neutral); }

.link-button {
  border: 0;
  padding: 0;
  background: none;
  color: var(--accent);
  font-size: 12px;
  text-decoration: underline;
}

/* Header */
.site-header {
  padding: var(--s3) var(--s4);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  display: grid;
  gap: var(--s2);
}
.title-row, .header-row { display: flex; align-items: center; gap: var(--s2) var(--s3); flex-wrap: wrap; }
.header-row { justify-content: space-between; }
.actions { display: flex; gap: var(--s2); }

.badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  line-height: 1;
  padding: 4px 6px;
  border-radius: 4px;
  border: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
}
.badge-warn { background: var(--warn-bg); color: var(--warn-text); border-color: transparent; }

/* Layout */
.layout { display: grid; grid-template-columns: 1fr; }

.inputs { padding: var(--s3); display: grid; gap: var(--s2); align-content: start; }
.results { padding: var(--s3); display: grid; gap: var(--s3); align-content: start; min-width: 0; }

@media (min-width: 1024px) {
  .layout { grid-template-columns: 380px 1fr; height: calc(100vh - var(--header-h, 96px)); }
  .inputs { overflow-y: auto; border-right: 1px solid var(--border); }
  .results { overflow-y: auto; padding: var(--s4); }
  .sticky-bar { display: none !important; }
}

/* Input groups */
details {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
summary {
  cursor: pointer;
  padding: 12px var(--s3);
  font-weight: 600;
  display: flex;
  align-items: baseline;
  gap: var(--s2);
  flex-wrap: wrap;
}
.group-body { padding: 0 var(--s3) var(--s3); }
.group-body > h3:first-child { margin-top: 0; }

.segmented {
  display: flex;
  flex-wrap: wrap;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.segmented button {
  flex: 1 1 auto;
  border: 0;
  border-radius: 0;
  border-right: 1px solid var(--border);
  padding: 6px 8px;
  font-size: 13px;
}
.segmented button:last-child { border-right: 0; }
.segmented button[aria-checked="true"] { background: var(--accent); color: #fff; }

.custom-mix { margin-top: var(--s2); }

.subhead { display: flex; justify-content: space-between; align-items: baseline; margin-top: var(--s3); }
.subhead h3 { margin: 0 0 var(--s2); }

/* Slider + number field */
.field { display: grid; grid-template-columns: 1fr 88px; grid-template-areas: "name num" "range range"; gap: 2px var(--s2); align-items: center; padding: 6px 0; }
.field-name { display: flex; gap: var(--s2); align-items: baseline; }
.field .field-name { grid-area: name; }
.field input[type="range"] { grid-area: range; width: 100%; accent-color: var(--accent); margin: 0; }
.field .num-wrap { grid-area: num; }

.num-wrap { display: flex; align-items: center; gap: 4px; }
.num-wrap input {
  width: 100%;
  min-width: 0;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 6px;
  text-align: right;
}
.num-wrap .unit { color: var(--muted); font-size: 12px; }

.grid-fields { display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--s2) var(--s3); }
.grade-field { display: grid; gap: 2px; }
.grade-field .field-name { font-size: 13px; }

.edited-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); display: inline-block; align-self: center; }

/* Price rows */
.price-row { display: grid; grid-template-columns: 1fr 120px; gap: 4px var(--s2); padding: 10px 0; border-bottom: 1px solid var(--border); }
.price-row:last-child { border-bottom: 0; }
.price-name { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; font-weight: 600; }
.price-meta { grid-column: 1 / -1; display: flex; gap: var(--s2); align-items: center; justify-content: space-between; font-size: 12px; color: var(--muted); }
.price-meta .source { cursor: help; text-decoration: underline dotted; text-underline-offset: 2px; }
.badge-stale { color: var(--negative); border-color: currentColor; }
.spark { width: 72px; height: 20px; }

/* Results */
.error { background: var(--surface); border: 1px solid var(--negative); color: var(--negative); border-radius: var(--radius); padding: var(--s2) var(--s3); }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }

.key-numbers { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s2); }
@media (min-width: 1280px) { .key-numbers { grid-template-columns: 1.2fr 1fr 1fr 1.3fr; } }
.key { padding: var(--s3); display: grid; gap: 2px; align-content: start; }
.key-label { font-size: 12px; color: var(--muted); font-weight: 600; }
.key-value { font-size: 22px; font-weight: 650; letter-spacing: -0.01em; }
.key-value.big { font-size: 32px; }
.key-sub { font-size: 12px; }
.positive { color: var(--positive); }
.negative { color: var(--negative); }

.charts { display: grid; grid-template-columns: 1fr; gap: var(--s3); }
@media (min-width: 1280px) {
  .charts { grid-template-columns: 1fr 1fr; }
  .chart-card.wide { grid-column: 1 / -1; }
}
.chart-card { margin: 0; padding: var(--s3); display: grid; gap: var(--s2); min-width: 0; }
.chart-card figcaption { display: grid; gap: 2px; }
.chart-box { position: relative; height: 280px; }
.chart-box.short { height: 220px; }

/* Footer */
.site-footer { padding: var(--s3) var(--s4) 80px; border-top: 1px solid var(--border); color: var(--muted); font-size: 12px; display: grid; gap: var(--s2); }
@media (min-width: 1024px) {
  .site-footer { padding-bottom: var(--s3); }
}

/* Mobile sticky margin bar */
.sticky-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px var(--s3);
  background: var(--surface);
  border-top: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}
.sticky-bar strong { font-size: 18px; }

@media (max-width: 480px) {
  .site-header { padding: var(--s3); }
  .inputs, .results { padding: var(--s2) var(--s3); }
  .key-value.big { font-size: 26px; }
  .chart-box { height: 260px; }
}

/* Subtitle and intro */
.subtitle { color: var(--muted); max-width: 70ch; }

.intro { margin: var(--s3) var(--s4) 0; }
.intro summary { font-weight: 600; }
.intro-body {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: var(--s3) var(--s4);
  padding: 0 var(--s3) var(--s3);
}
.intro-body h2 { margin-bottom: var(--s1); }
.intro-body p + p { margin-top: var(--s2); }
.intro-body ul { margin: 0 0 var(--s2); padding-left: 18px; }
@media (max-width: 480px) { .intro { margin: var(--s2) var(--s3) 0; } }

/* Help toggles */
.help-btn {
  display: inline-grid;
  place-items: center;
  width: 18px;
  height: 18px;
  padding: 0;
  margin-left: 6px;
  border-radius: 50%;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  vertical-align: middle;
  flex: none;
}
.help-btn:hover, .help-btn[aria-expanded="true"] { color: var(--accent); border-color: var(--accent); }
.help-btn.static { cursor: default; }
.help-text {
  display: block;
  grid-column: 1 / -1;
  font-size: 12px;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text);
  background: var(--bg);
  border-left: 2px solid var(--accent);
  border-radius: 4px;
  padding: 6px 10px;
  margin: 4px 0 6px;
}
details > .help-text { margin: 0 var(--s3) var(--s2); }
.help-text[hidden] { display: none; }
.field { grid-template-areas: "name num" "range range" "help help"; }
.field .help-text { grid-area: help; }
h3 .help-btn { margin-top: -2px; }

h4 { font-size: 13px; margin: var(--s3) 0 0; }
.group-body h3 + div > h4:first-child { margin-top: 0; }

.composition { margin-top: var(--s2); color: var(--muted); }
.composition.warn { color: var(--negative); }

/* Assumptions */
.assumptions { padding: var(--s3); display: grid; gap: var(--s2); }
.assumptions h3 { margin: var(--s2) 0 0; }
.assumptions ul { margin: 0; padding-left: 18px; }
.assumptions li + li { margin-top: 4px; }
.table-wrap { overflow-x: auto; }
.assumptions table { width: 100%; border-collapse: collapse; font-size: 13px; }
.assumptions th, .assumptions td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.assumptions th { color: var(--muted); font-weight: 600; font-size: 12px; }
.assumptions td:last-child { white-space: nowrap; color: var(--muted); }
