:root {
    --bg: #0b0c0f;
    --panel: #111318;
    --elev: #161922;
    --text: #e7e9ee;
    --muted: #9aa3b2;
    --line: #262a35;
    --accent: #4f7cff;
    --accent-weak: rgba(79, 124, 255, 0.2);
    --radius: 16px;
    --radius-sm: 12px;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
    --space-1: 8px;
    --space-2: 12px;
    --space-3: 16px;
    --space-4: 20px;
    --space-5: 28px;
    --space-6: 40px;
    --content-width: 1100px;
    --content-wide: 1680px;
    --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
    --sans: system-ui, -apple-system, Segoe UI, Roboto, "Noto Sans", Ubuntu, Cantarell, "Helvetica Neue", Arial, sans-serif;
    --header-h: 64px;
    --control-h: 40px;
}

@media (prefers-color-scheme: light) {
    :root {
        --bg: #f6f7fb;
        --panel: #ffffff;
        --elev: #ffffff;
        --text: #111318;
        --muted: #5b6472;
        --line: #e6e8ef;
        --accent: #335cff;
        --accent-weak: rgba(51, 92, 255, 0.12);
        --shadow: 0 6px 24px rgba(0, 0, 0, 0.08);
    }
}

* { box-sizing: border-box; }
html, body { height: 100dvh; }
body {
    margin: 0;
    font-family: var(--sans);
    color: var(--text);
    background:
        radial-gradient(1200px 800px at 10% -10%, rgba(79,124,255,0.07), transparent),
        radial-gradient(1200px 800px at 90% 110%, rgba(147, 72, 255, 0.06), transparent),
        var(--bg);
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow: hidden;
}

.container {
    width: 100%;
    max-width: var(--content-width);
    padding: 0 var(--space-3);
    margin: 0 auto;
}

.site-header {
    position: sticky;
    top: 0;
    backdrop-filter: saturate(120%) blur(8px);
    background: color-mix(in oklab, var(--bg) 80%, transparent);
    border-bottom: 1px solid var(--line);
    z-index: 10;
}
.header-inner {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: var(--space-3);
    height: var(--header-h);
}
.brand {
    margin: 0;
    font-size: 20px;
    letter-spacing: 0.3px;
    font-weight: 700;
}

.nav {
    justify-self: center;
    display: flex;
    align-items: center;
    gap: var(--space-3);
    width: 100%;
    justify-content: space-between;
}

.nav-link {
    text-decoration: none;
    color: var(--text);
    opacity: 0.9;
    padding: 10px 12px;
    border-radius: 10px;
    transition: opacity 160ms ease;
}
.nav-link:hover { opacity: 1; }
.edge-link { justify-self: end; }

.divider { display: none !important; }

.segmented {
    display: inline-flex;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 4px;
    gap: 4px;
    height: var(--control-h);
    align-items: center;
}
.segmented-item {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: calc(var(--control-h) - 8px);
    padding: 0 14px;
    border-radius: 999px;
    text-decoration: none;
    color: var(--muted);
    border: 1px solid transparent;
    transition: background 160ms ease, color 160ms ease, border-color 160ms ease;
    font-variant-numeric: tabular-nums;
}
.segmented-item:hover { color: var(--text); background: var(--elev); }
.segmented-item[aria-current="page"] {
    color: var(--text);
    background: var(--accent-weak);
    border-color: color-mix(in oklab, var(--accent) 60%, transparent);
}

.btn {
    appearance: none;
    border: 1px solid var(--line);
    background: var(--panel);
    color: var(--text);
    padding: 8px 14px;
    border-radius: 10px;
    cursor: pointer;
    transition: transform 80ms ease, background 160ms ease, border-color 160ms ease;
}
.btn:hover { background: var(--elev); }
.btn:active { transform: translateY(1px); }
.btn.primary {
    border-color: color-mix(in oklab, var(--accent) 50%, var(--line));
    background: var(--accent-weak);
}
.btn.icon {
    width: var(--control-h);
    height: var(--control-h);
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    font-size: 18px;
}
.btn.icon svg { display: block; }

.content {
    padding: var(--space-3) 0 var(--space-4);
    display: grid;
    grid-template-rows: auto auto 1fr;
    gap: var(--space-3);
    height: calc(100dvh - var(--header-h));
}

.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: clamp(12px, 2.2vw, 24px);
}
.panel + .panel { margin-top: 0; }

.panel-head { margin-bottom: var(--space-2); }
.panel-title {
    margin: 0 0 4px 0;
    font-size: clamp(18px, 2.2vw, 22px);
}
.panel-sub { margin: 0; color: var(--muted); font-size: 14px; }

