/* Traffic Simulator — application shell.
 *
 * The simulation is the point of this app, so the layout gives it the room and
 * keeps everything you might want while watching it — controls, live figures,
 * the trend charts — on screen at the same time. Nothing scrolls the page; the
 * two side panels scroll independently if they need to.
 */

/* ── Tokens ──────────────────────────────────────────────────────────── */

:root {
    --topbar-h: 52px;
    --gap: 12px;
    --panel-w: 268px;
    --metrics-w: 310px;

    /* Chart ink and series colours. Both modes are chosen for their own
       surface rather than flipped, and each chart carries a single series, so
       these are two independent one-hue scales, not a categorical set. */
    --viz-series-1: #2a78d6;   /* throughput */
    --viz-series-2: #eb6834;   /* average speed */
    --viz-grid:     #e1e0d9;
    --viz-muted:    #898781;
    --status-critical: #d03b3b;

    /* Signal lamp colours: real-world semantics, shared with the canvas signal
       heads -- keep in step with SIGNAL in js/render/palette.js. */
    --signal-red:    #e05348;
    --signal-amber:  #e9b949;
    --signal-green:  #4fae62;
    --signal-off:    #3a3e44;
}

[data-bs-theme="dark"] {
    --viz-series-1: #3987e5;
    --viz-series-2: #d95926;
    --viz-grid:     #2c2c2a;
    --viz-muted:    #898781;
    --status-critical: #e66767;
}

/* ── Shell ───────────────────────────────────────────────────────────── */

html, body {
    height: 100%;
}

body.app {
    margin: 0;
    display: grid;
    grid-template-rows: var(--topbar-h) minmax(0, 1fr);
    height: 100dvh;
    overflow: hidden;
    background: var(--bs-body-bg);
    color: var(--bs-body-color);
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

.workspace {
    display: grid;
    grid-template-columns: var(--panel-w) minmax(0, 1fr) var(--metrics-w);
    gap: var(--gap);
    padding: var(--gap);
    min-height: 0;
    overflow: hidden;
}

.panel {
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    background: var(--bs-tertiary-bg);
    border: 1px solid var(--bs-border-color);
    border-radius: 10px;
    padding: 14px;
}

/* ── Top bar ─────────────────────────────────────────────────────────── */

.topbar {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 0 14px;
    background: var(--bs-tertiary-bg);
    border-bottom: 1px solid var(--bs-border-color);
    min-width: 0;
}

.brand {
    font-weight: 650;
    font-size: 0.98rem;
    white-space: nowrap;
}

.scenario-switch {
    display: flex;
    gap: 2px;
    flex: 1 1 auto;
    min-width: 0;
    overflow-x: auto;
    scrollbar-width: none;
}
.scenario-switch::-webkit-scrollbar { display: none; }

.scenario-btn {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 6px 12px;
    border: 0;
    border-radius: 7px;
    background: transparent;
    color: var(--bs-secondary-color);
    font-size: 0.84rem;
    font-weight: 550;
    white-space: nowrap;
    cursor: pointer;
}
.scenario-btn:hover { background: var(--bs-secondary-bg); color: var(--bs-body-color); }
.scenario-btn.active {
    background: var(--bs-body-bg);
    color: var(--bs-emphasis-color);
    box-shadow: inset 0 0 0 1px var(--bs-border-color),
                inset 0 -2px 0 var(--viz-series-1);
}
.scenario-btn i { font-size: 0.85em; opacity: 0.75; }

.scenario-btn:focus-visible,
.seg-btn:focus-visible,
.stage-badge:focus-visible {
    outline: 2px solid var(--viz-series-1);
    outline-offset: 1px;
}

.transport, .topbar-end {
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 0 0 auto;
}
.transport .btn-text { margin-left: 6px; }

/* ── Stage ───────────────────────────────────────────────────────────── */

.stage {
    min-width: 0;
    min-height: 0;
    display: grid;
}

/* min-width/min-height 0 all the way down: a grid item's default `auto`
   minimum is its content size, and the canvas has an intrinsic 800x600, so
   without this the stage refuses to shrink below it and the canvas is clipped
   rather than scaled. */
.stage-content {
    position: relative;
    min-width: 0;
    min-height: 0;
    display: grid;
}

/* Bootstrap hides inactive panes; the active one becomes the centring box the
   canvas measures itself against. */
.stage-content > .tab-pane { display: none; }
.stage-content > .tab-pane.active {
    display: grid;
    place-items: center;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    animation: pane-in 140ms ease-out;
}
@keyframes pane-in {
    from { opacity: 0.35; }
    to   { opacity: 1; }
}

/* Sized in JS to the largest 4:3 box that fits this pane, at device
   resolution — see Renderer.resizeCanvas. */
.simulation-canvas {
    display: block;
    max-width: 100%;
    border-radius: 10px;
    border: 1px solid var(--bs-border-color);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.18);
}

