:root {
    color-scheme: light dark;

    /* Surfaces & ink — from the reference data-viz palette (light) */
    --page: #f9f9f7;
    --surface: #fcfcfb;
    --surface-2: #f2f1ed;
    --text-primary: #0b0b0b;
    --text-secondary: #52514e;
    --text-muted: #898781;
    --border: rgba(11, 11, 11, 0.10);
    --gridline: #e1e0d9;
    --axis: #c3c2b7;
    --accent: #2a78d6;

    --shadow: 0 1px 2px rgba(11, 11, 11, 0.06), 0 4px 16px rgba(11, 11, 11, 0.05);
    --radius: 14px;
    --radius-sm: 9px;
    --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --page: #0d0d0d;
        --surface: #1a1a19;
        --surface-2: #232320;
        --text-primary: #ffffff;
        --text-secondary: #c3c2b7;
        --text-muted: #898781;
        --border: rgba(255, 255, 255, 0.10);
        --gridline: #2c2c2a;
        --axis: #383835;
        --accent: #3987e5;
        --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 20px rgba(0, 0, 0, 0.35);
    }
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: var(--font);
    color: var(--text-primary);
    background: var(--page);
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3 {
    margin: 0;
    font-weight: 600;
    letter-spacing: -0.01em;
}

a {
    color: var(--accent);
    text-decoration: none;
}

a:hover:not(.leaflet-bar a) {
    text-decoration: underline;
}

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

.app-header {
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}

/* Shares the layout's max-width and horizontal padding so the title lines up
   with the sensor list below it. */
.header-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.4rem 1rem;
    max-width: 1400px;
    margin: 0 auto;
    padding: 0.9rem 1.4rem;
}

.gh-link {
    display: inline-flex;
    color: var(--text-secondary);
}

.gh-link:hover {
    color: var(--accent);
}

.app-header h1 {
    font-size: 1.25rem;
    font-weight: 600;
    white-space: nowrap;
}

.app-header .accent {
    color: var(--accent);
}

/* ---------- Layout ---------- */

.layout {
    display: grid;
    grid-template-columns: 300px 1fr;
    gap: 1.1rem;
    padding: 1.1rem 1.4rem;
    max-width: 1400px;
    margin: 0 auto;
    align-items: start;
}

@media (max-width: 860px) {
    .layout {
        grid-template-columns: 1fr;
    }
}

/* ---------- Sidebar / sensor list ---------- */

.sidebar {
    position: sticky;
    top: 1.1rem;
    min-width: 0; /* let the grid track shrink instead of stretching to the longest sensor name */
    display: flex;
    flex-direction: column;
    gap: 0.7rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.9rem;
    box-shadow: var(--shadow);
    max-height: calc(100vh - 2.2rem);
}

@media (max-width: 860px) {
    .sidebar {
        position: static;
        max-height: none;
        order: 1; /* below the detail/map/chart — temperature data comes first */
    }
}

.search-row input {
    width: 100%;
    padding: 0.55rem 0.7rem;
    font: inherit;
    font-size: 0.92rem;
    color: var(--text-primary);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    outline: none;
}

.search-row input:focus {
    border-color: var(--accent);
}

