/* ==========================================================================
   Outpost base styles
   Everything visual comes from the theme tokens (CSS variables) below.
   A theme is just a file in web/themes/ that sets those tokens.
   The values here are the fallbacks used if no theme file loads.
   See docs/themes.md.
   ========================================================================== */

/* ---- Fonts ---------------------------------------------------------------
   Share Tech Mono (SIL Open Font License) is bundled from assets/fonts/.
   If the file is missing, the stacks fall back to Courier New.             */
@font-face {
    font-family: "Share Tech Mono";
    src: url("assets/fonts/ShareTechMono-Regular.ttf") format("truetype");
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}

:root {
    --bg: #071007;
    --bg-glow: rgba(80, 120, 40, 0.22);
    --panel: rgba(16, 36, 12, 0.92);
    --cell-bg: rgba(5, 14, 5, 0.65);

    --text: #9cff57;
    --text-dim: #6fad3e;
    --accent: #ffb000;
    --warn: #ffb000;
    --danger: #ff5a36;
    --danger-bg: rgba(80, 20, 10, 0.6);

    --border: #6fad3e;
    --border-width: 2px;
    --radius: 0;

    --text-glow: 0 0 6px rgba(156, 255, 87, 0.55);
    --icon-glow: drop-shadow(0 0 5px rgba(156, 255, 87, 0.55));
    --scanlines: 1;

    --font-display: "Share Tech Mono", "Courier New", monospace;
    --font-body: "Share Tech Mono", "Courier New", monospace;
    --title-spacing: 0.14em;
    --title-logo: 1;

    /* The map panel (web/map.js). A theme may set its own; these follow its colors */
    --map-land: var(--bg);
    --map-water: color-mix(in srgb, var(--text) 16%, var(--bg));
    --map-road: color-mix(in srgb, var(--accent) 55%, var(--bg));
    --map-border: var(--text-dim);
    --map-label: var(--text);
    --map-halo: var(--bg);
    --map-marker: var(--accent);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

[hidden] { display: none !important; }

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    background:
        radial-gradient(circle at center, var(--bg-glow), transparent 65%),
        var(--bg);
    color: var(--text);
    font-family: var(--font-body);
    text-shadow: var(--text-glow);
}

/* Hide the page until the theme is applied (avoids a flash of the wrong theme).
   The animation reveals it after 2 seconds no matter what, as a safety net. */
body.theme-loading {
    visibility: hidden;
    animation: theme-reveal 0s 2s forwards;
}

@keyframes theme-reveal {
    to { visibility: visible; }
}

/* Scanline overlay: rows lit faintly in the theme's text color with darker gaps between them,
   and the screen edges a little darker, like a picture tube. --scanlines sets the strength
   (0 turns it off). */
body::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 40; /* above the content (see "Layers" below) */
    pointer-events: none;
    opacity: var(--scanlines);
    background:
        radial-gradient(ellipse 75% 70% at center, transparent 55%, rgba(0, 0, 0, 0.4) 100%),
        repeating-linear-gradient(
            to bottom,
            color-mix(in srgb, var(--text) 6%, transparent) 0 2px,
            rgba(0, 0, 0, 0.3) 2px 4px
        );
}

/* Rolling band: a soft band of the theme's text color drifting down the screen, like a picture
   tube's refresh. It appears wherever the scanlines do, at the same strength, and --scan-roll sets
   its speed (one trip down the screen; "none" stops it). It moves with transform, which is cheap
   for the browser to draw, even on slow wall tablets. */
.scan-roll {
    position: fixed;
    inset: 0;
    z-index: 40; /* with the scanlines */
    overflow: hidden;
    pointer-events: none;
    opacity: var(--scanlines);
}
.scan-roll::before {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    height: 30%;
    background: linear-gradient(
        to bottom,
        transparent,
        color-mix(in srgb, var(--text) 5%, transparent) 45%,
        color-mix(in srgb, var(--text) 8%, transparent) 50%,
        transparent
    );
    transform: translateY(-100%);
    animation: scan-roll var(--scan-roll, 9s) linear infinite;
}
@keyframes scan-roll {
    from { transform: translateY(-100%); }
    to { transform: translateY(334%); } /* the band is 30% of the screen, so this is just past the bottom */
}

/* Layers, bottom to top: page content, scanlines and the rolling band (40), the .frame decoration (50), the theme
   switcher (100), and the night dimming (9999). */