/* Paused chip over the canvas: makes the transport state visible where you
   are actually looking, and doubles as a tap-to-play target. */
.stage-badge {
    position: absolute;
    left: 50%;
    bottom: 26px;
    transform: translateX(-50%);
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 6px 14px;
    border: 1px solid var(--bs-border-color);
    border-radius: 999px;
    background: var(--bs-body-bg);
    color: var(--bs-secondary-color);
    font-size: 0.74rem;
    font-weight: 550;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    cursor: pointer;
    opacity: 0;
    pointer-events: none;
    transition: opacity 150ms ease;
}
.stage-badge.visible {
    opacity: 1;
    pointer-events: auto;
}
.stage-badge:hover { color: var(--bs-body-color); }
.stage-badge i { font-size: 0.8em; color: var(--signal-green); }

/* ── Controls ────────────────────────────────────────────────────────── */

.ctl-group + .ctl-group { margin-top: 18px; }

.ctl-title {
    margin: 0 0 10px;
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.09em;
    text-transform: uppercase;
    color: var(--bs-secondary-color);
}

/* One spacing rule for everything stacked inside a group, so a slider that
   follows help text or a segmented control is spaced like any other. */
.ctl-group > * + * { margin-top: 12px; }
.ctl-group > .ctl-help { margin-top: 8px; }

.ctl-label {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 3px;
    font-size: 0.8rem;
    color: var(--bs-body-color);
}

.ctl-value {
    font-weight: 650;
    color: var(--bs-emphasis-color);
    font-variant-numeric: tabular-nums;
}
/* Brief emphasis when a slider writes a new value (re-triggered from JS) */
.ctl-value.value-flash {
    animation: value-flash 450ms ease-out;
}
@keyframes value-flash {
    from { color: var(--viz-series-1); }
    to   { color: var(--bs-emphasis-color); }
}
.ctl-unit {
    font-size: 0.72rem;
    color: var(--bs-secondary-color);
    margin-left: -2px;
}

.ctl-help {
    margin: 0;
    font-size: 0.71rem;
    line-height: 1.45;
    color: var(--bs-secondary-color);
}

.form-range { margin: 0; }

/* Segmented buttons */
.seg {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2px;
    padding: 2px;
    background: var(--bs-secondary-bg);
    border-radius: 8px;
}
.seg-3 { grid-template-columns: repeat(3, 1fr); }

.seg-btn {
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: var(--bs-secondary-color);
    padding: 5px 4px;
    font-size: 0.76rem;
    font-weight: 550;
    cursor: pointer;
}
.seg-btn:hover { color: var(--bs-body-color); }
.seg-btn.active {
    background: var(--bs-body-bg);
    color: var(--bs-emphasis-color);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
}

/* Signal state read-out */
.signal-state {
    display: grid;
    gap: 6px;
}
.signal-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.76rem;
    color: var(--bs-secondary-color);
}
.lamps {
    display: inline-flex;
    gap: 4px;
    padding: 4px 7px;
    background: #23272b;
    border-radius: 999px;
}
.lamp {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--signal-off);
    transition: background 0.2s ease, box-shadow 0.2s ease;
}
/* Signal colours are real-world semantics, not theme decoration; the same
   values light the canvas heads (SIGNAL in js/render/palette.js). */
.lamp.red.active    { background: var(--signal-red);   box-shadow: 0 0 7px var(--signal-red); }
.lamp.yellow.active { background: var(--signal-amber); box-shadow: 0 0 7px var(--signal-amber); }
.lamp.green.active  { background: var(--signal-green); box-shadow: 0 0 7px var(--signal-green); }

/* Vehicle mix rows: the preview is the label */
.mix-row {
    display: grid;
    grid-template-columns: 34px minmax(0, 1fr);
    align-items: center;
    gap: 8px;
}
.mix-row .ctl { margin-top: 0; min-width: 0; }
.mix-preview {
    width: 34px;
    height: auto;
    opacity: 0.9;
}

.shortcuts {
    margin: 20px 0 0;
    padding-top: 12px;
    border-top: 1px solid var(--bs-border-color);
    font-size: 0.68rem;
    line-height: 1.9;
    color: var(--bs-secondary-color);
}
.shortcuts kbd { font-size: 0.64rem; padding: 1px 4px; }

/* ── Metrics ─────────────────────────────────────────────────────────── */

.hero { margin-bottom: 16px; }

.hero-value {
    font-size: 3rem;
    font-weight: 650;
    line-height: 1;
    color: var(--viz-series-1);
    /* Proportional figures: tabular digits make a large number look loose. */
}
.hero-label {
    margin-top: 4px;
    font-size: 0.72rem;
    line-height: 1.35;
    color: var(--bs-secondary-color);
}

