/* ═══ MOBILE / RESPONSIVE ═══
   Loaded AFTER styles.css and the theme; everything here sits in a media query, so the desktop is unchanged.
   One file instead of rules spread over styles.css: the layout is the same in every theme, so responsive
   rules belong here exactly once. Breakpoints: ≤ 900px tablet, ≤ 640px phone, ≤ 380px small phone.
   !important only where an inline style of the templates demonstrably holds against it. */


/* ── Tablet ──────────────────────────────────────────────────────────────── */
@media (max-width: 900px) {

  main:not(#login-screen) { padding: 1rem; }

  /* Zweispaltige Bereiche zusammenklappen */
  .sgrid2         { grid-template-columns: 1fr; gap: 1rem; }

  .fgrid { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: .75rem; }
}


/* ── Phone ─────────────────────────────────────────────────────────────── */
@media (max-width: 640px) {

  /* ─ Skeleton ─ */
  main:not(#login-screen) { padding: .75rem 16px 5rem; }         /* 16 px margin; room at the bottom for the thumb */

  /* With large text the header grows and wraps instead of shortening names. */
  header {
    padding: 4px .75rem;
    height: auto;
    min-height: 52px;
    gap: .5rem;
  }
  header .logo span   { display: none; }      /* Shorten the word mark */
  header .logo .header-view:not([hidden]) { display: block; }
  /* With a page name only logo and name: both words together would be cut off.
     While the heading itself is visible, the brand stands there. */
  header .logo .header-view.title-visible { display: none; }
  header .logo:has(.header-view:not([hidden]):not(.title-visible)) .brand { display: none; }
  header .logo .header-view::before { content: none; }
  /* The NAME is shortened, not the button: the three stripes live in the same element, and a width limit on
     the button cut them off first on the phone. */
  .ubadge-name        { max-width: 9ch; }
  /* On the phone only the icon: the name is in the menu, cut off it did not help. */
  .ubadge .ubadge-name { display: none; }
  .ubadge .ubadge-ico  { display: inline-block; width: 20px; height: 20px; }

  /* Capture bar: main button and import menu side by side; the menu opens across the full width of the bar. */
  .capture-bar { flex-wrap: wrap; position: relative; }
  .capture-bar .btn.bp { flex: 1 1 auto; justify-content: center; }
  .capture-bar .import-menu { flex: 0 0 auto; position: static; }
  .import-toggle { left: 0; right: 0; min-width: 0; }

  /* Tile: year and theme wrap, the badges below. */
  .smeta { flex-direction: column; align-items: flex-start; gap: 4px; }
  .smeta > span { max-width: 100%; }

  /* Filters behind a button: search and view stay, the select fields expand. Before, five fields took half the screen. */
  .filter-button { display: inline-flex; align-items: center; gap: 6px; }
  /* Expanded, the button looks pressed like the selected view next to it: aria-expanded alone was not visible. */
  .filter-button[aria-expanded="true"] { background: var(--b50); color: var(--b700); border-color: var(--b600); box-shadow: inset 0 0 0 1px var(--b600); }
  .filter-bar > input.si, .filter-bar > input.search-field { flex: 1 1 140px; width: auto !important; min-width: 0; }
  .filter-bar > select { display: none; }
  /* max-width and min-width 0: otherwise the field takes the width of its longest option. */
  .filter-bar.open > select { display: block; flex: 1 1 100%; width: 100%; max-width: 100%; min-width: 0; }
  /* On the phone cards only (js/dom.js phoneWidth): a table row would be a screen high. */
  .filter-bar > .filter-button { order: -2; }
  :is(.filter-bar, .filter-row) > .vtgl { display: none; }

  /* The statistics in the header do not need the room */
  /* Header: name and import bar give way, the user menu never. */
  header .logo        { min-width: 0; flex: 0 1 auto; }
  header .logo .brand, header .logo .header-view { min-width: 0; white-space: normal; overflow: visible;
    text-overflow: clip; overflow-wrap: anywhere; line-height: 1.15; }
  /* The brand breaks at the <wbr> ("Brick / Inventory"), the page name by syllables. */
  header .logo .header-view { hyphens: auto; }
  .hright             { min-width: 0; flex: 1 1 auto; justify-content: flex-end; gap: .5rem; }
  .umenu              { flex: 0 0 auto; }
  /* The panel ends above the bottom bar (two rows with large text). */
  .umenu-pop          { max-height: calc(100dvh - 9rem); }
  #global-import-bar  { min-width: 0 !important; max-width: none !important; flex: 1 1 60px; }

  /* ─ Navigation ──────────────────────────────────────────────────────── */
  /* Five targets fit side by side: bar at the bottom, within thumb reach, icon above label. */
  nav {
    position: fixed;
    top: auto; bottom: 0; left: 0; right: 0;
    z-index: 100;
    padding: 0 0 env(safe-area-inset-bottom);
    border-top: 1px solid var(--bdr);
    border-bottom: none;
    justify-content: space-around;
    overflow: visible;
    box-shadow: 0 -2px 10px rgba(15, 23, 42, .06);
  }
  .ntab {
    flex: 1 1 0;
    min-width: 0;
    flex-direction: column;
    gap: 3px;
    padding: 8px 2px 7px;                       /* ≥ 44px hit area */
    font-size: var(--fs-0);
    border-bottom: none;
    border-top: 2px solid transparent;
    margin: -1px 0 0;
  }
  /* Wrap instead of cutting: with increased letter spacing (WCAG 1.4.12) the "Collection" label no longer fit into 64 px. */
  .ntab .tab-label { max-width: 100%; white-space: normal; overflow-wrap: break-word; hyphens: auto; text-align: center; line-height: 1.15; }
  .ntab .ico { width: 1.375rem; height: 1.375rem; }
  .ntab.active, .ntab.group-active { border-top-color: var(--b600); color: var(--b600); }

  .collection-choice { display: flex; width: 100%; }
  .collection-choice button { flex: 1 1 0; justify-content: center; padding: 8px 4px; }

  /* ─ Surfaces and cards ─ */
  .card, .sc, .ssec  { padding: 1rem .85rem; border-radius: 12px; }
  .ptitle            { font-size: var(--fs-5); }

  /* Two columns from about 360 px, one below and with large text. */
  .sgrid       { grid-template-columns: repeat(auto-fill, minmax(min(100%, 9.5rem), 1fr)); gap: .7rem; }
  /* Parts and minifigs in two columns like the sets: three narrow columns left half a row empty with one or
     two minifigs and looked different from the collection. */
  .parts-grid, .parts-list, .man-grid { grid-template-columns: repeat(auto-fill, minmax(min(100%, 8.5rem), 1fr)); gap: .7rem; }
  /* minmax(0, …) instead of 1fr: a column may get narrower than its longest number, which then wraps —
     otherwise the total value pushed the tile over the edge. */
  .fgrid       { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .6rem; }
  .fstat       { min-width: 0; padding: .85rem .9rem; }
  /* An amount never breaks inside the number: one line, the font shrinks with the width; below 420 px one tile per row. */
  .fval        { white-space: nowrap; font-size: clamp(1.05rem, 5.4vw, var(--fs-6)); }

  /* ─ Formularzeilen ──────────────────────────────────────────────────── */
  .frow, .sform {
    flex-wrap: wrap;
    gap: .5rem;
  }
  .frow > * { min-width: 0; }
  /* Form fields two per row, the first and the button across the full width. The max-width of the inline
     styles only fits the desktop. */
  .frow > .fg         { flex: 1 1 calc(50% - .25rem); max-width: none !important; }
  .frow > .fg:first-child, .frow > .btn { flex: 1 1 100%; }
  /* New user: password and checkbox one full row each; two per row broke the password rule into three lines. */
  .user-new > .fg { flex-basis: 100%; max-width: none; }
  .user-new > .user-role { margin-top: 0; }
  /* In grids the column sets the width, not the min width of .fg */
  [style*="display:grid"] > .fg, .grid-160 > .fg { min-width: 0; }

  /* Rows without wrapping (inline styles of the templates) may wrap instead of widening the page. Except what
     stays single-line on purpose, and labels: there the text slid below its checkbox. */
  main [style*="display:flex"]:not([style*="nowrap"]):not([style*="column"]):not(label),
  .modal [style*="display:flex"]:not([style*="nowrap"]):not([style*="column"]):not(label),
  main .row:not(label), .modal .row:not(label) { flex-wrap: wrap; }

  /* Detail rows: label on top, controls below when it gets tight */
  .dr                 { flex-wrap: wrap; gap: .35rem .75rem; }
  .dr > *             { min-width: 0; }
  .dr select, .dr input { max-width: 100%; }
  /* Price alert on the phone: label on top, the three fields as their own full-width row. */
  .dr.alert-row { flex-direction: column; align-items: stretch !important; }
  .dr.alert-row > .dv { width: 100%; }
  .alert-row .alert-field { flex: 1 1 0; min-width: 0; width: auto; }
  /* Condition, direction and price do not fit one line on a small phone: the selects share the first, the
     price with its label the second. */
  .alert-row .dv > select.alert-field { flex: 1 1 8rem; }
  /* The currency stays on the line of the price field instead of centred below it. */
  .alert-row .alert-price { flex: 1 1 calc(100% - 4.5rem); display: flex; flex-wrap: wrap; align-items: center; gap: .25rem .5rem; }
  .alert-row .alert-currency { flex: none; margin-left: 0; }
  .alert-row .alert-price > input { flex: 1 1 auto; }

  /* Key figure tiles: number on one line */
  /* With large text a long label wraps instead of sticking out. */
  .btn { white-space: normal; text-align: center; }
  input, select, textarea, .btn {
    font-size: 16px;              /* < 16px makes iOS zoom in on focus */
    font-size: max(16px, 1rem);   /* … and with large text it grows along */
    min-height: 44px;
  }
  /* 44 px is the size of a fingertip (Apple HIG, WCAG 2.5.5). The small variants stay small on the desktop;
     on the phone it applies to all. */
  .btn, .btn-sm, .vb, .v-btn, .period-btn, .ubadge-button, .collection-choice button, .umenu-item,
  details > summary, .import-menu > summary, #tab-profile .btn, .acq-delete { min-height: 44px; }
  .vb, .v-btn, .btn-sm, .ubadge-button, .acq-delete { min-width: 44px; }
  .location-icon { min-width: 44px; }
  .chip-remove { min-width: 44px; min-height: 44px; margin: 0; }
  .user-tbl .roles-cell { justify-content: flex-end; }
  .mon-plan, .mon-plan input { min-height: 44px; }
  #monitor-content a.close-view { min-height: 44px; min-width: 44px; justify-content: center; }
  /* Checkbox: drawn at 22 px but with a 44 px target on the box itself — even without a label. Two background
     layers paint the box in the centre; the negative margin keeps 22 px in the layout. !important: some
     checkboxes carry an inline width:auto. */
  input[type="radio"] { width: 22px !important; height: 22px; min-height: 0; }
  input[type="checkbox"] {
    -webkit-appearance: none; appearance: none; flex: 0 0 auto;
    width: 44px !important; height: 44px !important; min-height: 0; margin: -11px !important;
    padding: 0; border: 0; border-radius: 0; cursor: pointer;
    background: linear-gradient(var(--sur), var(--sur)) center / 18px 18px no-repeat,
                linear-gradient(var(--field-border), var(--field-border)) center / 22px 22px no-repeat;
  }
  input[type="checkbox"]:checked {
    background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8.5l3 3 6-7' fill='none' stroke='%23fff' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 16px 16px no-repeat,
                linear-gradient(var(--b600), var(--b600)) center / 22px 22px no-repeat;
  }
  input[type="checkbox"]:disabled { opacity: .5; cursor: default; }
  input[type="checkbox"]:focus-visible { outline-offset: -9px; }
  /* Targets at least 44 px — also the delete button and the links to the keys in settings and setup. */
  .ca { top: 4px; right: 4px; z-index: 2; }
  .delbtn { width: 44px; height: 44px; }
  .ii a, .ii-remove { min-height: 44px; }
  .ii-remove { min-width: 44px; }
  /* Table rows that open a dialog (sets of a part) are targets. */
  tr[tabindex="0"] > td { height: 44px; }
  /* Room for the 44 px button so it covers neither image nor price. */
  .man-tile:has(> .ca) { padding-top: 52px; }
  .mk-row { padding-right: 56px; }
  /* Tag and price get their own line below the text: next to the image there is no room for three columns,
     and the title would be 0 px wide. */
  .mk-row { display: grid; grid-template-columns: auto minmax(0, 1fr); column-gap: 12px; row-gap: 6px; }
  .mk-row > .mk-right { grid-column: 2; }
  .ibox a[href], a.text-1[href] { display: inline-flex; align-items: center; min-height: 44px; }
  /* Long labels wrap instead of sticking out. */
  .ssec .btn { white-space: normal; }
  /* A checkbox inside its label: the whole row is the target. */
  label:has(> input[type="checkbox"]), label:has(> input[type="radio"]) { min-height: 44px; display: flex; align-items: center; }

  /* Neutralise fixed px widths from the inline styles of the templates */
  [style*="width:"][style*="px"]:not(img):not(svg):not(.color-dot):not(.man-tile-swatch),
  :is(.search-field, .min-100, .max-82, .max-110, .max-120, .max-150, .max-160, .max-220, .max-240, .max-360,
      .max-420, .max-440, .max-480, .max-520, .max-560, .max-620, .max-720, .max-780)[class] {
    max-width: 100% !important;
  }

  /* ─ Tables → cards ───────────────────────────────────────────────── */
  /* The data-label attributes come from mobile.js: a MutationObserver stamps every <td> with its column label,
     so no table template had to change and future tables get the behaviour automatically. */
  .tw { overflow-x: visible; }

  .dt {
    width: auto !important;
    min-width: 0 !important;
    table-layout: auto !important;
    display: block;
  }
  .dt thead { display: none; }
  .dt tbody { display: block; }

  .dt tr {
    display: block;
    background: var(--sur);
    border: 1px solid var(--bdr);
    border-radius: 10px;
    padding: .5rem .7rem;
    margin-bottom: .6rem;
  }

  .dt td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    padding: .35rem 0;
    border: none;
    border-bottom: 1px dashed var(--bdr);
    text-align: right;
  }
  .dt tr td:last-child { border-bottom: none; }
  /* Field error below a card (fieldError): attached to it instead of being a card of its own. */
  .dt tr:has(+ .field-error-row:not([hidden])) { margin-bottom: 0; border-bottom: none; border-radius: 10px 10px 0 0; }
  .dt tr.field-error-row { border-top: none; border-radius: 0 0 10px 10px; padding-top: 0; }
  .dt tr.field-error-row > td { display: block; text-align: left; }
  /* When it gets tight (large text), the value stands below its label. */
  .dt td { flex-wrap: wrap; row-gap: .25rem; }
  .dt td > * { max-width: 100%; }
  .mon-table td { justify-content: flex-start; text-align: left; column-gap: .75rem; }
  .mon-table td::before { flex-basis: 100%; }
  /* break-word instead of anywhere: anywhere makes every character a break point; the title shrank next to the
     icon and broke without a hyphen. Now it breaks at spaces and soft hyphens. */
  .ptitle { min-width: 0; max-width: 100%; overflow-wrap: break-word; hyphens: auto; }
  .ptitle > span { min-width: 0; }
  /* break-word does not lower the min width: without min-width 0 the header kept a long title word at full
     width and widened the window. */
  .ph > div { min-width: 0; }

  .dt td::before {
    content: attr(data-label);
    flex: 0 0 auto;
    text-align: left;
    font-size: var(--fs-0);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .4px;
    color: var(--mut);
  }
  /* Columns without a heading (action buttons) need no label */
  .dt td[data-label=""]::before,
  .dt td:not([data-label])::before { content: none; }
  .dt td[data-label=""] { justify-content: flex-end; }
  .user-tbl td.user-name { justify-content: flex-start; font-size: var(--fs-4); text-align: left; }
  .user-tbl .user-id { display: none; }
  /* Purchase price cards: when it gets tight (large text), the field stands below its label. */
  .acq-table td { flex-wrap: wrap; gap: .25rem 1rem; }
  .acq-table td > * { margin-left: auto; }

  /* Finances: one compact row per entry — image, name, value, P&L. Everything else is in the detail view
     opened by tapping; ten fields per card made the page about 8000 px long. */
  .dt-fin tbody tr {
    display: grid;
    grid-template-columns: 44px minmax(0, 1fr) auto;
    grid-template-areas: "image name value" "image name pnl";
    column-gap: .7rem; row-gap: .1rem; align-items: center;
    padding: .55rem .7rem;
  }
  .dt-fin tbody td { display: block; border: none; padding: 0; text-align: left; }
  .dt-fin tbody td::before { content: none; }
  .dt-fin td.fin-detail, .dt-fin td.fin-status { display: none; }
  .dt-fin td.fin-image { grid-area: image; }
  .dt-fin td.fin-image img { width: 44px; height: 44px; object-fit: contain; }
  .dt-fin td.fin-name { grid-area: name; font-weight: 600; line-height: 1.3; }
  .dt-fin td.fin-name > span:first-child { max-width: none !important; white-space: normal !important; }
  .dt-fin td.fin-value { grid-area: value; text-align: right; font-weight: 700; }
  .dt-fin td.fin-guv  { grid-area: pnl;  text-align: right; }
  .dt-fin .fin-year { display: none; }
  .fin-sub { display: block !important; font-size: var(--fs-0); font-weight: 400; color: var(--mut); margin-top: 2px; }
  .fin-sub-purchase { display: inline; }
  .dt-fin .fin-no-market { display: inline; font-size: var(--fs-1); font-weight: 400; color: var(--mut); }
  .dt-fin .fin-strich, .dt-fin td.fin-guv:has(.fin-guv-empty) { display: none; }
  /* Sums: name on top, cost basis and market value below with their own labels. */
  .dt-sum tfoot { display: block; }
  .dt-sum tr { display: grid; grid-template-columns: 1fr 1fr; gap: .2rem .75rem; padding: .55rem .7rem; }
  .dt-sum td { display: block; border: none; padding: 0 !important; text-align: left !important; width: auto !important; }
  .dt-sum td.fin-footer-name { grid-column: 1 / -1; }
  .dt-sum td::before { content: none; }
  .dt-sum td:empty { display: none; }
  .dt-fin td[data-short]::before, .dt-sum td[data-short]::before {
    content: attr(data-short); display: block; font-size: var(--fs-0); font-weight: 700;
    text-transform: uppercase; letter-spacing: .4px; color: var(--mut);
  }
  .fin-footer-value { font-size: var(--fs-3); }
  .dt-fin tfoot { display: block; }
  /* The background of the sum row covers the whole row, not only the numbers. */
  .dt-fin tfoot tr { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-end; gap: .75rem;
                     background: var(--b50); border: none; border-radius: 8px; padding: .5rem .7rem; }
  .dt-fin tfoot td, .dt-sum tfoot td { background: transparent !important; }
  .dt-sum tfoot tr { background: var(--b50); }
  .dt-fin tfoot td { display: block; border: none; padding: 0 !important; text-align: left !important; }
  .dt-fin tfoot td:empty { display: none; }
  .dt-fin tfoot td::before { content: none; }

  /* Sparklines and charts in table cells must not burst the card */
  .dt td svg:not(.ico), .dt td canvas { max-width: 60%; height: auto; }

  /* ─ Modals as sheets ─ */
  .ovl { align-items: flex-end; }
  .modal {
    /* Padding through the variables, not padding: title and footer bars in styles.css cancel it with negative
       margins; with only padding here they would be too wide on the phone. */
    --modal-padx: .9rem;
    --modal-pady: 1.1rem;
    --modal-padb: calc(1.1rem + env(safe-area-inset-bottom));
    width: 100%;
    max-width: none;
    max-height: 92vh;
    border-radius: 16px 16px 0 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
  }
  /* No sticky .mhd here any more: styles.css has it for ALL widths; a rule here would override its padding. */
  .mimg { max-height: 34vh; object-fit: contain; }

  /* ─ Fortschritts-Overlay ────────────────────────────────────────────── */
  .prog-box   { width: 94%; padding: 1.1rem .9rem; }
  .prog-steps { flex-wrap: wrap; gap: .4rem; }

  /* ─ Toasts across the full width ─ */
  /* At the bottom above the bar (and the floating button); at the top they covered the page title. Only with
     an open dialog at the top, because its fields are at the bottom. */
  #toasts { left: .6rem; right: .6rem; top: auto; bottom: calc(4.75rem + env(safe-area-inset-bottom)); }
  body:has(:is(#tab-gallery, #tab-parts, #tab-minifigs).active) #toasts { bottom: calc(4.75rem + 60px + env(safe-area-inset-bottom)); }
  body:has(.ovl.open, .ovl[style*="display: flex"]) #toasts { top: calc(56px + env(safe-area-inset-top)); bottom: auto; }
  .toast  { width: auto; min-width: 0; max-width: 100%; overflow-wrap: anywhere; }

  /* ─ Login, registration and startup screens ─ */
  #login-screen, #startup-screen { padding: 1rem .8rem; }
  .lcard {
    max-width: none;
    padding: 1.6rem 1.15rem;
    border-radius: 14px;
  }
  .llogo       { margin-bottom: 1.4rem; }
  .llogo .llogo-name    { font-size: var(--fs-5); }
  #startup-screen > div { width: 100%; max-width: 420px; }
}


/* Five labels need about 3.9 em each (em of the browser font size). Below that two rows of three targets
   with slightly smaller text: the names stay readable instead of showing icons only. */
@media (max-width: 640px) {
  /* The bar measures itself (container query in em of its font): so two rows apply with a large browser
     font AND with an enlarged page font that a media query does not see. */
  nav:not(.settings-nav) { container: bar / inline-size; flex-wrap: wrap; }
}
@container bar (max-width: 20.5em) {
  /* Only the names, no icon: icon above name made each row about 57 px and the bar 115 px high.
     The names stay readable (icons alone explain neither the build list nor the catalog). */
  .ntab { flex: 0 0 33.333%; padding: 2px; min-height: 44px; gap: 0; justify-content: center; }
  .ntab .ico { display: none; }
  /* A little smaller than the rest, but never below 12 px: what then does not fit into a third
     wraps into a second line instead of becoming unreadably small. */
  .ntab .tab-label { font-size: max(12px, calc(var(--fs-0) * .8)); line-height: 1.15; white-space: normal; overflow: visible;
                     text-overflow: clip; overflow-wrap: anywhere; hyphens: auto; text-align: center; }
}


/* Very narrow (320 px at 200 %): fields, selects and long rows give way instead of sticking out.
   Measured on the content itself, like the bar. */
@media (max-width: 640px) { main:not(#login-screen) { container: main / inline-size; } }
@container main (max-width: 20.5em) {
  .wanted-capture > .fg { flex: 1 1 100%; max-width: none !important; }
  .wanted-capture > .btn { flex: 1 1 100%; }
  .voucher-row { padding: .6rem .7rem; gap: .5rem; }
  /* Each button as wide as its word; wrapping happens between buttons. */
  .period-choice .period-btn { flex: 1 1 auto; overflow-wrap: normal; hyphens: manual; padding-inline: .6em; }
  .chart-card { padding: .75rem; }
  main :is(.filter-bar, .filter-row) > :is(input.si, input.search-field) { flex: 1 1 100%; width: 100% !important; }
  /* Locations: the field across the whole row, usage and buttons below. */
  #storage-list .location-grid { grid-template-columns: minmax(0, 1fr) max-content max-content !important; }
  #storage-list .location-field { grid-column: 1 / -1; border-bottom: 0 !important; padding-bottom: 0 !important; }
  #storage-list .location-used { justify-content: flex-start !important; white-space: normal !important; padding-left: 0 !important; }
  .capture-bar .import-menu { flex: 1 1 100%; min-width: 0; }
  .capture-bar .import-menu > summary { width: 100%; white-space: normal; }
  main :is(p, li, h1, h2, h3, label, .ibox, .stit, .hint-paragraph) { hyphens: auto; }
  main select, main input:not([type=checkbox]):not([type=radio]) { max-width: 100%; min-width: 0; }
  main .fg, main :is(.row, .sform, .ssec) > *, .chart-values > *, .apk-btn, .apk-btn-txt { min-width: 0; max-width: 100%; }
  #tab-locations .sgrid2 { grid-template-columns: minmax(0, 1fr) !important; }
  main .btn { white-space: normal; max-width: 100%; }
  .voucher-row, .voucher-actions { flex-wrap: wrap; }
  .empty.empty-state :is(h2, p, .btn), .dt-sum td { overflow-wrap: anywhere; hyphens: auto; max-width: 100%; min-width: 0; }
  /* Buttons break between words: with "anywhere" they shrank to a single character. */
  main .btn, .umenu-item > span { overflow-wrap: break-word; }
  .chart-values > *, .apk-btn-txt, #household-state, #tab-household .text-medium { overflow-wrap: anywhere; white-space: normal; }
}

/* As a card the table row does not highlight on hover. */
@media (max-width: 640px) and (hover: hover) {
  .dt tr:hover td { background: none; }
}


/* ── Small phone ─────────────────────────────────────────────────────── */
@media (max-width: 420px) {
  .fgrid { grid-template-columns: 1fr; }
}

@media (max-width: 380px) {
  .sgrid      { gap: .5rem; }
  .fgrid      { grid-template-columns: 1fr; }
  .ntab       { font-size: var(--fs-0); }
  /* At 320 px each target has 64 px: the "Collection" label only fits without padding and slightly tighter. */
  .ntab       { padding-left: 0; padding-right: 0; letter-spacing: -.02em; word-spacing: 0; }
  [data-theme="brick"] nav { padding-left: 2px; padding-right: 2px; column-gap: 0; }
  /* The base plate without side margin: with 2 × 16 px the collection choice lacked room and the minifigs label broke into a second row. */
  [data-theme="brick"] #app { padding-left: 0; padding-right: 0; }
  /* The minifigs label does not touch the edge of the collection choice. */
  .collection-choice button { gap: 4px; padding-left: 2px; padding-right: 2px; }
  .collection-choice button .ico { width: 1rem; height: 1rem; }
}


/* ═══ Brick theme only ═══
   Everything above applies to all themes. Here only the places where brick.css brings its own layout —
   mainly the stud rows and the large statistics tiles. */
@media (max-width: 640px) {

  /* Key figures (all themes) as one compact line "4 sets · 394 parts …": as tiles they filled the first screen
     on the phone. The breakdown is in the respective tab. */
  .brick-stats { display: flex; flex-wrap: wrap; align-items: flex-start; gap: .1rem 0; margin-bottom: var(--sp-4); }
  /* Plain text with a dot between, no surface: as a pill the number looked like a second switch. */
  .bstat, [data-theme="brick"] .bstat { display: inline-flex; flex-direction: row-reverse; align-items: baseline; gap: .35em;
    flex: 0 0 auto; padding: 0; border: 0 !important; border-radius: 0; box-shadow: none; background: none; color: var(--mut); }
  /* The dot stands after a key figure, not before the next: otherwise a wrapped line starts with an orphaned
     "·". order:-2 puts it at the end in the reversed row (the fourth figure is hidden on the phone). */
  .bstat:not(:last-child):not(:nth-child(3))::after { content: "·"; margin: 0 0 0 .5em; color: var(--mut); order: -2; }
  /* The dot does not raise the line: with its full line height the figure without a dot stood 2 px higher. */
  .bstat::after { line-height: 1; }
  .bstat .bstat-v, [data-theme="brick"] .bstat .bstat-v { font-size: var(--fs-4); margin: 0; line-height: 1.3; }
  .bstat .bstat-l, [data-theme="brick"] .bstat .bstat-l { text-transform: none; letter-spacing: 0; font-size: var(--fs-2); }
  .bstat .bstat-s { display: none; }
  [data-theme="brick"] .bstat::before { display: none; }

  /* The pills of the brick theme become the bar at the bottom like in the others. */
  [data-theme="brick"] nav {
    position: fixed; top: auto; bottom: 0; left: 0; right: 0; margin: 0;
    gap: 2px 0; padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
    background: var(--sur); border-top: 1px solid var(--bdr);
    box-shadow: 0 -2px 10px rgba(15, 23, 42, .06);
  }
  /* The own padding overrides the bar rules; without a min height 34 px remained at 200 % zoom. */
  [data-theme="brick"] .ntab { border: none; background: none; padding: 6px 0; font-size: var(--fs-0); min-height: 44px; justify-content: center; }

  /* Scale stud rows narrower, otherwise they look coarse on the phone */
  [data-theme="brick"] .sc::after,
  [data-theme="brick"] .lcard::after {
    background-size: 13px 100%;
    background-image: radial-gradient(circle at 6.5px center, rgba(255,255,255,.42) 2.4px, transparent 3px);
  }
  [data-theme="brick"] .sc::before,
  [data-theme="brick"] .sc::after,
  [data-theme="brick"] .lcard::before,
  [data-theme="brick"] .lcard::after { height: 11px; }

  [data-theme="brick"] .lcard { padding: 26px 18px 22px; border-radius: 18px; }

  /* The table cards get the same frame as .sc in the brick theme */
  [data-theme="brick"] .dt tr { border: none; box-shadow: var(--sh0); }
}

/* Narrow phone with normal text: the collection as a list with the image left; a full-width tile was 460 px
   high. With large text (below 15 em) the tile stays, otherwise no room is left next to the image. */
@container main (min-width: 15em) and (max-width: 22em) {
  #gallery .sgrid { grid-template-columns: minmax(0, 1fr); gap: .5rem; }
  #gallery .sc { display: grid; grid-template-columns: 5.5rem minmax(0, 1fr); align-items: stretch; }
  #gallery .sc > .sci { aspect-ratio: auto; height: 100%; min-height: 5.5rem; border-bottom: 0; border-right: 1px solid var(--bdr); }
  #gallery .sc > .sci img { padding: 6px; }
  #gallery .sc > .scb { padding: 8px 10px; min-width: 0; }
  #gallery .sc { padding: 6px; }
}


/* ── Little height: 200 % zoom, phone in landscape ──
   Header, hint band and bottom bar took almost half the height at 640 × 450. The header gives way when
   scrolling down (class from mobile.js) and the bottom bar gets narrower: name next to the icon, below
   600 px icons only (the name stays for screen readers). Wider than 640 px the bar sits below the header. */
@media (max-height: 500px) {
  header { transition: transform .2s ease; }
  .header-remove header { transform: translateY(-100%); }
}
@media (max-height: 500px) and (min-width: 641px) {
  nav { transition: top .2s ease; }
  .header-remove nav { top: 0; }
}
@media (max-height: 500px) and (max-width: 640px) {
  /* Without the header the jump bar moves to the very top instead of leaving a gap above it. */
  .header-remove :is(.account-nav, #global-settings-grid > .settings-nav) { top: 0; }
  .ntab { padding: 10px 2px; gap: 0; }
  main:not(#login-screen) { padding-bottom: 3.5rem; }
}
@media (max-height: 500px) and (max-width: 600px) {
  .ntab .tab-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
}
@media (max-height: 500px) and (min-width: 601px) and (max-width: 640px) {
  .ntab { flex-direction: row; justify-content: center; gap: 4px; }
  .ntab .tab-label { white-space: nowrap; }
}
@media (max-height: 500px) and (prefers-reduced-motion: reduce) {
  header, nav { transition: none; }
}
/* Large text on the phone: header and two-row bar took about 40 % of the height; both give way when scrolling down. */
@media (max-width: 640px) {
  .text-large header, .text-large nav:not(.settings-nav) { transition: transform .2s ease; }
  .text-large.header-remove header { transform: translateY(-100%); }
  .text-large.header-remove nav:not(.settings-nav) { transform: translateY(100%); }
  .text-large.header-remove :is(.account-nav, #global-settings-grid > .settings-nav) { top: 0; }
}
@media (max-width: 640px) and (prefers-reduced-motion: reduce) {
  .text-large header, .text-large nav:not(.settings-nav) { transition: none; }
}


/* ── Collection on the phone: the first card above the fold ──
   At 320 × 640 hint, switch, figures, two buttons and two filter rows filled the first screen. Capture becomes
   a floating button, import an overflow menu next to the title, figures and filters one line each. */
@media (max-width: 640px) {
  :is(#tab-gallery, #tab-parts, #tab-minifigs) { position: relative; }
  /* The title is as high as the button next to it: the subtitle only starts below. */
  :is(#tab-gallery, #tab-parts, #tab-minifigs) > .ph h1 { padding-right: 52px; min-height: 44px; }
  /* The Sets/Parts/Minifigs switch is the visible heading here (aria-current); the title stays for screen
     readers. Two header lines pushed the first card below 40 % of the height. */
  :is(#tab-gallery, #tab-parts, #tab-minifigs) > .ph h1 { position: absolute; opacity: 0; pointer-events: none; }
  /* Absolutely positioned it would take the width of its longest word, which widened the window at 200 %. */
  :is(#tab-gallery, #tab-parts, #tab-minifigs) > .ph h1 { left: 0; right: 0; overflow: hidden; }
  :is(#tab-gallery, #tab-parts, #tab-minifigs) > .ph :is(.ptitle-sub, .breadcrumb) { display: none; }
  :is(#tab-gallery, #tab-parts, #tab-minifigs) > .ph .collection-choice { width: auto; flex: 1 1 auto; margin-right: 52px; }
  .capture-bar { margin: 0; position: static; }
  .capture-bar .import-menu { position: absolute; top: 0; right: 0; flex: none; }
  .capture-bar .import-menu > summary { width: 44px; min-width: 44px; padding: 0; justify-content: center; }
  .capture-bar .import-menu :is(.import-file, .import-arrow) { display: none; }
  .capture-bar .import-menu .import-more { display: inline-block; width: 22px; height: 22px; }
  .capture-bar .import-text, .filter-bar .filter-text { display: none; }
  :is(.capture-bar, .filter-bar) .only-phone { display: inline; }
  .capture-bar .import-toggle { left: auto; right: 0; width: min(18rem, calc(100vw - 32px)); }
  :root { --floating-bottom: calc(4.75rem + env(safe-area-inset-bottom)); --floating-height: 52px; }
  .button-floating {
    position: fixed; z-index: 90; right: 16px; bottom: var(--floating-bottom);
    min-height: var(--floating-height); padding: 0 20px 0 16px; border-radius: 999px; box-shadow: var(--sh2), 0 6px 16px rgba(15, 23, 42, .18);
  }
  .capture-bar .btn.bp.button-floating { flex: none; }
  /* Catalog expanded: both years side by side, they belong together. */
  #tab-catalog .filter-bar.open > :is(#cat-year-from, #cat-year-to) { flex: 1 1 40%; width: auto; }
  /* Add set: price and currency need the width of the whole row; in half a column nothing was left for the example. */
  .capture-grid > .fg:has(> .price-with-currency) { grid-column: 1 / -1; }
  /* Build list: the field for several set numbers across the full width, "Add" below. */
  .pl-setnr { flex-basis: 100%; }
  /* Disabled (offline) opaque and grey: semi-transparent, button and card text overlapped and neither was readable. */
  :root .btn.button-floating:disabled { opacity: 1; background: var(--s100); color: var(--mut); border-color: var(--bdr); }
  /* The last row scrolls out above the floating button: distance to its top edge plus some air. */
  main:has(:is(#tab-gallery, #tab-parts, #tab-minifigs).active) { padding-bottom: calc(var(--floating-bottom) + var(--floating-height) + 1rem); }

  /* Key figures wrap: with five-digit numbers the line no longer fit and widened the page. */
  .brick-stats { flex-wrap: wrap; gap: .35rem; margin-bottom: var(--sp-3); }
  .brick-stats .bstat { padding: 0; max-width: 100%; min-width: 0; }
  .brick-stats .bstat:nth-child(4) { display: none; }
  .bstat .bstat-v, [data-theme="brick"] .bstat .bstat-v { font-size: var(--fs-3); }
  .bstat .bstat-l, [data-theme="brick"] .bstat .bstat-l { font-size: var(--fs-1); }

  /* Filter as an icon, search and view next to it on one line. */
  .filter-bar > .filter-button { width: 44px; min-width: 44px; padding: 0; justify-content: center; }
  .filter-bar > input.search-field { flex: 1 1 8rem; }

  /* The card image 4:3 instead of square: a card is a quarter shorter. */
  #gallery .sc > .sci { aspect-ratio: 4 / 3; }
}
/* Large text: search and figures wrap; the floating button shows only the icon (on two lines it covered the
   search field). Likewise when scrolling down, where it otherwise covers cards; aria-label carries the name. */
@container main (max-width: 20.5em) {
  .filter-bar > input.search-field { flex: 1 1 100%; }
  .brick-stats { flex-wrap: wrap; }
  .button-floating { width: 3.5rem; min-width: 3.5rem; padding: 0; justify-content: center; }
  .button-floating > span { display: none; }
}
@media (max-width: 640px) {
  /* Import status in the header: with a 180 px min width it pushed the page 50 px over the edge with large
     text. On the phone only the shortened text; the bar is in the progress dialog. */
  .import-bar { min-width: 0; max-width: 45vw; flex: 0 1 auto; }
  .import-bar-bar { display: none; }
  #gib-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
  .hright { min-width: 0; }
  /* Wanted list: fields finger-wide; the table scrolls with the dialog instead of inside its own narrow box. */
  #wanted-xml-modal :is(input, select) { min-height: 44px; }
  #wanted-xml-table { max-height: none !important; }
  /* PDF: print and download only as icons; the name stays for screen readers. */
  .pdf-tools .btn > span { display: none; }
  .prog-minimize { min-width: 44px; min-height: 44px; }
  .pdf-tools { gap: 4px; }
  .header-remove .button-floating { width: 56px; min-width: 56px; padding: 0; justify-content: center; }
  .header-remove .button-floating > span { display: none; }
  /* Target on the phone: a table's row button is as high as a finger. */
  .row-open { min-height: 44px; min-width: 44px; }
  .toast-close { min-width: 44px; min-height: 44px; top: 0; right: 0; }
  .toast:has(> .toast-close) { padding-right: 48px; }
}
@container main (min-width: 15em) and (max-width: 22em) {
  .filter-bar > input.search-field { flex: 1 1 8rem; width: auto !important; }
}
/* Very large text: floating, the button covered search field and figures; it then stands right at the title
   below "•••", the title keeps its width. */
@container main (max-width: 15em) {
  :is(#tab-gallery, #tab-parts, #tab-minifigs) > .ph h1 { min-height: 96px; }
  .capture-bar .btn.bp.button-floating { position: absolute; top: 52px; right: 0; bottom: auto; width: 44px; min-width: 44px;
    min-height: 44px; padding: 0; box-shadow: none; }
}
/* Little height (200 % zoom on 1280 × 900): floating, the button covered part of every third card at 450 px
   height. It then also stands at the title. */
@media (max-width: 640px) and (max-height: 500px) {
  :is(#tab-gallery, #tab-parts, #tab-minifigs) > .ph h1 { min-height: 96px; }
  .capture-bar .btn.bp.button-floating { position: absolute; top: 52px; right: 0; bottom: auto; width: 44px; min-width: 44px;
    min-height: 44px; padding: 0; box-shadow: none; }
  .capture-bar .button-floating > span { display: none; }
}

/* Set dialog on the phone and with little height: small image left, value and profit next to it, the other
   details below. Otherwise the image filled the first screen. */
@media (max-width: 640px), (max-height: 600px) and (max-width: 899px) {
  #set-modal .m-header { display: grid; grid-template-columns: 7.5rem minmax(0, 1fr); gap: .5rem .75rem; align-items: start; }
  #set-modal .m-header :is(#m-det, #m-det-links) > * { grid-column: 1 / -1; }
  #set-modal .m-header .zoom-button { grid-column: 1; grid-row: 1; }
  #set-modal .m-header .mimg { max-height: none; margin: 0; }
  #set-modal .m-header #m-det-links > .m-value-header { grid-column: 2; grid-row: 1; margin: 0; padding: .5rem .65rem; align-self: stretch; align-content: center; }
  #set-modal .wk-value { font-size: var(--fs-4); }
}
/* Large text: no room for the amount next to the image. Measured on the dialog in rem, because a media query
   in em does not see the page font size (html 200 %), only the browser's. */
@media (max-width: 640px) { #set-modal > .modal { container: setdialog / inline-size; } }
@container setdialog (max-width: 19rem) {
  #set-modal .m-header { grid-template-columns: minmax(0, 1fr); }
  #set-modal .m-header #m-det-links > .m-value-header { grid-column: 1; grid-row: auto; }
  #set-modal .m-header .zoom-button { width: 7.5rem; }
}

/* The explanation of "0 parts" stands on its own line below the figures on the phone: inside the line it tore
   "0 parts" from the minifigs and left a double dot. */
@media (max-width: 640px) {
  .bstat-hint:not(:empty) { display: block; margin: calc(-1 * var(--sp-2)) 0 var(--sp-3); font-size: var(--fs-1); color: var(--mut); }
}

/* Background jobs on the phone: label left, the values below each other in the second column, which may
   wrap. In a non-wrapping flex row name, tag and hint pushed the card wider than the window. */
@media (max-width: 640px) {
  .mon-single > summary { min-height: 44px; }
  .mon-table td { display: grid; grid-template-columns: 6.5rem minmax(0, 1fr); align-items: baseline; justify-items: start; }
  .mon-table td > * { grid-column: 2; max-width: 100%; min-width: 0; }
  .mon-role { white-space: normal; }
  .mon-table td:empty, .mon-table td:has(> .mon-buttons:empty) { display: none; }
}
@container main (max-width: 20.5em) {
  .mon-table td { grid-template-columns: minmax(0, 1fr); }
  .mon-table td > * { grid-column: 1; }
}

/* Job kinds on the phone as a compact row: name, only non-zero numbers, next run and buttons flow side by side. */
@media (max-width: 640px) {
  :is(.mon-kinds, .mon-services) tbody tr { display: flex; flex-wrap: wrap; align-items: center; gap: .15rem .9rem; }
  :is(.mon-kinds, .mon-services) td { max-width: 100%; flex-wrap: wrap; display: inline-flex; border: 0; padding: .1rem 0; gap: .35rem; }
  :is(.mon-kinds, .mon-services) td::before { flex: none; }
  :is(.mon-kinds, .mon-services) td:first-child { flex: 1 1 100%; font-weight: 700; }
  :is(.mon-kinds, .mon-services) td:first-child::before, .mon-services td:nth-child(2)::before { content: none; }
  .mon-kinds td:has(> .mon-digit.null) { display: none; }
  :is(.mon-kinds, .mon-services) td:last-child { flex: 1 1 100%; }
  :is(.mon-kinds, .mon-services) td:last-child::before { content: none; }
  :is(.mon-kinds, .mon-services) .mon-buttons { display: flex; flex-wrap: wrap; gap: .4rem; }
  .mon-services td:has(> .mon-buttons > .mon-quiet:only-child) { display: none; }
}

/* Vouchers on the phone, a calm card: number and PIN with equally sized icon buttons (the name is in
   aria-label), "Edit" and "More" side by side; stacked only with very large text. */
@media (max-width: 640px) {
  .voucher-row :is(.v-btn, .btn) { font-size: var(--fs-2); }
  .voucher-row .v-btn { width: 44px; padding: 0; }
  .voucher-row .v-btn .v-btn-text { display: none; }
  .voucher-row .voucher-actions { flex: 1 1 100%; margin-left: 0; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .voucher-actions > .more-menu { display: block; }
  .voucher-actions > .btn, .voucher-actions > .more-menu > summary { width: 100%; justify-content: center; }
  :is([data-click="queryAllBalances"], [data-click="exportVouchers"]) { flex: 1 1 100%; }
  /* Thumb-sized targets also in the toast and the build list chip. */
  .toast-action { min-height: 44px; }
  .pl-chip-remove { flex: none; min-width: 44px; min-height: 44px; }
}
@container main (max-width: 15em) {
  .voucher-row .voucher-actions { grid-template-columns: minmax(0, 1fr); }
}

/* Narrow content (large text): values below the names instead of next to them — otherwise the amount takes
   the whole width and the name is 0 px wide. */
@container main (max-width: 17rem) {
  .dt-fin tbody tr { grid-template-columns: 44px minmax(0, 1fr); grid-template-areas: "image name" "image value" "image pnl"; }
  .dt-fin td.fin-value, .dt-fin td.fin-guv { text-align: left; }
  .mon-actions.danger-zone { padding: .5rem; }
  .mon-actions.danger-zone .btn { flex: 1 1 100%; hyphens: auto; padding-left: .5rem; padding-right: .5rem; }
  .mon-actions.danger-zone .btn .ico { display: none; }
}
/* Price alerts on the phone: state, threshold and delete below the name. */
@container main (max-width: 26rem) {
  .alert-item { grid-template-columns: auto minmax(0, 1fr); grid-template-areas: "image text" ". status" ". threshold" ". delete"; }
  .alert-item-status { align-items: flex-start; }
  .alert-item-delete { justify-self: start; }
}

/* Parts on the phone as a compact list too: a card was 150 px high. The cards/list choice is the same as on
   the desktop (#parts-view); the list renders the cards as rows, since a table row would be a screen high. */
@media (max-width: 640px) {
  #tab-parts .filter-bar > .vtgl { display: flex; flex: none; }
  #tab-parts:has(#parts-view option[value="table"]:checked) :is(.parts-list, .parts-grid) { grid-template-columns: minmax(0, 1fr); gap: .4rem; }
  #tab-parts:has(#parts-view option[value="table"]:checked) .part-card {
    display: grid; grid-template-columns: 2.75rem minmax(0, 1fr) auto; grid-template-areas: "image name more" "image num more" "image color more";
    column-gap: .75rem; align-items: center; padding: .4rem .75rem; text-align: left; }
  #tab-parts:has(#parts-view option[value="table"]:checked) .part-card > .part-img { grid-area: image; width: 2.75rem; height: 2.75rem; margin: 0; }
  #tab-parts:has(#parts-view option[value="table"]:checked) .part-card > .part-name { grid-area: name; margin: 0; }
  #tab-parts:has(#parts-view option[value="table"]:checked) .part-card > .part-num { grid-area: num; margin: 0; }
  #tab-parts:has(#parts-view option[value="table"]:checked) .part-card > .part-color { grid-area: color; justify-content: flex-start; }
  #tab-parts:has(#parts-view option[value="table"]:checked) .part-card > .tile-more { grid-area: more; text-align: right; }
  #tab-parts:has(#parts-view option[value="table"]:checked) .part-card .part-qty { margin: 0; }
}
