: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;
}
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 .data-status {
    margin-left: auto;
    font-size: 0.78rem;
    color: var(--text-muted);
}
header .data-status:empty { display: none; }
header .data-status a { color: inherit; text-decoration: none; }
header .data-status a:hover { text-decoration: underline; }

/* View toggle (Map / Time series) in the header. */
.view-toggle {
    display: flex;
    gap: 0;
    border: 1px solid var(--border);
    border-radius: 5px;
    overflow: hidden;
}
.view-toggle button {
    border: none;
    border-radius: 0;
    padding: 0.32rem 0.8rem;
}
.view-toggle button + button { border-left: 1px solid var(--border); }

/* ── 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: 0.5rem; }

.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-item.disabled { opacity: 0.4; cursor: not-allowed; }
.source-item.disabled:hover { background: none; }

.source-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
    background: var(--text-muted);
}
.source-name { font-size: 0.8rem; }
.source-sub { font-size: 0.7rem; color: var(--text-muted); }

/* Year-range From/To selects on the map. */
.year-range { display: flex; align-items: center; gap: 0.4rem; }
.year-range .substance-select { width: auto; flex: 1; }
.year-range-sep { color: var(--text-muted); }

/* Chart caption — selection summary above the plot. */
.chart-caption {
    padding: 0.4rem 0.75rem 0.1rem;
    font-size: 0.78rem;
    color: var(--text);
    background: var(--chart-bg);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Substance selector as a scrollable single-select list. */
.substance-select {
    background: var(--chart-bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.4rem 0.5rem;
    font-size: 0.82rem;
    width: 100%;
    color-scheme: dark;
}

/* ── 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;
}

/* Info views (Network / Methodology): no controls apply, so collapse the
   sidebar and let the article span the full width. */
#app.info-view {
    grid-template-columns: 1fr;
    grid-template-areas:
        "header"
        "main";
}
#app.info-view aside { display: none; }
#app.info-view .mobile-toggle { display: none; }

/* ── Info / About article ── */
.info-page {
    height: 100%;
    overflow-y: auto;
    background: var(--chart-bg);
}
.info-content {
    max-width: 820px;
    margin: 0 auto;
    padding: 2rem 1.5rem 3.5rem;
    line-height: 1.62;
    color: var(--text);
}
.info-content header { display: block; padding: 0; background: none; border: none; margin-bottom: 1.5rem; }
.info-content h1 {
    color: var(--highlight);
    font-size: 1.55rem;
    font-weight: 600;
    line-height: 1.25;
    margin-bottom: 0.75rem;
}
.info-content section { margin-top: 2.25rem; }
.info-content h2 {
    font-size: 1.18rem;
    font-weight: 600;
    color: var(--text);
    border-bottom: 1px solid var(--border);
    padding-bottom: 0.35rem;
    margin-bottom: 0.85rem;
}
.info-content h3 {
    font-size: 0.98rem;
    font-weight: 600;
    color: #b9c4da;
    margin: 1.25rem 0 0.4rem;
}
.info-content p { margin-bottom: 0.85rem; }
.info-content ul, .info-content ol { margin: 0 0 0.85rem 1.4rem; }
.info-content li { margin-bottom: 0.5rem; }
.info-content strong { color: #fff; font-weight: 600; }
.info-content em { color: var(--text-muted); }
.info-content a {
    color: #6db3ff;
    text-decoration: none;
    overflow-wrap: anywhere;
}
.info-content a:hover { text-decoration: underline; }
/* Footnote reference superscripts: compact, slightly muted. */
.info-content sup { font-size: 0.7em; }
.info-content sup a { padding: 0 0.05em; }
/* Sources list: smaller, with breathing room for long URLs. */
.info-content section:last-of-type ol { font-size: 0.86rem; color: var(--text-muted); }
.info-content section:last-of-type li { margin-bottom: 0.7rem; }
/* Anchored footnote targets get a brief highlight when jumped to. */
.info-content li:target { background: rgba(233,69,96,0.12); border-radius: 3px; }

/* ── Chart ── */
.chart-container {
    flex: 1;
    background: var(--chart-bg);
    position: relative;
    display: flex;
    flex-direction: column;
    min-height: 0;
}
.chart-container svg.chart-plot { flex: 1; width: 100%; min-height: 0; }

.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; }
.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;
}

