:root {
    --bg: #f4f3ef;
    --panel: #fcfcfa;
    --border: #e2e0d9;
    --ink: #1f2328;
    --muted: #6b7280;
    --accent: #3b82f6;
    --edge: #9aa0aa;
    --edge-hover: #e5484d;
    --node: #e5484d;
    --node-hover: #cf3a3f;
    --settled: #5b6b7f;
    --current: #e8910c;
    --route: #2fa36b;
    --weight-bg: #fcfcfa;
    --field: #ffffff;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 4px 16px rgb(0 0 0 / 0.04);
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #15171a;
        --panel: #1c1f23;
        --border: #2d3137;
        --ink: #e8eaed;
        --muted: #9aa1ab;
        --accent: #6aa0ff;
        --edge: #5d6570;
        --node: #e5484d;
        --node-hover: #f06065;
        --settled: #6e7f95;
        --weight-bg: #1c1f23;
        --field: #24282d;
        --shadow: none;
        color-scheme: dark;
    }
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font: 15px/1.5 "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h1, h2 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.6rem; letter-spacing: -0.01em; }
h2 { font-size: 1rem; margin-bottom: 0.6rem; }

button, input, select { font: inherit; color: inherit; }

/* ── Page layout ───────────────────────────────────────────────────── */

.site-header, .layout, .site-footer {
    max-width: 1280px;
    margin: 0 auto;
    padding: 0 16px;
}

.site-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 16px;
    flex-wrap: wrap;
    padding-top: 28px;
    padding-bottom: 20px;
}

.tagline { margin: 0.35rem 0 0; color: var(--muted); }

.source-link {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: var(--ink);
    text-decoration: none;
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 6px 14px;
    background: var(--panel);
}
.source-link:hover { border-color: var(--muted); }

.layout {
    display: grid;
    grid-template-columns: 320px minmax(0, 1fr) 290px;
    grid-template-areas:
        "controls canvas distances"
        "controls matrix matrix";
    gap: 16px;
    align-items: start;
}

.controls { grid-area: controls; }
.canvas { grid-area: canvas; }
.distances { grid-area: distances; }
.matrix { grid-area: matrix; }

@media (max-width: 1100px) {
    .layout {
        grid-template-columns: 320px minmax(0, 1fr);
        grid-template-areas:
            "controls canvas"
            "distances canvas"
            "matrix matrix";
    }
}

@media (max-width: 720px) {
    .layout {
        grid-template-columns: minmax(0, 1fr);
        grid-template-areas: "canvas" "controls" "distances" "matrix";
    }
}

.panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 18px;
    box-shadow: var(--shadow);
    min-width: 0;
}

.site-footer {
    color: var(--muted);
    font-size: 0.85rem;
    padding-top: 24px;
    padding-bottom: 40px;
}
.site-footer p { max-width: 70ch; margin: 0; }

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

.controls section + section {
    border-top: 1px solid var(--border);
    margin-top: 16px;
    padding-top: 16px;
}

.field {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 10px;
}
.field.stacked { flex-direction: column; align-items: stretch; gap: 4px; flex: 1; }
.label { color: var(--muted); font-size: 0.9rem; }

input[type="number"], select {
    background: var(--field);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 6px 10px;
    min-height: 36px;
}
.field input[type="number"] { width: 90px; }
select { width: 100%; }

button {
    background: var(--field);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 7px 12px;
    min-height: 36px;
    cursor: pointer;
    white-space: nowrap;
    transition: border-color 0.15s, background 0.15s;
}
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: 0.4; cursor: default; }
button:focus-visible, input:focus-visible, select:focus-visible, .node:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

button.primary {
    width: 100%;
    background: var(--ink);
    border-color: var(--ink);
    color: var(--panel);
    font-weight: 600;
    margin-top: 4px;
}
button.primary:hover { opacity: 0.9; }

.button-row { display: flex; gap: 8px; }
.button-row > * { flex: 1; }

