:root {
  color-scheme: light;
  --bg: #f4f3ef;
  --surface-1: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e0da;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #6d6c67;
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-soft: #e3eefb;
  --series-after: #2a78d6;
  --series-before: #b4b2a9;
  --pos: #2a78d6;
  --neg: #e34948;
  --grid: #e7e5df;
  --warn-bg: #fff4dc;
  --warn-ink: #6b4a00;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface-1: #1a1a19; --surface-2: #232321; --border: #33332f;
    --text-primary: #f0efec; --text-secondary: #c3c2b7; --text-muted: #9a998f;
    --accent: #3987e5; --accent-ink: #86b6ef; --accent-soft: #1b2b40;
    --series-after: #3987e5; --series-before: #5c5b56; --pos: #3987e5; --neg: #e66767;
    --grid: #2c2c29; --warn-bg: #3a2f14; --warn-ink: #f3d590; --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface-1: #1a1a19; --surface-2: #232321; --border: #33332f;
  --text-primary: #f0efec; --text-secondary: #c3c2b7; --text-muted: #9a998f;
  --accent: #3987e5; --accent-ink: #86b6ef; --accent-soft: #1b2b40;
  --series-after: #3987e5; --series-before: #5c5b56; --pos: #3987e5; --neg: #e66767;
  --grid: #2c2c29; --warn-bg: #3a2f14; --warn-ink: #f3d590; --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text-primary);
  font: 15px/1.55 "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased; overflow-x: hidden;
}
.wrap { max-width: 1080px; margin: 0 auto; padding: 0 16px; }
h1 { font-size: clamp(22px, 3.2vw, 30px); line-height: 1.25; margin: 8px 0 10px; letter-spacing: -.01em; }
h2 { font-size: 18px; margin: 0; }
h3 { font-size: 15px; margin: 22px 0 4px; }
p { margin: 6px 0; }
.muted { color: var(--text-secondary); }
.small { font-size: 13px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
b { font-weight: 600; }

/* header */
.top { background: var(--surface-1); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
.top-inner { display: flex; align-items: center; justify-content: space-between; height: 60px; }
.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 32px; height: 32px; border-radius: 8px; background: var(--accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath d='M8 22l5-6 4 3 7-9' stroke='white' stroke-width='3' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center/100% no-repeat; }
.brand-name { font-weight: 700; font-size: 16px; }
.brand-sub { font-size: 12px; color: var(--text-secondary); }
.theme-btn { border: 1px solid var(--border); background: var(--surface-2); color: var(--text-primary); width: 36px; height: 36px; border-radius: 8px; cursor: pointer; font-size: 16px; }

.intro { padding: 24px 0 8px; max-width: 760px; }
.caveat { font-size: 13px; color: var(--warn-ink); background: var(--warn-bg); padding: 8px 12px; border-radius: 8px; margin-top: 12px; }
.tag { display: inline-block; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; border: 1px solid currentColor; border-radius: 4px; padding: 0 5px; margin-right: 6px; }

/* cards */
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px; padding: 20px; margin: 16px 0; box-shadow: var(--shadow); }
.step-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.step-no { width: 28px; height: 28px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 14px; flex: none; }

select { font: inherit; color: var(--text-primary); background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; }
.row-select { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; }
#unitSel { font-size: 16px; font-weight: 600; min-width: 220px; }

/* profile */
.profile { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-top: 14px; }
.kpi { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; min-width: 0; }
.kpi .lab { font-size: 12px; color: var(--text-secondary); }
.kpi .val { font-size: 22px; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12px; color: var(--text-muted); }
.profile .wide { grid-column: span 2; }
.chip { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent-ink); margin: 2px 4px 2px 0; }

/* presets */
.presets { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 6px; }
.preset { font: inherit; font-size: 13px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text-primary); border-radius: 999px; padding: 6px 12px; cursor: pointer; }
.preset:hover { border-color: var(--accent); }
.preset.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.preset.reset { background: transparent; color: var(--text-secondary); }

/* levers */
.lgroup { border: 1px solid var(--border); border-radius: 10px; margin-top: 12px; overflow: hidden; }
.lgroup > summary { cursor: pointer; padding: 12px 14px; font-weight: 600; background: var(--surface-2); list-style: none; display: flex; justify-content: space-between; gap: 8px; }
.lgroup > summary::-webkit-details-marker { display: none; }
.lgroup > summary .cnt { font-weight: 500; color: var(--text-secondary); font-size: 13px; }
.lever { display: grid; grid-template-columns: minmax(0, 1fr) 220px 92px; gap: 12px; align-items: center; padding: 10px 14px; border-top: 1px solid var(--border); }
.lever.changed { background: var(--accent-soft); }
.lever .nm { font-size: 14px; }
.lever .meta { font-size: 12px; color: var(--text-muted); }
.lever input[type=range] { width: 100%; accent-color: var(--accent); }
.lever .vals { font-size: 13px; text-align: right; font-variant-numeric: tabular-nums; }
.lever .vals .d { font-weight: 700; }
.d.pos { color: var(--accent-ink); }
.d.neg { color: var(--neg); }