/* 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; }

/* IQA acceptability reference lines on the time-series chart. */
.chart-threshold { stroke: rgba(255, 255, 255, 0.38); stroke-width: 1; stroke-dasharray: 5 4; }
.chart-threshold-label { fill: var(--text-muted); font-size: 10px; }

.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; }

.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; }

/* ── Map (full-size primary content in Map view) ── */
.map-container {
    flex: 1;
    min-height: 0;
    background: #0a1628;
    position: relative;
    overflow: hidden;
}

.map-tile {
    position: absolute;
    user-select: none;
    -webkit-user-drag: none;
    z-index: 0;
}

/* IDW heatmap overlay sits above tiles, below markers. */
.heatmap-canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;
}

.map-marker { position: absolute; cursor: default; z-index: 2; }
.map-marker:hover { z-index: 10; }

.marker-dot {
    position: absolute;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 2px solid #ffffff;
    background: var(--text-muted);
    transform: translate(-50%, -50%);
    box-shadow: 0 0 0 1px rgba(0,0,0,0.6);
}
.map-marker.nodata .marker-dot {
    background: transparent;
    border-color: #6a7488;
    border-style: dashed;
}

.marker-label {
    position: absolute;
    left: 0;
    top: 12px;
    transform: translateX(-50%);
    white-space: nowrap;
    font-size: 11px;
    line-height: 1.25;
    color: #ffffff;
    text-align: center;
    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;
}
.marker-label .marker-value {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-weight: 600;
}
.marker-label .marker-driver {
    font-size: 0.92em;
    color: #cfd8ea;
    opacity: 0.95;
}
.map-marker:hover .marker-label { opacity: 1; }
.map-marker.label-above .marker-label { top: auto; bottom: 12px; }
/* Always show the value chip (compact) even without hover, so the map reads
   at a glance; the full name appears on hover. */
.marker-value-chip {
    position: absolute;
    left: 0;
    top: 11px;
    transform: translateX(-50%);
    font-size: 10px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-weight: 600;
    color: #fff;
    white-space: nowrap;
    pointer-events: none;
    text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
}
.map-marker:hover .marker-value-chip { opacity: 0; }

.map-hint, .map-attribution {
    position: absolute;
    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;
    z-index: 5;
}
.map-hint { top: 8px; left: 10px; }
.map-attribution { bottom: 4px; right: 8px; color: #fff; opacity: 0.75; font-size: 9px; }

/* Colour-bar legend (value → colour key), bottom-left of the map. */
.colorbar {
    position: absolute;
    left: 10px;
    bottom: 14px;
    z-index: 5;
    background: rgba(13,27,42,0.82);
    border: 1px solid var(--border);
    border-radius: 5px;
    padding: 0.5rem 0.6rem;
    pointer-events: none;
    min-width: 180px;
}
.colorbar-title { font-size: 0.72rem; color: var(--text); margin-bottom: 0.3rem; }
.colorbar-gradient { height: 10px; border-radius: 2px; border: 1px solid var(--border); }
.colorbar-scale {
    display: flex;
    justify-content: space-between;
    font-size: 0.64rem;
    color: var(--text-muted);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    margin-top: 0.2rem;
}
.colorbar-note { font-size: 0.62rem; color: var(--text-muted); margin-top: 0.25rem; }
.colorbar-note.warn { color: #f0b27a; }

/* IQA acceptability bands (Good / Acceptable / Poor) */
.colorbar-bands {
    display: flex;
    height: 12px;
    border-radius: 2px;
    overflow: hidden;
    border: 1px solid var(--border);
}
.colorbar-bands > span { flex: 1; }
.colorbar-bandlabels { display: flex; margin-top: 0.2rem; }
.colorbar-bandlabels > span {
    flex: 1;
    text-align: center;
    font-size: 0.6rem;
    color: var(--text-muted);
}

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

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

.mobile-toggle { display: none; }

.lang-toggle {
    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; }

    #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 { flex-wrap: wrap; padding: 0.6rem 0.85rem; gap: 0.6rem; }
    header h1 { font-size: 1rem; }
    header .subtitle { display: none; }
    header .data-status { font-size: 0.72rem; }

    button { padding: 0.5rem 0.85rem; font-size: 0.85rem; }
    .view-toggle button { padding: 0.45rem 0.9rem; }
    .source-item { padding: 0.55rem 0.7rem; }
    .source-name { font-size: 0.85rem; }

    .chart-tooltip { white-space: normal; max-width: 70vw; }
    .colorbar { min-width: 150px; }
}