.stepper { display: inline-flex; align-items: center; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.stepper button { border: 0; border-radius: 0; width: 36px; font-size: 1.1rem; padding: 0; }
.stepper output { min-width: 36px; text-align: center; font-weight: 600; font-variant-numeric: tabular-nums; }

.hint { color: var(--muted); font-size: 0.88rem; margin: 8px 0 0; }

.result { font-weight: 600; margin: 14px 0 10px; }
.result.found { color: var(--route); }
.result.not-found { color: var(--node); }

.step-controls { display: flex; gap: 6px; }
.step-controls button { padding: 6px 0; width: 38px; flex: none; display: inline-grid; place-items: center; }
.step-controls button.replay { flex: 1; width: auto; min-width: 0; padding: 6px 8px; overflow: hidden; text-overflow: ellipsis; }

/* ── Graph ─────────────────────────────────────────────────────────── */

.canvas svg {
    display: block;
    width: 100%;
    max-height: min(78vh, 760px);
    aspect-ratio: 1;
    margin: 0 auto;
    user-select: none;
    -webkit-user-select: none;
}

.edge { cursor: pointer; }
.edge-hit { stroke: transparent; stroke-width: 18; }
.edge-line { stroke: var(--edge); stroke-width: 2.5; stroke-linecap: round; transition: stroke 0.2s, stroke-width 0.2s; }
.edge:hover .edge-line { stroke: var(--edge-hover); stroke-width: 3.5; stroke-dasharray: 6 5; }
.edge.relaxed .edge-line { stroke: var(--current); stroke-width: 4; }
.edge.on-route .edge-line { stroke: var(--route); stroke-width: 6; }

.weight rect { fill: var(--weight-bg); stroke: var(--border); }
.weight text { font-size: 15px; font-weight: 700; text-anchor: middle; fill: var(--ink); pointer-events: none; }
.weight { pointer-events: none; }

.node { cursor: pointer; outline: none; }
.node .body { fill: var(--node); transition: fill 0.25s; }
.node:hover .body { fill: var(--node-hover); }
.node .ring { fill: none; stroke: transparent; stroke-width: 3; }
.node text { fill: #fff; font-size: 20px; font-weight: 650; text-anchor: middle; pointer-events: none; }
.node.selected .ring { stroke: var(--accent); }
.node.settled .body { fill: var(--settled); }
.node.current .body { fill: var(--current); }
.node.on-route .body { fill: var(--route); }
.node.source .ring, .node.target .ring { stroke: var(--ink); stroke-dasharray: 3 4; }
.node.selected.source .ring, .node.selected.target .ring { stroke: var(--accent); stroke-dasharray: none; }

.badge rect { fill: var(--panel); stroke: var(--border); }
.badge text { font-size: 14px; font-weight: 700; text-anchor: middle; fill: var(--muted); }
.badge.updated rect { stroke: var(--current); }
.badge.updated text { fill: var(--current); }

.legend {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px 18px;
    list-style: none;
    padding: 0;
    margin: 8px 0 0;
    color: var(--muted);
    font-size: 0.85rem;
}
.legend li { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }
.node-swatch { background: var(--node); }
.current-swatch { background: var(--current); }
.settled-swatch { background: var(--settled); }
.route-swatch { background: var(--route); }

/* ── Tables ────────────────────────────────────────────────────────── */

table { border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: center; padding: 4px 6px; }
thead th { color: var(--muted); font-weight: 600; font-size: 0.85rem; }
tbody th { color: var(--muted); font-weight: 600; }

.distances table { width: 100%; margin-top: 10px; }
.distances td, .distances tbody th { border-top: 1px solid var(--border); }
.distances tr.current td, .distances tr.current th { background: color-mix(in srgb, var(--current) 18%, transparent); }

.matrix .scroll { overflow-x: auto; margin-top: 10px; }
.matrix td { padding: 2px; }
.matrix td.diagonal { color: var(--border); }
.matrix input[type="number"] {
    width: 44px;
    min-height: 32px;
    padding: 4px 2px;
    text-align: center;
    font-weight: 600;
    -moz-appearance: textfield;
    appearance: textfield;
}
.matrix input::-webkit-outer-spin-button,
.matrix input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.matrix td.empty input { color: var(--muted); font-weight: 400; border-color: transparent; background: transparent; }
.matrix td.empty input:hover, .matrix td.empty input:focus { border-color: var(--border); background: var(--field); }

/* ── Loading and error states ──────────────────────────────────────── */

.loading {
    min-height: 100vh;
    display: grid;
    place-content: center;
    justify-items: center;
    gap: 14px;
    color: var(--muted);
}
.loading-dots { display: flex; gap: 10px; }
.loading-dots span {
    width: 16px; height: 16px; border-radius: 50%;
    background: var(--node);
    animation: pulse 1.2s infinite ease-in-out;
}
.loading-dots span:nth-child(2) { animation-delay: 0.2s; background: var(--current); }
.loading-dots span:nth-child(3) { animation-delay: 0.4s; background: var(--route); }
@keyframes pulse { 0%, 100% { transform: scale(0.6); opacity: 0.5; } 50% { transform: scale(1); opacity: 1; } }

#blazor-error-ui {
    display: none;
    position: fixed;
    inset: auto 0 0 0;
    padding: 12px 16px;
    background: #fff4e5;
    color: #5c3b00;
    box-shadow: 0 -2px 8px rgb(0 0 0 / 0.15);
    z-index: 1000;
}
#blazor-error-ui .reload { margin-left: 8px; }
#blazor-error-ui .dismiss { float: right; cursor: pointer; }

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