:root {
    --bg: #0e1511;
    --surface: #151f19;
    --surface-2: #1c2821;
    --border: #26362c;
    --border-strong: #3a4f42;
    --text: #e7eee9;
    --muted: #9fb1a5;
    --accent: #5bd08a;
    --accent-hover: #7cdea2;
    --accent-ink: #08170e;
    --accent-soft: #1d3a29;
    --amber: #f0bf4c;
    --error: #ff8f80;
    --error-border: #e0614f;
    --success: #5bd08a;
    --focus: #f0bf4c;

    --cat-under: #5b9fd8;
    --cat-normal: #4cb876;
    --cat-over: #e9b93f;
    --cat-ob1: #e88b3c;
    --cat-ob2: #d95b42;
    --cat-ob3: #b33e63;

    --display: "Fraunces", Georgia, "Times New Roman", serif;
    --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --radius: 14px;
    color-scheme: dark;
}

:root[data-theme="light"] {
    --bg: #f4f1e8;
    --surface: #fbfaf5;
    --surface-2: #efebdf;
    --border: #dcd6c5;
    --border-strong: #b9b29d;
    --text: #18221c;
    --muted: #545f57;
    --accent: #1d7444;
    --accent-hover: #155c35;
    --accent-ink: #ffffff;
    --accent-soft: #dcebdc;
    --amber: #a86d00;
    --error: #b42318;
    --error-border: #b42318;
    --success: #1d7444;
    --focus: #1d7444;
    color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--sans);
    font-size: 16px;
    line-height: 1.6;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.sprite { position: absolute; }

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

.skip-link {
    position: absolute;
    left: 16px;
    top: -60px;
    background: var(--accent);
    color: var(--accent-ink);
    padding: 8px 14px;
    border-radius: 8px;
    font-weight: 600;
    z-index: 10;
}
.skip-link:focus { top: 12px; }

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

a { color: var(--accent); text-underline-offset: 3px; }
a:hover { color: var(--accent-hover); }

.icon {
    width: 20px;
    height: 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex: none;
}
.icon-accent { color: var(--accent); }
.icon-inline { width: 16px; height: 16px; vertical-align: -2px; margin-left: 3px; }

.wrap {
    width: 100%;
    max-width: 1080px;
    margin: 0 auto;
    padding: 0 16px;
}

/* Header */
.site-header { padding: 18px 0; border-bottom: 1px solid var(--border); }
.header-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; }

.brand { display: flex; align-items: center; gap: 12px; color: var(--text); text-decoration: none; border-radius: 10px; }
.brand:hover { color: var(--text); }
.brand-mark {
    width: 40px;
    height: 40px;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 11px;
    padding: 4px;
}
.mark-ruler { stroke: var(--accent); }
.mark-pin { stroke: var(--amber); }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-name { font-family: var(--display); font-weight: 600; font-size: 21px; letter-spacing: 0.01em; }
.brand-tag { color: var(--muted); font-size: 14px; }

.icon-btn {
    display: inline-grid;
    place-items: center;
    width: 44px;
    height: 44px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--amber);
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s;
}
.icon-btn:hover { background: var(--surface-2); border-color: var(--border-strong); }
.icon-btn .icon { width: 22px; height: 22px; }
.theme-toggle .icon-moon { display: none; }
:root[data-theme="light"] .theme-toggle .icon-sun { display: none; }
:root[data-theme="light"] .theme-toggle .icon-moon { display: block; }

/* Layout */
.layout {
    flex: 1;
    display: grid;
    grid-template-columns: minmax(0, 410px) minmax(0, 1fr);
    gap: 56px;
    padding-top: 36px;
    padding-bottom: 48px;
    align-items: start;
}
.col { display: flex; flex-direction: column; gap: 40px; min-width: 0; }

/* Calculator panel */
.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 26px 24px 24px;
}
.panel-title {
    font-family: var(--display);
    font-weight: 600;
    font-size: 24px;
    line-height: 1.25;
    margin: 0 0 20px;
}

form { display: flex; flex-direction: column; gap: 18px; }