.sensor-list {
    list-style: none;
    margin: 0;
    padding: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

@media (max-width: 860px) {
    .sensor-list {
        max-height: 320px;
    }
}

.sensor-item {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.5rem 0.6rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 0.12s;
}

.sensor-item:hover {
    background: var(--surface-2);
}

.sensor-item.active {
    background: color-mix(in srgb, var(--accent) 16%, var(--surface));
}

.sensor-swatch {
    flex: 0 0 auto;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    box-shadow: 0 0 0 2px var(--surface);
}

.sensor-name {
    flex: 1 1 auto;
    min-width: 0; /* let the flex item shrink so ellipsis works and the row never forces page width */
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sensor-temp {
    flex: 0 0 auto;
    font-size: 0.9rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: var(--text-secondary);
}

.sensor-item.stale .sensor-temp {
    color: var(--text-muted);
}

.list-placeholder,
.list-empty {
    padding: 0.7rem 0.6rem;
    color: var(--text-muted);
    font-size: 0.88rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* ---------- Content column ---------- */

.content {
    display: flex;
    flex-direction: column;
    gap: 1.1rem;
    min-width: 0;
}

.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.4rem 1rem;
    padding: 0.8rem 1rem;
    border-bottom: 1px solid var(--border);
}

.panel-head h2 {
    font-size: 0.95rem;
    color: var(--text-secondary);
}

/* ---------- Detail panel ---------- */

.detail-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.2rem 1.3rem;
}

.detail-panel.stale {
    border-color: color-mix(in srgb, #d03b3b 40%, var(--border));
}

.detail-placeholder {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 7.5rem;
}

.detail-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 1rem;
    flex-wrap: wrap;
}

.detail-head h2 {
    font-size: 1.15rem;
}

.detail-meta {
    font-size: 0.82rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.stale-badge {
    color: #d03b3b;
    font-weight: 600;
}

.detail-body {
    display: flex;
    align-items: center;
    gap: 1.6rem 2rem;
    margin-top: 1.1rem;
}

.detail-body .hero {
    flex: 0 0 auto;
    margin: 0;
}

.detail-body .metric-grid {
    flex: 1 1 auto;
}

.hero-block {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.hero {
    display: flex;
    align-items: flex-end;
    gap: 0.9rem;
}

.hero-trend {
    font-size: 1rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    padding-bottom: 0.45rem;
}

.hero-trend.up {
    color: #d0663b;
}

.hero-trend.down {
    color: var(--accent);
}

.hero-caption {
    font-size: 0.82rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.hero-value {
    font-size: 3.6rem;
    font-weight: 600;
    line-height: 1;
    letter-spacing: -0.02em;
}

.hero-unit {
    font-size: 1.3rem;
    color: var(--text-secondary);
    padding-bottom: 0.35rem;
}

.hero-dot {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    align-self: center;
    box-shadow: 0 0 0 3px var(--surface);
}

.metric-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
    gap: 0.7rem;
}

.metric {
    display: flex;
    flex-direction: column;
    background: var(--surface-2);
    border-radius: var(--radius-sm);
    padding: 0.7rem 0.8rem;
}

.metric-label {
    font-size: 0.78rem;
    color: var(--text-muted);
}

.metric-value {
    font-size: 1.35rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    margin-top: auto; /* pin to the bottom so values align across 1- and 2-line labels */
    padding-top: 0.15rem;
}

.metric-value .unit {
    font-size: 0.85rem;
    font-weight: 400;
    color: var(--text-secondary);
    margin-left: 0.15rem;
}

@media (max-width: 860px) {
    .header-inner {
        padding: 0.8rem 1.4rem;
    }

    .app-header h1 {
        font-size: 1.1rem;
    }

    .detail-panel {
        padding: 1rem;
    }

    .detail-body {
        flex-direction: column;
        align-items: stretch;
        gap: 0.85rem;
        margin-top: 0.6rem;
    }

    .hero {
        align-items: baseline;
        gap: 0.4rem;
        margin: 0.1rem 0 0;
    }

    .hero-value {
        font-size: 3.4rem;
    }

    .hero-unit {
        padding-bottom: 0;
    }

    .metric-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 0.5rem;
    }

    .metric {
        padding: 0.55rem 0.6rem;
    }

    .metric-label {
        overflow-wrap: anywhere;
        hyphens: auto;
    }

    .metric-value {
        font-size: 1.1rem;
    }
}

/* ---------- Map ---------- */

.map {
    position: relative;
    height: 420px;
    width: 100%;
    background: var(--surface-2);
}

.map-tools {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem 0.9rem;
}

/* Always-visible scale below the map, explaining the marker/list/heat colors. */
.scale-legend {
    padding: 0.7rem 1rem 0.8rem;
    border-top: 1px solid var(--border);
}

.scale-legend-bar {
    --dot: 14px;
    position: relative;
    height: 10px;
    border-radius: 3px;
}

.scale-legend-dot {
    position: absolute;
    top: 50%;
    width: var(--dot);
    height: var(--dot);
    transform: translateY(-50%);
    box-sizing: border-box;
    border-radius: 50%;
    border: 2px solid var(--surface);
    /* A solid 2px ring (not a thin sub-pixel one) so the outline stays crisp at
       the top and bottom tangents under fractional browser zoom. */
    box-shadow: 0 0 0 2px var(--text-primary);
}

.scale-legend-ticks {
    position: relative;
    height: 0.9rem;
    margin-top: 0.3rem;
}

.scale-legend-ticks span {
    position: absolute;
    transform: translateX(-50%);
    font-size: 0.72rem;
    font-variant-numeric: tabular-nums;
    color: var(--text-secondary);
    white-space: nowrap;
}

.map-tools-label {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.marker-dot {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 2px solid #fff;
    /* A dark hairline just outside the white ring keeps the dot crisp on any
       background — pale fills no longer melt into the map or the overlay. */
    box-shadow: 0 0 0 1.5px rgba(0, 0, 0, 0.55), 0 1px 4px rgba(0, 0, 0, 0.5);
}

.marker-dot.selected {
    width: 22px;
    height: 22px;
    border-width: 3px;
    box-shadow: 0 0 0 3px var(--accent), 0 0 0 4.5px rgba(0, 0, 0, 0.55), 0 1px 5px rgba(0, 0, 0, 0.5);
    z-index: 1000;
}

.leaflet-popup-content {
    font: inherit;
    font-size: 0.85rem;
    margin: 0.6rem 1.6rem 0.6rem 0.8rem; /* room for the close button on the right */
}

.leaflet-popup-close-button {
    width: 28px !important;
    height: 28px !important;
    font-size: 22px !important;
    padding: 2px 0 0 0 !important;
}

/* ---------- Chart ---------- */

.chart {
    padding: 0.6rem 0.4rem 0.2rem;
    position: relative;
}

.chart svg {
    display: block;
    width: 100%;
    height: auto;
    overflow: visible;
}

.chart .axis-label {
    fill: var(--text-muted);
    font-size: 11px;
    font-variant-numeric: tabular-nums;
}

.chart .comfort-label {
    fill: var(--text-muted);
    font-size: 10px;
    letter-spacing: 0.02em;
    paint-order: stroke fill;
    stroke: var(--surface);
    stroke-width: 3px;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.chart .comfort-line {
    stroke: var(--text-muted);
    stroke-width: 1;
    stroke-dasharray: 2 4;
    opacity: 0.6;
    shape-rendering: crispEdges;
    vector-effect: non-scaling-stroke;
}

/* The chart SVG scales with the viewBox, so its text shrinks on narrow
   screens; enlarge the user-unit font sizes to keep it legible. */
@media (max-width: 860px) {
    .chart .axis-label {
        font-size: 15px;
    }

    .chart .comfort-label {
        font-size: 14px;
    }
}

@media (max-width: 520px) {
    .chart .axis-label {
        font-size: 20px;
    }

    .chart .comfort-label {
        font-size: 17px;
    }
}

.chart .grid-line {
    stroke: var(--gridline);
    stroke-width: 1;
    shape-rendering: crispEdges;
    vector-effect: non-scaling-stroke;
}

.chart .axis-line {
    stroke: var(--axis);
    stroke-width: 1;
    shape-rendering: crispEdges;
    vector-effect: non-scaling-stroke;
}

.chart .series-line {
    fill: none;
    stroke-width: 2;
    stroke-linejoin: round;
    stroke-linecap: round;
}

/* City-wide average, kept muted so the sensor's own line stays dominant. */
.chart .reference-line {
    fill: none;
    stroke: var(--text-muted);
    stroke-width: 1.5;
    stroke-dasharray: 4 3;
    stroke-linejoin: round;
    opacity: 0.55;
}

.chart-legend {
    display: flex;
    gap: 1rem;
    padding: 0.3rem 0.4rem 0.1rem;
    font-size: 0.72rem;
    color: var(--text-muted);
}

.chart-legend .key {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}

.chart-legend .key::before {
    content: "";
    width: 18px;
    border-top: 2px solid var(--accent);
}

.chart-legend .key-ref::before {
    border-top-style: dashed;
    border-top-color: var(--text-muted);
}

.tooltip-ref {
    color: var(--text-muted);
    font-weight: 400;
}

.chart-empty,
.chart-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    min-height: 240px;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.chart-crosshair {
    stroke: var(--axis);
    stroke-width: 1;
    stroke-dasharray: 3 3;
    shape-rendering: crispEdges;
    vector-effect: non-scaling-stroke;
}

.chart-tooltip {
    position: absolute;
    pointer-events: none;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    padding: 0.4rem 0.6rem;
    font-size: 0.8rem;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    transform: translate(-50%, calc(-100% - 12px));
    opacity: 0;
    transition: opacity 0.1s;
}

.range-controls,
.segmented {
    display: inline-flex;
    gap: 2px;
    background: var(--surface-2);
    border-radius: var(--radius-sm);
    padding: 2px;
}

.range-controls button,
.segmented button {
    font: inherit;
    font-size: 0.82rem;
    white-space: nowrap;
    padding: 0.3rem 0.6rem;
    border: none;
    border-radius: 7px;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: background 0.1s, color 0.1s;
}

.range-controls button:hover:not(.active),
.segmented button:hover:not(.active) {
    background: var(--surface);
    color: var(--text-primary);
}

.range-controls button.active,
.segmented button.active {
    background: var(--surface);
    color: var(--text-primary);
    box-shadow: var(--shadow);
}

/* The list sort spans the sidebar width, one third per option. */
.sort-controls {
    display: flex;
}

.sort-controls button {
    flex: 1 1 0;
    text-align: center;
}

/* Metric and range pickers share the chart header, wrapping on narrow screens. */
.chart-controls {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.4rem 0.6rem;
}

/* ---------- Footer ---------- */

.app-footer {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    justify-content: center;
    padding: 1.4rem;
    color: var(--text-muted);
    font-size: 0.82rem;
}

/* ---------- Loader (three pulsing dots) ---------- */

.loader {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    vertical-align: middle;
}

.loader i {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--accent);
    animation: loader-bounce 0.9s ease-in-out infinite;
}

.loader i:nth-child(2) {
    animation-delay: 0.15s;
}

.loader i:nth-child(3) {
    animation-delay: 0.3s;
}

.loader-lg {
    gap: 7px;
}

.loader-lg i {
    width: 10px;
    height: 10px;
}

@keyframes loader-bounce {
    0%, 100% { opacity: 0.3; transform: translateY(2px) scale(0.8); }
    40% { opacity: 1; transform: translateY(-2px) scale(1); }
}

.error {
    color: #d03b3b;
    font-size: 0.9rem;
}
