/*
 * DashboardSystem styles.
 *
 * Everything lives in the `ds` cascade layer. Declaring `base` first slots `ds` after
 * Tailwind's preflight but before its components and utilities, so a host can override
 * the library with utilities or with any unlayered CSS. Without Tailwind, `base` is empty.
 * Third-party stylesheets (Leaflet) are loaded into `ds-vendor`, just below `ds`.
 *
 * Theming: redefine the --ds-* custom properties. The theme is picked by the
 * data-theme attribute on <html> ("dark" | "light"), set by <DsHead />.
 */
@layer base, ds-vendor, ds;

@layer ds {
    /* ---------- Tokens ---------- */

    :root,
    [data-theme="dark"] {
        color-scheme: dark;
        --ds-bg: #0a0c0f;
        --ds-surface: #0f1216;
        --ds-surface-2: #14181d;
        --ds-surface-hover: #1b2026;
        --ds-surface-active: #232931;
        --ds-border: #1e232a;
        --ds-border-strong: #2c333c;
        --ds-text: #e4e7eb;
        --ds-text-muted: #8a929d;
        --ds-text-subtle: #5a626d;
        --ds-accent: #4da3ff;
        --ds-accent-contrast: #04111f;
        --ds-accent-soft: rgb(77 163 255 / 0.13);
        --ds-danger: #ff6b6b;
        --ds-danger-soft: rgb(255 107 107 / 0.14);
        --ds-positive: #0ca30c;
        --ds-negative: #ff6b6b;
        /* Categorical chart palette, in fixed order (validated for color vision deficiencies
           against --ds-surface; text never wears these colors). */
        --ds-chart-1: #3987e5;
        --ds-chart-2: #d95926;
        --ds-chart-3: #199e70;
        --ds-chart-4: #c98500;
        --ds-chart-5: #d55181;
        --ds-chart-6: #008300;
        --ds-chart-7: #9085e9;
        --ds-chart-8: #e66767;
        --ds-scrim: rgb(0 0 0 / 0.55);
        --ds-shadow-overlay: 0 16px 40px -8px rgb(0 0 0 / 0.6);
        --ds-grid-guide: rgb(255 255 255 / 0.03);
    }

    [data-theme="light"] {
        color-scheme: light;
        --ds-bg: #f3f4f6;
        --ds-surface: #ffffff;
        --ds-surface-2: #ffffff;
        --ds-surface-hover: #eef0f3;
        --ds-surface-active: #e3e6ea;
        --ds-border: #e1e4e8;
        --ds-border-strong: #cbd0d6;
        --ds-text: #0f1216;
        --ds-text-muted: #5b6471;
        --ds-text-subtle: #8c949e;
        --ds-accent: #0b6fd8;
        --ds-accent-contrast: #ffffff;
        --ds-accent-soft: rgb(11 111 216 / 0.1);
        --ds-danger: #d42f36;
        --ds-danger-soft: rgb(212 47 54 / 0.1);
        --ds-positive: #006300;
        --ds-negative: #d42f36;
        --ds-chart-1: #2a78d6;
        --ds-chart-2: #eb6834;
        --ds-chart-3: #1baf7a;
        --ds-chart-4: #eda100;
        --ds-chart-5: #e87ba4;
        --ds-chart-6: #008300;
        --ds-chart-7: #4a3aa7;
        --ds-chart-8: #e34948;
        --ds-scrim: rgb(15 18 22 / 0.18);
        --ds-shadow-overlay: 0 16px 40px -8px rgb(15 18 22 / 0.18);
        --ds-grid-guide: rgb(15 18 22 / 0.045);
    }

    :root {
        --ds-font-sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        --ds-font-mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, monospace;
        --ds-radius-sm: 4px;
        --ds-radius: 6px;
        --ds-radius-lg: 10px;
        --ds-header-height: 40px;
        --ds-drawer-width: 280px;
        --ds-duration: 180ms;
        --ds-duration-move: 220ms;
        --ds-ease: cubic-bezier(0.2, 0.8, 0.2, 1);
        --ds-z-drawer: 40;
    }

    @media (prefers-reduced-motion: reduce) {
        :root {
            --ds-duration: 0ms;
            --ds-duration-move: 0ms;
        }
    }

    @keyframes ds-fade-in {
        from {
            opacity: 0;
        }
    }

    @keyframes ds-expand {
        from {
            opacity: 0;
            transform: scale(0.98);
        }
    }

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

    /* ---------- Shell ---------- */

    body:has(.ds-shell) {
        margin: 0;
        background: var(--ds-bg);
    }

    .ds-shell {
        /* A full-size widget stops at the header. */
        --ds-fullscreen-top: var(--ds-header-height);
        position: relative;
        display: grid;
        grid-template-rows: var(--ds-header-height) minmax(0, 1fr);
        height: 100dvh;
        background: var(--ds-bg);
        color: var(--ds-text);
        font-family: var(--ds-font-sans);
        font-size: 14px;
        line-height: 1.45;
        -webkit-font-smoothing: antialiased;
    }

    .ds-shell *,
    .ds-shell *::before,
    .ds-shell *::after {
        box-sizing: border-box;
    }

    .ds-header {
        position: relative;
        z-index: calc(var(--ds-z-drawer) + 1);
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        padding: 0 6px;
        background: var(--ds-surface);
        border-bottom: 1px solid var(--ds-border);
    }

    .ds-header__start,
    .ds-header__actions {
        display: flex;
        align-items: center;
        gap: 2px;
        min-width: 0;
    }

    .ds-header__brand {
        display: flex;
        align-items: center;
        min-width: 0;
        margin-left: 6px;
    }

    .ds-main {
        position: relative;
        min-height: 0;
        overflow: auto;
    }

    /* ---------- Drawer ---------- */

    .ds-scrim {
        position: fixed;
        inset: var(--ds-header-height) 0 0 0;
        z-index: var(--ds-z-drawer);
        background: var(--ds-scrim);
        opacity: 0;
        visibility: hidden;
        transition: opacity var(--ds-duration) var(--ds-ease), visibility 0s linear var(--ds-duration);
    }

    .ds-drawer {
        position: fixed;
        top: var(--ds-header-height);
        bottom: 0;
        left: 0;
        z-index: var(--ds-z-drawer);
        display: flex;
        flex-direction: column;
        width: min(var(--ds-drawer-width), 100vw - 48px);
        overflow-y: auto;
        background: var(--ds-surface);
        border-right: 1px solid var(--ds-border);
        box-shadow: var(--ds-shadow-overlay);
        transform: translateX(-100%);
        visibility: hidden;
        transition: transform var(--ds-duration) var(--ds-ease), visibility 0s linear var(--ds-duration);
    }

    .ds-drawer:focus {
        outline: none;
    }

    .ds-shell[data-menu-open="true"] .ds-scrim {
        opacity: 1;
        visibility: visible;
        transition: opacity var(--ds-duration) var(--ds-ease);
    }

    .ds-shell[data-menu-open="true"] .ds-drawer {
        transform: none;
        visibility: visible;
        transition: transform var(--ds-duration) var(--ds-ease);
    }

    /* Wide screens (DsShell PushContent): the open menu pushes the content aside, without scrim. ds-shell.js reads
       --ds-menu-pushes to keep the menu open from one page to the next. */
    @media (min-width: 1024px) {
        .ds-shell--push {
            --ds-menu-pushes: 1;
        }

        .ds-shell--push .ds-scrim {
            display: none;
        }

        .ds-shell--push .ds-drawer {
            width: var(--ds-drawer-width);
            box-shadow: none;
        }

        .ds-shell--push .ds-main {
            transition: margin-left var(--ds-duration) var(--ds-ease);
        }

        .ds-shell--push[data-menu-open="true"] {
            /* A full-size widget stops at the menu. */
            --ds-fullscreen-left: var(--ds-drawer-width);
        }

        .ds-shell--push[data-menu-open="true"] .ds-main {
            margin-left: var(--ds-drawer-width);
        }
    }

    /* ---------- Loading screen ----------
       DsLoadingScreen covers the page while the app downloads and starts. Its bar follows the variable Blazor sets on
       the root element; ds-loading.js fills the tiles (data-ds-step: one per quarter) and fades it out
       (ds-loading-screen--done) once the app has started. */

    .ds-loading-screen {
        --ds-progress: var(--blazor-load-percentage, 0%);
        position: fixed;
        inset: 0;
        z-index: calc(var(--ds-z-drawer) + 10);
        display: grid;
        place-items: center;
        background: var(--ds-bg);
        color: var(--ds-text);
        font-family: var(--ds-font-sans);
        font-size: 14px;
        line-height: 1.45;
        -webkit-font-smoothing: antialiased;
        transition: opacity 300ms ease, visibility 0s linear 300ms;
    }

    .ds-loading-screen *,
    .ds-loading-screen *::before,
    .ds-loading-screen *::after {
        box-sizing: border-box;
    }

    .ds-loading-screen--done {
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
    }

    .ds-loading-screen__body {
        display: grid;
        justify-items: center;
        gap: 14px;
        width: min(260px, 100vw - 48px);
    }

    .ds-loading-screen__icon {
        color: var(--ds-accent);
    }

    /* The tiles breathe one after the other, like widgets falling into place, and fill as the download goes. */
    .ds-loading-screen__tile {
        fill: var(--ds-accent);
        fill-opacity: 0;
        transform-box: fill-box;
        transform-origin: center;
        transition: fill-opacity 250ms ease;
        animation: ds-loading-screen-wave 1.4s ease-in-out infinite;
    }

    .ds-loading-screen__tile:nth-child(2) { animation-delay: 175ms; }
    .ds-loading-screen__tile:nth-child(3) { animation-delay: 350ms; }
    .ds-loading-screen__tile:nth-child(4) { animation-delay: 525ms; }

    .ds-loading-screen[data-ds-step="1"] .ds-loading-screen__tile:nth-child(-n + 1),
    .ds-loading-screen[data-ds-step="2"] .ds-loading-screen__tile:nth-child(-n + 2),
    .ds-loading-screen[data-ds-step="3"] .ds-loading-screen__tile:nth-child(-n + 3),
    .ds-loading-screen[data-ds-step="4"] .ds-loading-screen__tile {
        fill-opacity: 0.9;
    }

    @keyframes ds-loading-screen-wave {
        0%, 60%, 100% { transform: scale(1); }
        30% { transform: scale(0.78); }
    }

    .ds-loading-screen__title {
        font-size: 15px;
        font-weight: 600;
        letter-spacing: -0.01em;
    }

    .ds-loading-screen__bar {
        width: 100%;
    }

    .ds-loading-screen__label {
        min-height: 1.45em;
        color: var(--ds-text-muted);
        font-size: 12px;
    }

    @media (prefers-reduced-motion: reduce) {
        .ds-loading-screen,
        .ds-loading-screen__tile {
            transition: none;
            animation: none;
        }
    }

    /* ---------- Menu ---------- */

    .ds-menu {
        display: flex;
        flex-direction: column;
        gap: 2px;
        padding: 8px;
    }

    .ds-menu__section {
        padding: 12px 8px 6px;
        color: var(--ds-text-subtle);
        font: 500 11px/1 var(--ds-font-mono);
        letter-spacing: 0.06em;
        text-transform: uppercase;
    }

    .ds-menu__item {
        display: flex;
        align-items: center;
        gap: 10px;
        height: 32px;
        padding: 0 8px;
        border-radius: var(--ds-radius);
        color: var(--ds-text-muted);
        text-decoration: none;
        transition: background-color var(--ds-duration) var(--ds-ease), color var(--ds-duration) var(--ds-ease);
    }

    .ds-menu__item:hover {
        background: var(--ds-surface-hover);
        color: var(--ds-text);
    }

    .ds-menu__item.active,
    .ds-menu__item[aria-current="page"] {
        background: var(--ds-accent-soft);
        color: var(--ds-text);
    }

    .ds-menu__item.active .ds-icon,
    .ds-menu__item[aria-current="page"] .ds-icon {
        color: var(--ds-accent);
    }

    .ds-menu__item:focus-visible {
        outline: 2px solid var(--ds-accent);
        outline-offset: -2px;
    }

    /* ---------- Grid ----------
     * Widgets are absolutely positioned from the --ds-x/y/w/h cell variables set by DsWidget,
     * so a change of cell animates with plain CSS transitions. --ds-col-w and --ds-row-h hold
     * percentages that resolve against the grid when used on its children.
     */

    .ds-grid-host {
        min-width: 0;
    }

    .ds-grid-host--fit {
        height: 100%;
    }

    .ds-grid-host--stack-auto {
        container: ds-grid / inline-size;
    }

    .ds-grid {
        --ds-col-w: calc((100% - (var(--ds-cols) - 1) * var(--ds-gap)) / var(--ds-cols));
        position: relative;
        min-width: 0;
    }

    .ds-grid--scroll {
        --ds-row-h: var(--ds-row-height);
    }

    .ds-grid--fit {
        --ds-row-h: calc((100% - (var(--ds-rows) - 1) * var(--ds-gap)) / var(--ds-rows));
        height: 100%;
    }

    /* Gives the grid the height of its occupied rows. */
    .ds-grid__sizer {
        height: max(0px, var(--ds-rows-used) * (var(--ds-row-h) + var(--ds-gap)) - var(--ds-gap));
    }

    .ds-widget,
    .ds-grid__placeholder {
        position: absolute;
        left: calc(var(--ds-x) * (var(--ds-col-w) + var(--ds-gap)));
        top: calc(var(--ds-y) * (var(--ds-row-h) + var(--ds-gap)));
        width: calc(var(--ds-w) * var(--ds-col-w) + (var(--ds-w) - 1) * var(--ds-gap));
        height: calc(var(--ds-h) * var(--ds-row-h) + (var(--ds-h) - 1) * var(--ds-gap));
        transition:
            left var(--ds-duration-move) var(--ds-ease),
            top var(--ds-duration-move) var(--ds-ease),
            width var(--ds-duration-move) var(--ds-ease),
            height var(--ds-duration-move) var(--ds-ease);
    }

    /* Shown while a widget is moved or resized: one faint tile per cell. */
    .ds-grid__guides {
        position: absolute;
        inset: 0;
        background: var(--ds-grid-guide);
        mask-image:
            linear-gradient(to right, #000 calc(100% - var(--ds-gap)), transparent 0),
            linear-gradient(to bottom, #000 calc(100% - var(--ds-gap)), transparent 0);
        mask-size: calc(var(--ds-col-w) + var(--ds-gap)) calc(var(--ds-row-h) + var(--ds-gap));
        mask-composite: intersect;
        pointer-events: none;
        animation: ds-fade-in var(--ds-duration) var(--ds-ease);
    }

    .ds-grid__placeholder {
        border-radius: var(--ds-radius-lg);
        background: var(--ds-accent-soft);
        outline: 1px dashed color-mix(in srgb, var(--ds-accent) 55%, transparent);
        outline-offset: -1px;
        pointer-events: none;
    }

    /* ---------- Widget ---------- */

    .ds-widget--active {
        z-index: 2;
        transition: none;
    }

    /* Focusable only so that a tap reveals the controls on touch screens. */
    .ds-widget:focus {
        outline: none;
    }

    .ds-widget__frame {
        position: absolute;
        inset: 0;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        background: var(--ds-surface);
        border: 1px solid var(--ds-border);
        border-radius: var(--ds-radius-lg);
        transition: border-color var(--ds-duration) var(--ds-ease), box-shadow var(--ds-duration) var(--ds-ease);
    }

    .ds-widget:hover .ds-widget__frame,
    .ds-widget--active .ds-widget__frame {
        border-color: var(--ds-border-strong);
    }

    /* Classes set by ds-grid.js while the frame follows the pointer, then settles into its cell. */
    .ds-widget__frame--lifted {
        z-index: 3;
        box-shadow: var(--ds-shadow-overlay);
    }

    .ds-widget__frame--settling {
        transition:
            transform var(--ds-duration-move) var(--ds-ease),
            width var(--ds-duration-move) var(--ds-ease),
            height var(--ds-duration-move) var(--ds-ease),
            box-shadow var(--ds-duration-move) var(--ds-ease);
    }

    .ds-widget__bar {
        display: flex;
        flex: none;
        align-items: center;
        gap: 6px;
        height: 32px;
        padding: 0 4px 0 10px;
        border-bottom: 1px solid var(--ds-border);
        user-select: none;
    }

    /* Untitled widgets: the bar floats over the content and only shows on hover. */
    .ds-widget:not(.ds-widget--titled) .ds-widget__bar {
        position: absolute;
        inset: 0 0 auto;
        z-index: 2;
        background: color-mix(in srgb, var(--ds-surface) 88%, transparent);
        backdrop-filter: blur(8px);
        opacity: 0;
        transform: translateY(-4px);
        transition: opacity var(--ds-duration) var(--ds-ease), transform var(--ds-duration) var(--ds-ease);
    }

    .ds-widget:not(.ds-widget--titled):is(:hover, :focus-within, .ds-widget--active) .ds-widget__bar {
        opacity: 1;
        transform: none;
    }

    .ds-grid--editable .ds-widget__bar {
        cursor: grab;
        touch-action: none;
    }

    .ds-grid--editable .ds-widget--active .ds-widget__bar {
        cursor: grabbing;
    }

    .ds-widget__grip {
        margin-left: -2px;
        color: var(--ds-text-subtle);
        opacity: 0;
        transition: opacity var(--ds-duration) var(--ds-ease);
    }

    .ds-widget:is(:hover, .ds-widget--active) .ds-widget__grip {
        opacity: 1;
    }

    .ds-widget__title {
        min-width: 0;
        overflow: hidden;
        color: var(--ds-text-muted);
        font: 500 11px/1 var(--ds-font-mono);
        letter-spacing: 0.06em;
        text-overflow: ellipsis;
        text-transform: uppercase;
        white-space: nowrap;
    }

    .ds-widget__help {
        flex: none;
    }

    .ds-widget__actions {
        display: flex;
        align-items: center;
        gap: 2px;
        margin-left: auto;
        opacity: 0;
        transition: opacity var(--ds-duration) var(--ds-ease);
    }

    .ds-widget:is(:hover, :focus-within, .ds-widget--fullscreen) .ds-widget__actions {
        opacity: 1;
    }

    .ds-widget__body {
        position: relative;
        flex: 1;
        min-height: 0;
        overflow: auto;
    }

    /* Full size: covers the dashboard area below the header, above every other widget. */
    .ds-widget--fullscreen {
        position: fixed;
        inset: var(--ds-fullscreen-top, 0px) 0 0 var(--ds-fullscreen-left, 0px);
        z-index: 30;
        width: auto;
        height: auto;
        background: var(--ds-bg);
        transition: none;
    }

    .ds-widget--fullscreen .ds-widget__frame {
        inset: 12px;
        border-color: var(--ds-border-strong);
        animation: ds-expand var(--ds-duration-move) var(--ds-ease);
    }

    /* The bar of an untitled widget stops floating: its controls must stay reachable. */
    .ds-widget--fullscreen:not(.ds-widget--titled) .ds-widget__bar {
        position: static;
        opacity: 1;
        transform: none;
        background: none;
        backdrop-filter: none;
    }

    .ds-widget__resize {
        position: absolute;
        bottom: 0;
        z-index: 3;
        width: 18px;
        height: 18px;
        opacity: 0;
        touch-action: none;
        transition: opacity var(--ds-duration) var(--ds-ease);
    }

    .ds-widget__resize::after {
        content: "";
        position: absolute;
        bottom: 5px;
        width: 7px;
        height: 7px;
        border-bottom: 1.5px solid var(--ds-text-subtle);
        transition: border-color var(--ds-duration) var(--ds-ease);
    }

    .ds-widget__resize--se {
        right: 0;
        cursor: nwse-resize;
    }

    .ds-widget__resize--se::after {
        right: 5px;
        border-right: 1.5px solid var(--ds-text-subtle);
        border-bottom-right-radius: 3px;
    }

    .ds-widget__resize--sw {
        left: 0;
        cursor: nesw-resize;
    }

    .ds-widget__resize--sw::after {
        left: 5px;
        border-left: 1.5px solid var(--ds-text-subtle);
        border-bottom-left-radius: 3px;
    }

    .ds-widget:is(:hover, .ds-widget--active) .ds-widget__resize {
        opacity: 1;
    }

    .ds-widget__resize:hover::after {
        border-color: var(--ds-accent);
    }

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

    .ds-loading {
        position: absolute;
        inset: 0;
        display: grid;
        place-items: center;
        background: var(--ds-surface);
    }

    .ds-loading::after {
        content: "";
        width: 18px;
        height: 18px;
        border: 2px solid var(--ds-border-strong);
        border-top-color: var(--ds-accent);
        border-radius: 50%;
        animation: ds-spin 0.8s linear infinite;
    }

    .ds-mounted-error {
        position: absolute;
        inset: 0;
        display: grid;
        place-items: center;
        margin: 0;
        padding: 12px;
        color: var(--ds-text-subtle);
        font-size: 12px;
        text-align: center;
    }

    /* Text */

    .ds-text {
        padding: 12px 14px;
        color: var(--ds-text-muted);
        font-size: 14px;
        line-height: 1.6;
    }

    .ds-text > * {
        margin: 0;
    }

    .ds-text > * + * {
        margin-top: 0.75em;
    }

    .ds-text :is(h1, h2, h3, h4) {
        color: var(--ds-text);
        font-weight: 600;
        line-height: 1.3;
        letter-spacing: -0.01em;
    }

    .ds-text h1 { font-size: 1.25rem; }
    .ds-text h2 { font-size: 1.1rem; }
    .ds-text h3 { font-size: 1rem; }
    .ds-text h4 { font-size: 0.9rem; }

    .ds-text strong {
        color: var(--ds-text);
        font-weight: 600;
    }

    .ds-text a {
        color: var(--ds-accent);
        text-underline-offset: 2px;
    }

    .ds-text ul,
    .ds-text ol {
        padding-left: 1.25em;
    }

    .ds-text ul { list-style: disc; }
    .ds-text ol { list-style: decimal; }

    .ds-text li + li {
        margin-top: 0.25em;
    }

    .ds-text code {
        padding: 0.1em 0.35em;
        border-radius: var(--ds-radius-sm);
        background: var(--ds-surface-hover);
        color: var(--ds-text);
        font: 0.9em var(--ds-font-mono);
    }

    .ds-text__plain {
        white-space: pre-line;
    }

    /* Stat tile: label, value, signed delta (arrow + color), sparkline with the current value in the accent. */

    .ds-stat {
        display: flex;
        flex-direction: column;
        gap: 6px;
        height: 100%;
        padding: 12px 14px;
    }

    .ds-stat__label {
        color: var(--ds-text-muted);
        font-size: 12px;
    }

    .ds-stat__value {
        color: var(--ds-text);
        font-size: 28px;
        font-weight: 600;
        line-height: 1.1;
        letter-spacing: -0.02em;
    }

    .ds-stat__unit {
        margin-left: 4px;
        color: var(--ds-text-muted);
        font-size: 0.5em;
        font-weight: 500;
        letter-spacing: 0;
    }

    .ds-stat__delta {
        display: flex;
        align-items: center;
        gap: 4px;
        font-size: 12px;
        font-weight: 500;
    }

    .ds-stat__delta--positive { color: var(--ds-positive); }
    .ds-stat__delta--negative { color: var(--ds-negative); }
    .ds-stat__delta--neutral { color: var(--ds-text-muted); }

    .ds-stat__caption {
        color: var(--ds-text-subtle);
        font-weight: 400;
    }

    .ds-stat__trend {
        position: relative;
        flex: 1;
        min-height: 16px;
        margin-top: 4px;
    }

    .ds-stat__trend svg {
        position: absolute;
        inset: 0;
        width: 100%;
        height: 100%;
        overflow: visible;
    }

    .ds-stat__trend-line {
        fill: none;
        stroke: var(--ds-text-subtle);
        stroke-width: 1.5;
        stroke-linecap: round;
        stroke-linejoin: round;
        vector-effect: non-scaling-stroke;
    }

    .ds-stat__trend-area {
        fill: var(--ds-text-subtle);
        opacity: 0.1;
    }

    .ds-stat__trend-dot {
        position: absolute;
        width: 8px;
        height: 8px;
        margin: -4px 0 0 -4px;
        border-radius: 50%;
        background: var(--ds-accent);
        box-shadow: 0 0 0 2px var(--ds-surface);
    }

    /* Iframe, video */

    .ds-iframe,
    .ds-video {
        position: relative;
        width: 100%;
        height: 100%;
    }

    .ds-iframe iframe {
        display: block;
        width: 100%;
        height: 100%;
        border: 0;
    }

    .ds-video {
        background: #000;
    }

    .ds-video__player {
        display: block;
        width: 100%;
        height: 100%;
        border: 0;
        object-fit: contain;
    }

    /* Chart and map: the library mounts into the canvas element. */

    .ds-chart,
    .ds-map {
        position: relative;
        width: 100%;
        height: 100%;
        min-height: 120px;
        /* The SVG renderer can outgrow its box by a scrollbar's width, which would then shrink the box. */
        overflow: hidden;
    }

    .ds-chart__canvas,
    .ds-map__canvas {
        position: absolute;
        inset: 0;
    }

    /* Leaflet panes use z-index 400 and up: keep them inside the map, under the header and drawer. */
    .ds-map {
        isolation: isolate;
    }

    .ds-map__canvas.leaflet-container {
        background: var(--ds-surface-2);
        font: 12px/1.4 var(--ds-font-sans);
    }

    [data-theme="dark"] .ds-map--dim-dark .leaflet-tile-pane {
        filter: invert(1) hue-rotate(180deg) brightness(0.95) contrast(0.85) saturate(0.5);
    }

    .ds-map .leaflet-bar {
        overflow: hidden;
        border: 1px solid var(--ds-border-strong);
        border-radius: var(--ds-radius);
        box-shadow: none;
    }

    .ds-map .leaflet-bar a {
        border-bottom-color: var(--ds-border);
        background: var(--ds-surface);
        color: var(--ds-text);
    }

    .ds-map .leaflet-bar a:hover {
        background: var(--ds-surface-hover);
    }

    .ds-map .leaflet-control-attribution {
        background: color-mix(in srgb, var(--ds-surface) 80%, transparent);
        color: var(--ds-text-subtle);
    }

    .ds-map .leaflet-control-attribution a {
        color: var(--ds-text-muted);
    }

    .ds-map .leaflet-tooltip {
        border-color: var(--ds-border-strong);
        background: var(--ds-surface-2);
        box-shadow: none;
        color: var(--ds-text);
    }

    .ds-map .leaflet-tooltip-top::before { border-top-color: var(--ds-border-strong); }
    .ds-map .leaflet-tooltip-bottom::before { border-bottom-color: var(--ds-border-strong); }
    .ds-map .leaflet-tooltip-left::before { border-left-color: var(--ds-border-strong); }
    .ds-map .leaflet-tooltip-right::before { border-right-color: var(--ds-border-strong); }

    /* WebGL map (MapLibre): same box as the chart and the map. */

    .ds-geomap {
        position: relative;
        width: 100%;
        height: 100%;
        min-height: 120px;
        overflow: hidden;
        isolation: isolate;
    }

    .ds-geomap__canvas {
        position: absolute;
        inset: 0;
        background: var(--ds-surface-2);
        font: 12px/1.4 var(--ds-font-sans);
    }

    .ds-geomap .maplibregl-ctrl-group {
        overflow: hidden;
        border: 1px solid var(--ds-border-strong);
        border-radius: var(--ds-radius);
        background: var(--ds-surface);
        box-shadow: none;
    }

    .ds-geomap .maplibregl-ctrl-group button + button {
        border-top-color: var(--ds-border);
    }

    .ds-geomap .maplibregl-ctrl-group button:hover {
        background: var(--ds-surface-hover);
    }

    /* The control icons are dark drawings: lighten them in the dark theme. */
    [data-theme="dark"] .ds-geomap .maplibregl-ctrl-icon {
        filter: invert(1);
    }

    .ds-geomap .maplibregl-ctrl-attrib {
        background: color-mix(in srgb, var(--ds-surface) 80%, transparent);
        color: var(--ds-text-subtle);
    }

    .ds-geomap .maplibregl-ctrl-attrib a {
        color: var(--ds-text-muted);
    }

    .ds-geomap__tooltip .maplibregl-popup-content {
        padding: 6px 8px;
        border: 1px solid var(--ds-border-strong);
        border-radius: var(--ds-radius);
        background: var(--ds-surface-2);
        box-shadow: none;
        color: var(--ds-text);
        font: 12px/1.4 var(--ds-font-sans);
    }

    .ds-geomap__tooltip .maplibregl-popup-tip {
        display: none;
    }

    /* ---------- Globe ---------- */

    .ds-globe {
        position: relative;
        width: 100%;
        height: 100%;
        min-height: 120px;
        overflow: hidden;
        isolation: isolate;
    }

    .ds-globe__canvas {
        position: absolute;
        inset: 0;
        background: var(--ds-surface-2);
        font: 12px/1.4 var(--ds-font-sans);
    }

    /* The credits of the imagery, small and discreet, in the corner. */
    .ds-globe .cesium-widget-credits {
        padding: 1px 6px;
        border-radius: var(--ds-radius);
        background: color-mix(in srgb, var(--ds-surface) 80%, transparent);
        color: var(--ds-text-subtle);
        font-size: 10px;
    }

    .ds-globe__tooltip {
        position: absolute;
        z-index: 1;
        padding: 6px 8px;
        border: 1px solid var(--ds-border-strong);
        border-radius: var(--ds-radius);
        background: var(--ds-surface-2);
        color: var(--ds-text);
        font: 12px/1.4 var(--ds-font-sans);
        pointer-events: none;
        white-space: nowrap;
    }

    /* ---------- Table ---------- */

    .ds-table {
        position: relative;
        height: 100%;
        overflow: auto;
        font-size: 12px;
        font-variant-numeric: tabular-nums;
    }

    .ds-table table {
        width: 100%;
        border-collapse: collapse;
        table-layout: fixed;
    }

    .ds-table th {
        position: sticky;
        top: 0;
        z-index: 1;
        height: 30px;
        padding: 0 8px;
        border-bottom: 1px solid var(--ds-border);
        background: var(--ds-surface);
        color: var(--ds-text-subtle);
        font-weight: 500;
        text-align: start;
        white-space: nowrap;
    }

    .ds-table td {
        overflow: hidden;
        padding: 0 8px;
        border-bottom: 1px solid var(--ds-border);
        color: var(--ds-text);
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .ds-table .ds-table__cell--end { text-align: end; }
    .ds-table .ds-table__cell--center { text-align: center; }

    .ds-table__sort {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        padding: 0;
        border: 0;
        background: none;
        color: inherit;
        font: inherit;
        cursor: pointer;
    }

    .ds-table__sort:hover {
        color: var(--ds-text);
    }

    .ds-table__row--clickable {
        cursor: pointer;
    }

    .ds-table__row--clickable:hover td {
        background: var(--ds-surface-hover);
    }

    .ds-table__row--selected td {
        background: var(--ds-accent-soft);
    }

    .ds-table__empty {
        margin: 0;
        padding: 12px 8px;
        color: var(--ds-text-subtle);
    }

    /* ---------- Inputs ---------- */

    .ds-segmented {
        display: inline-flex;
        padding: 2px;
        border: 1px solid var(--ds-border);
        border-radius: var(--ds-radius);
        background: var(--ds-surface);
    }

    .ds-segmented__option,
    .ds-chips__chip {
        height: 24px;
        padding: 0 10px;
        border: 0;
        border-radius: calc(var(--ds-radius) - 2px);
        background: transparent;
        color: var(--ds-text-muted);
        font: 12px var(--ds-font-sans);
        white-space: nowrap;
        cursor: pointer;
        transition: background-color var(--ds-duration) var(--ds-ease), color var(--ds-duration) var(--ds-ease);
    }

    .ds-segmented__option:hover,
    .ds-chips__chip:hover {
        color: var(--ds-text);
    }

    .ds-segmented__option[aria-pressed="true"] {
        background: var(--ds-surface-hover);
        color: var(--ds-text);
    }

    .ds-chips {
        display: inline-flex;
        flex-wrap: wrap;
        gap: 4px;
    }

    .ds-chips__chip {
        border: 1px solid var(--ds-border);
        border-radius: 999px;
    }

    .ds-chips__chip[aria-pressed="true"] {
        border-color: var(--ds-accent);
        background: var(--ds-accent-soft);
        color: var(--ds-text);
    }

    .ds-segmented__option:focus-visible,
    .ds-chips__chip:focus-visible {
        outline: 2px solid var(--ds-accent);
        outline-offset: 1px;
    }

    /* Two native range inputs on one track: only their thumbs take the pointer. */
    .ds-range {
        display: grid;
        gap: 2px;
        min-width: 160px;
        font-size: 11px;
    }

    .ds-range__values {
        display: flex;
        justify-content: space-between;
        color: var(--ds-text-muted);
        font-variant-numeric: tabular-nums;
    }

    .ds-range__track {
        position: relative;
        height: 18px;
    }

    .ds-range__track::before,
    .ds-range__track::after {
        content: "";
        position: absolute;
        top: 7px;
        height: 4px;
        border-radius: 2px;
    }

    .ds-range__track::before {
        inset-inline: 0;
        background: var(--ds-border-strong);
    }

    .ds-range__track::after {
        left: var(--ds-range-low);
        right: calc(100% - var(--ds-range-high));
        background: var(--ds-accent);
    }

    .ds-range__input {
        position: absolute;
        inset: 0;
        z-index: 1;
        width: 100%;
        margin: 0;
        background: none;
        pointer-events: none;
        appearance: none;
    }

    .ds-range__input::-webkit-slider-thumb {
        width: 14px;
        height: 14px;
        border: 2px solid var(--ds-surface);
        border-radius: 50%;
        background: var(--ds-accent);
        pointer-events: auto;
        cursor: pointer;
        appearance: none;
    }

    .ds-range__input::-moz-range-thumb {
        width: 10px;
        height: 10px;
        border: 2px solid var(--ds-surface);
        border-radius: 50%;
        background: var(--ds-accent);
        pointer-events: auto;
        cursor: pointer;
    }

    .ds-range__input:focus-visible::-webkit-slider-thumb {
        outline: 2px solid var(--ds-accent);
        outline-offset: 1px;
    }

    /* ---------- Narrow grid: one column ----------
     * Below 640px of grid width, widgets stack in reading order (row, then column) and keep
     * their height in rows. Moving and resizing are off (ds-grid.js only drags absolutely
     * positioned widgets); full size still works.
     */
    @container ds-grid (max-width: 639.98px) {
        .ds-grid-host--stack-auto > .ds-grid {
            display: flex;
            flex-direction: column;
            gap: var(--ds-gap);
            height: auto;
        }

        .ds-grid-host--stack-auto > .ds-grid > .ds-widget:not(.ds-widget--fullscreen) {
            position: relative;
            top: auto;
            left: auto;
            width: auto;
            height: calc(var(--ds-h) * var(--ds-row-height) + (var(--ds-h) - 1) * var(--ds-gap));
            order: calc(var(--ds-y) * 1000 + var(--ds-x));
            transition: none;
        }

        .ds-grid-host--stack-auto > .ds-grid > :is(.ds-grid__sizer, .ds-grid__guides, .ds-grid__placeholder),
        .ds-grid-host--stack-auto .ds-widget__grip,
        .ds-grid-host--stack-auto .ds-widget__resize {
            display: none;
        }

        .ds-grid-host--stack-auto .ds-widget__bar {
            cursor: auto;
            touch-action: auto;
        }
    }

    /* ---------- Touch screens ----------
     * No hover: the controls of titled widgets and the handles stay visible, and the handles
     * get a finger-sized target. An untitled widget shows its bar once tapped (focus).
     */
    @media (hover: none) {
        .ds-widget__actions,
        .ds-widget__grip,
        .ds-widget__resize {
            opacity: 1;
        }

        .ds-widget__resize {
            width: 28px;
            height: 28px;
        }
    }

    /* ---------- Primitives ---------- */

    .ds-icon {
        display: block;
        flex: none;
    }

    /* DsProgressBar: a thin bar and its percentage. --ds-progress comes from the component, or from its surroundings. */
    .ds-progress {
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .ds-progress__track {
        flex: 1;
        height: 4px;
        overflow: hidden;
        border-radius: 999px;
        background: var(--ds-border);
    }

    .ds-progress__fill {
        width: var(--ds-progress, 0%);
        height: 100%;
        border-radius: inherit;
        background: var(--ds-accent);
        transition: width var(--ds-duration-move) var(--ds-ease);
    }

    .ds-progress__value {
        flex: none;
        min-width: 4.5ch;
        color: var(--ds-text-muted);
        font: 500 11px/1 var(--ds-font-mono);
        font-variant-numeric: tabular-nums;
        text-align: end;
    }

    /* The "?" badge of DsHelp, and its tooltip: a manual popover placed by ds-help.js. */
    .ds-help {
        display: inline-flex;
        vertical-align: middle;
    }

    .ds-help__button {
        display: inline-grid;
        place-items: center;
        width: 16px;
        height: 16px;
        padding: 0;
        border: 1px solid var(--ds-border-strong);
        border-radius: 50%;
        background: transparent;
        color: var(--ds-text-subtle);
        font: 600 10px/1 var(--ds-font-sans);
        cursor: help;
        transition: color var(--ds-duration) var(--ds-ease), border-color var(--ds-duration) var(--ds-ease);
    }

    .ds-help__button:is(:hover, :focus-visible) {
        border-color: var(--ds-accent);
        color: var(--ds-accent);
        outline: none;
    }

    .ds-help__tip {
        position: fixed;
        inset: auto;
        width: max-content;
        max-width: min(320px, calc(100vw - 16px));
        margin: 0;
        padding: 8px 10px;
        border: 1px solid var(--ds-border-strong);
        border-radius: var(--ds-radius);
        background: var(--ds-surface-2);
        box-shadow: var(--ds-shadow-overlay);
        color: var(--ds-text);
        font: 12px/1.45 var(--ds-font-sans);
        text-transform: none;
        letter-spacing: normal;
        white-space: pre-line;
        pointer-events: none;
    }

    .ds-icon-btn {
        display: inline-grid;
        place-items: center;
        width: 28px;
        height: 28px;
        padding: 0;
        border: 0;
        border-radius: var(--ds-radius);
        background: transparent;
        color: var(--ds-text-muted);
        font: inherit;
        cursor: pointer;
        transition: background-color var(--ds-duration) var(--ds-ease), color var(--ds-duration) var(--ds-ease);
    }

    .ds-icon-btn:hover,
    .ds-icon-btn[aria-expanded="true"] {
        background: var(--ds-surface-hover);
        color: var(--ds-text);
    }

    .ds-icon-btn:active {
        background: var(--ds-surface-active);
    }

    .ds-icon-btn:focus-visible {
        outline: 2px solid var(--ds-accent);
        outline-offset: 1px;
    }

    .ds-icon-btn:disabled {
        cursor: default;
    }

    .ds-icon-btn--sm {
        width: auto;
        min-width: 24px;
        height: 24px;
        border-radius: var(--ds-radius-sm);
    }

    /* Destructive action waiting for confirmation: icon plus a short text. */
    .ds-icon-btn--danger,
    .ds-icon-btn--danger:hover {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        padding: 0 8px;
        background: var(--ds-danger-soft);
        color: var(--ds-danger);
        font: 500 11px/1 var(--ds-font-mono);
        white-space: nowrap;
    }

    .ds-icon-btn--busy .ds-icon {
        animation: ds-spin 0.8s linear infinite;
    }

    @media (prefers-reduced-motion: reduce) {
        .ds-icon-btn--busy .ds-icon {
            animation: none;
            opacity: 0.5;
        }
    }

    /* The toggle shows the theme it switches to. */
    .ds-theme-toggle__to-dark,
    [data-theme="light"] .ds-theme-toggle__to-light {
        display: none;
    }

    [data-theme="light"] .ds-theme-toggle__to-dark {
        display: block;
    }
}