.run-bar { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: end; gap: 12px; margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }
.opts { display: flex; flex-wrap: wrap; gap: 12px; }
.opts label { display: flex; flex-direction: column; font-size: 12px; color: var(--text-secondary); gap: 4px; }
.run-right { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.btn-primary { font: inherit; font-weight: 600; background: var(--accent); color: #fff; border: 0; border-radius: 10px; padding: 11px 22px; cursor: pointer; font-size: 15px; }
.btn-primary:disabled { opacity: .45; cursor: not-allowed; }

/* results */
.stale { background: var(--warn-bg); color: var(--warn-ink); padding: 8px 12px; border-radius: 8px; font-size: 13px; }
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.tile { border: 1px solid var(--border); border-radius: 12px; padding: 14px; min-width: 0; }
.tile .lab { font-size: 12px; color: var(--text-secondary); }
.tile .big { font-size: 28px; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; margin: 4px 0; }
.tile .rng { font-size: 12px; color: var(--text-muted); }
.tile .note { font-size: 12px; color: var(--text-secondary); margin-top: 4px; }

.steps { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0; margin-top: 8px; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.stp { padding: 12px 14px; position: relative; }
.stp + .stp { border-left: 1px solid var(--border); }
.stp .lab { font-size: 12px; color: var(--text-secondary); }
.stp .v { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stp .s { font-size: 12px; color: var(--text-muted); }
.stp .bar { height: 6px; border-radius: 3px; background: var(--grid); margin-top: 8px; overflow: hidden; }
.stp .bar i { display: block; height: 100%; background: var(--accent); border-radius: 3px; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-secondary); margin: 6px 0; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.sw-before { background: var(--series-before); }
.sw-after { background: var(--series-after); }
.chart svg { width: 100%; height: auto; display: block; overflow: visible; }
.chart text { fill: var(--text-secondary); font: 12px "Be Vietnam Pro", system-ui, sans-serif; }
.chart .ink { fill: var(--text-primary); }
.chart .grid { stroke: var(--grid); }
.chart .zero { stroke: var(--text-muted); stroke-dasharray: 3 3; }
.chart .hit { fill: transparent; cursor: default; }

.table-wrap { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
.tbl th, .tbl td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); }
.tbl th { font-weight: 600; color: var(--text-secondary); font-size: 12px; }
.tbl td.num { text-align: right; white-space: nowrap; }
.dbar { position: relative; height: 10px; width: 120px; background: var(--surface-2); border-radius: 2px; }
.dbar i { position: absolute; top: 0; height: 10px; border-radius: 2px; }
.dbar::after { content: ""; position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px; background: var(--text-muted); }

.method { margin-top: 20px; border-top: 1px solid var(--border); padding-top: 12px; font-size: 14px; }
.method summary { cursor: pointer; font-weight: 600; }
.method li { margin: 6px 0; }

.foot { padding: 8px 0 40px; font-size: 13px; color: var(--text-secondary); }

.tip { position: fixed; z-index: 20; pointer-events: none; background: var(--surface-1); color: var(--text-primary); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 12px; box-shadow: 0 6px 24px rgba(0,0,0,.15); max-width: 260px; }

@media (max-width: 860px) {
  .profile, .tiles { grid-template-columns: repeat(2, 1fr); }
  .grid2 { grid-template-columns: 1fr; }
  .lever { grid-template-columns: 1fr 84px; }
  .lever .slider { grid-column: 1 / -1; grid-row: 2; }
}
@media (max-width: 520px) {
  .card { padding: 14px; border-radius: 12px; }
  .steps { grid-template-columns: 1fr; }
  .stp + .stp { border-left: 0; border-top: 1px solid var(--border); }
  .tile .big { font-size: 24px; }
  .kpi .val { font-size: 19px; }
  #unitSel { width: 100%; }
  .run-right { width: 100%; justify-content: space-between; }
  .hide-sm { display: none; }
  .tbl td:first-child { min-width: 150px; }
}

/* đơn vị */
.unit { font-size: 12.5px; color: var(--text-secondary); margin-top: 2px; }
.unit .u-lab { font-weight: 600; color: var(--text-primary); }
.def { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.def summary { cursor: pointer; }
.def p { margin: 4px 0 2px; color: var(--text-secondary); }
.def .chg { color: var(--warn-ink); }
.vals .pt { font-size: 11px; color: var(--text-muted); }
.scale { background: var(--surface-2); border-radius: 10px; padding: 10px 14px; margin: 10px 0 4px; font-size: 13.5px; }
.scale summary { cursor: pointer; font-weight: 600; }
.scale ul { margin: 6px 0 2px; padding-left: 20px; }
.scale li { margin: 3px 0; }
.formula { font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; background: var(--surface-1); border: 1px solid var(--border); border-radius: 6px; padding: 6px 10px; overflow-x: auto; }