.stage { margin-top: 0; }
.stage-inner {
    min-height: 0 !important;
    padding: 0;
    display: grid;
    place-items: center;
}
.stage-inner canvas {
    display: block;
    max-width: 100%;
    height: auto;
}

.howto {
    margin: 0;
    padding-left: 18px;
    line-height: 1.55;
    font-size: 14px;
}
kbd {
    font-family: var(--mono);
    font-size: 12px;
    padding: 2px 6px;
    border-radius: 6px;
    border: 1px solid var(--line);
    background: color-mix(in oklab, var(--elev) 80%, transparent);
    box-shadow:
        inset 0 1px 0 color-mix(in oklab, #fff 18%, transparent),
        inset 0 -1px 0 color-mix(in oklab, #000 28%, transparent);
}

.controls-grid {
    display: grid;
    grid-template-columns: repeat(12, 1fr);
    gap: var(--space-3);
}
.control {
    grid-column: span 6;
    display: flex;
    flex-direction: column;
    gap: 10px;
}
@media (max-width: 900px) { .control { grid-column: span 12; } }

.control-label {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    font-size: 14px;
}
.readout {
    min-width: 44px;
    text-align: right;
    font-family: var(--mono);
    font-size: 13px;
    color: var(--muted);
    padding: 2px 8px;
    border: 1px dashed var(--line);
    border-radius: 10px;
}

.range {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 34px;
    background: transparent;
}
.range:focus { outline: none; }
.range::-webkit-slider-runnable-track {
    height: 6px;
    background: linear-gradient(90deg, var(--accent-weak), var(--line));
    border-radius: 999px;
}
.range::-moz-range-track {
    height: 6px;
    background: linear-gradient(90deg, var(--accent-weak), var(--line));
    border-radius: 999px;
}
.range::-webkit-slider-thumb {
    -webkit-appearance: none; appearance: none;
    margin-top: -8px;
    width: 22px; height: 22px; border-radius: 50%;
    background: var(--accent);
    border: 2px solid color-mix(in oklab, var(--accent) 40%, #fff);
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}
.range::-moz-range-thumb {
    width: 22px; height: 22px; border-radius: 50%;
    background: var(--accent);
    border: 2px solid color-mix(in oklab, var(--accent) 40%, #fff);
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}

.hint { margin-top: var(--space-3); color: var(--muted); font-size: 13px; }

.switch {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 12px;
    padding: 8px 12px;
    border-radius: 12px;
    border: 1px solid var(--line);
    background: var(--panel);
}
.switch input {
    position: absolute;
    inline-size: 1px; block-size: 1px;
    margin: -1px; overflow: hidden;
    clip: rect(0 0 0 0); clip-path: inset(50%);
    white-space: nowrap; border: 0; padding: 0;
}
.switch:has(input:focus-visible) {
    outline: 2px solid color-mix(in oklab, var(--accent) 70%, transparent);
    outline-offset: 3px;
}
.switch .slider {
    width: 54px; height: 30px;
    border-radius: 999px;
    position: relative;
    background: var(--line);
    transition: background-color 160ms ease, box-shadow 160ms ease;
    box-shadow: inset 0 0 0 2px color-mix(in oklab, var(--bg) 65%, var(--line));
}
.switch .slider::after {
    content: "";
    position: absolute;
    top: 3px; left: 3px;
    width: 24px; height: 24px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 3px rgba(0,0,0,0.35);
    transition: transform 160ms ease, background-color 160ms ease;
}
.switch input:checked + .slider {
    background: color-mix(in oklab, var(--accent) 85%, #000);
    box-shadow: inset 0 0 0 2px color-mix(in oklab, var(--accent) 40%, var(--line));
}
.switch input:checked + .slider::after { transform: translateX(24px); background: #fff; }

.switch .switch-text {
    font-size: 14px;
    color: var(--muted);
    user-select: none;
}

.modal::backdrop {
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(2px);
}
.modal { border: none; padding: 0; background: transparent; }
.modal-card {
    width: min(720px, 92vw);
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    display: grid;
    grid-template-rows: auto 1fr auto;
    max-height: 85vh;
}
.modal-head, .modal-foot {
    padding: 14px 16px;
    border-bottom: 1px solid var(--line);
}
.modal-foot { border-bottom: none; border-top: 1px solid var(--line); }
.modal-title { margin: 0; font-size: 18px; }
.modal-body { padding: 16px; overflow: auto; }

.site-footer {
    padding: var(--space-5) var(--space-3) var(--space-6);
    color: var(--muted);
    font-size: 12px;
    text-align: center;
    opacity: 0.9;
}

@media (max-width: 820px) {
    .header-inner {
        grid-template-columns: auto 1fr;
        grid-auto-rows: auto;
        align-items: center;
        height: auto;
        padding-block: 8px;
        row-gap: 8px;
    }
    .brand        { grid-column: 1; }
    .edge-link    { grid-column: 2; justify-self: end; }
    .nav {
        grid-column: 1 / -1;
        grid-row: 2;
        width: 100%;
        display: flex;
        gap: 8px;
        justify-content: space-between;
    }
    .segmented { width: auto; }
    .divider   { display: none; }
}

@media (max-width: 340px) {
    .nav {
        display: flex;
        flex-wrap: nowrap;
        gap: 8px;
        overflow-x: auto;
        padding-bottom: 6px;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: thin;
        justify-content: space-between;
    }
    .nav::-webkit-scrollbar { height: 6px; }
    .nav::-webkit-scrollbar-thumb { background: var(--line); border-radius: 999px; }
    .segmented { flex: 1 0 auto; max-width: none; }
}

@media (max-height: 700px) {
    :root { --header-h: 52px; }
    .content { gap: 12px; padding: 12px 0 14px; }
    .panel { padding: 12px; }
    .howto { font-size: 13px; line-height: 1.45; }
    .stage-inner { min-height: clamp(200px, 52dvh, 520px); }
    .hint { display: none; }
}

@media (max-height: 560px) {
    :root { --header-h: 48px; }
    .stage-inner { min-height: clamp(180px, 48dvh, 500px); }
    .panel-title { font-size: 16px; }
    .nav-link { padding: 8px 10px; }
    .btn.icon { width: 36px; height: 36px; }
}

@media (max-width: 820px) { .segmented { width: max-content; } }

.nav-link {
    text-decoration: none;
    color: var(--accent);
    font-weight: 600;
    opacity: 1;
    padding: 8px 14px;
    border-radius: 8px;
    transition: background 160ms ease, color 160ms ease;
}

.nav-link:hover {
    background: var(--accent-weak);
    color: var(--text);
}

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

.controls .btn {
    height: var(--control-h);
    padding: 0 16px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

.controls .switch {
    height: var(--control-h);
    padding: 0 12px;
    gap: 10px;
}

.controls .switch .slider {
    width: 48px;
    height: 24px;
    border-radius: 999px;
}

.controls .switch .slider::after {
    top: 2px;
    left: 2px;
    width: 20px;
    height: 20px;
}

.controls .switch input:checked + .slider::after {
    transform: translateX(24px);
}

.controls .switch .switch-text {
    color: color-mix(in oklab, var(--text) 85%, var(--muted));
    font-weight: 600;
}

.page-genetic .container { max-width: min(var(--content-wide), 96vw); }
.page-genetic .content { padding-top: 24px; }

.page-genetic .stage--full {
    width: 100vw;
    margin-left: calc(50% - 50vw);
    margin-right: calc(50% - 50vw);
}

.page-genetic .stage--scroll .stage-inner {
    overflow-x: auto;
    overflow-y: auto;
    display: block;
    padding: clamp(8px, 2vw, 24px);
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    touch-action: pan-x pan-y pinch-zoom;
}

.page-genetic .stage--scroll .stage-inner::-webkit-scrollbar { height: 8px; }
.page-genetic .stage--scroll .stage-inner::-webkit-scrollbar-thumb {
    background: var(--line);
    border-radius: 999px;
}

.page-genetic #p5-root canvas {
    display: block;
    margin-inline: auto;
    max-width: none !important;
}

.page-genetic {
    overflow-y: auto;
    overflow-x: auto;
    touch-action: pan-x pan-y pinch-zoom;
}

.page-genetic .content {
    height: auto;
    min-height: calc(100dvh - var(--header-h));
}

.page-genetic .stage-inner,
.page-genetic #p5-root,
.page-genetic #p5-root canvas {
    touch-action: pan-x pan-y pinch-zoom;
}

@media (pointer: coarse) {
    .page-genetic #p5-root canvas {
        pointer-events: none;
    }
}

.page-genetic .panel-skin {
    position: relative;
    margin: clamp(8px, 2vw, 24px) 0;
    padding: clamp(8px, 2vw, 24px);
}

.page-genetic .panel-skin::before {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: var(--radius);
    background: var(--panel);
    border: 1px solid var(--line);
    box-shadow: var(--shadow);
    z-index: 0;
}

.page-genetic .panel-skin > * {
    position: relative;
    z-index: 1;
}

.page-genetic {
    overflow-y: auto;
    overflow-x: auto;
}
.page-genetic .content {
    height: auto;
    min-height: calc(100dvh - var(--header-h));
}

.page-genetic #p5-root,
.page-genetic #p5-root canvas {
    touch-action: pan-x pan-y pinch-zoom;
}