:root {
    --bg: #1a1a2e;
    --panel: #16213e;
    --accent: #0f3460;
    --highlight: #e94560;
    --text: #eaeaea;
    --text-muted: #8892a4;
    --border: #2a3a5c;
    --chart-bg: #0d1b2a;
    --font: "Inter", system-ui, sans-serif;
}

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

html, body {
    height: 100%;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
}

#app {
    display: grid;
    height: 100vh;
    grid-template-rows: auto 1fr;
    grid-template-columns: 280px 1fr;
    grid-template-areas:
        "header header"
        "sidebar main";
}

/* ── Header ── */
header {
    grid-area: header;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    padding: 0.75rem 1.25rem;
    display: flex;
    align-items: center;
    gap: 1rem;
}

header h1 {
    font-size: 1.15rem;
    font-weight: 600;
    color: var(--highlight);
    letter-spacing: 0.02em;
}

/* The h1 wraps a link to the GitHub repo; keep the red headline look and
   only reveal underline on hover, mirroring the .vdm-link / .telraam-seg
   pattern used elsewhere in the header. */
header h1 a {
    color: inherit;
    text-decoration: none;
}
header h1 a:hover { text-decoration: underline; }

header .subtitle {
    color: var(--text-muted);
    font-size: 0.8rem;
}

header .load-status {
    margin-left: auto;
    font-size: 0.78rem;
    color: var(--text-muted);
}

header .data-status {
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-left: 1rem;
}

header .data-status:empty { display: none; }

/* Per-segment Telraam chip — same hue as ambient text by default; the
   stale variant flags a sensor whose latest record is hours behind. */