.tiles {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}
.tile {
    background: var(--bs-body-bg);
    border: 1px solid var(--bs-border-color);
    border-radius: 8px;
    padding: 8px 10px;
    transition: border-color 120ms ease;
}
.tile:hover { border-color: var(--bs-secondary-color); }
.tile-value {
    font-size: 1.15rem;
    font-weight: 650;
    line-height: 1.15;
    color: var(--bs-emphasis-color);
}
.tile-label {
    font-size: 0.68rem;
    color: var(--bs-secondary-color);
}

/* Collisions carry a status colour, so they also carry an icon and a word —
   never colour alone. Neutral until there is something to report. */
.collisions {
    margin-top: 10px;
    padding: 9px 11px;
    background: var(--bs-body-bg);
    border: 1px solid var(--bs-border-color);
    border-radius: 8px;
    color: var(--bs-secondary-color);
}
.collisions.has-collisions {
    border-color: var(--status-critical);
    color: var(--status-critical);
}
/* One-shot pulse when the count ticks up (class re-applied from JS) */
.collisions.collision-pulse {
    animation: collision-pulse 500ms ease-out;
}
@keyframes collision-pulse {
    0%   { box-shadow: 0 0 0 0 color-mix(in srgb, var(--status-critical) 45%, transparent); }
    100% { box-shadow: 0 0 0 8px transparent; }
}
@media (prefers-reduced-motion: reduce) {
    .collisions.collision-pulse,
    .ctl-value.value-flash,
    .stage-content > .tab-pane.active { animation: none; }
}
.collisions-head {
    display: flex;
    align-items: baseline;
    gap: 7px;
    font-size: 0.78rem;
}
.collisions-head i { font-size: 0.8em; }
.collisions-count { font-size: 1.05rem; font-weight: 650; }
.collisions-word { flex: 1 1 auto; }
.collisions-rate { font-variant-numeric: tabular-nums; }
.collisions-breakdown {
    margin-top: 3px;
    font-size: 0.67rem;
    color: var(--bs-secondary-color);
}

/* ── Charts ──────────────────────────────────────────────────────────── */

.viz {
    margin: 16px 0 0;
}
.viz-title {
    font-size: 0.7rem;
    font-weight: 650;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--bs-secondary-color);
    margin-bottom: 6px;
}
.viz-unit {
    font-weight: 400;
    text-transform: none;
    letter-spacing: 0;
    color: var(--viz-muted);
}

/* Chart.js sizes the canvas to its parent; if that parent's height is auto the
   parent then grows to fit the canvas and the pair expand without bound. The
   plot box therefore has an explicit height, sized to include the axis band. */
.viz-plot {
    position: relative;
    width: 100%;
    height: 116px;
}
.viz-plot canvas { max-width: 100%; }

.viz-x {
    margin: 4px 0 0;
    font-size: 0.66rem;
    color: var(--viz-muted);
    text-align: center;
}

/* ── Responsive ──────────────────────────────────────────────────────── */

/* Metrics drop under the stage before the controls do: you can watch the sim
   without the charts, but not without the controls. */
@media (max-width: 1200px) {
    body.app { overflow: auto; height: auto; min-height: 100dvh; }
    .workspace {
        grid-template-columns: var(--panel-w) minmax(0, 1fr);
        grid-template-rows: minmax(360px, 62vh) auto;
        overflow: visible;
    }
    .panel-controls { grid-row: 1 / span 2; }
    .panel-metrics { grid-column: 2; overflow: visible; }
    .tiles { grid-template-columns: repeat(4, 1fr); }
    .viz-plot { height: 130px; }
}

@media (max-width: 820px) {
    .workspace {
        grid-template-columns: minmax(0, 1fr);
        grid-template-rows: minmax(300px, 54vh) auto auto;
    }
    .panel-controls { grid-row: 3; }
    .panel-metrics { grid-column: 1; grid-row: 2; }
    .stage { grid-row: 1; }
    .topbar { gap: 10px; }
    .brand { display: none; }
    .scenario-btn span { display: none; }
    .scenario-btn { padding: 6px 10px; }
    .transport .btn-text { display: none; }
    .tiles { grid-template-columns: 1fr 1fr; }
}

/* ── Scrollbars ──────────────────────────────────────────────────────── */

.panel::-webkit-scrollbar { width: 8px; }
.panel::-webkit-scrollbar-track { background: transparent; }
.panel::-webkit-scrollbar-thumb {
    background: var(--bs-border-color);
    border-radius: 4px;
}
.panel::-webkit-scrollbar-thumb:hover { background: var(--bs-secondary-color); }