/* An empty full-screen layer for a theme to draw a bezel or frame on (with ::before and ::after, or
   a background). It never catches taps. */
.frame {
    position: fixed;
    inset: 0;
    z-index: 50;
    pointer-events: none;
}

/* Day/night dimming: a black layer whose opacity is set from --dim (1 = full brightness).
   The fade is only enabled after the first paint so a reload at night doesn't visibly fade in. */
#dim-overlay {
    position: fixed;
    inset: 0;
    z-index: 9999;
    pointer-events: none;
    background: #000;
    opacity: calc(1 - var(--dim, 1));
}

#dim-overlay.animated { transition: opacity 20s linear; }

main {
    width: min(1200px, 100vw);
    margin: 0 auto;
    /* --frame-inset (optional, set by a theme with a .frame bezel) keeps content inside the bezel;
       --frame-inset-bottom (optional) is for a bezel with a deeper bottom edge */
    --pad-base: 28px;
    --pad: calc(var(--pad-base) + var(--frame-inset, 0px));
    --pad-bottom: calc(var(--pad-base) + var(--frame-inset-bottom, var(--frame-inset, 0px)));
    /* Keep content clear of notches and rounded screen corners */
    padding-top: max(var(--pad), env(safe-area-inset-top));
    padding-right: max(var(--pad), env(safe-area-inset-right));
    padding-bottom: max(var(--pad-bottom), env(safe-area-inset-bottom));
    padding-left: max(var(--pad), env(safe-area-inset-left));
}

/* Fit to screen (fit.js, turned on by a theme's --fit-screen: 1): <main> is scaled by --fit-scale
   so the whole page fits without scrolling. Widths and padding are divided by the scale so they
   stay the same size on screen, and the page is centered vertically. */
:root[data-fit="on"] body {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    justify-content: center;
}
:root[data-fit="on"] main {
    zoom: var(--fit-scale, 1);
    /* --fit-width (optional, set by a theme): how wide the page may get; the default is the usual */
    width: calc(min(var(--fit-width, 1200px), 100vw) / var(--fit-scale, 1));
    min-height: 0;
    padding-top: calc(max(var(--pad), env(safe-area-inset-top)) / var(--fit-scale, 1));
    padding-right: calc(max(var(--pad), env(safe-area-inset-right)) / var(--fit-scale, 1));
    padding-bottom: calc(max(var(--pad-bottom), env(safe-area-inset-bottom)) / var(--fit-scale, 1));
    padding-left: calc(max(var(--pad), env(safe-area-inset-left)) / var(--fit-scale, 1));
}

.panel {
    position: relative; /* so a theme can place decorations with ::before and ::after */
    border: var(--border-width) solid var(--border);
    border-radius: var(--radius);
    background: var(--panel);
    padding: 18px;
    margin-bottom: 22px;
}

h2 { margin-top: 0; letter-spacing: 0.1em; }

.header { text-align: center; }

/* The title is the logo (--title-logo: 1) or the word OUTPOST in the theme's font (--title-logo: 0).
   The text stays in the page either way, so screen readers always read it. */
.title {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 0;
    font-family: var(--font-display);
    font-weight: bold;
}

/* A filter is applied before a mask, so the glow goes on a wrapper (like .wx-glow) */
.title-logo-glow { filter: var(--icon-glow); }

.title-logo {
    display: block;
    height: calc(var(--title-logo) * clamp(3.5rem, 11vw, 7.5rem));
    aspect-ratio: 148 / 44;
    background-color: currentColor;
    /* A theme can use its own logo with a rule here; the url is relative to its theme.css */
    -webkit-mask: url("assets/logo/wordmark.svg") center / contain no-repeat;
    mask: url("assets/logo/wordmark.svg") center / contain no-repeat;
}

.title-text {
    font-size: calc((1 - var(--title-logo)) * clamp(3rem, 9vw, 7rem));
    letter-spacing: var(--title-spacing);
}

.subtitle {
    color: var(--text-dim);
    font-size: clamp(1rem, 2.5vw, 1.5rem);
    letter-spacing: 0.08em;
}

.location {
    margin-top: 14px;
    color: var(--accent);
    font-size: clamp(1.8rem, 4vw, 3rem);
}

.current {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 24px;
    align-items: center;
}

.current-main {
    display: flex;
    align-items: center;
    gap: 20px;
}

/* Weather icons: an SVG used as a mask, filled with the current text color */
.wx-glow { filter: var(--icon-glow); }