fieldset { margin: 0; padding: 0; border: 0; min-width: 0; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { display: block; font-weight: 600; font-size: 16px; padding: 0; }

.legend-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.input-wrap {
    display: flex;
    align-items: center;
    background: var(--bg);
    border: 1px solid var(--border-strong);
    border-radius: 10px;
    transition: border-color 0.15s, box-shadow 0.15s;
}
.input-wrap:hover { border-color: var(--muted); }
.input-wrap:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.input-wrap input {
    flex: 1;
    min-width: 0;
    width: 100%;
    border: 0;
    background: transparent;
    color: var(--text);
    font: inherit;
    font-size: 18px;
    font-variant-numeric: tabular-nums;
    padding: 11px 14px;
    border-radius: 10px;
}
.input-wrap input:focus { outline: none; }
.input-wrap input::placeholder { color: var(--muted); opacity: 0.6; }
.suffix { color: var(--muted); padding-right: 14px; font-size: 16px; user-select: none; }
.input-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.input-short { max-width: 190px; }

.input-wrap.invalid { border-color: var(--error-border); }
.error { margin: 0; color: var(--error); font-size: 15px; min-height: 0; }
.error:empty { display: none; }
.hint { margin: 0; color: var(--muted); font-size: 15px; }

/* Segmented unit control */
.segmented-track {
    display: grid;
    grid-template-columns: 1fr 1fr;
    background: var(--bg);
    border: 1px solid var(--border-strong);
    border-radius: 12px;
    padding: 4px;
    margin-top: 6px;
    position: relative;
}
.segmented input, .mini-toggle input {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
    margin: 0;
}
.segmented label {
    text-align: center;
    padding: 8px 10px;
    border-radius: 9px;
    font-weight: 600;
    color: var(--muted);
    cursor: pointer;
    transition: background-color 0.15s, color 0.15s;
}
.segmented label:hover { color: var(--text); }
.segmented input:checked + label { background: var(--accent); color: var(--accent-ink); }
.segmented input:focus-visible + label,
.mini-toggle input:focus-visible + label { outline: 3px solid var(--focus); outline-offset: 2px; }

.mini-toggle { position: relative; display: inline-flex; border: 1px solid var(--border-strong); border-radius: 999px; padding: 2px; }
.mini-toggle label {
    padding: 2px 12px;
    border-radius: 999px;
    font-size: 15px;
    color: var(--muted);
    cursor: pointer;
}
.mini-toggle label:hover { color: var(--text); }
.mini-toggle input:checked + label { background: var(--surface-2); color: var(--text); box-shadow: inset 0 0 0 1px var(--border-strong); }

/* Buttons */
.btn-primary {
    margin-top: 6px;
    width: 100%;
    border: 0;
    border-radius: 11px;
    background: var(--accent);
    color: var(--accent-ink);
    font: inherit;
    font-size: 17px;
    font-weight: 650;
    padding: 14px 18px;
    cursor: pointer;
    transition: background-color 0.15s, transform 0.05s;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:active { transform: translateY(1px); }

.btn-secondary {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    align-self: flex-start;
    border: 1px solid var(--border-strong);
    background: transparent;
    color: var(--text);
    font: inherit;
    font-weight: 600;
    border-radius: 10px;
    padding: 10px 16px;
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s;
}
.btn-secondary .icon { color: var(--accent); }
.btn-secondary:hover { background: var(--surface); border-color: var(--accent); }
.btn-secondary:disabled { cursor: default; color: var(--muted); border-color: var(--border); background: transparent; }
.btn-secondary.saved .icon { color: var(--success); }

.btn-text {
    border: 0;
    background: none;
    color: var(--error);
    font: inherit;
    font-size: 15px;
    font-weight: 600;
    padding: 6px 8px;
    border-radius: 8px;
    cursor: pointer;
}
.btn-text:hover { text-decoration: underline; text-underline-offset: 3px; }

/* Result */
.result { min-height: 200px; }
.result-empty {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 14px;
    color: var(--muted);
    max-width: 440px;
    padding-top: 8px;
}
.result-empty p { margin: 0; font-size: 17px; }
.empty-icon {
    width: 48px;
    height: 48px;
    fill: none;
    stroke: var(--accent);
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.result-adult { display: flex; flex-direction: column; gap: 22px; }
.eyebrow { margin: 0; color: var(--muted); font-size: 16px; font-weight: 600; }
.headline { display: flex; flex-direction: column; gap: 6px; }
.bmi-figure {
    margin: 0;
    font-family: var(--display);
    font-weight: 500;
    font-size: 104px;
    line-height: 1;
    letter-spacing: -0.02em;
    font-variant-numeric: lining-nums tabular-nums;
}
.bmi-category {
    margin: 0;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 22px;
    font-weight: 600;
}
.swatch { width: 14px; height: 14px; border-radius: 4px; flex: none; background: var(--muted); }

/* Scale */
.scale { position: relative; padding-top: 30px; }
.scale-bar { display: flex; align-items: center; gap: 3px; height: 22px; }
.seg { height: 10px; border-radius: 3px; transition: height 0.2s; }
.seg.active { height: 22px; border-radius: 4px; }
.seg[data-cat="under"], .swatch[data-cat="under"] { background: var(--cat-under); }
.seg[data-cat="normal"], .swatch[data-cat="normal"] { background: var(--cat-normal); }
.seg[data-cat="over"], .swatch[data-cat="over"] { background: var(--cat-over); }
.seg[data-cat="ob1"], .swatch[data-cat="ob1"] { background: var(--cat-ob1); }
.seg[data-cat="ob2"], .swatch[data-cat="ob2"] { background: var(--cat-ob2); }
.seg[data-cat="ob3"], .swatch[data-cat="ob3"] { background: var(--cat-ob3); }

.scale-marker {
    position: absolute;
    top: 0;
    bottom: 22px;
    width: 0;
    left: 0;
    transition: left 0.35s ease;
}
.scale-marker::after {
    content: "";
    position: absolute;
    left: -1.5px;
    top: 22px;
    bottom: -2px;
    width: 3px;
    border-radius: 2px;
    background: var(--text);
}
.scale-marker-label {
    position: absolute;
    top: 0;
    left: 0;
    transform: translateX(-50%);
    background: var(--text);
    color: var(--bg);
    font-size: 14px;
    font-weight: 700;
    line-height: 1;
    padding: 4px 7px;
    border-radius: 6px;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
}
.scale-marker.edge-start .scale-marker-label { transform: translateX(-4px); }
.scale-marker.edge-end .scale-marker-label { transform: translateX(calc(-100% + 4px)); }

.scale-ticks { position: relative; height: 22px; margin-top: 4px; }
.scale-ticks span {
    position: absolute;
    top: 0;
    transform: translateX(-50%);
    font-size: 13px;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.legend {
    list-style: none;
    margin: -6px 0 0;
    padding: 0;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px 16px;
}
.legend li { display: flex; align-items: flex-start; gap: 8px; color: var(--muted); font-size: 14px; line-height: 1.35; }
.legend .swatch { width: 10px; height: 10px; margin-top: 4px; border-radius: 3px; }
.legend li.active { color: var(--text); font-weight: 600; }
.legend-range { display: block; font-weight: 400; font-variant-numeric: tabular-nums; }

.range { border-top: 1px solid var(--border); padding-top: 18px; }
.range-title { display: flex; align-items: center; gap: 8px; margin: 0 0 4px; font-size: 16px; font-weight: 600; color: var(--muted); }
.range-value { margin: 0; font-family: var(--display); font-size: 30px; font-weight: 500; line-height: 1.25; font-variant-numeric: lining-nums; }
.range-note { margin: 4px 0 0; color: var(--muted); }

.meaning { margin: 0; max-width: 62ch; }

.result-minor { max-width: 560px; }
.result-minor p { margin: 0 0 12px; }
.minor-title { font-family: var(--display); font-weight: 600; font-size: 30px; line-height: 1.2; margin: 4px 0 14px; }

/* About */
.about { border-top: 1px solid var(--border); padding-top: 22px; color: var(--muted); }
.about p { margin: 0 0 10px; max-width: 64ch; }
.about p:last-child { color: var(--text); margin-bottom: 0; }

.section-title { display: flex; align-items: center; gap: 8px; margin: 0 0 12px; font-size: 18px; font-weight: 650; color: var(--text); }

/* History */
.history-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 6px; }
.history-head .section-title { margin: 0; }
.history-empty { margin: 0; color: var(--muted); }
.history-list { list-style: none; margin: 0; padding: 0; }
.history-list li { display: flex; align-items: stretch; gap: 4px; border-bottom: 1px solid var(--border); }
.history-list li:first-child { border-top: 1px solid var(--border); }
.entry-open {
    flex: 1;
    min-width: 0;
    display: grid;
    grid-template-columns: auto auto 1fr;
    grid-template-rows: auto auto;
    column-gap: 12px;
    align-items: center;
    text-align: left;
    border: 0;
    background: none;
    color: var(--text);
    font: inherit;
    padding: 12px 8px;
    border-radius: 8px;
    cursor: pointer;
}
.entry-open:hover { background: var(--surface); }
.entry-open .swatch { grid-row: 1 / span 2; width: 10px; height: 34px; border-radius: 3px; }
.entry-bmi { grid-row: 1 / span 2; font-family: var(--display); font-size: 26px; font-weight: 500; min-width: 3.2ch; font-variant-numeric: lining-nums tabular-nums; }
.entry-cat { font-weight: 600; }
.entry-meta { grid-column: 3; color: var(--muted); font-size: 14px; overflow-wrap: anywhere; }
.entry-delete {
    flex: none;
    align-self: center;
    display: grid;
    place-items: center;
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: 10px;
    background: none;
    color: var(--muted);
    cursor: pointer;
}
.entry-delete:hover { color: var(--error); background: var(--surface); }

/* Footer */
.site-footer { border-top: 1px solid var(--border); padding: 18px 0 24px; color: var(--muted); font-size: 15px; }
.site-footer p { margin: 0; }

@media (max-width: 860px) {
    .layout { grid-template-columns: minmax(0, 1fr); gap: 32px; padding-top: 24px; }
    .col { display: contents; }
    .calc { order: 1; }
    .result { order: 2; }
    .about { order: 3; }
    .history { order: 4; }
}

@media (max-width: 520px) {
    .panel { padding: 20px 16px 18px; }
    .bmi-figure { font-size: 84px; }
    .legend { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .brand-tag { display: none; }
    .range-value { font-size: 26px; }
}

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