/**
 * App styles — the `@layer app` slot.
 *
 * The app's own structural and domain CSS. Sits below `theme`, so a theme can
 * restate a colour or radius without fighting these rules, and above
 * `components`, so a `ui-*` / `civ-*` element can still be placed in a layout.
 *
 * Tokens belong in `theme.css`; setting one here will not win.
 */

@layer app {
  /* ── Map page height chain ──────────────────────────────────────────────── */

  /* Fix body height when map is visible so map fills remaining space */
  body:has(r-home) {
    height: 100dvh;
    overflow: hidden;
  }

  body:has(r-home) > main {
    overflow: hidden;
  }

  r-home {
    display: block;
    height: 100%;
    overflow: hidden;
  }

  /* Centred spinner shown in `main` until the router mounts the first view. */
  .boot-loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  /*
    The map is our own fixed chrome, which the framework's safe-area padding
    can't reach — so everything positioned against the header or the tab bar
    below uses `--header-block` / `--footer-block` (height + display cutout)
    rather than the bare heights. With `viewport-fit=cover` those differ on a
    notched iPhone and are identical everywhere else.
  */
  m-map {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 1;
    padding-bottom: var(--footer-block);
    box-sizing: border-box;
  }

  m-map[hidden],
  header[hidden] {
    display: none;
  }

  m-map #map {
    height: calc(100% + var(--header-block) + var(--footer-block));
    width: 100%;
    margin-top: calc(-1 * var(--header-block));
    margin-bottom: calc(-1 * var(--footer-block));
    /* Shown until the first tiles render; matches the style's background
       layer (mapBackground() in layers.ts) so there's no colour flash. */
    background-color: hsl(47, 26%, 88%);
  }

  m-map #map canvas {
    z-index: 1;
  }

  m-map .maplibregl-marker {
    z-index: 5;
  }

  m-map .maplibregl-popup {
    z-index: 100;
  }

  m-map > .download-btn {
    z-index: 10;
  }

  m-map .maplibregl-ctrl-top-left,
  m-map .maplibregl-ctrl-top-right {
    padding-top: var(--header-block);
    z-index: 10;
  }

  m-map .maplibregl-ctrl-bottom-left,
  m-map .maplibregl-ctrl-bottom-right {
    padding-bottom: var(--footer-block);
    z-index: 10;
  }

  m-map .maplibregl-ctrl-group {
    margin-top: var(--header-block);
    margin-bottom: var(--footer-block);
  }

  m-map .maplibregl-ctrl-attrib {
    margin-bottom: var(--footer-block);
  }

  m-map .download-btn {
    position: absolute;
    bottom: calc(var(--footer-block) + var(--s4));
    left: calc(var(--safe-left) + var(--s3));
    padding: var(--s2) var(--s4);
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--br-base);
    font-size: var(--f4);
    font-weight: var(--fw-medium);
    cursor: pointer;
    box-shadow: var(--shadow-md);
    z-index: 10;
    transition: opacity var(--transition-fast);
    height: 3em;
  }

  m-map .download-btn:hover {
    opacity: 0.9;
  }

  /* Mid-download: the same chip, stacking its label over a full-width bar.
     The bar rides on the primary fill, so it borrows the button's own
     colour instead of the (invisible here) `--primary`. */
  m-map .download-btn--progress {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: center;
    gap: var(--s1);
    height: auto;
    min-width: 200px;
    cursor: default;
    --progress-width: 100%;
    --progress-color: currentColor;
  }

  m-map .download-btn--progress:hover {
    opacity: 1;
  }

  m-map .download-btn-label {
    font-size: var(--f5);
    text-align: left;
  }

  m-map .download-btn--progress .progress-label {
    opacity: 0.85;
  }

  /* Map options. Top-left, clearing the header the map sits under. Just the
     icon until opened, so the map stays uncluttered. */
  m-map .map-menu {
    position: absolute;
    /* No header to clear here, but on a notched phone the map still runs
       under the status bar — so clear the cutout instead. */
    top: calc(var(--safe-top) + var(--s3));
    left: calc(var(--safe-left) + var(--s3));
    z-index: 10;
  }

  /* The map runs under the header where a route mounts one (the list view);
     on the map route the header is empty, so there's nothing to clear. */
  body:has(header > *) m-map .map-menu {
    top: calc(var(--header-block) + var(--s3));
  }

  m-map .map-menu-button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    padding: 0;
    /* Tracks the app theme (`--background`/`--body`) rather than a fixed
       light surface, so the control matches the rest of the chrome instead
       of staying a light chip against a dark map. */
    background: color-mix(in srgb, var(--background) 92%, transparent);
    color: var(--body);
    border: none;
    border-radius: var(--br-base);
    box-shadow: var(--shadow-base);
    cursor: pointer;
    transition: opacity var(--transition-fast);
  }

  m-map .map-menu-button:hover {
    opacity: 0.85;
  }

  m-map .map-menu-panel {
    position: absolute;
    top: calc(100% + var(--s2));
    left: 0;
    min-width: 240px;
    padding: var(--s2);
    background: color-mix(in srgb, var(--background) 97%, transparent);
    color: var(--body);
    border-radius: var(--br-base);
    box-shadow: var(--shadow-lg);
  }

  m-map .map-menu-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s3);
    padding: var(--s2);
    font-size: var(--f5);
    cursor: pointer;
  }

  m-map .map-menu-row > span {
    display: flex;
    flex-direction: column;
  }

  m-map .map-menu-hint {
    font-size: var(--f6);
    opacity: 0.6;
  }

  m-map .map-menu-zoom {
    margin: 0;
    padding: var(--s2);
    border-top: var(--border-width) solid color-mix(in srgb, currentColor 12%,
      transparent);
    font-size: var(--f6);
    opacity: 0.6;
  }

  m-map .map-menu-row select {
    font-size: var(--f5);
    color: inherit;
    background: var(--background);
    border: var(--border-width) solid color-mix(in srgb, currentColor 20%,
      transparent);
    border-radius: var(--br-sm);
    padding: var(--s1) var(--s2);
  }

  m-map .map-menu-lists {
    padding: var(--s2);
    border-top: var(--border-width) solid color-mix(in srgb, currentColor 12%,
      transparent);
    font-size: var(--f5);
  }

  m-map .map-menu-lists summary {
    cursor: pointer;
  }

  m-map .map-menu-list-option {
    display: flex;
    align-items: center;
    gap: var(--s2);
    padding: var(--s2) var(--s2) var(--s2) var(--s4);
    font-size: var(--f5);
    cursor: pointer;
  }

  /* MapLibre popups hardcode a white background (maplibre-gl.css), so both
     the surface and the text need an explicit, theme-aware colour instead of
     the library's fixed #fff/inherited body text — the tip triangle below
     gets the same treatment so it doesn't stay a white sliver on a dark
     popup. Not done with a blanket filter: unlike the ctrl-group icons,
     popups can contain real photo thumbnails, which a filter would invert.
     `!important`: this whole file lives in `@layer theme`, and maplibre-gl.css
     is unlayered — unlayered rules always beat layered ones regardless of
     source order, so without it the background stayed stuck at the library's
     `#fff` while the text below (which the library doesn't set) still
     switched to the dark theme's light body colour, leaving white-on-white. */
  m-map .maplibregl-popup-content,
  .maplibregl-popup-content {
    background: var(--background) !important;
  }

  m-map .maplibregl-popup-content,
  m-map .maplibregl-popup-content a,
  .maplibregl-popup-content,
  .maplibregl-popup-content a {
    color: var(--body);
  }

  .maplibregl-popup-content input,
  .maplibregl-popup-content textarea {
    color: var(--body);
    background: var(--background);
  }

  .maplibregl-popup-anchor-top .maplibregl-popup-tip,
  .maplibregl-popup-anchor-top-left .maplibregl-popup-tip,
  .maplibregl-popup-anchor-top-right .maplibregl-popup-tip {
    border-bottom-color: var(--background) !important;
  }

  .maplibregl-popup-anchor-bottom .maplibregl-popup-tip,
  .maplibregl-popup-anchor-bottom-left .maplibregl-popup-tip,
  .maplibregl-popup-anchor-bottom-right .maplibregl-popup-tip {
    border-top-color: var(--background) !important;
  }

  .maplibregl-popup-anchor-left .maplibregl-popup-tip {
    border-right-color: var(--background) !important;
  }

  .maplibregl-popup-anchor-right .maplibregl-popup-tip {
    border-left-color: var(--background) !important;
  }

  /* Popup bodies (m-bookmark-add-popup / m-bookmark-view-popup). MapLibre
     mounts popup content inside the map element's light DOM, so ordinary
     page classes reach it — no inline styles needed. */
  .bm-popup {
    display: flex;
    flex-direction: column;
    gap: 6px;
  }

  .bm-popup--add {
    min-width: 180px;
  }

  .bm-popup-address {
    margin: 0;
    font-size: 0.75em;
    opacity: 0.6;
    word-break: break-word;
    max-width: 220px;
  }

  .bm-popup input[type='text'] {
    display: block;
    width: 100%;
    box-sizing: border-box;
  }

  .bm-popup-save {
    width: 100%;
  }

  .bm-popup-thumbs {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
  }

  .bm-popup-thumb {
    width: 64px;
    height: 64px;
    object-fit: cover;
    border-radius: 3px;
    cursor: pointer;
    display: block;
  }

  .bm-popup-link {
    font-size: 0.8em;
  }

  /* ── Icons ────────────────────────────────────────────────────────────── */

  /* Monochrome icons painted as a mask so they inherit `currentColor`. An
     SVG in an `<img>` is a separate document and can't be recoloured from
     here, which is why it stayed black when the theme went dark. See
     `utils/icon.ts`. */
  .icon {
    display: inline-block;
    flex: none;
    width: 1em;
    height: 1em;
    background-color: currentColor;
    -webkit-mask: var(--icon) center / contain no-repeat;
    mask: var(--icon) center / contain no-repeat;
  }

  /* ── Header ───────────────────────────────────────────────────────────── */

  header {
    border-bottom: var(--border-width) solid currentColor;
    /* Civility lays out `body` as a grid with `header` as a grid item, whose
       default `min-width: auto` sizes it to the nowrap h1's full text width —
       pushing the header actions off-screen instead of letting the title
       ellipsize. */
    min-width: 0;
  }

  ui-header-content,
  m-header,
  m-bookmark-header,
  m-bookmarks-header,
  m-list-header {
    display: flex;
    align-items: center;
    gap: var(--s2);
    height: var(--header-height);
    padding: 0 var(--container-padding);
  }

  /* The share dialog is a header child for lifecycle reasons only; keep it
     out of the flex row. */
  m-bookmark-header civ-share-dialog,
  m-list-header civ-share-dialog {
    display: contents;
  }

  m-bookmark-header .bm-icon-btn {
    width: 34px;
    height: 34px;
  }

  m-list-header .list-view-count {
    font-size: var(--f5);
    opacity: 0.6;
    white-space: nowrap;
  }

  ui-header-content h1,
  m-header h1,
  m-bookmark-header h1,
  m-bookmarks-header h1,
  m-list-header h1 {
    flex: 1;
    /* A flex item's default `min-width: auto` floors its shrink at its
       content size — with `white-space: nowrap` that's the full title, so
       without this a long list/bookmark name pushes the header (and the
       page) wider than the viewport instead of ellipsizing. */
    min-width: 0;
    margin: 0;
    font-size: var(--f4);
    font-weight: var(--fw-semibold);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  m-header .header-back,
  m-bookmark-header .header-back,
  m-list-header .header-back {
    background: transparent;
    border: none;
    padding: var(--s1);
    color: var(--primary);
    cursor: pointer;
    flex-shrink: 0;
  }

  m-header .header-back .icon,
  m-bookmark-header .header-back .icon,
  m-list-header .header-back .icon {
    width: 20px;
    height: 20px;
  }
  m-header .header-actions,
  m-bookmark-header .header-actions,
  m-bookmarks-header .header-actions,
  m-list-header .header-actions {
    display: flex;
    align-items: center;
    gap: var(--s2);
  }

  /* Reserves a little room for the title so the header-actions row (which
     never shrinks — see `.bm-icon-btn`) starts folding into the overflow
     menu before the title is squeezed to nothing. */
  m-bookmark-header h1 {
    min-width: 3rem;
  }

  /* A trigger + panel used wherever actions collapse behind a
     `more-vertical` button: the bookmarks list toolbar, the list detail
     header, and the bookmark detail header once it runs out of room. */
  .header-menu {
    position: relative;
  }

  .header-menu-panel {
    position: absolute;
    top: calc(100% + var(--s2));
    right: 0;
    min-width: 180px;
    padding: var(--s1);
    display: flex;
    flex-direction: column;
    background: color-mix(in srgb, var(--primary) 6%, var(--background));
    color: var(--body);
    border-radius: var(--br-base);
    box-shadow: var(--shadow-lg);
    z-index: 20;
  }

  .header-menu-item {
    display: flex;
    align-items: center;
    gap: var(--s2);
    width: 100%;
    padding: var(--s2);
    background: transparent;
    border: none;
    border-radius: var(--br-sm);
    color: inherit;
    font-size: var(--f5);
    text-align: left;
    cursor: pointer;
    /* A tap on the kebab button and a tap on a menu item it just revealed
       land close together in time and space — without this, WebKit reads
       that as a double-tap and zooms the whole page instead of firing the
       second tap's click. */
    touch-action: manipulation;
  }

  .header-menu-item:hover {
    background: color-mix(in srgb, currentColor 8%, transparent);
  }

  .header-menu-item--danger {
    color: var(--error);
  }

  /* ── Fixed bottom tab bar ──────────────────────────────────────────────── */

  footer[fixed] {
    /* The tab bar is edge-to-edge, so the framework's gutter padding goes —
       but its safe-area padding has to be restored by hand, or the tabs sit
       under the home indicator. `height` covers the cutout too (the framework
       sets `box-sizing: border-box` here). */
    padding: 0 var(--safe-right) var(--safe-bottom) var(--safe-left);
    height: var(--footer-block);
    /*background-color: transparent;*/
    /*border: none;*/
  }

  /*ui-bottom-bar {
    background-color: transparent;
  }

  ui-bottom-bar a {
    opacity: 1;
  }*/

  ui-bottom-bar .icon {
    width: 24px;
    height: 24px;
  }

  /* ── Page host elements ────────────────────────────────────────────────── */
  body > main {
    min-width: 0;
  }

  r-list {
    display: block;
  }

  r-search,
  r-settings,
  r-settings-sync,
  r-settings-downloads,
  r-settings-about,
  r-bookmarks {
    display: block;
    padding: var(--s4) var(--s3);
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
    box-sizing: border-box;
  }

  /* ── Generic settings sections ──────────────────────────────────────────── */

  r-settings section,
  r-settings-sync section,
  r-settings-downloads section,
  r-settings-about section {
    margin-bottom: var(--s5);
  }

  r-settings h2,
  r-settings-sync h2,
  r-settings-downloads h2,
  r-settings-about h2 {
    font-size: var(--f4);
    font-weight: var(--fw-semibold);
    margin-bottom: var(--s3);
  }

  r-settings section > p,
  r-settings-sync section > p,
  r-settings-downloads section > p,
  r-settings-about section > p {
    opacity: 0.6;
    margin-bottom: var(--s3);
    font-size: var(--f5);
  }

  /* ── Settings hint trigger ────────────────────────────────────────────
     `.settings-toggle-row` and its `select` are the framework's now
     (`@layer components`); what stays here is the part it has no equivalent
     for. Maps' hints run long — three sentences on the online-tiles row — so
     the title doubles as a trigger and the description shows as a floating
     tooltip on hover, or stays pinned open on click/tap for touch. The
     framework's `label > span` hint is always visible, which is right for a
     one-line hint and wrong for these. */

  .settings-hint-trigger {
    position: relative;
    display: inline-flex;
    align-items: center;
    background: none;
    border: none;
    margin: 0;
    padding: 0;
    font: inherit;
    color: inherit;
    text-align: left;
    cursor: pointer;
  }

  .settings-hint-trigger > span:first-child {
    font-size: var(--f5);
    font-weight: var(--fw-medium);
    border-bottom: var(--border-width) dotted currentColor;
  }

  .settings-hint-text {
    position: absolute;
    left: 0;
    bottom: calc(100% + var(--s2));
    width: max-content;
    max-width: 260px;
    padding: var(--s2) var(--s3);
    background: color-mix(in srgb, var(--background) 97%, transparent);
    color: var(--body);
    border-radius: var(--br-base);
    box-shadow: var(--shadow-lg);
    font-size: var(--f6);
    font-weight: var(--fw-normal);
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--transition-fast);
    z-index: 5;
    pointer-events: none;
  }

  .settings-hint-trigger:hover .settings-hint-text,
  .settings-hint-trigger:focus-visible .settings-hint-text,
  .settings-hint-text.is-open {
    opacity: 1;
    visibility: visible;
  }

  /* ── Data actions (import/export) ────────────────────────────────────── */

  .action {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--s2);
    padding: var(--s3);
    border: var(--border-width) solid currentColor;
    border-radius: var(--br-base);
    text-decoration: none;
    color: inherit;
    font-weight: var(--fw-medium);
    transition: opacity var(--transition-base);
    width: 100%;
    box-sizing: border-box;
    background: transparent;
    font-size: var(--f5);
  }

  .action:hover {
    opacity: 0.7;
    transform: none;
  } /* An .action sized to its content — for buttons sitting inside a row. */
  .action--compact {
    width: auto;
    padding: var(--s2) var(--s3);
  }

  /* ── civ-sync-input component ─────────────────────────────────────────────── */

  civ-sync-input form {
    display: flex;
    flex-direction: column;
    gap: var(--s3);
  }

  civ-sync-input label {
    display: flex;
    flex-direction: column;
    gap: var(--s1);
    cursor: default;
    border: none;
    padding: 0;
    font-weight: var(--fw-normal);
    border-radius: 0;
    background: none;
  }

  civ-sync-input label span {
    font-size: var(--f6);
    opacity: 0.7;
  }

  civ-sync-input label input {
    width: 100%;
    box-sizing: border-box;
  } /* ── civ-sync-state component ─────────────────────────────────────────────── */
  civ-store-import div {
    display: flex;
    gap: var(--s3);
    margin-bottom: var(--s3);
  }

  /* Shared dot shape — used by both badge and inline/detailed inner dot */ /* Badge: the root span IS the dot */
  /* Status colors — applied via modifier class on the wrapper */ /* Inline variant */
  /* Detailed variant */
  .settings-data-status--error {
    color: var(--error);
  }

  /* ── m-download-progress component ──────────────────────────────────────
     Shared by the downloads page and the map's download button. Each host
     sizes and colours it through `--progress-width`/`--progress-color`
     rather than restyling the parts. */

  m-download-progress {
    display: flex;
    align-items: center;
    gap: var(--s2);
  }

  m-download-progress .progress-track {
    position: relative;
    overflow: hidden;
    width: var(--progress-width, 96px);
    height: 6px;
    border-radius: var(--br-full);
    background: color-mix(in srgb, currentColor 20%, transparent);
  }

  m-download-progress .progress-fill {
    height: 100%;
    border-radius: inherit;
    background: var(--progress-color, var(--primary));
    transition: width var(--transition-fast);
  }

  /* Unknown total: sweep a short bar instead of pretending to measure. */
  m-download-progress .progress-track--indeterminate .progress-fill {
    width: 40%;
    animation: progress-sweep 1.2s ease-in-out infinite;
  }
  @keyframes progress-sweep {
    0% {
      transform: translateX(-100%);
    }
    100% {
      transform: translateX(250%);
    }
  }
  @media (prefers-reduced-motion: reduce) {
    m-download-progress .progress-track--indeterminate .progress-fill {
      animation: none;
      width: 100%;
      opacity: 0.5;
    }
  }

  m-download-progress .progress-label {
    font-size: var(--f6);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    opacity: 0.7;
    min-width: 4ch;
    text-align: right;
  }

  /* ── Downloads page ─────────────────────────────────────────────────────── */

  .tile-list {
    display: flex;
    flex-direction: column;
    gap: var(--s2);
  }

  .tile-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s3);
    padding: var(--s3);
    border: var(--border-width) solid currentColor;
    border-radius: var(--br-base);
  }

  .tile-item-info {
    flex: 1;
    min-width: 0;
  }

  .tile-item-name {
    font-weight: var(--fw-semibold);
    font-size: var(--f5);
    display: block;
  }

  .tile-item-meta {
    font-size: var(--f6);
    opacity: 0.6;
    margin-top: 2px;
  }

  .tile-item-action {
    display: flex;
    align-items: center;
    gap: var(--s2);
    flex-shrink: 0;
  }

  .tile-status-cached {
    font-size: var(--f6);
    font-weight: var(--fw-medium);
    color: var(--success);
    white-space: nowrap;
  }

  /* ── Search page ────────────────────────────────────────────────────────── */

  .search-input-wrap {
    margin-bottom: var(--s3);
  }

  .search-input-wrap input[type='search'] {
    width: 100%;
    padding: var(--s2) var(--s3);
    border-radius: var(--br-full);
    font-size: var(--f5);
    box-sizing: border-box;
    margin: 0;
  }

  .search-section-title {
    font-size: var(--f6);
    font-weight: var(--fw-semibold);
    opacity: 0.45;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin: var(--s3) 0 var(--s1) 0;
    padding: 0;
  }

  .search-history-list {
    display: flex;
    flex-direction: column;
    gap: var(--s2);
    max-width: 95vw;
    margin: auto;
  }

  .search-history-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--s3) var(--s3);
    gap: var(--s3);
    cursor: pointer;
    background: transparent;
    border: var(--border-width) solid currentColor;
    border-radius: var(--br-base);
    color: inherit;
    text-align: left;
    font-size: var(--f5);
    width: 100%;
    min-width: 0;
    transition: opacity var(--transition-fast);
    touch-action: manipulation;
  }

  .search-history-item:hover {
    opacity: 0.6;
    transform: none;
  }

  .search-history-item > span {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* A bookmark or online result: name + address/coords stacked, each
     independently truncated — overrides the single-line rule above, which
     was written for the plain query-text rows and would otherwise force
     this two-line block onto one line. */
  .search-history-item > span.search-result-text {
    display: flex;
    flex-direction: column;
    gap: 1px;
    white-space: normal;
  }

  .search-result-title {
    display: block;
    font-size: var(--f5);
    font-weight: var(--fw-medium);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .search-result-subtitle {
    display: block;
    font-size: var(--f6);
    opacity: 0.55;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .search-empty {
    padding: var(--s5) var(--s3);
    text-align: center;
    opacity: 0.5;
    font-size: var(--f5);
  }

  .search-history-clear {
    display: flex;
    justify-content: flex-end;
    padding: var(--s1) 0 var(--s2);
  }

  /* ── Bookmarks page ─────────────────────────────────────────────────────── */

  .bm-empty {
    padding: var(--s5) var(--s3);
    text-align: center;
    opacity: 0.5;
    font-size: var(--f5);
  }

  .bm-empty--folder {
    padding: var(--s3);
  }

  .bm-folder {
    display: flex;
    align-items: center;
    gap: var(--s2);
    padding: var(--s3);
    border: var(--border-width) solid currentColor;
    border-radius: var(--br-base);
    color: inherit;
    text-decoration: none;
  }

  .bm-folder-name {
    flex: 1;
    font-weight: var(--fw-semibold);
    font-size: var(--f5);
  }

  .bm-folder-count {
    font-size: var(--f6);
    opacity: 0.5;
    flex-shrink: 0;
  }

  .bm-section {
    margin-bottom: var(--s2);
  }

  .bm-section-title {
    font-size: var(--f6);
    font-weight: var(--fw-semibold);
    opacity: 0.5;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: var(--s2);
    padding: 0 var(--s3);
  }

  .bm-folder,
  .bm-section {
    max-width: 90vw;
    margin: var(--s2) auto var(--s2) auto;
  }

  .bm-item {
    display: flex;
    align-items: center;
    gap: var(--s2);
    padding: var(--s2) var(--s3);
  }

  /* The row body is the link to the bookmark's page; the trailing icon
     buttons stay separately clickable. */
  .bm-item-info {
    flex: 1;
    min-width: 0;
    display: block;
    color: inherit;
    text-decoration: none;
  }

  .bm-item-name {
    display: block;
    font-size: var(--f5);
    font-weight: var(--fw-medium);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .bm-item-coords {
    display: block;
    font-size: var(--f6);
    opacity: 0.5;
    margin-top: 1px;
  }

  .bm-item-actions {
    display: flex;
    align-items: center;
    gap: var(--s1);
    flex-shrink: 0;
  }

  .bm-icon-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: var(--br-sm);
    cursor: pointer;
    opacity: 0.6;
    flex-shrink: 0;
    transition: opacity var(--transition-fast);
    touch-action: manipulation;
  }

  .bm-icon-btn:hover {
    opacity: 1;
    transform: none;
  }

  .bm-item-checkbox {
    flex-shrink: 0;
  }

  /* "Select" mode toolbar — a dedicated cancel button + select-all/count on
     the left (the only way out used to be a "Cancel selection" entry buried
     in the header's kebab menu), a `.header-menu`-style Actions dropdown on
     the right. */
  .bm-select-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s2);
    padding: var(--s2) var(--s3);
    max-width: 90vw;
    margin: 0 auto var(--s2) auto;
  }

  .bm-select-bar-left {
    display: flex;
    align-items: center;
    gap: var(--s1);
    min-width: 0;
  }

  .bm-select-cancel {
    margin: 0 calc(-1 * var(--s1)) 0 0;
  }

  .bm-select-all {
    display: flex;
    align-items: center;
    gap: var(--s2);
    font-size: var(--f5);
    font-weight: var(--fw-medium);
    min-width: 0;
  }

  .bm-select-actions {
    position: relative;
  }

  .bm-select-actions-btn {
    display: flex;
    align-items: center;
    gap: var(--s1);
    padding: var(--s1) var(--s2);
    background: transparent;
    border: var(--border-width) solid currentColor;
    border-radius: var(--br-sm);
    color: inherit;
    font-size: var(--f5);
    cursor: pointer;
    touch-action: manipulation;
  }

  .bm-select-actions-btn:hover {
    opacity: 0.7;
    transform: none;
  }

  .bm-select-actions-btn:disabled {
    opacity: 0.4;
    cursor: default;
  }

  .bm-select-actions .header-menu-panel {
    right: 0;
  }

  .bm-add-form {
    margin-top: var(--s3);
    padding: var(--s3);
    border: var(--border-width) solid currentColor;
    border-radius: var(--br-base);
    display: flex;
    flex-direction: column;
    gap: var(--s2);
  }

  .bm-coord-row {
    display: flex;
    gap: var(--s2);
  }

  .bm-coord-row input {
    flex: 1;
    min-width: 0;
  }

  .bm-form-actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--s2);
  }

  .bm-btn-danger {
    margin-left: auto;
    color: var(--error);
  }

  /* ── Bookmark edit dialog ───────────────────────────────────────────────── */

  .bm-dialog {
    padding: var(--s4);
    min-width: min(320px, 90vw);
    display: flex;
    flex-direction: column;
    gap: var(--s3);
  }

  .bm-dialog-title {
    font-size: var(--f4);
    font-weight: var(--fw-semibold);
    margin: 0;
  }

  .bm-dialog form {
    display: flex;
    flex-direction: column;
    gap: var(--s3);
  }

  .bm-dialog .bm-form-actions {
    margin-top: var(--s1);
  }

  .bm-dialog textarea {
    resize: vertical;
    min-height: 60px;
    font-family: inherit;
    font-size: var(--f5);
  }

  .bm-dialog-coords {
    font-size: var(--f6);
    opacity: 0.5;
    margin: 0;
    font-variant-numeric: tabular-nums;
  }

  /* `<civ-share-link>`'s URL is one unbreakable token; without help it forces
     the flex row (and the dialog around it) wider than the viewport instead
     of wrapping, which is what forced the horizontal scroll. */
  civ-share-link {
    display: block;
    min-width: 0;
  }
  .bm-export-actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--s2);
  }

  .bm-scope-toggle {
    display: flex;
    gap: var(--s2);
  }

  .bm-scope-btn {
    flex: 1;
    padding: var(--s2);
    border: var(--border-width) solid currentColor;
    border-radius: var(--br-sm);
    background: transparent;
    color: inherit;
    font: inherit;
    cursor: pointer;
    opacity: 0.7;
  }

  .bm-scope-btn:hover {
    opacity: 1;
  }

  .bm-scope-btn[aria-pressed='true'] {
    background: var(--success);
    border-color: transparent;
    color: white;
    opacity: 1;
  }

  .bm-image-thumb {
    position: relative;
    width: 80px;
    height: 80px;
    flex-shrink: 0;
  }

  .bm-image-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 4px;
    display: block;
  }

  .bm-image-remove {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 20px;
    height: 20px;
    padding: 0;
    line-height: 1;
    font-size: 14px;
    border-radius: 50%;
    cursor: pointer;
  }

  .bm-image-add {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 80px;
    height: 80px;
    border: 2px dashed currentColor;
    border-radius: 4px;
    font-size: 24px;
    opacity: 0.5;
    cursor: pointer;
    flex-shrink: 0;
  }

  .bm-image-add:hover {
    opacity: 1;
  }

  /* A lightbox isn't a card: drop the dialog's width cap, background and
     shadow so the photo itself is the surface, and darken the backdrop. */
  dialog:has(.bm-lightbox) {
    max-width: none;
    width: auto;
    background: transparent;
    box-shadow: none;
  }

  dialog:has(.bm-lightbox)::backdrop {
    opacity: 0.85;
  }

  .bm-lightbox {
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .bm-lightbox img {
    max-width: 96vw;
    max-height: 90vh;
    object-fit: contain;
    display: block;
    border-radius: var(--br-sm);
    cursor: zoom-out;
  }

  /* ── Import confirmation dialog ─────────────────────────────────────────── */

  .bm-import-error {
    font-size: var(--f6);
    color: var(--error);
    margin: 0 0 var(--s3);
  }

  .bm-import-summary {
    font-size: var(--f5);
    opacity: 0.6;
    margin: 0;
  }

  .bm-import-group + .bm-import-group {
    border-top: var(--border-width) solid currentColor;
  }

  .bm-import-group-name {
    font-size: var(--f6);
    font-weight: var(--fw-semibold);
    opacity: 0.5;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin: 0;
    padding: var(--s2) var(--s3) var(--s1);
  }

  .bm-import-item {
    padding: var(--s2) var(--s3);
    display: flex;
    flex-direction: column;
    gap: 1px;
  }

  .bm-import-item + .bm-import-item {
    border-top: var(--border-width) solid color-mix(in srgb, currentColor 15%,
      transparent);
  }

  .bm-import-item--duplicate {
    opacity: 0.4;
  }

  .bm-import-dupe-badge {
    font-size: var(--f7);
    font-weight: var(--fw-semibold);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    opacity: 0.7;
  }

  /* ── Welcome dialog ─────────────────────────────────────────────────────── */

  .welcome-dialog {
    padding: var(--s4);
    min-width: min(420px, 90vw);
    display: flex;
    flex-direction: column;
    gap: var(--s3);
  }

  .welcome-title {
    font-size: var(--f3);
    font-weight: var(--fw-semibold);
    margin: 0;
  }

  .welcome-intro {
    margin: 0;
    font-size: var(--f5);
    opacity: 0.75;
  }

  .welcome-section {
    display: flex;
    flex-direction: column;
    gap: var(--s2);
    padding-top: var(--s3);
    border-top: var(--border-width) solid color-mix(in srgb, currentColor 15%,
      transparent);
  }

  .welcome-section h3 {
    margin: 0;
    font-size: var(--f4);
    font-weight: var(--fw-semibold);
  }

  .welcome-muted {
    margin: 0;
    font-size: var(--f6);
    opacity: 0.6;
  }

  .welcome-link {
    font-size: var(--f5);
    color: var(--primary);
    text-decoration: none;
    padding: var(--s1) 0;
    align-self: flex-start;
  }

  .welcome-link:hover {
    opacity: 0.7;
  }

  .welcome-done {
    margin: 0;
    font-size: var(--f6);
    color: var(--success);
    font-weight: var(--fw-medium);
  }

  .welcome-error {
    margin: 0;
    font-size: var(--f6);
    color: var(--error);
  }

  .welcome-actions {
    display: flex;
    justify-content: flex-end;
    margin-top: var(--s2);
  }

  .welcome-tile-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: var(--s2);
  }

  .welcome-tile-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s3);
    padding: var(--s2) var(--s3);
    border: var(--border-width) solid currentColor;
    border-radius: var(--br-base);
  }

  .welcome-tile-label {
    font-size: var(--f5);
    font-weight: var(--fw-medium);
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .welcome-tile-action {
    flex-shrink: 0;
  }
  @media (max-width: 768px) {
    input,
    textarea,
    select {
      font-size: 1rem;
    }
  }

  /* ── Public shared-location viewer (?id=&server=) ─────────────────────── */

  .share-view-loading {
    display: flex;
    justify-content: center;
    padding: var(--s6) 0;
  }

  .share-view {
    display: block;
    max-width: 640px;
    margin: 0 auto;
    padding: 1rem;
  }

  /* Shared by the public share viewer and the bookmark detail page. Tall enough
     to be worth panning around in, without pushing the details below the fold. */
  m-mini-map {
    display: block;
    width: 100%;
    height: min(60vh, 380px);
    border-radius: var(--br-base);
    overflow: hidden;
    background: hsl(47, 26%, 88%);
  }

  m-mini-map .mini-map {
    width: 100%;
    height: 100%;
  }

  /* ── Bookmark detail page ──────────────────────────────────────────────── */

  /* Matches the other page hosts: centred column, page gutter, and room for
     the fixed tab bar. */
  .bookmark-page {
    display: block;
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
    padding: var(--s3) var(--s3) calc(var(--footer-block) + var(--s4));
    box-sizing: border-box;
  }

  .bookmark-page-body {
    display: flex;
    flex-direction: column;
    gap: var(--s3);
    padding: var(--s3) 0 0;
  }

  /* The name is already the header title; in the body it's a quieter anchor. */
  .bookmark-page-title {
    margin: 0;
    font-size: var(--f4);
    font-weight: var(--fw-semibold);
  }

  .bookmark-page-body > * {
    margin: 0;
  }

  /* A value with its copy button beside it. */
  .bookmark-page-field {
    display: flex;
    align-items: center;
    gap: var(--s2);
    margin: 0;
  }

  .bookmark-page-field > span:first-child {
    min-width: 0;
  }

  .bookmark-page-copy {
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.75rem;
    height: 1.75rem;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: var(--br-sm);
    color: inherit;
    opacity: 0.55;
    cursor: pointer;
  }

  .bookmark-page-copy:hover {
    opacity: 1;
  }

  .bookmark-page-lists {
    display: flex;
    flex-wrap: wrap;
    gap: var(--s2);
    margin: 0;
  }

  .bookmark-page-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.35em;
    padding: 0.15rem 0.55rem;
    border: var(--border-width) solid color-mix(in srgb, currentColor 25%,
      transparent);
    border-radius: var(--br-full);
    font-size: var(--f6);
    text-decoration: none;
    color: inherit;
  }

  .bookmark-page-chip-dot {
    width: 0.6em;
    height: 0.6em;
    border-radius: 50%;
  }

  /* Name, address and coordinates as one block; the verbs live in the header. */
  .bookmark-page-head-text {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    min-width: 0;
  }

  .bookmark-page-lists-field {
    border: var(--border-width) solid color-mix(in srgb, currentColor 20%,
      transparent);
    border-radius: var(--br-base);
    padding: var(--s2) var(--s3);
  }

  .bookmark-page-lists-field legend {
    font-size: var(--f6);
    opacity: 0.6;
    padding: 0 var(--s2);
  }

  .bookmark-page-list-option {
    display: flex;
    align-items: center;
    gap: var(--s2);
    padding: var(--s2) 0;
    cursor: pointer;
  }

  .bookmark-page-list-option input {
    width: auto;
    margin: 0;
  }

  .bookmark-page-form {
    display: flex;
    flex-direction: column;
    gap: var(--s2);
  }

  /* Header icon button in its "on" state (visited). */
  .bm-icon-btn--on {
    background: var(--success);
    color: white;
    border-radius: var(--br-sm);
  }

  /* Header icon button in its "on" state (favorite). */
  .bm-icon-btn--favorite-on {
    background: var(--pink);
    color: white;
    border-radius: var(--br-sm);
  }

  /* The heart badge shown next to a favorited location's name — the map
     popup, shared-list rows, and the owner's own bookmarks/detail pages. */
  .bm-favorite-badge {
    color: var(--pink);
    font-size: 0.9em;
    display: inline-flex;
    vertical-align: -0.1em;
  }

  /* The share icon shown next to a list/bookmark's name on `/bookmarks` when
     it currently has a live public link. */
  .bm-share-badge {
    opacity: 0.6;
    font-size: 0.9em;
    display: inline-flex;
    vertical-align: -0.1em;
  }

  .share-view-body {
    padding: 1rem 0.25rem;
  }

  .share-view-swatch,
  .bm-folder-swatch {
    display: inline-block;
    width: 0.7em;
    height: 0.7em;
    border-radius: 50%;
    vertical-align: baseline;
    flex-shrink: 0;
  }

  /* Sized to its own content so `@container` below can react to how much
     room is actually left once the title has taken its share. */
  .share-view-header {
    container-type: inline-size;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--s2);
    margin-bottom: 0.35rem;
  }

  .share-view-actions {
    display: flex;
    flex-shrink: 0;
    gap: var(--s1);
  }

  .share-view-action-btn {
    display: flex;
    align-items: center;
    gap: 0.35em;
    padding: var(--s1) var(--s2);
    background: transparent;
    border: none;
    border-radius: var(--br-sm);
    color: inherit;
    font: inherit;
    font-weight: var(--fw-medium);
    white-space: nowrap;
    cursor: pointer;
    opacity: 0.75;
  }

  .share-view-action-btn:hover {
    opacity: 1;
    background: color-mix(in srgb, currentColor 8%, transparent);
  }
  @container (max-width: 380px) {
    .share-view-action-label {
      display: none;
    }
  }

  .share-view-list {
    list-style: none;
    margin: 0.5rem 0 1rem;
    padding: 0;
    display: flex;
    flex-direction: column;
  }

  .share-view-list-row {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    border-bottom: var(--border-width) solid color-mix(in srgb, currentColor
      12%, transparent);
  }

  .share-view-list-item {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    flex: 1;
    min-width: 0;
    text-align: left;
    background: transparent;
    border: none;
    padding: 0.6rem 0.25rem;
    cursor: pointer;
    color: inherit;
    font: inherit;
  }

  .share-view-list-item:hover {
    background: color-mix(in srgb, currentColor 5%, transparent);
  }

  .share-view-list-name {
    font-weight: var(--fw-medium);
  }

  .share-view-list-meta {
    font-size: 0.8em;
    opacity: 0.6;
    word-break: break-word;
  }

  .share-view-list-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: none;
    width: 32px;
    height: 32px;
    margin-top: 0.4rem;
    background: transparent;
    border: none;
    border-radius: var(--br-sm);
    cursor: pointer;
    opacity: 0.6;
    transition: opacity var(--transition-fast), transform
      var(--transition-fast);
  }

  .share-view-list-toggle:hover {
    opacity: 1;
  }

  .share-view-list-toggle[aria-expanded='true'] {
    transform: rotate(180deg);
  }

  .share-view-list-detail {
    flex: 1 1 100%;
    padding: 0 0.25rem 0.6rem;
  }

  .share-view-list-empty {
    list-style: none;
    opacity: 0.7;
    padding: 0.6rem 0.25rem;
  }

  .share-view-reset {
    margin-left: 0.5rem;
    background: none;
    border: none;
    color: inherit;
    font: inherit;
    text-decoration: underline;
    opacity: 0.8;
    cursor: pointer;
  }

  .share-view-reset:hover {
    opacity: 1;
  }

  .share-view-title {
    margin: 0;
    font-size: 1.25rem;
    min-width: 0;
    overflow-wrap: break-word;
  }

  /* The tick is an icon now, so it needs to sit on the text baseline. */
  .share-view-visited,
  .bm-visited-badge {
    color: var(--success);
    font-size: 0.9em;
    display: inline-flex;
    vertical-align: -0.1em;
  }

  .share-view-coords {
    margin: 0 0 0.5rem;
    opacity: 0.6;
    font-size: var(--f6);
    font-variant-numeric: tabular-nums;
  }

  .share-view-address {
    margin: 0 0 0.5rem;
    font-size: var(--f5);
  }

  .share-view-description {
    margin: 0 0 0.75rem;
    white-space: pre-wrap;
  }

  /* On the detail page the photo grid carries its own [+] tile, so it needs to
     lay out like the editor's grid rather than a plain image row. */
  .bookmark-page .share-view-images {
    display: flex;
    flex-wrap: wrap;
    gap: var(--s2);
    align-items: center;
  }

  .bookmark-page .share-view-images .bm-image-thumb,
  .bookmark-page .share-view-images .share-view-image {
    width: 80px;
    height: 80px;
    object-fit: cover;
  }

  .share-view-images {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
  }

  .share-view-image {
    width: 120px;
    height: 120px;
    object-fit: cover;
    border-radius: var(--br-sm);
    display: block;
  }

  .bookmark-page-links {
    display: flex;
    flex-wrap: wrap;
    gap: var(--s2);
    margin: 0 0 0.75rem;
    padding: 0;
    list-style: none;
  }

  .bookmark-page-link {
    display: inline-flex;
    align-items: center;
    gap: 0.4em;
    padding: 0.3rem 0.6rem;
    border: var(--border-width) solid color-mix(in srgb, currentColor 25%,
      transparent);
    border-radius: var(--br-full);
    font-size: var(--f5);
    text-decoration: none;
    color: inherit;
    transition: opacity var(--transition-fast);
  }

  .bookmark-page-link:hover {
    opacity: 0.7;
  }

  .bookmark-page-links-field {
    border: var(--border-width) solid color-mix(in srgb, currentColor 20%,
      transparent);
    border-radius: var(--br-base);
    padding: var(--s2) var(--s3);
    display: flex;
    flex-direction: column;
    gap: var(--s2);
  }

  .bookmark-page-links-field legend {
    font-size: var(--f6);
    opacity: 0.6;
    padding: 0 var(--s2);
  }

  .bookmark-page-link-row {
    display: flex;
    align-items: center;
    gap: var(--s2);
  }

  .bookmark-page-link-row input {
    flex: 1;
    min-width: 0;
  }
  @media (max-width: 480px) {
    .bookmark-page-link-row {
      flex-wrap: wrap;
    }

    .bookmark-page-link-row input[type='url'] {
      flex-basis: calc(100% - 2.25rem);
    }
  }

  .bookmark-page-link-remove {
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.75rem;
    height: 1.75rem;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: var(--br-sm);
    color: inherit;
    opacity: 0.55;
    cursor: pointer;
  }

  .bookmark-page-link-remove:hover {
    opacity: 1;
  }

  .share-view-open {
    display: inline-block;
    margin-top: 0.5rem;
    color: var(--primary);
    font-weight: 600;
  }

  .bookmark-page-visitor-actions {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--s3);
  }

  .bookmark-page-export-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.35em;
    margin-top: 0.5rem;
    padding: 0;
    background: transparent;
    border: none;
    color: var(--primary);
    font-weight: 600;
    font-size: inherit;
    font-family: inherit;
    cursor: pointer;
  }

  .bookmark-page-export-btn:hover {
    opacity: 0.7;
  }

  .share-view-error {
    opacity: 0.7;
  }
}
