* {
    box-sizing: border-box;
}

:root {
    --bg: #f7f7f5;
    --card: #fff;
    --ink: #111;
    --muted: #646464;
    --line: #d9d9d4;
    --soft-line: #ececea;
    --panel-bg: #fafaf8;
    /* functional legend colors (match canvas drawing) */
    --blue: #2468a6;
    --amber: #e69d28;
    --red: #d64f62;
    --violet: #6f5bb8;
}

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;
}

.top-bar-right {
    display: flex;
    align-items: center;
    gap: 14px;
}

.home-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 50%;
    background: var(--card);
    color: var(--ink);
    text-decoration: none;
    font-size: 13px;
    font-weight: 600;
    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(--ink);
    outline: none;
}

.readout {
    display: grid;
    gap: 4px;
    min-width: 150px;
    padding: 10px 14px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel-bg);
    text-align: right;
}

.readout span {
    color: var(--muted);
    font-size: 12px;
}

.readout strong {
    font-size: 17px;
    font-variant-numeric: tabular-nums;
}

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

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

.controls-panel {
    display: grid;
    align-content: start;
    gap: 18px;
    padding: 20px;
}

.field-panel {
    padding: 18px;
}

.graph-panel {
    grid-column: 1 / -1;
    padding: 18px;
}

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

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

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,
.preset-button {
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}

.ghost-button {
    min-height: 34px;
    padding: 0 14px;
    border: 1px solid var(--line);
    border-radius: 6px;
    color: var(--ink);
    background: #fff;
    transition: background 0.15s ease;
}

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

.primary-button {
    min-height: 38px;
    padding: 0 16px;
    border: 1px solid var(--ink);
    border-radius: 6px;
    color: #ffffff;
    background: var(--ink);
    transition: opacity 0.15s ease;
}

.primary-button:hover {
    opacity: 0.85;
}

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

.slider-row output {
    color: var(--ink);
    font-variant-numeric: tabular-nums;
    text-align: right;
}

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

.preset-row,
.button-row {
    display: grid;
    gap: 10px;
}

.preset-row {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.button-row {
    grid-template-columns: minmax(0, 1fr) auto;
}

.preset-button {
    min-height: 38px;
    border: 1px solid var(--line);
    border-radius: 6px;
    color: var(--ink);
    background: #fff;
    transition: background 0.15s ease;
}

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

.preset-button.active {
    border-color: var(--ink);
    color: #ffffff;
    background: var(--ink);
}

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

.formula-box p {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 6px 0;
    color: var(--muted);
    font-size: 14px;
}

.formula-box strong {
    color: var(--ink);
    font-variant-numeric: tabular-nums;
    text-align: right;
}

.legend {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 12px;
    color: var(--muted);
    font-size: 13px;
    flex-wrap: wrap;
}

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

.field-swatch,
.particle-swatch,
.path-swatch,
.switch-swatch {
    display: inline-block;
    width: 18px;
    height: 4px;
    border-radius: 999px;
}

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

.particle-swatch {
    background: var(--amber);
}

.path-swatch {
    background: var(--violet);
}

.switch-swatch {
    width: 9px;
    height: 9px;
    background: var(--red);
}

canvas {
    display: block;
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
}

#motionCanvas {
    height: min(50vw, 520px);
    min-height: 380px;
}

#graphCanvas {
    height: 360px;
}

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

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

    .graph-panel {
        grid-column: auto;
    }

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

    .legend {
        justify-content: flex-start;
    }
}
/* ---- Dark mode · follows system colour scheme ---- */
@media (prefers-color-scheme: dark) {
    :root {
        --bg: #14161a;
        --card: #1d2024;
        --ink: #e7e8ea;
        --muted: #9aa0a8;
        --line: #363a41;
        --soft-line: #2a2e34;
        --panel-bg: #212429;
    }
    /* filled emphasis buttons keep a light fill (var(--ink)); flip their text dark */
    .primary-button,
    .mode-button.active,
    .segbtn.active,
    .preset-button.active,
    .field-type-btn.active,
    .field-switch.active,
    .reveal-button {
        color: var(--bg);
    }
    /* outline / ghost controls sit on the dark card, not white */
    .ghost-button,
    .chip,
    .field-switch,
    .secondary-button,
    .preset-button,
    .field-type-btn {
        background: var(--card);
    }
    .ghost-button:hover,
    .secondary-button:hover,
    .field-type-btn:hover {
        background: var(--soft-line);
    }
}