header .telraam-seg.stale { color: var(--warning, #f5a623); }

/* Status-bar links (VdM dataset, per-segment Telraam pages). Inherit the
   ambient colour so an empty hover state matches the static text styling,
   and only reveal the link affordance on hover. */
header .vdm-link,
header .telraam-seg,
header .cdn-ndg-link {
    color: inherit;
    text-decoration: none;
}
header .vdm-link:hover,
header .telraam-seg:hover,
header .cdn-ndg-link:hover { text-decoration: underline; }

/* ── Sidebar ── */
aside {
    grid-area: sidebar;
    background: var(--panel);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.control-group label.section-label {
    font-size: 0.7rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
    padding-bottom: 0.35rem;
}

.btn-group {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
}

button {
    background: var(--accent);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.3rem 0.65rem;
    font-size: 0.8rem;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

button:hover { background: #1a4a8a; border-color: #4a6fa0; }
button.active { background: var(--highlight); border-color: var(--highlight); color: #fff; }
button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}
button:disabled:hover { background: var(--accent); border-color: var(--border); }

.source-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.source-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.4rem 0.6rem;
    border-radius: 4px;
    border: 1px solid var(--border);
    cursor: pointer;
    transition: background 0.12s;
}

.source-item:hover { background: var(--accent); }
.source-item.selected { border-color: var(--highlight); background: rgba(233,69,96,0.12); }

.source-group {
    border: 1px solid var(--border);
    border-radius: 4px;
    overflow: hidden;
}
.source-group .source-item {
    border: none;
    border-radius: 0;
}
.source-group .source-item + .source-item {
    border-top: 1px solid var(--border);
}
.source-group .source-item.selected {
    border-left: 2px solid var(--highlight);
}

.source-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

.source-name { font-size: 0.8rem; }
.source-dates { font-size: 0.7rem; color: var(--text-muted); }

/* ── Date range inputs ── */
.date-range {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}

.date-range input[type="date"] {
    background: var(--chart-bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.3rem 0.5rem;
    font-size: 0.8rem;
    width: 100%;
    color-scheme: dark;
}

/* ── Main content ── */
main {
    grid-area: main;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chart-container {
    flex: 1;
    background: var(--chart-bg);
    position: relative;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.chart-container svg {
    flex: 1;
    width: 100%;
    min-height: 0;
}

/* Discrete export buttons anchored in the top-right of the chart area:
   download-as-PNG (camera) and copy-to-clipboard. Transparent background
   so the buttons sit unobtrusively on top of the plot, muted icon color
   until hovered. */
.chart-export-group {
    position: absolute;
    top: 6px;
    right: 8px;
    z-index: 5;
    display: flex;
    gap: 4px;
}
.chart-export {
    width: 26px;
    height: 26px;
    padding: 3px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}
.chart-export:hover { color: var(--text); border-color: var(--text-muted); }
.chart-export svg { width: 18px; height: 18px; display: block; }
/* Brief success flash on the clipboard button after a write resolves. */
.chart-export.flash { color: var(--highlight); border-color: var(--highlight); }

.chart-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 0.3rem 1.1rem;
    padding: 0.45rem 0.75rem;
    border-top: 1px solid var(--border);
    background: var(--chart-bg);
}

.chart-legend-item {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.71rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.legend-stats {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.66rem;
    opacity: 0.75;
    margin-left: 0.25rem;
}

.map-container {
    height: 220px;
    background: #0a1628;
    border-top: 1px solid var(--border);
    position: relative;
    overflow: hidden;
}

.map-content {
    position: absolute;
    inset: 0;
    transform-origin: 50% 50%;
}

.map-tiles {
    position: absolute;
    inset: 0;
    pointer-events: none;
}

.map-tile {
    position: absolute;
    width: 256px;
    height: 256px;
    user-select: none;
    -webkit-user-drag: none;
}

.map-marker {
    position: absolute;
    cursor: pointer;
    z-index: 1;
}

.map-marker:hover,
.map-marker.selected {
    z-index: 10;
}

.marker-dot {
    position: absolute;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 2px solid #a8b8d0;
    background: #1a2a4a;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5);
}

.map-marker.selected .marker-dot {
    border-width: 3px;
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.7);
}

.marker-label {
    position: absolute;
    left: 0;
    top: 12px;
    transform: translateX(-50%);
    white-space: nowrap;
    font-size: 11px;
    line-height: 1.2;
    color: #ffffff;
    text-shadow:
        -1px -1px 0 #000, 1px -1px 0 #000,
        -1px  1px 0 #000, 1px  1px 0 #000,
        -2px  0   0 #000, 2px  0   0 #000,
         0  -2px  0 #000, 0   2px  0 #000;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.1s;
}

.map-marker:hover .marker-label,
.map-marker.selected .marker-label {
    opacity: 1;
}

.map-marker.label-above .marker-label {
    top: auto;
    bottom: 12px;
}

.map-hint {
    position: absolute;
    top: 6px;
    left: 8px;
    color: #c0c8d8;
    font-size: 10px;
    text-shadow:
        -1px -1px 0 #000, 1px -1px 0 #000,
        -1px  1px 0 #000, 1px  1px 0 #000;
    pointer-events: none;
}

.map-attribution {
    position: absolute;
    bottom: 4px;
    right: 8px;
    color: #ffffff;
    font-size: 9px;
    opacity: 0.75;
    text-shadow:
        -1px -1px 0 #000, 1px -1px 0 #000,
        -1px  1px 0 #000, 1px  1px 0 #000;
    pointer-events: none;
}

/* SVG chart elements */
.chart-axis line, .chart-axis path { stroke: var(--border); }
.chart-axis text { fill: var(--text-muted); font-size: 11px; }
.chart-axis-title { fill: var(--text); font-size: 11px; opacity: 0.85; }
.chart-grid line { stroke: var(--border); stroke-dasharray: 3 4; }
.chart-line { fill: none; stroke-width: 2; }
.chart-area { opacity: 0.15; }
.chart-title { fill: var(--text); font-size: 12px; }

/* Hover crosshair + dot markers (rendered above the chart, click-through) */
.chart-hover { pointer-events: none; }
.chart-crosshair {
    stroke: rgba(255, 255, 255, 0.35);
    stroke-width: 1;
    stroke-dasharray: 3 3;
}
.chart-hover-dot {
    stroke: #fff;
    stroke-width: 1.2;
}
.chart-hover-area { cursor: crosshair; }

/* Floating tooltip showing date + per-series values at the cursor */
.chart-tooltip {
    position: fixed;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.45rem 0.65rem;
    font-size: 0.75rem;
    color: var(--text);
    pointer-events: none;
    white-space: nowrap;
    z-index: 20;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
}
.chart-tooltip-date {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-bottom: 0.3rem;
}
.chart-tooltip-row {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    line-height: 1.45;
}
.chart-tooltip-swatch {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 2px;
    flex-shrink: 0;
}
.chart-tooltip-label { flex: 1; }
.chart-tooltip-value {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-weight: 600;
}

/* Loading / empty states */
.placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    font-size: 0.9rem;
}
.placeholder .show-mobile { display: none; }

/* Tooltip */
.tooltip {
    position: absolute;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.4rem 0.65rem;
    font-size: 0.78rem;
    pointer-events: none;
    white-space: nowrap;
    z-index: 10;
}

/* Scrollbar */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: var(--panel); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* Mobile-only toggle button — hidden on desktop. */
.mobile-toggle { display: none; }

/* Header language toggle (always visible). Shows the *other* language. */
.lang-toggle {
    margin-left: 0.6rem;
    font-size: 0.78rem;
    padding: 0.3rem 0.55rem;
    letter-spacing: 0.05em;
    font-weight: 600;
}

/* ── Narrow viewports (phones) ── */
@media (max-width: 768px) {
    html, body { font-size: 15px; }

    /* Single column: header, then main; sidebar is a drawer below the header
       that's only present when sidebar-open is set on #app. */
    #app {
        grid-template-columns: 1fr;
        grid-template-areas:
            "header"
            "main";
    }
    #app.sidebar-open {
        grid-template-areas:
            "header"
            "sidebar"
            "main";
        grid-template-rows: auto auto 1fr;
    }
    aside { display: none; }
    #app.sidebar-open aside {
        display: flex;
        max-height: 60vh;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }

    .mobile-toggle {
        display: inline-flex;
        align-items: center;
        font-size: 0.85rem;
        padding: 0.4rem 0.7rem;
    }

    .placeholder .show-desktop { display: none; }
    .placeholder .show-mobile { display: inline; }

    /* Header tightens up; load-status falls below the title to avoid wrap. */
    header { flex-wrap: wrap; padding: 0.6rem 0.85rem; gap: 0.6rem; }
    header h1 { font-size: 1rem; }
    header .subtitle { display: none; }
    header .load-status,
    header .data-status { font-size: 0.72rem; margin-left: 0; }

    /* Larger tap targets across the sidebar. */
    button { padding: 0.5rem 0.85rem; font-size: 0.85rem; }
    .source-item { padding: 0.55rem 0.7rem; }
    .source-name { font-size: 0.85rem; }

    /* Map shrinks so the chart keeps the dominant viewport space. */
    .map-container { height: 160px; }

    /* Tooltip can hit the screen edges — let it wrap rather than overflow. */
    .chart-tooltip { white-space: normal; max-width: 70vw; }
}