.wx-icon {
    display: block;
    background-color: currentColor;
    -webkit-mask: var(--icon, none) center / contain no-repeat;
    mask: var(--icon, none) center / contain no-repeat;
}

.current-icon { width: clamp(80px, 14vw, 160px); aspect-ratio: 1; }

.temperature {
    font-family: var(--font-display);
    font-size: clamp(4rem, 12vw, 9rem);
    line-height: 0.9;
}

.condition {
    color: var(--accent);
    font-size: clamp(1.7rem, 4vw, 3.2rem);
}

/* Air quality (#88), under the condition: "Air quality 34 Fair", then the scale and pollen (each
   .air-part kept on one line). Fitted to a screen, it takes the width the temperature and condition
   give it (contain: inline-size) and wraps inside it, so it never widens that column, and in a wide
   layout it sits beside the readings, adding no height. */
.air-quality {
    margin-top: 0.6em;
    font-size: 1.05rem;
    line-height: 1.3;
}
.air-quality .air-label { color: var(--text-dim); }
.air-note {
    display: block;
    color: var(--text-dim);
    font-size: 0.85em;
}
.air-part { white-space: nowrap; }
:where(:root[data-fit="on"]) .air-quality { contain: inline-size; }

.details {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
}

.detail-box {
    position: relative;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px;
    background: var(--cell-bg);
}

/* Second lines in a reading box: gusts under the wind (.wind-gust), the dew point under the humidity
   (.dew-point), and how old the readings are under Updated (.reading-time). Each is a <small>, so themes
   that size every span in a reading box as the big value leave it alone. Themes can restyle or hide
   them all with .reading-note, or one by its own class. */
.reading-note {
    display: block;
    margin-top: 2px;
    font-size: 0.8em;
    opacity: 0.85;
}

/* Sensor and power panels (sensors.js): Home Assistant readings as tiles like the weather readings
   above. #homePanels holds both, so the screen layout places them as one. */
.sensor-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 12px;
}
.sensor-value { overflow-wrap: anywhere; }
/* Power and sensors side by side when there is room, one above the other when not. The power
   tiles (at most four) stay on one row. */
.home-panels {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 440px), 1fr));
    column-gap: 22px;
}
.power-list { grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); }
/* The battery's charge as a bar along the bottom of its tile (a theme with gauges can hide it and
   use --value and data-gauge like any other gauge) */
.detail-box .power-bar {
    display: block;
    height: 6px;
    margin-top: 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}
.detail-box .power-bar::after {
    content: "";
    display: block;
    height: 100%;
    width: calc(var(--value, 0) * 100%);
    background: var(--accent);
}
.detail-box[data-missing] .power-bar { display: none; }

/* Hourly forecast (hourly.js), at the top of the forecast panel: one box with a line of hours in it,
   as many as fit (the rest are on rows of no height, so they never take room) */
.hourly-strip { margin-bottom: 12px; }
.hourly {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(56px, 1fr));
    grid-template-rows: auto;
    grid-auto-rows: 0;
    overflow: hidden;
    padding: 4px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--cell-bg);
}
.hourly-hour {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    min-width: 0;
    padding: 4px 2px;
    text-align: center;
}
.hourly-hour[data-now] .hourly-time { color: var(--accent); }
.hourly-time { font-size: 0.85em; color: var(--text-dim); white-space: nowrap; }
.hourly-icon { width: 30px; aspect-ratio: 1; margin: 2px 0; }
.hourly-temp { font-size: 1.15em; white-space: nowrap; }
.hourly-rain { font-size: 0.8em; color: var(--text-dim); white-space: nowrap; }

.forecast {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 12px;
}

.forecast-card {
    position: relative;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
    text-align: center;
    background: var(--cell-bg);
}

.forecast-icon { width: 64px; aspect-ratio: 1; margin: 6px auto; }
.forecast-extra { display: block; }
.forecast-amount { color: var(--text-dim); white-space: nowrap; }

.forecast-condition {
    color: var(--accent);
    min-height: 3em;
}

.alert-item { margin-bottom: 10px; }
.alert-item.severity-moderate { border-color: var(--warn); color: var(--warn); }
.alert-item.severity-severe,
.alert-item.severity-extreme { border-color: var(--danger); color: var(--danger); }
.panel.alert-active { border-color: var(--danger); }

