/* BillFlow console — one stylesheet, no framework, no build step. */

:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --sidebar: #16202e;
  --sidebar-ink: #cbd5e1;
  --sidebar-ink-strong: #ffffff;
  --line: #dde3ec;
  --ink: #131922;
  --muted: #64708a;
  --accent: #2a4f8f;
  --accent-soft: #e8eefb;
  --ok: #16714a;
  --ok-soft: #e2f4ec;
  --warn: #8a5b00;
  --warn-soft: #fbf0d9;
  --stop: #a2262a;
  --stop-soft: #fbe6e7;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(19, 25, 34, 0.05), 0 8px 24px rgba(19, 25, 34, 0.06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d131c;
    --surface: #151d29;
    --surface-2: #1a2331;
    --sidebar: #0a0f17;
    --sidebar-ink: #94a3b8;
    --line: #27323f;
    --ink: #e6edf6;
    --muted: #8b98ad;
    --accent: #6f9be8;
    --accent-soft: #1b2941;
    --ok: #64d3a1;
    --ok-soft: #14301f;
    --warn: #e0b357;
    --warn-soft: #322510;
    --stop: #f08a8e;
    --stop-soft: #351619;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 12px 32px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

/* The drawer and the modal set `display`, which would otherwise beat the
   browser's rule for [hidden] and leave both of them open on load. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 620; letter-spacing: -0.01em; }
h1 { font-size: 19px; }
h2 { font-size: 15px; }
h3 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 600; }
p { margin: 2px 0; }
a { color: var(--accent); }
.muted { color: var(--muted); font-size: 12.5px; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }

/* ---------------------------------------------------------------- app shell */
.app { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }

.sidebar {
  background: var(--sidebar);
  color: var(--sidebar-ink);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  gap: 18px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; gap: 10px; align-items: center; padding: 0 6px; }
