:root {
    --bg:           #0b0d10;
    --panel:        #11151a;
    --panel-border: #1f2630;
    --fg:           #e6e6e6;
    --muted:        #8a93a0;
    --accent:       #4ea1ff;
    --bar:          #d8d8d8;
    --read:         #ff5577;
    --write:        #ffd24a;
    --pivot:        #ffb84d;
    --range:        rgba(78, 161, 255, 0.18);
    --done:         #9eff6e;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
    background-color: var(--bg);
    color: var(--fg);
    font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    margin: 0;
    display: grid;
    grid-template-rows: auto 1fr;
    grid-template-columns: 1fr 280px;
    grid-template-areas:
                "controls controls"
                "stage    sidebar";
}

/* Controls */
#controls {
    grid-area: controls;
    display: flex;
    flex-wrap: wrap;
    gap: 10px 14px;
    align-items: center;
    padding: 10px 16px;
    border-bottom: 1px solid var(--panel-border);
    background: var(--panel);
}
#controls label {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--muted);
}
#controls input, #controls select {
    background: #161a1f;
    color: var(--fg);
    border: 1px solid #2a3038;
    border-radius: 4px;
    padding: 5px 8px;
    font-size: 13px;
    font-family: inherit;
}
#controls input[type="number"] { width: 78px; }
#controls input#seed { width: 110px; }
#controls button {
    background: var(--accent);
    border: 1px solid var(--accent);
    color: #0b0d10;
    font-weight: 600;
    border-radius: 4px;
    padding: 6px 12px;
    font-size: 13px;
    cursor: pointer;
    font-family: inherit;
}
#controls button:hover:not(:disabled) { filter: brightness(1.12); }
#controls button:disabled { opacity: 0.4; cursor: not-allowed; }
#controls button.secondary {
    background: transparent;
    color: var(--fg);
    border-color: #2a3038;
}
#controls .spacer { flex: 1; }
#controls .audio-toggle { font-size: 12px; color: var(--muted); }

/* Stage */
#stage {
    grid-area: stage;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
}
#container {
    flex: 1;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding: 12px;
    overflow: hidden;
}

/* Bars */
.bar {
    flex: 1;
    margin: 0 1px;
    min-width: 1px;
    background-color: var(--bar);
    transition: height 80ms linear;
    position: relative;
}
.bar.in-range { background-color: color-mix(in srgb, var(--bar) 65%, var(--accent) 35%); }
.bar.cursor   { outline: 2px solid var(--accent); outline-offset: -2px; z-index: 1; }
.bar.pivot    { background-color: var(--pivot); }
.bar.done     { background-color: var(--done); transition: background-color 200ms ease; }

@keyframes flash-read  { from { background-color: var(--read);  } to {} }
@keyframes flash-write { from { background-color: var(--write); } to {} }
.bar.flash-read  { animation: flash-read  140ms ease-out; }
.bar.flash-write { animation: flash-write 140ms ease-out; }

/* Sidebar */
#sidebar {
    grid-area: sidebar;
    border-left: 1px solid var(--panel-border);
    background: var(--panel);
    padding: 14px 16px;
    overflow: auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
}
.section h3 {
    margin: 0 0 8px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.08em;
    color: var(--muted);
    text-transform: uppercase;
}
.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px 10px;
}
.stats-grid div { font-size: 12px; }
.stats-grid .k { color: var(--muted); }
.stats-grid .v { font-variant-numeric: tabular-nums; font-weight: 600; }
#complexity,
#advanced_controls {
    font-size: 11px;
    color: var(--muted);
    line-height: 1.5;
}
#pseudocode {
    font-family: ui-monospace, "Cascadia Code", Menlo, monospace;
    font-size: 12px;
    line-height: 1.55;
    white-space: pre;
    color: #cdd3da;
}
.pseudo-line {
    padding: 2px 6px;
    border-left: 2px solid transparent;
    border-radius: 2px;
}
.pseudo-line.active {
    background: rgba(78, 161, 255, 0.14);
    border-left-color: var(--accent);
    color: #fff;
}

/* Compare mode: hide single-only controls, container, and sidebar; expand stage */
#panes { display: none; }
.compare-only { display: none; }
body[data-mode="compare"] #controls label.single-only,
body[data-mode="compare"] #controls .single-only { display: none; }
body[data-mode="compare"] #controls .compare-only { display: inline-flex; }
body[data-mode="compare"] #container  { display: none; }
body[data-mode="compare"] #sidebar    { display: none; }
body[data-mode="compare"] { grid-template-columns: 1fr; grid-template-areas: "controls" "stage"; }
body[data-mode="compare"] #panes {
    display: grid;
    grid-template-columns: repeat(var(--cols, 2), minmax(0, 1fr));
    grid-template-rows:    repeat(var(--rows, 1), minmax(0, 1fr));
    gap: 8px;
    padding: 8px;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

/* Pane card */
.pane {
    display: grid;
    grid-template-rows: auto 1fr auto;
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: 6px;
    min-height: 0;
    min-width: 0;
    overflow: hidden;
}
.pane-header {
    display: flex;
    gap: 6px;
    align-items: center;
    padding: 6px 8px;
    border-bottom: 1px solid var(--panel-border);
}
.pane-header select {
    background: #161a1f;
    color: var(--fg);
    border: 1px solid #2a3038;
    border-radius: 4px;
    padding: 4px 6px;
    font-size: 12px;
    font-family: inherit;
}
.pane-algo { flex: 1; min-width: 0; }
.pane-race-info {
    font-size: 10px;
    color: var(--muted);
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
}
.pane-remove {
    background: transparent;
    color: var(--muted);
    border: 1px solid #2a3038;
    border-radius: 4px;
    padding: 2px 8px;
    font-size: 13px;
    cursor: pointer;
    line-height: 1;
}
.pane-remove:hover { color: var(--read); border-color: var(--read); }
.pane-body {
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding: 8px;
    overflow: hidden;
    min-height: 0;
}
.pane-footer {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 4px 8px;
    padding: 6px 8px;
    border-top: 1px solid var(--panel-border);
    font-size: 11px;
}
.pane-stat { display: flex; flex-direction: column; min-width: 0; }
.pane-stat .k { color: var(--muted); font-size: 10px; }
.pane-stat .v { font-variant-numeric: tabular-nums; font-weight: 600; }


/* Mobile fallback: collapse sidebar below */
@media (max-width: 760px) {
    body { grid-template-columns: 1fr; grid-template-areas: "controls" "stage" "sidebar"; }
    #sidebar { border-left: none; border-top: 1px solid var(--panel-border); max-height: 40vh; }
    body[data-mode="compare"] { grid-template-areas: "controls" "stage"; }
}