.alert {
    display: none;
    border-color: var(--danger);
    color: var(--danger);
    background: var(--danger-bg);
    text-align: center;
    font-size: 1.4rem;
}

.alert.visible { display: block; }

/* One-line note shown in place of the Alerts panel when it is hidden */
.status-line {
    text-align: right;
    color: var(--text-dim);
    margin: -8px 0 14px;
}

footer {
    text-align: right;
    color: var(--text-dim);
}

/* ---- Theme switcher ------------------------------------------------------- */
.theme-button {
    position: fixed;
    left: max(12px, env(safe-area-inset-left));
    bottom: max(12px, env(safe-area-inset-bottom));
    z-index: 100;
    width: 44px;
    height: 44px;
    padding: 0;
    display: grid;
    place-items: center;
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: linear-gradient(var(--panel), var(--panel)), var(--bg);
    cursor: pointer;
    /* Faint until used, so a wall display stays clean; still easy to tap */
    opacity: 0.22;
    transition: opacity 0.2s;
}

.theme-button:hover,
.theme-button:focus-visible,
.theme-button[aria-expanded="true"] { opacity: 1; }

.theme-panel {
    position: fixed;
    left: max(12px, env(safe-area-inset-left));
    bottom: calc(max(12px, env(safe-area-inset-bottom)) + 52px);
    z-index: 100;
    width: min(280px, calc(100vw - 24px));
    max-height: 70vh;
    max-height: 70dvh;
    overflow-y: auto;
    padding: 12px;
    /* Opaque: the panel tint over the solid page color, so the content behind never shows through */
    background: linear-gradient(var(--panel), var(--panel)), var(--bg);
    color: var(--text);
    border: var(--border-width) solid var(--border);
    border-radius: var(--radius);
}

.theme-panel-title { letter-spacing: 0.1em; margin-bottom: 8px; color: var(--text-dim); }

.theme-option,
.theme-reset {
    display: block;
    width: 100%;
    min-height: 44px;
    padding: 8px 10px;
    text-align: left;
    font: inherit;
    color: var(--text);
    text-shadow: var(--text-glow);
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius);
    cursor: pointer;
}

.theme-option:hover,
.theme-option:focus-visible,
.theme-reset:hover,
.theme-reset:focus-visible { border-color: var(--border); outline: none; }

.theme-option[aria-checked="true"] { background: var(--cell-bg); border-color: var(--border); }
.theme-option[aria-checked="true"]::before { content: "> "; }

.theme-reset { margin-top: 6px; color: var(--accent); }
.theme-note { margin-top: 8px; font-size: 0.85rem; color: var(--text-dim); }

/* LOCATION (web/place-panel.js): home and saved places, each with a button to forget it, then
   the search box and what it found. Set apart from the THEME list below it. */
.place-section { margin-bottom: 14px; }
.place-row { display: flex; align-items: stretch; gap: 4px; }
.place-row .place-option { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.place-forget {
    flex: none;
    width: 44px;
    min-height: 44px;
    font: inherit;
    color: var(--text-dim);
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius);
    cursor: pointer;
}
.place-forget:hover,
.place-forget:focus-visible { color: var(--text); border-color: var(--border); outline: none; }

