/* ==========================================================================
   SBG+ Design Tokens & Styles
   Restrained palette: tinted indigo-grey neutrals + signal-amber accent.
   Sharp corners, IBM Plex Mono, dark-only.
   ========================================================================== */

/* --- Design tokens ------------------------------------------------------ */

:root {
    /* Neutrals */
    --bg: oklch(14% 0.008 258);
    --bg-header: oklch(17% 0.012 258);
    --bg-surface: oklch(20% 0.008 258);
    --bg-diagram-block: oklch(22% 0.006 258);
    --border: oklch(32% 0.008 258);
    --border-subtle: oklch(24% 0.006 258);
    --text-primary: oklch(95% 0 0);
    --text-secondary: oklch(72% 0.006 258);
    --text-muted: oklch(50% 0.006 258);
    --text-inverse: oklch(14% 0.008 258);

    /* Accent — signal amber, ≤10% of surface */
    --accent: oklch(78% 0.16 82);
    --accent-dim: oklch(78% 0.16 82 / 0.15);
    --accent-glow: oklch(78% 0.16 82 / 0.08);

    /* Semantic — Status */
    --status-ontime: oklch(62% 0.12 148);
    --status-delayed: oklch(68% 0.14 68);
    --status-late: oklch(55% 0.18 28);
    --status-scheduled: oklch(50% 0.006 258);

    /* Spacing scale */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 12px;
    --space-lg: 16px;
    --space-xl: 24px;

    /* Typography */
    --font: "IBM Plex Mono", "Berkeley Mono", "Cascadia Code", "SF Mono", ui-monospace, monospace;
    --font-size-title: 1rem;
    --font-size-body: 0.8125rem;
    --font-size-small: 0.6875rem;
    --font-size-micro: 0.625rem;
    --line-height: 1.3;
    --line-height-tight: 1.2;
}

/* --- Reset & base ------------------------------------------------------- */

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

html,
body {
    height: 100%;
}

body {
    font-family: var(--font);
    font-size: var(--font-size-body);
    line-height: var(--line-height);
    background: var(--bg);
    color: var(--text-primary);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    -webkit-font-smoothing: antialiased;
}

/* --- Header ------------------------------------------------------------- */