.brand .mark {
  width: 30px; height: 30px; border-radius: 8px;
  display: grid; place-items: center;
  background: linear-gradient(150deg, #3f6fc4, #2a4f8f);
  color: #fff; font-weight: 700; font-size: 15px;
}
.brand strong { display: block; color: var(--sidebar-ink-strong); font-size: 15px; }
.brand em { display: block; font-style: normal; font-size: 11.5px; opacity: 0.65; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 170px; }

nav { display: flex; flex-direction: column; gap: 1px; flex: 1; overflow-y: auto; }
.nav-group { font-size: 10px; text-transform: uppercase; letter-spacing: .1em; opacity: .5; padding: 12px 10px 4px; }
.nav-group:first-child { padding-top: 2px; }
nav button {
  display: flex; align-items: center; gap: 10px;
  width: 100%; padding: 8px 10px;
  border: none; border-radius: 8px; background: none;
  color: inherit; font: inherit; text-align: left; cursor: pointer;
}
nav button:hover { background: rgba(255, 255, 255, 0.06); }
nav button.active { background: rgba(255, 255, 255, 0.12); color: var(--sidebar-ink-strong); font-weight: 600; }
nav button .count {
  margin-left: auto; font-size: 11px; padding: 0 6px; border-radius: 999px;
  background: rgba(255, 255, 255, 0.14); font-variant-numeric: tabular-nums;
}
nav button.active .count { background: rgba(255, 255, 255, 0.22); }
nav svg { width: 16px; height: 16px; flex: none; opacity: 0.85; }

.sidebar-foot { display: grid; gap: 10px; }
.as-on { display: flex; flex-direction: column; padding: 0 6px; }
.as-on strong { color: var(--sidebar-ink-strong); font-variant-numeric: tabular-nums; }
.sidebar-foot .btn.ghost { border-color: rgba(255, 255, 255, 0.18); color: var(--sidebar-ink); background: transparent; }
.sidebar-foot .btn.ghost:hover { background: rgba(255, 255, 255, 0.08); }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; justify-content: space-between; align-items: flex-end; gap: 16px;
  padding: 18px 26px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.scope-switcher { display: flex; gap: 6px; align-items: center; }
.scope-switcher select { font: inherit; font-size: 12.5px; padding: 5px 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); }
.scope-switcher .scoped { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.acting-chip { font-size: 11.5px; color: var(--muted); border-left: 1px solid var(--line); padding-left: 10px; }
.acting-chip strong { color: var(--ink); }
main { padding: 22px 26px 64px; }

/* ------------------------------------------------------------------ pieces */
.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); margin-bottom: 16px; overflow: hidden;
}
.card > header {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--line);
}
.card > .body { padding: 14px 16px; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; margin-bottom: 18px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.kpi .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.kpi .value { font-size: 22px; font-weight: 640; margin-top: 4px; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.kpi .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.kpi.ok .value { color: var(--ok); }
.kpi.stop .value { color: var(--stop); }
.exception-tiles { margin: 0; padding: 14px; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
.exception-tile { font: inherit; text-align: left; cursor: pointer; }
.exception-tile:hover { border-color: var(--accent); }
.exception-tile .label { text-transform: none; letter-spacing: 0; font-size: 12.5px; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  position: sticky; top: 0; background: var(--surface-2); z-index: 1;
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted);
  font-weight: 600; text-align: left; padding: 8px 14px; border-bottom: 1px solid var(--line);
}
tbody td { padding: 9px 14px; border-bottom: 1px solid var(--line); vertical-align: top; }
tbody tr:last-child td { border-bottom: none; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: var(--surface-2); }
td .sub { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.table-wrap { overflow-x: auto; }
td.nowrap, th.nowrap { white-space: nowrap; }

.pill {
  display: inline-block; padding: 1px 9px; border-radius: 999px;
  font-size: 11.5px; font-weight: 600; letter-spacing: 0.01em;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--line);
}
.pill.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.pill.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.pill.stop { background: var(--stop-soft); color: var(--stop); border-color: transparent; }
.pill.info { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

.btn {
  font: inherit; font-size: 13px; font-weight: 550;
  padding: 6px 12px; border-radius: 8px; border: 1px solid transparent;
  background: var(--accent); color: #fff; cursor: pointer; white-space: nowrap;
}
.btn:hover { filter: brightness(1.07); }
.btn:disabled { opacity: 0.45; cursor: default; filter: none; }
.btn.danger { color: var(--stop); }
.btn.danger:not(.ghost) { background: var(--stop); color: #fff; }
.btn.ghost { background: var(--surface); color: var(--ink); border-color: var(--line); }
.btn.ghost:hover { background: var(--surface-2); filter: none; }
.btn.small { padding: 3px 9px; font-size: 12.5px; }
.btn.icon { padding: 4px 9px; }
.btn.block { width: 100%; }
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  font: inherit; font-size: 12.5px; padding: 3px 11px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); color: var(--muted); cursor: pointer;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

.note { border-left: 3px solid var(--line); padding: 4px 0 4px 10px; margin-top: 6px; font-size: 12.5px; color: var(--muted); }
.note.stop { border-color: var(--stop); color: var(--stop); }
.note.warn { border-color: var(--warn); color: var(--warn); }
.note.info { border-color: var(--accent); }
.note.ok { border-color: var(--ok); color: var(--ok); }
.note ul { margin: 4px 0 0; padding-left: 18px; }
.note li { margin: 2px 0; }

.empty { padding: 34px 16px; text-align: center; color: var(--muted); }
.empty strong { display: block; color: var(--ink); margin-bottom: 4px; }

pre {
  margin: 0; padding: 14px 16px; overflow: auto; max-height: 460px;
  background: var(--surface-2); border-top: 1px solid var(--line);
  font: 12px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--ink);
}

dl.facts { display: grid; grid-template-columns: 132px 1fr; gap: 6px 14px; margin: 0; font-size: 13px; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; }

.exception {
  display: grid; grid-template-columns: 1fr auto auto; gap: 12px; align-items: center;
  padding: 11px 16px; border-bottom: 1px solid var(--line);
}
.exception:last-child { border-bottom: none; }
.exception .what { font-weight: 560; }
.exception .why { color: var(--muted); font-size: 12.5px; margin-top: 2px; }

/* ------------------------------------------------------------ drawer, modal */
.drawer-scrim, .modal-scrim {
  position: fixed; inset: 0; background: rgba(10, 15, 23, 0.42); z-index: 20;
}
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(720px, 94vw); z-index: 21;
  background: var(--surface); border-left: 1px solid var(--line); box-shadow: var(--shadow);
  display: flex; flex-direction: column;
}
.drawer > header, .modal > header {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 12px;
  padding: 16px 20px; border-bottom: 1px solid var(--line);
}
.drawer > div { overflow: auto; padding: 18px 20px 40px; }
/* The document is laid out for A4 (210mm ≈ 794px). Scale it to fit the drawer
   rather than reflowing it — the preview has to look like what prints. */
.preview {
  height: 620px; overflow: auto; background: #fff;
  border: 1px solid var(--line); border-radius: 8px;
}
.preview iframe { width: 794px; height: 1400px; border: 0; transform: scale(0.79); transform-origin: top left; }

.modal {
  position: fixed; z-index: 21; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: min(760px, 94vw); max-height: 88vh; display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.modal > div { overflow: auto; padding: 18px 20px; }

.field { display: grid; gap: 4px; margin-bottom: 12px; }
.field label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.field input, .field select {
  font: inherit; padding: 7px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
}
.field input:focus, .field select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0 14px; }
input.num { text-align: right; }

.summary {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 14px; margin: 14px 0;
}
.summary div span { display: block; font-size: 12px; color: var(--muted); }
.summary div strong { font-size: 15px; font-variant-numeric: tabular-nums; }
.summary div.over strong { color: var(--stop); }
.summary div.left strong { color: var(--warn); }

.toasts { position: fixed; right: 20px; bottom: 20px; z-index: 40; display: grid; gap: 8px; justify-items: end; }
.toast {
  background: var(--sidebar); color: #fff; padding: 10px 14px; border-radius: 9px;
  max-width: 520px; font-size: 13px; box-shadow: var(--shadow);
  display: flex; align-items: center; gap: 12px;
}
.toast.stop { background: var(--stop); }
.toast.ok { background: var(--ok); }
.toast-action {
  flex: none; border: 1px solid rgb(255 255 255 / 70%); border-radius: 7px;
  background: #fff; color: var(--ink); padding: 5px 9px; font: inherit; font-weight: 700; cursor: pointer;
}



/* ------------------------------------------------------------------- forms */
.field { display: grid; gap: 4px; margin-bottom: 12px; align-content: start; }
.field label { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.field input, .field select, .field textarea {
  font: inherit; padding: 7px 10px; border-radius: 8px; width: 100%;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field .hint { font-size: 11.5px; color: var(--muted); }
.field.span-2 { grid-column: span 2; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0 14px; }
input.num { text-align: right; }
label.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink); text-transform: none; letter-spacing: 0; }
label.check input { width: auto; }
table.compact td { padding: 2px 4px; border-bottom: none; }
table.compact .field { margin-bottom: 4px; }
.bulk { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; border-bottom: 1px solid var(--line); }

.summary {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 14px; margin: 14px 0;
}
.summary div span { display: block; font-size: 12px; color: var(--muted); }
.summary div strong { font-size: 15px; font-variant-numeric: tabular-nums; }
.summary div.over strong { color: var(--stop); }
.summary div.left strong { color: var(--warn); }

@media (max-width: 900px) {
  .grid-2, .grid-3, .summary { grid-template-columns: 1fr; }
  .field.span-2 { grid-column: span 1; }
}


/* ---------------------------------------------------- cross-links, search */
a.xlink { color: var(--accent); text-decoration: none; border-bottom: 1px dotted currentColor; cursor: pointer; }
a.xlink:hover { border-bottom-style: solid; }
td .sub a.xlink { color: var(--muted); }
td .sub a.xlink:hover { color: var(--accent); }
#search {
  font: inherit; font-size: 13px; padding: 6px 10px; min-width: 230px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--ink);
}
#search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
@media (max-width: 900px) { #search { min-width: 0; width: 100%; } }

/* Reports share one set of filters and expose their supported scope. */
.report-group { border-top: 1px solid var(--line, #e3e8ef); }
.report-group > summary { cursor: pointer; padding: 16px 20px; font-weight: 600; }
.report-group > summary .muted { margin-left: 8px; font-weight: 400; }
.report-group .btn-row { min-width: 250px; }
.report-options { border: 0; padding: 0; margin: 14px 0; }
.report-options legend { font-size: 12px; color: #5b6472; margin-bottom: 8px; }
.report-options .chip { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }
.report-options .chip:has(input:checked) { background: #e8f0ff; border-color: #7b9cc8; color: #1f4d8f; }
.report-options input { width: auto; margin: 0; }
.report-shortcuts { justify-content: flex-start; margin-bottom: 14px; }
.report-more { margin-bottom: 18px; }
.report-more summary { cursor: pointer; padding: 8px 0; }
.report-unavailable { max-width: 360px; font-size: 12px; }
#report-controls [hidden] { display: none; }

.late-fee-terms { margin: 12px 0; }
.late-fee-terms summary { cursor: pointer; font-weight: 600; padding: 8px 0; }
#late-fee-preview details { min-width: 260px; max-width: 720px; }
#late-fee-preview details p { font-size: 12px; margin: 8px 0; }

#late-fee-preview > .body { padding: 14px 16px; }


/* Login remains visible until a session has been checked. */
[hidden] { display: none !important; }
.login-screen { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(100%, 420px); padding: 32px; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); }
.login-card .brand { padding: 0 0 28px; color: var(--ink); }
.login-card .brand strong { color: var(--ink); }
.login-card h1 { font-size: 26px; margin: 0 0 8px; }
.login-card form { display: grid; gap: 10px; margin: 26px 0; }
.login-card input { width: 100%; min-height: 44px; font: inherit; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--ink); }
.login-card input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.login-card label { font-weight: 600; }
.login-card .btn { min-height: 44px; justify-content: center; }
.login-card .note p { margin: 8px 0 0; }
.login-error { color: var(--stop); margin: 0; }
.login-error:empty { display: none; }
.signed-in-user { color: var(--sidebar-ink-strong); margin-bottom: 10px; overflow-wrap: anywhere; }
.sidebar-foot > .btn { color: var(--sidebar-ink-strong); margin-bottom: 18px; }


/* ==========================================================================
   The phone
   ==========================================================================
   Below 900px this stops being a desktop console with smaller margins and
   becomes an app: a bottom tab bar for the four screens a working day touches,
   the sidebar as a full-height More sheet, and the drawer and modal as bottom
   sheets, which is the gesture a phone user already expects.

   Everything stays reachable. A billing console has wide tables and no amount
   of layout makes a thirty-column sales register comfortable on a 390px
   screen, so those scroll inside their own container with the first column
   pinned, rather than being hidden or reflowed into something that no longer
   reconciles.
   ========================================================================== */

/* Chrome that only exists on a phone. */
.tabbar, .nav-scrim, .sheet-close, .sheet-grip { display: none; }

@media (max-width: 900px) {
  /* iOS zooms any input whose text is under 16px when it takes focus, and never
     zooms back out. Every control that accepts typing has to clear that bar. */
  .field input, .field select, .field textarea, #search, .login-card input, .scope-switcher select {
    font-size: 16px;
  }

  .app { grid-template-columns: 1fr; }
  main { padding: 14px 14px calc(78px + env(safe-area-inset-bottom)); }

  /* ------------------------------------------------------------ the app bar */
  .topbar {
    padding: 10px 14px calc(10px);
    padding-top: calc(10px + env(safe-area-inset-top));
    align-items: flex-start;
    flex-direction: column;
    gap: 8px;
  }
  .topbar h1 { font-size: 21px; letter-spacing: -0.02em; }
  .topbar-actions { width: 100%; }
  .topbar-actions .btn { flex: 1; min-height: 40px; }
  .scope-switcher { width: 100%; flex-wrap: wrap; }
  .scope-switcher select { flex: 1; min-height: 40px; }
  #search { width: 100%; min-height: 40px; }

  /* ------------------------------------------------- sidebar as More sheet */
  .sidebar {
    position: fixed;
    inset: 0;
    height: 100dvh;
    z-index: 30;
    padding: calc(16px + env(safe-area-inset-top)) 14px calc(84px + env(safe-area-inset-bottom));
    overflow-y: auto;
    transform: translateY(100%);
    visibility: hidden;
    transition: transform 0.22s ease, visibility 0.22s;
  }
  .sidebar.open { transform: none; visibility: visible; }
  @media (prefers-reduced-motion: reduce) {
    .sidebar { transition: none; }
  }
  body.nav-open { overflow: hidden; }

  .nav-scrim { display: block; position: fixed; inset: 0; z-index: 29; background: rgba(10, 15, 23, 0.45); }
  .sheet-close {
    display: grid; place-items: center;
    position: absolute; top: calc(14px + env(safe-area-inset-top)); right: 14px;
    width: 34px; height: 34px; border-radius: 999px;
    border: 1px solid rgba(255, 255, 255, 0.18);
    background: rgba(255, 255, 255, 0.08); color: var(--sidebar-ink);
    font: inherit; font-size: 15px; cursor: pointer;
  }

  nav { flex: none; overflow: visible; }
  nav button { min-height: 46px; font-size: 15px; }
  .nav-group { font-size: 11px; padding: 16px 10px 6px; }
  .sidebar-foot { margin-top: 20px; }
  .sidebar-foot .btn { min-height: 44px; }

  /* ------------------------------------------------------ the bottom tabs */
  .tabbar {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 31;
    background: color-mix(in srgb, var(--surface) 88%, transparent);
    backdrop-filter: saturate(180%) blur(12px);
    -webkit-backdrop-filter: saturate(180%) blur(12px);
    border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tabbar button {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    /* 44px is the smallest target a thumb hits reliably. */
    min-height: 52px; padding: 7px 2px 5px;
    border: 0; background: none; color: var(--muted);
    font: inherit; font-size: 10.5px; font-weight: 550;
    cursor: pointer; -webkit-tap-highlight-color: transparent;
  }
  .tabbar button svg { width: 22px; height: 22px; }
  .tabbar button.active { color: var(--accent); }
  .tabbar button.active svg { stroke-width: 2.1; }
  .tabbar button span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  /* -------------------------------------------- drawer and modal as sheets */
  .drawer, .modal {
    top: auto; left: 0; right: 0; bottom: 0;
    width: 100%; max-width: none;
    transform: none;
    height: 92dvh; max-height: 92dvh;
    border: 0; border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0;
  }
  .drawer > header, .modal > header { padding: 4px 18px 14px; }
  .drawer > div, .modal > div { padding: 16px 18px calc(28px + env(safe-area-inset-bottom)); }
  .sheet-grip {
    display: block; width: 38px; height: 4px; border-radius: 999px;
    background: var(--line); margin: 9px auto 2px; flex: none;
  }
  .drawer-scrim, .modal-scrim { background: rgba(10, 15, 23, 0.5); }

  /* An A4 document scaled to a desktop drawer is unreadable on a phone; give it
     the width it needs and let the container scroll both ways. */
  .preview { height: 66dvh; }
  .preview iframe { transform: scale(0.46); }

  /* ------------------------------------------------------ lists and tables */
  .card { border-radius: 12px; margin-bottom: 12px; }
  .card > header { padding: 12px 14px; flex-wrap: wrap; }
  .card > .body { padding: 12px 14px; }

  /* KPIs become a swipeable row rather than a stack five screens tall. */
  .kpis {
    display: flex; gap: 10px; margin: 0 -14px 14px; padding: 0 14px 4px;
    overflow-x: auto; scroll-snap-type: x mandatory;
    scrollbar-width: none;
  }
  .kpis::-webkit-scrollbar { display: none; }
  .kpi { flex: 0 0 72%; scroll-snap-align: start; }
  .kpi .value { font-size: 24px; }
  /* The exception tiles are a checklist, not a carousel: keep them stacked. */
  .exception-tiles { display: grid; grid-template-columns: 1fr; margin: 0; padding: 12px; overflow: visible; }
  .exception-tiles .kpi, .exception-tiles .exception-tile { flex: auto; }

  .table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    /* A shadow on the right edge while there is more table to the right. */
    background:
      linear-gradient(to right, var(--surface) 30%, transparent) left / 24px 100% no-repeat,
      linear-gradient(to left, var(--surface) 30%, transparent) right / 24px 100% no-repeat,
      radial-gradient(farthest-side at 0 50%, rgba(19, 25, 34, 0.14), transparent) left / 12px 100% no-repeat,
      radial-gradient(farthest-side at 100% 50%, rgba(19, 25, 34, 0.14), transparent) right / 12px 100% no-repeat;
    background-attachment: local, local, scroll, scroll;
  }
  /* The first column says which row you are reading; it stays put. */
  .table-wrap table thead th:first-child,
  .table-wrap table tbody td:first-child {
    position: sticky; left: 0; z-index: 2;
    background: var(--surface);
    box-shadow: 1px 0 0 var(--line);
  }
  .table-wrap table thead th:first-child { z-index: 3; background: var(--surface-2); }
  thead th, tbody td { padding-left: 12px; padding-right: 12px; white-space: nowrap; }

  .exception { grid-template-columns: 1fr; gap: 8px; padding: 12px 14px; }
  .exception .btn-row { justify-content: flex-start; }

  dl.facts { grid-template-columns: 1fr; gap: 2px 0; }
  dl.facts dt { margin-top: 8px; font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.04em; }

  .grid-2, .grid-3, .summary { grid-template-columns: 1fr; }
  .field.span-2 { grid-column: span 1; }
  .summary { grid-template-columns: repeat(2, 1fr); }

  .btn { min-height: 38px; padding: 8px 14px; }
  .btn.small { min-height: 32px; }
  .btn-row { justify-content: stretch; }
  .btn-row .btn { flex: 1 1 auto; }
  .chips { gap: 8px; }
  .chip { min-height: 34px; padding: 6px 13px; }

  /* Above the tab bar, not behind it. */
  .toasts { right: 12px; left: 12px; bottom: calc(64px + env(safe-area-inset-bottom)); justify-items: stretch; }
  .toast { max-width: none; }

  .login-screen { padding: 20px; align-content: center; }
  .login-card { padding: 24px; border: 0; box-shadow: none; background: transparent; }
  .login-card h1 { font-size: 24px; }
}

/* A phone in landscape, and small tablets: two columns of KPIs, sheets stay. */
@media (max-width: 900px) and (min-width: 560px) {
  .kpi { flex: 0 0 40%; }
  .grid-2, .summary { grid-template-columns: 1fr 1fr; }
}

/* Installed to the home screen, there is no browser chrome to scroll away, so
   the app bar can be flatter and the safe areas do the spacing. */
@media (display-mode: standalone) {
  .topbar { border-bottom: 1px solid var(--line); }
  body { overscroll-behavior-y: none; }
}