.my-location { margin-top: 4px; color: var(--accent); }
.my-location::before { content: "@ "; }
.my-location:disabled { opacity: 0.6; cursor: progress; }
.place-search { display: flex; gap: 6px; margin-top: 8px; }
.place-query {
    flex: 1;
    min-width: 0;
    min-height: 44px;
    padding: 8px 10px;
    font: inherit;
    /* 16px or more, so phones do not zoom in when the box is tapped */
    font-size: max(16px, 1em);
    color: var(--text);
    background: var(--cell-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.place-query::placeholder { color: var(--text-dim); opacity: 1; }
.place-query:focus-visible { outline: 1px solid var(--accent); outline-offset: 1px; }
.place-go {
    flex: none;
    min-height: 44px;
    padding: 8px 12px;
    font: inherit;
    color: var(--accent);
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
}
.place-go:hover,
.place-go:focus-visible { background: var(--cell-bg); outline: none; }
.place-results { margin-top: 6px; }
.place-result { overflow-wrap: anywhere; }
.place-result::before { content: "+ "; color: var(--text-dim); }

/* The reduced-effects and 24-hour clock toggles are checkboxes, not themes: [x] or [ ], set apart
   from the list */
.effects-toggle { margin-top: 8px; }
.theme-option.effects-toggle::before,
.theme-option.theme-check::before { content: "[ ] "; }
.theme-option.effects-toggle[aria-checked="true"]::before,
.theme-option.theme-check[aria-checked="true"]::before { content: "[x] "; }

/* ---- Map panel (web/map.js, OUTPOST_MAP) ---------------------------------------
   A fixed map drawn on a canvas in the --map-* colors above, with the credit the tile
   server asks for in the bottom-right corner. Wide on large screens, squarer on phones. */
.map-frame {
    position: relative;
    width: 100%;
    aspect-ratio: 2 / 1;
    max-height: 420px;
    overflow: hidden;
    background: var(--map-land);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
@media (max-width: 600px) { .map-frame { aspect-ratio: 4 / 3; } }
.map-canvas { display: block; width: 100%; height: 100%; font-family: var(--font-body); }
.map-credits {
    position: absolute;
    right: 0;
    bottom: 0;
    display: flex;
}
.map-credit {
    padding: 2px 6px;
    font-size: 0.75rem;
    color: var(--text);
    text-decoration: none;
    background: var(--bg);
    background: color-mix(in srgb, var(--bg) 80%, transparent);
}
.map-credit:hover,
.map-credit:focus-visible { text-decoration: underline; outline: none; }
/* When the radar on the map is from, in the place's time zone */
.map-radar-time {
    position: absolute;
    left: 0;
    top: 0;
    padding: 2px 6px;
    font-size: 0.75rem;
    color: var(--text);
    background: var(--bg);
    background: color-mix(in srgb, var(--bg) 80%, transparent);
}
.map-note {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 12px;
    text-align: center;
    color: var(--text-dim);
}

/* ---- Reduced effects ---------------------------------------------------------
   web/effects.js sets data-effects="reduced" (OUTPOST_EFFECTS, the viewer's toggle, or the
   device's reduced-motion setting). Scanlines, glow, and animation go in every theme, which
   also helps slow wall tablets. A theme's own effects should check it too (docs/themes.md).
   These win over a theme's :root tokens because the selector is more specific. */
:root[data-effects="reduced"] {
    --scanlines: 0;
    --text-glow: none;
    --icon-glow: none;
}

/* body keeps its reveal animation (the safety net in body.theme-loading), and the night
   dimming keeps its slow fade */
:root[data-effects="reduced"] *:not(body):not(#dim-overlay),
:root[data-effects="reduced"] *::before,
:root[data-effects="reduced"] *::after {
    animation: none !important;
    transition: none !important;
}

/* ---- Small screens ----------------------------------------------------------
   Tablets and phones: stack the current conditions, use three columns of detail
   boxes, and keep the forecast to one row of five cards.                      */
@media (max-width: 850px) {
    main { --pad-base: 14px; }

    .panel { padding: 14px; margin-bottom: 14px; }
    .title { font-size: clamp(2.6rem, 12vw, 5rem); }
    .current { grid-template-columns: 1fr; gap: 16px; text-align: center; }
    .current-main { justify-content: center; }
    .temperature { font-size: clamp(3.6rem, 16vw, 7rem); }
    .details { gap: 8px; }
    .detail-box { padding: 8px 10px; overflow-wrap: break-word; }
    .forecast { gap: 8px; }
    .forecast-card { padding: 10px 6px; }
    .forecast-icon { width: 48px; }
}

/* Phones: forecast days become compact rows instead of five narrow cards */
@media (max-width: 600px) {
    .title { letter-spacing: 0.08em; }
    .current-icon { width: clamp(64px, 20vw, 96px); }
    .detail-box { font-size: 0.9rem; }

    .forecast { grid-template-columns: 1fr; }

    .forecast-card {
        display: grid;
        grid-template-columns: 52px 1fr auto;
        grid-template-areas:
            "icon day   temps"
            "icon cond  extra";
        column-gap: 12px;
        row-gap: 2px;
        align-items: center;
        text-align: left;
        padding: 8px 12px;
    }

    .forecast-icon-wrap { grid-area: icon; }
    .forecast-day { grid-area: day; white-space: nowrap; }
    .forecast-condition { grid-area: cond; min-height: 0; }
    .forecast-temps { grid-area: temps; text-align: right; white-space: nowrap; }
    .forecast-extra { grid-area: extra; justify-self: end; white-space: nowrap; font-size: 0.85em; color: var(--text-dim); }
    .forecast-uv { display: none; } /* UV is shown in the current conditions; keep the rows short */
    .forecast-icon { width: 44px; margin: 0; }
}

/* Very small phones: slightly smaller text so labels such as "Visibility" fit */
@media (max-width: 400px) {
    .detail-box { font-size: 0.8rem; padding: 6px; }
}

/* Phones turned sideways: very little height, so keep the header short and use two columns */
@media (orientation: landscape) and (max-height: 500px) {
    .title { font-size: 2.4rem; }
    .header .location { margin-top: 4px; font-size: 1.4rem; }
    .current { grid-template-columns: 1fr 1fr; text-align: left; }
    .current-main { justify-content: flex-start; }
    .temperature { font-size: clamp(3rem, 12vh, 5rem); }
}

/* Tall screens (portrait tablets and monitors): scale everything up and center it vertically */
@media (orientation: portrait) and (min-width: 700px) {
    html { font-size: clamp(16px, 2vw, 30px); }
    main { min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; justify-content: center; }
}

/* ---- Screen layout ------------------------------------------------------------
   While the page is fitted to the screen (a tablet or wall screen), it is laid out
   for one screen instead of a long page. The header takes one line; in portrait the map takes the
   height left over; in landscape, with the map on, the current conditions and forecast go on the
   left and the map and alerts on the right. Fit to screen then scales the result to fill the screen.
   The selectors are in :where(), so a theme's own rules for these elements still win. */
:where(:root[data-fit="on"]) .header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 4px 24px;
    padding-block: 12px;
    text-align: left;
}
:where(:root[data-fit="on"]) .title { align-items: flex-start; }
:where(:root[data-fit="on"]) .title-logo { height: calc(var(--title-logo) * 3.6rem); }
:where(:root[data-fit="on"]) .title-text { font-size: calc((1 - var(--title-logo)) * 3.4rem); }
:where(:root[data-fit="on"]) .header .location { margin-top: 0; font-size: 2.4rem; }
:where(:root[data-fit="on"]) .header .subtitle { order: 3; flex-basis: 100%; }

/* The map fills the space it is given (at least --map-min-height) */
:where(:root[data-fit="on"]) .map-panel { display: flex; flex-direction: column; }
:where(:root[data-fit="on"]) .map-frame {
    flex: 1 1 auto;
    aspect-ratio: auto;
    max-height: none;
    min-height: var(--map-min-height, 180px);
}
/* Out of the flow, so the size the map was last drawn at never holds the frame open */
:where(:root[data-fit="on"]) .map-canvas { position: absolute; inset: 0; }

/* Current conditions: the readings beside the temperature when there is room, under it when not.
   Sizes are fixed rather than following the screen width, because the page is scaled to fit. */
:where(:root[data-fit="on"]) .current {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 16px 24px;
    text-align: left;
}
:where(:root[data-fit="on"]) .current-main { flex: 0 0 auto; justify-content: flex-start; }
:where(:root[data-fit="on"]) .details { flex: 1 1 var(--details-min-width, 480px); }
:where(:root[data-fit="on"]) .details { grid-template-columns: repeat(3, minmax(0, 1fr)); }
:where(:root[data-fit="on"]) .detail-box { overflow-wrap: break-word; }
:where(:root[data-fit="on"]) .current-icon { width: 120px; }
:where(:root[data-fit="on"]) .temperature { font-size: 7rem; }
:where(:root[data-fit="on"]) .condition { font-size: 2.4rem; }

/* Portrait: one column as tall as the screen, the map taking what is left */
@media (orientation: portrait) {
    :where(:root[data-fit="on"]) main {
        display: flex;
        flex-direction: column;
        min-height: calc(100dvh / var(--fit-scale, 1));
    }
    :where(:root[data-fit="on"]) .map-panel { flex: 1 1 auto; }
}

/* Landscape with the map, the sensors or power (#homePanels), or the alerts shown: two columns, the
   current conditions, forecast, and power on the left, and on the right the map, then the sensors,
   then the alerts (whichever are shown). The map's row takes the height the left column has to
   spare, so the map fills it. A hidden panel's row has no height, so power needs no layouts of its own. */
@media (orientation: landscape) {
    :where(:root[data-fit="on"]) main:has(> #mapPanel:not([hidden])),
    :where(:root[data-fit="on"]) main:has(> #homePanels:not([hidden])),
    :where(:root[data-fit="on"]) main:has(> #alertsPanel:not([hidden])) {
        display: grid;
        grid-template-columns: minmax(0, 3fr) minmax(0, 2fr);
        column-gap: 22px;
        align-content: start;
    }
    /* Map, sensors, and alerts */
    :where(:root[data-fit="on"]) main:has(> #mapPanel:not([hidden])):has(> #homePanels:not([hidden])):has(> #alertsPanel:not([hidden])) {
        grid-template-areas:
            "header   header"
            "banner   banner"
            "current  map"
            "forecast map"
            "forecast sensors"
            "forecast alerts"
            "power    alerts"
            "status   status"
            "footer   footer";
        grid-template-rows: auto auto auto 1fr auto auto auto auto auto;
    }
    /* Map and sensors */
    :where(:root[data-fit="on"]) main:has(> #mapPanel:not([hidden])):has(> #homePanels:not([hidden])):has(> #alertsPanel[hidden]) {
        grid-template-areas:
            "header   header"
            "banner   banner"
            "current  map"
            "forecast map"
            "forecast sensors"
            "power    sensors"
            "status   status"
            "footer   footer";
        grid-template-rows: auto auto auto 1fr auto auto auto auto;
    }
    /* Map and alerts */
    :where(:root[data-fit="on"]) main:has(> #mapPanel:not([hidden])):has(> #homePanels[hidden]):has(> #alertsPanel:not([hidden])) {
        grid-template-areas:
            "header   header"
            "banner   banner"
            "current  map"
            "forecast map"
            "forecast alerts"
            "status   status"
            "footer   footer";
        grid-template-rows: auto auto auto 1fr auto auto auto;
    }
    /* Map */
    :where(:root[data-fit="on"]) main:has(> #mapPanel:not([hidden])):has(> #homePanels[hidden]):has(> #alertsPanel[hidden]) {
        grid-template-areas:
            "header   header"
            "banner   banner"
            "current  map"
            "forecast map"
            "status   status"
            "footer   footer";
        grid-template-rows: auto auto auto 1fr auto auto;
    }
    /* Sensors and alerts */
    :where(:root[data-fit="on"]) main:has(> #mapPanel[hidden]):has(> #homePanels:not([hidden])):has(> #alertsPanel:not([hidden])) {
        grid-template-areas:
            "header   header"
            "banner   banner"
            "current  sensors"
            "forecast alerts"
            "power    alerts"
            "status   status"
            "footer   footer";
        grid-template-rows: auto auto auto auto auto auto auto;
    }
    /* Sensors */
    :where(:root[data-fit="on"]) main:has(> #mapPanel[hidden]):has(> #homePanels:not([hidden])):has(> #alertsPanel[hidden]) {
        grid-template-areas:
            "header   header"
            "banner   banner"
            "current  sensors"
            "forecast sensors"
            "power    sensors"
            "status   status"
            "footer   footer";
        grid-template-rows: auto auto auto auto auto auto auto;
    }
    /* Alerts */
    :where(:root[data-fit="on"]) main:has(> #mapPanel[hidden]):has(> #homePanels[hidden]):has(> #alertsPanel:not([hidden])) {
        grid-template-areas:
            "header   header"
            "banner   banner"
            "current  alerts"
            "forecast alerts"
            "status   status"
            "footer   footer";
        grid-template-rows: auto auto auto auto auto auto;
    }
    :where(:root[data-fit="on"]) main > .header { grid-area: header; }
    :where(:root[data-fit="on"]) main > #alertBanner { grid-area: banner; }
    :where(:root[data-fit="on"]) main > .current { grid-area: current; }
    :where(:root[data-fit="on"]) main > [data-panel="forecast"] { grid-area: forecast; }
    :where(:root[data-fit="on"]) main > #mapPanel { grid-area: map; }
    /* The power panel goes under the forecast, and the sensors in the right-hand column: their
       wrapper steps aside so each is placed on its own */
    :where(:root[data-fit="on"]) main > #homePanels:not([hidden]) { display: contents; }
    :where(:root[data-fit="on"]) main > #homePanels > #powerPanel { grid-area: power; }
    :where(:root[data-fit="on"]) main > #homePanels > #sensorsPanel { grid-area: sensors; }
    :where(:root[data-fit="on"]) main > #alertsPanel { grid-area: alerts; }
    :where(:root[data-fit="on"]) main > #alertsStatus { grid-area: status; }
    :where(:root[data-fit="on"]) main > footer { grid-area: footer; }
}