header {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: var(--space-lg);
    background: var(--bg-header);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

header h1 {
    font-size: var(--font-size-title);
    font-weight: 600;
    color: var(--text-primary);
    letter-spacing: -0.02em;
}

.controls {
    display: flex;
    gap: var(--space-sm);
}

/* --- Station input ------------------------------------------------------ */

.station-input-wrap {
    position: relative;
}

input[type="text"] {
    padding: var(--space-xs) var(--space-sm);
    border: 1px solid var(--border);
    border-radius: 0;
    background: var(--bg);
    color: var(--text-primary);
    font-family: var(--font);
    font-size: var(--font-size-body);
    outline: none;
}

input[type="text"]::placeholder {
    color: var(--text-muted);
}

input[type="text"]:focus {
    border-color: var(--accent);
}

/* --- Buttons ------------------------------------------------------------ */

button {
    padding: var(--space-xs) var(--space-sm);
    border: 1px solid var(--border);
    border-radius: 0;
    background: var(--bg-surface);
    color: var(--text-primary);
    font-family: var(--font);
    font-size: var(--font-size-body);
    font-weight: 600;
    cursor: pointer;
}

button:hover {
    color: var(--accent);
    border-color: var(--accent);
}

button:active {
    background: var(--accent-dim);
}

button:disabled {
    color: var(--text-muted);
    border-color: var(--border-subtle);
    cursor: not-allowed;
}

button:disabled:hover {
    color: var(--text-muted);
    border-color: var(--border-subtle);
}

/* --- Mode toggle (Plan | Actueel) ----------------------------------- */

.mode-toggle-group {
    display: flex;
    gap: 0;
}

.mode-toggle {
    padding: var(--space-xs) var(--space-sm);
    border: 1px solid var(--border);
    border-radius: 0;
    background: var(--bg-surface);
    color: var(--text-secondary);
    font-family: var(--font);
    font-size: var(--font-size-small);
    cursor: pointer;
}

.mode-toggle--active {
    background: var(--accent-dim);
    color: var(--accent);
    border-color: var(--accent);
}

.mode-toggle:hover:not(.mode-toggle--active) {
    color: var(--text-primary);
    border-color: var(--text-muted);
}

/* --- Autocomplete dropdown ---------------------------------------------- */

.autocomplete-list {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 0;
    max-height: 300px;
    overflow-y: auto;
    z-index: 100;
}

.autocomplete-list.open {
    display: block;
}

.autocomplete-item {
    padding: var(--space-xs) var(--space-sm);
    cursor: pointer;
    font-size: var(--font-size-small);
    color: var(--text-secondary);
}

.autocomplete-item:hover,
.autocomplete-item.selected {
    background: var(--accent-glow);
    color: var(--text-primary);
}

.ac-code {
    font-weight: 600;
    color: var(--accent);
    margin-right: var(--space-sm);
}

/* --- Load status -------------------------------------------------------- */

.load-status {
    font-size: var(--font-size-small);
    color: var(--accent);
    display: none;
    align-items: center;
    gap: var(--space-xs);
}

.load-status--visible {
    display: flex;
}

.load-status--error {
    color: var(--status-late);
}

.load-status__spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid var(--border-subtle);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* --- Main layout -------------------------------------------------------- */

main {
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

#diagram-container {
    height: 100%;
    padding: var(--space-lg);
    overflow: auto;
}

/* ==========================================================================
   SVG diagram elements
   ========================================================================== */

.platform-label {
    fill: var(--text-primary);
    font-size: 12px;
    text-anchor: middle;
    font-weight: 600;
}

.platform-headers-bg {
    fill: var(--bg);
}

.time-label {
    fill: var(--text-muted);
    font-size: 11px;
    font-family: var(--font);
}

.gridline--major {
    stroke: var(--border);
    stroke-width: 1;
}

.gridline--minor {
    stroke: var(--border-subtle);
    stroke-width: 0.5;
}

.platform-block {
    fill: var(--bg-diagram-block);
    stroke: var(--border);
    stroke-width: 1;
}

.platform-label--parent {
    font-size: 14px;
    fill: var(--text-primary);
}

.platform-label--child {
    font-size: 10px;
    fill: var(--text-muted);
}

.track-guide {
    stroke: var(--border-subtle);
    stroke-width: 0.5;
    stroke-dasharray: 2 4;
}

.train-block {
    stroke-width: 1;
}

/* --- Train type colours ------------------------------------------------ */

.train-type-ic {
    fill: #b8802c;
    stroke: #d9a448;
}
.train-type-spr {
    fill: #4a7a3a;
    stroke: #5c9648;
}
.train-type-ice {
    fill: #6e3a7a;
    stroke: #8a4a96;
}
.train-type-icd {
    fill: #9a3030;
    stroke: #b84040;
}
.train-type-thal {
    fill: #3a5a8a;
    stroke: #4a72a8;
}
.train-type-eur {
    fill: #b8983a;
    stroke: #d8b848;
}
.train-type-other {
    fill: #5a6880;
    stroke: #708098;
}

/* --- Arrival / departure gradients ------------------------------------- */

.train-block--arrival-only {
    fill: url(#grad-arrival-only);
    stroke: none;
}

.train-block--departure-only {
    fill: url(#grad-departure-only);
    stroke: none;
}

.train-block--arrival-materieel {
    fill: url(#grad-arrival-materieel);
    stroke: none;
}

.train-block--departure-materieel {
    fill: url(#grad-departure-materieel);
    stroke: none;
}

/* --- Korte stop --------------------------------------------------------- */

.korte-stop-dot {
    stroke: var(--accent);
    stroke-width: 1.5;
}

.train-block--korte-stop {
    stroke: var(--accent);
    stroke-width: 1.5;
    stroke-dasharray: 3 2;
}

.train-group--cancelled {
    opacity: 0.3;
}

.train-block--cancelled {
    fill: #5a6880;
    stroke: var(--text-muted);
    stroke-dasharray: 4 4;
}

/* --- Delay severity bar + pill badge (actual mode) -------------------- */

.delay-severity-bar {
    stroke: none;
    pointer-events: none;
}

.delay-severity--slight {
    fill: var(--status-delayed);
}

.delay-severity--significant {
    fill: var(--status-late);
}

.delay-pill-bg {
    stroke: none;
    pointer-events: none;
}

.delay-pill--slight {
    fill: var(--status-delayed);
}

.delay-pill--significant {
    fill: var(--status-late);
}

.delay-pill-text {
    fill: var(--text-inverse);
    font-size: 10px;
    font-family: var(--font);
    font-weight: 700;
    text-anchor: middle;
    pointer-events: none;
}

/* --- Hover reveal ------------------------------------------------------- */

.train-group .train-detail {
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.08s ease-out;
}

.train-group:hover .train-detail {
    opacity: 1;
    pointer-events: auto;
}

.callout-leader {
    stroke: var(--text-muted);
    stroke-width: 0.75;
}

.callout-label {
    fill: var(--text-secondary);
    font-family: var(--font);
}

.callout-label__nr {
    font-weight: 600;
    font-size: 11px;
    fill: var(--text-primary);
}

.callout-label__type {
    font-size: 9px;
    fill: var(--text-muted);
}

.callout-label__kopmaken {
    font-size: 8px;
    fill: var(--accent);
    font-style: italic;
}

.train-time-label {
    fill: var(--text-secondary);
    font-size: 9px;
    font-family: var(--font);
}

/* --- Direction indicators ----------------------------------------------- */

.direction-indicator {
    stroke: var(--indicator-color, #708098);
    stroke-width: 1;
    stroke-linecap: round;
    opacity: 0.2;
    transition: opacity 0.15s ease-out, stroke-width 0.15s ease-out;
}

.train-group:hover .direction-indicator {
    opacity: 1;
    stroke: var(--text-primary);
    stroke-width: 1.5;
}

/* --- Now line ----------------------------------------------------------- */

.now-line {
    stroke: var(--accent);
    stroke-width: 2;
    stroke-dasharray: 6 3;
}

.now-label {
    fill: var(--accent);
    font-size: 11px;
    font-weight: 600;
    font-family: var(--font);
}

/* --- Selected train state ----------------------------------------------- */

.train-group--selected .train-block {
    stroke: var(--text-primary);
    stroke-width: 2;
    filter: drop-shadow(0 0 6px oklch(95% 0 0 / 0.45));
}

/* --- Materieel connectors ----------------------------------------------- */

.materieel-connector {
    fill: none;
    stroke: oklch(62% 0.12 148);
    stroke-width: 1;
    stroke-dasharray: 3 3;
    opacity: 0.6;
}

.materieel-connector--replacement {
    stroke: var(--accent);
    stroke-dasharray: 2 4;
    opacity: 0.5;
}

.materieel-connector--partial_transfer {
    stroke-dasharray: 2 6;
    opacity: 0.45;
}

.materieel-connector-label {
    fill: oklch(62% 0.12 148);
    font-size: 8px;
    font-family: var(--font);
    font-weight: 600;
    text-anchor: middle;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.08s ease-out;
}

.train-group:hover .materieel-connector-label {
    opacity: 1;
}

/* --- Orphan badge (units left behind / added from depot) -------------- */

.orphan-badge {
    fill: var(--accent);
    stroke: var(--accent);
    stroke-width: 1;
    opacity: 0.8;
}

.orphan-badge--low-confidence {
    opacity: 0.4;
    stroke-dasharray: 2 1;
}

.orphan-badge-label {
    fill: var(--accent);
    font-size: 8px;
    font-family: var(--font);
    font-weight: 600;
    text-anchor: middle;
}

/* ==========================================================================
   Train detail sidebar
   ========================================================================== */

.train-detail {
    position: fixed;
    inset: 0;
    z-index: 1000;
    pointer-events: none;
    visibility: hidden;
}

.train-detail--open {
    pointer-events: auto;
    visibility: visible;
}

.train-detail__backdrop {
    position: absolute;
    inset: 0;
    background: oklch(0% 0 0 / 0.45);
    opacity: 0;
    transition: opacity 0.2s ease;
}

.train-detail--open .train-detail__backdrop {
    opacity: 1;
}

.train-detail__panel {
    position: absolute;
    top: 0;
    right: 0;
    width: 360px;
    max-width: 100vw;
    height: 100%;
    background: var(--bg-surface);
    border-left: 1px solid var(--border);
    border-radius: 0;
    transform: translateX(100%);
    transition: transform 0.2s ease-out;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.train-detail--open .train-detail__panel {
    transform: translateX(0);
}

.train-detail__close {
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    width: 32px;
    height: 32px;
    border: 1px solid transparent;
    background: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0;
    z-index: 10;
    transition: color 0.15s;
    font-family: var(--font);
}

.train-detail__close:hover {
    color: var(--accent);
    border-color: transparent;
    background: none;
}

.train-detail__content {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-lg);
    padding-top: 3rem;
}

/* --- Sidebar sections --------------------------------------------------- */

.td-section {
    padding: var(--space-lg) 0;
    border-bottom: 1px solid var(--border-subtle);
}

.td-section:first-child {
    padding-top: 0;
}

.td-section:last-child {
    border-bottom: none;
}

.td-section__title {
    font-size: var(--font-size-micro);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    margin: 0 0 var(--space-md) 0;
}

/* --- Identity section --------------------------------------------------- */

.td-identity__header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--space-xs);
}

.td-identity__nr-group {
    display: flex;
    align-items: baseline;
    gap: var(--space-sm);
}

.td-identity__nr {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.02em;
}

.td-identity__type {
    font-size: var(--font-size-small);
    font-weight: 700;
    padding: 0.15rem 0.4rem;
    border-radius: 0;
    letter-spacing: 0.04em;
}

.td-identity__status {
    font-size: var(--font-size-small);
    font-weight: 600;
    padding: 0.2rem 0.5rem;
    border-radius: 0;
    white-space: nowrap;
}

.td-status--ontime {
    background: oklch(62% 0.12 148 / 0.2);
    color: var(--status-ontime);
}

.td-status--delayed-slight {
    background: oklch(68% 0.14 68 / 0.2);
    color: var(--status-delayed);
}

.td-status--delayed {
    background: oklch(55% 0.18 28 / 0.2);
    color: var(--status-late);
}

.td-status--cancelled {
    background: oklch(55% 0.18 28 / 0.3);
    color: var(--status-late);
}

.td-status--scheduled {
    background: oklch(50% 0.006 258 / 0.2);
    color: var(--text-muted);
}

.td-identity__type-long {
    font-size: var(--font-size-body);
    color: var(--text-secondary);
    margin-bottom: var(--space-sm);
}

.td-identity__route {
    font-size: var(--font-size-body);
    color: var(--text-secondary);
    margin-bottom: var(--space-sm);
    line-height: var(--line-height);
}

.td-identity__station {
    color: var(--text-primary);
}

.td-identity__arrow {
    color: var(--text-muted);
    margin: 0 var(--space-xs);
}

.td-identity__track {
    font-size: var(--font-size-body);
    color: var(--text-secondary);
}

.td-identity__track-val {
    color: var(--text-primary);
    font-weight: 600;
}

.td-identity__track-change-wrap {
    display: inline-flex;
    align-items: center;
    gap: 0.2rem;
    margin-left: var(--space-sm);
    font-size: var(--font-size-small);
}

.td-identity__track-planned {
    color: var(--text-muted);
    text-decoration: line-through;
}

.td-identity__track-actual {
    color: var(--status-delayed);
    font-weight: 600;
}

.td-identity__track-change-arrow {
    color: var(--text-muted);
}

/* --- Times section ------------------------------------------------------ */

.td-times__row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: 0.35rem 0;
}

.td-times__label {
    font-size: var(--font-size-small);
    color: var(--text-muted);
    min-width: 5rem;
}

.td-times__label--arrival::before {
    content: "↓ ";
    color: var(--status-ontime);
}

.td-times__label--departure::before {
    content: "↑ ";
    color: #708098;
}

.td-times__label--dwell::before {
    content: "⏱ ";
}

.td-times__values {
    display: flex;
    gap: var(--space-xs);
    align-items: baseline;
}

.td-times__planned {
    font-size: var(--font-size-body);
    color: var(--text-primary);
    font-weight: 500;
}

.td-times__actual {
    font-size: var(--font-size-small);
    color: var(--text-muted);
}

.td-times__delay {
    font-size: var(--font-size-small);
    font-weight: 600;
    padding: 0.1rem 0.35rem;
    border-radius: 0;
}

.td-times__delay--late {
    background: oklch(55% 0.18 28 / 0.2);
    color: var(--status-late);
}

.td-times__delay--early {
    background: oklch(62% 0.12 148 / 0.2);
    color: var(--status-ontime);
}

.td-times__row--dwell {
    margin-top: var(--space-xs);
    padding-top: var(--space-sm);
    border-top: 1px solid var(--border-subtle);
}

/* --- Materieel section -------------------------------------------------- */

.td-materieel__kopmaken {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    flex-wrap: wrap;
    padding: var(--space-sm) 0.65rem;
    margin-bottom: var(--space-md);
    background: var(--accent-glow);
    border: 1px solid oklch(78% 0.16 82 / 0.25);
    border-radius: 0;
    font-size: var(--font-size-small);
    color: var(--text-secondary);
}

.td-materieel__kopmaken-icon {
    color: var(--accent);
    font-size: 1rem;
}

.td-materieel__kopmaken-link {
    background: none;
    border: none;
    color: var(--accent);
    font-weight: 700;
    cursor: pointer;
    font-size: var(--font-size-body);
    padding: 0;
    text-decoration: underline;
    text-underline-offset: 2px;
    border-radius: 0;
    font-family: var(--font);
}

.td-materieel__kopmaken-link:hover {
    color: var(--accent);
    opacity: 0.8;
}

.td-materieel__na {
    font-size: var(--font-size-body);
    color: var(--text-muted);
    font-style: italic;
}

.td-materieel__summary {
    font-size: var(--font-size-body);
    color: var(--text-secondary);
    margin-bottom: var(--space-sm);
}

.td-materieel__type-name {
    font-weight: 600;
    color: var(--text-primary);
}

.td-materieel__lengte {
    color: var(--text-muted);
    font-size: var(--font-size-small);
}

.td-materieel__car-bar {
    display: flex;
    gap: 2px;
    margin: var(--space-sm) 0 var(--space-md) 0;
}

.td-materieel__car {
    height: 28px;
    flex: 1;
    min-width: 6px;
    border-radius: 0;
    opacity: 0.85;
}

.td-materieel__numbers {
    font-size: var(--font-size-small);
    color: var(--text-muted);
}

.td-materieel__numbers-label {
    color: var(--text-muted);
}

.td-materieel__numbers-vals {
    color: var(--text-muted);
    font-family: var(--font);
}

.td-materieel__unit--leaving {
    color: var(--accent);
    font-weight: 600;
}

.td-materieel__unit--joining {
    color: oklch(62% 0.12 148);
    font-weight: 600;
}

.td-materieel__relation-badge {
    display: inline-block;
    font-size: var(--font-size-micro);
    font-weight: 600;
    color: oklch(62% 0.12 148);
    background: oklch(62% 0.12 148 / 0.12);
    padding: 0.1rem 0.35rem;
    border-radius: 0;
    margin-left: var(--space-xs);
}

/* --- Missing / empty state ---------------------------------------------- */

.td-missing {
    font-size: var(--font-size-body);
    color: var(--text-muted);
    font-style: italic;
    padding: var(--space-lg) 0;
}
