* {
    box-sizing: border-box;
}

:root {
    --bg: var(--site-bg);
    --card: var(--site-surface);
    --ink: var(--site-text);
    --muted: var(--site-muted);
    --line: var(--site-border);
    --soft-line: var(--site-border-soft);
    --panel-bg: var(--site-surface-soft);
    --control-fill: var(--site-green);
    --control-text: var(--site-on-accent);
    /* The drawing uses a fixed palette, so it keeps a light surface in either scheme. */
    --diagram-bg: var(--site-figure-bg);
    --blue: #0072B2;
    --orange: #D55E00;
    --teal: #009E73;
}

body {
    margin: 0;
    min-height: 100vh;
    color: var(--ink);
    background: var(--bg);
    font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
}

h1,
h2,
h3,
p {
    margin: 0;
}

.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 18px;
    width: min(1280px, calc(100% - 48px));
    margin: 0 auto;
    padding: 24px 0 18px;
}

.top-bar h1 {
    font-size: 30px;
    line-height: 1.15;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.home-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: none;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 50%;
    background: var(--card);
    color: var(--ink);
    text-decoration: none;
    transition: background 0.15s ease, border-color 0.15s ease;
}

.home-button svg {
    display: block;
}

.home-button:hover,
.home-button:focus-visible {
    background: var(--soft-line);
    border-color: var(--control-fill);
    outline: none;
}

.layout {
    display: grid;
    grid-template-columns: 340px minmax(0, 1fr);
    gap: 22px;
    align-items: start;
    width: min(1280px, calc(100% - 48px));
    margin: 0 auto;
    padding: 0 0 8px;
}

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

.controls-panel {
    display: grid;
    gap: 16px;
    padding: 20px;
}

.stage-panel {
    min-width: 0;
    padding: 18px;
}

.section-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    margin-bottom: 14px;
}

.controls-panel .section-title {
    margin-bottom: 2px;
}

h2 {
    font-size: 18px;
    line-height: 1.3;
    font-weight: 700;
}

h3 {
    margin-bottom: 10px;
    font-size: 15px;
    font-weight: 700;
}

.ghost-button,
.primary-button {
    min-height: 36px;
    padding: 0 14px;
    border: 1px solid var(--line);
    border-radius: 6px;
    color: var(--ink);
    background: var(--card);
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s ease;
}

.ghost-button:hover {
    background: var(--soft-line);
}

.primary-button {
    color: var(--control-text);
    background: var(--control-fill);
    border-color: var(--control-fill);
}

.primary-button:hover {
    background: var(--site-green-hover);
    border-color: var(--site-green-hover);
}

.ghost-button:focus-visible,
.primary-button:focus-visible,
input:focus-visible {
    outline: 2px solid var(--site-focus);
    outline-offset: 2px;
}

.button-row,
.chip-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

.group-label {
    margin-bottom: 8px;
    font-size: 13px;
    font-weight: 700;
}

.slider-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 8px 12px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel-bg);
    font-size: 14px;
    font-weight: 600;
}

.slider-row output {
    font-variant-numeric: tabular-nums;
    text-align: right;
}

.slider-row input {
    grid-column: 1 / -1;
}

input[type="range"] {
    width: 100%;
    margin: 0;
    accent-color: var(--ink);
}

.check-row {
    display: flex;
    align-items: center;
    gap: 9px;
    min-height: 34px;
    font-size: 14px;
    font-weight: 600;
}

.check-row input {
    width: 17px;
    height: 17px;
    accent-color: var(--ink);
}

.formula-box {
    padding: 14px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: var(--panel-bg);
}

.formula-box p {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    margin-top: 8px;
    color: var(--muted);
    line-height: 1.35;
}

.formula-box strong {
    color: var(--ink);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

.formula-box .note {
    display: block;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.45;
}

.legend {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 6px 14px;
    color: var(--muted);
    font-size: 13px;
}

.legend span {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.swatch {
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius: 50%;
}

.swatch.blue {
    background: var(--blue);
}

.swatch.orange {
    background: var(--orange);
}

.swatch.teal {
    background: var(--teal);
}

.figure {
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--diagram-bg);
}

canvas {
    display: block;
    width: 100%;
}

footer {
    width: min(1280px, calc(100% - 48px));
    margin: 0 auto;
    padding: 6px 0 26px;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.5;
}

@media (max-width: 900px) {
    .top-bar,
    .layout,
    footer {
        width: calc(100% - 32px);
    }

    .layout {
        grid-template-columns: 1fr;
    }

    .stage-panel {
        order: -1;
    }
}

@media (max-width: 560px) {
    .top-bar h1 {
        font-size: 24px;
    }

    .section-title {
        align-items: flex-start;
        flex-direction: column;
    }

    .legend {
        justify-content: flex-start;
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        scroll-behavior: auto;
        transition: none !important;
    }
}
