/* App styles. Mobile first: base rules are for phones; @media (min-width: 768px) adds the
   tablet/desktop layout. All values come from tokens.css. */

@layer reset, base, layout, components, utilities;

/* ---------------------------------------------------------------- reset */
@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  * { margin: 0; }
  html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
  img, svg { display: block; max-width: 100%; }
  input, button, select, textarea { font: inherit; color: inherit; }
  button { cursor: pointer; }
  [hidden] { display: none !important; }
}

/* ---------------------------------------------------------------- base */
@layer base {
  html { background: var(--surface-sunken); }
  body {
    min-height: 100dvh;
    font-family: var(--font-body);
    font-size: var(--text-md);
    line-height: var(--leading);
    color: var(--text);
    background: var(--surface-sunken);
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
  }
  h1, h2, h3 {
    font-family: var(--font-heading);
    font-weight: 800;
    line-height: var(--leading-tight);
    color: var(--ink);
    text-wrap: balance;
  }
  h1 { font-size: var(--text-2xl); }
  h2 { font-size: var(--text-xl); }
  h3 { font-size: var(--text-lg); }
  p { text-wrap: pretty; }
  a { color: var(--magenta-text); text-underline-offset: 0.15em; }
  strong { color: var(--ink); font-weight: 650; }
  :focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: var(--radius-sm); }
  ::selection { background: var(--lime-tint); color: var(--ink); }
  svg.icon { width: 24px; height: 24px; flex: none; }
  .list-row > svg.icon:first-child { color: var(--text-muted); }
}

/* ---------------------------------------------------------------- layout */
@layer layout {
  .app {
    min-height: 100dvh;
    padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  }

  /* Header: white, text-only logo (the full logo needs 170px+ and doesn't fit phones well) */
  .app-header {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    min-height: calc(var(--header-h) + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) calc(var(--gutter) + env(safe-area-inset-right)) 0
      calc(var(--gutter) + env(safe-area-inset-left));
    background: var(--surface);
    border-bottom: 1px solid var(--border);
  }
  .app-header .logo img { height: 34px; width: auto; }
  .scope-label {
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    max-width: 50%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .app-main {
    width: 100%;
    max-width: var(--content-max);
    margin-inline: auto;
    padding: var(--space-5) calc(var(--gutter) + env(safe-area-inset-right)) var(--space-6)
      calc(var(--gutter) + env(safe-area-inset-left));
  }

  /* Bottom tab bar (phones) */
  .tabbar {
    position: fixed;
    inset: auto 0 0 0;
    z-index: 30;
    display: grid;
    grid-auto-columns: 1fr;
    grid-auto-flow: column;
    padding: var(--space-1) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    background: var(--surface);
    border-top: 1px solid var(--border);
    box-shadow: 0 -2px 12px rgb(43 45 46 / 0.06);
  }
  .tabbar .rail-logo, .tabbar .rail-foot, .tabbar .rail-toggle-form { display: none; }
  .tab {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-height: calc(var(--tabbar-h) - var(--space-2));
    color: var(--text-muted);
    text-decoration: none;
    font-size: var(--text-xs);
    font-weight: 600;
  }
  .tab .tab-icon {
    display: grid;
    place-items: center;
    width: 56px;
    height: 30px;
    border-radius: var(--radius-pill);
    transition: background var(--fast) var(--ease);
  }
  .tab svg { width: 22px; height: 22px; }
  /* Six tabs at 320px: let the labels shorten rather than push "More" off the edge. */
  .tab { min-width: 0; }
  .tab .tab-label { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  @media (max-width: 374px) {
    .tab { font-size: 10px; }
    .tab .tab-icon { width: 40px; }
  }
  .tab[aria-current="page"] { color: var(--ink); }
  .tab[aria-current="page"] .tab-icon { background: var(--primary); }

  /* Tablet and up: the tab bar becomes a dark grey side rail with the full reversed logo */
  @media (min-width: 768px) {
    .app {
      padding-bottom: 0;
      padding-left: var(--rail-w);
    }
    .app-header .logo { display: none; }
    .app-header { justify-content: flex-end; }
    .tabbar {
      inset: 0 auto 0 0;
      width: var(--rail-w);
      /* A column, so the name and Sign out can sit at the very bottom. */
      display: flex;
      flex-direction: column;
      gap: var(--space-1);
      overflow-y: auto;
      padding: var(--space-5) var(--space-3);
      background: var(--surface-dark);
      border: 0;
      box-shadow: none;
    }
    .tabbar .rail-logo { display: block; padding: 0 var(--space-3) var(--space-5); }
    .tabbar .rail-logo img { width: 200px; height: auto; }
    .tab {
      flex-direction: row;
      justify-content: flex-start;
      gap: var(--space-3);
      min-height: var(--tap);
      padding: 0 var(--space-3);
      border-radius: var(--radius);
      color: var(--text-on-dark);
      font-size: var(--text-md);
      font-weight: 600;
    }
    .tab .tab-icon { width: 24px; height: 24px; background: none !important; }
    .tab:hover { background: rgb(255 255 255 / 0.08); }
    .tab[aria-current="page"] { background: var(--primary); color: var(--ink); }
    .tabbar .rail-foot {
      display: block;
      padding: var(--space-4) var(--space-3) 0;
      border-top: 1px solid rgb(255 255 255 / 0.15);
      color: rgb(255 255 255 / 0.85);
      font-size: var(--text-sm);
    }
    .rail-name { font-weight: 600; color: var(--text-on-dark); }
    .rail-signout {
      display: inline-flex;
      align-items: center;
      gap: var(--space-2);
      min-height: 40px;
      margin-top: var(--space-2);
      padding: 0;
      border: 0;
      background: none;
      color: inherit;
      font-size: var(--text-sm);
    }
    .rail-signout svg { width: 18px; height: 18px; }
    .rail-signout:hover { color: var(--brand-lime); }
    .app-main { padding: var(--space-6) var(--space-6) var(--space-7); }

    /* Close the menu: the rail shrinks to icons and the page takes the room.
       The choice is kept in a cookie, so the page renders this way from the first paint. */
    /* Above the divider, at the bottom of the menu; your name and Sign out sit below it. */
    .tabbar .rail-toggle-form { display: block; margin-top: auto; padding: 0 var(--space-3) var(--space-2); }
    .rail-toggle {
      display: flex;
      align-items: center;
      gap: var(--space-3);
      width: 100%;
      min-height: 40px;
      padding: 0;
      border: 0;
      background: none;
      color: rgb(255 255 255 / 0.85);
      font-size: var(--text-sm);
      font-weight: 600;
      cursor: pointer;
    }
    .rail-toggle:hover { color: var(--brand-lime); }
    .rail-toggle-icon {
      width: 20px;
      height: 20px;
      transform: rotate(180deg);
      transition: transform var(--fast) var(--ease);
    }

    .rail-collapsed { padding-left: var(--rail-w-collapsed); }
    .rail-collapsed .tabbar { width: var(--rail-w-collapsed); padding-inline: var(--space-2); }
    .rail-collapsed .rail-logo { display: none; }
    /* With the rail's logo gone, the header shows the text-only one, as it does on phones. */
    .rail-collapsed .app-header .logo { display: block; }
    .rail-collapsed .app-header { justify-content: space-between; }
    .rail-collapsed .tab { justify-content: center; gap: 0; padding: 0; }
    .rail-collapsed .rail-foot, .rail-collapsed .rail-toggle-form { padding-inline: 0; text-align: center; }
    .rail-collapsed .rail-name { display: none; }
    .rail-collapsed .rail-signout { justify-content: center; width: 100%; }
    .rail-collapsed .rail-toggle { justify-content: center; }
    .rail-collapsed .rail-toggle-icon { transform: none; }
    /* Hidden to the eye, still read out and still the button's name. */
    .rail-collapsed .tab-label,
    .rail-collapsed .rail-signout span,
    .rail-collapsed .rail-toggle-label {
      position: absolute;
      width: 1px;
      height: 1px;
      margin: -1px;
      padding: 0;
      overflow: hidden;
      clip-path: inset(50%);
      white-space: nowrap;
      border: 0;
    }
  }

  .stack > * + * { margin-top: var(--stack-gap, var(--space-4)); }
  .cluster { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: center; }
  .grid-auto { display: grid; gap: var(--space-4); grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr)); }
}

/* ---------------------------------------------------------------- components */
@layer components {
  /* Page title with the brand's magenta hand-drawn underline */
  .page-title { padding-bottom: var(--space-3); margin-bottom: var(--space-5); }
  /* Inline, so the underline follows the words on every line instead of the full width of a
     wrapped title's box; each line gets its own (box-decoration-break: clone). */
  .page-title h1 {
    display: inline;
    padding-bottom: 0.3em;
    line-height: 1.45;
    -webkit-box-decoration-break: clone;
    box-decoration-break: clone;
    text-wrap: balance;
    background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 10' preserveAspectRatio='none'%3E%3Cpath d='M2 7.5C40 3.5 120 2.2 198 5.2' fill='none' stroke='%23e6007e' stroke-width='3.2' stroke-linecap='round'/%3E%3C/svg%3E")
      left bottom / 100% 0.4em no-repeat;
  }
  .page-title p { margin-top: var(--space-2); color: var(--text-muted); }
  .page-title h1 + p { margin-top: var(--space-3); }
  .eyebrow {
    font-family: var(--font-heading);
    font-weight: 600;
    font-size: var(--text-sm);
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--text-muted);
  }

  /* Buttons: Tablet Gothic SemiBold, per the guidelines */
  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: var(--tap);
    padding: 0 var(--space-5);
    border: 2px solid transparent;
    border-radius: var(--radius);
    font-family: var(--font-heading);
    font-weight: 600;
    font-size: var(--text-md);
    letter-spacing: 0.01em;
    text-decoration: none;
    white-space: nowrap;
    transition: background var(--fast) var(--ease), border-color var(--fast) var(--ease), transform var(--fast) var(--ease);
    user-select: none;
  }
  .btn svg { width: 20px; height: 20px; }
  .btn:active { transform: scale(0.98); }
  .btn-primary { background: var(--primary); color: var(--on-primary); }
  .btn-primary:hover { background: var(--lime-hover); }
  .btn-primary:active { background: var(--lime-pressed); }
  .btn-secondary { background: var(--surface); color: var(--ink); border-color: var(--brand-dark-grey); }
  .btn-secondary:hover { background: var(--surface-sunken); }
  .btn-dark { background: var(--surface-dark); color: var(--text-on-dark); }
  .btn-dark:hover { background: var(--ink); }
  .btn-danger { background: var(--danger); color: #fff; }
  .btn-ghost { background: transparent; color: var(--ink); }
  .btn-ghost:hover { background: var(--surface-sunken); }
  .btn-block { width: 100%; }
  .btn[disabled], .btn[aria-disabled="true"] { opacity: 0.45; pointer-events: none; }

  /* Cards and lists */
  .card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--space-4);
    box-shadow: var(--shadow-1);
  }
  .card h2, .card h3 { margin-bottom: var(--space-2); }
  .list {
    list-style: none;
    padding: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
  }
  .list-row {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: 60px;
    padding: var(--space-3) var(--space-4);
    color: inherit;
    text-decoration: none;
  }
  .list-row + .list-row, .list > li + li { border-top: 1px solid var(--border); }
  a.list-row:hover { background: var(--surface-sunken); }
  .list-row .grow { flex: 1 1 8rem; min-width: 0; }
  .list-row { flex-wrap: wrap; }
  /* Narrow phones: keep the chevron on the same line as the title. */
  @media (max-width: 374px) { .list-row { flex-wrap: nowrap; } }
  .row-actions { display: flex; gap: var(--space-1); margin-left: auto; }
  .list-row .title { font-weight: 600; color: var(--ink); }
  .list-row .meta { font-size: var(--text-sm); color: var(--text-muted); }
  .list-row .chev { width: 18px; height: 18px; color: var(--border-strong); flex: none; }

  /* Badges */
  .badge {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    padding: 2px 10px;
    border-radius: var(--radius-pill);
    font-size: var(--text-xs);
    font-weight: 700;
    letter-spacing: 0.02em;
    white-space: nowrap;
  }
  .badge-ok { background: var(--ok-tint); color: var(--ok); }
  .badge-warn { background: var(--warn-tint); color: var(--warn); }
  .badge-danger { background: var(--danger-tint); color: var(--danger); }
  .badge-neutral { background: var(--surface-sunken); color: var(--text); }
  .badge-accent { background: var(--accent); color: #fff; }

  /* Forms */
  .field { display: grid; gap: var(--space-1); }
  .field label, .label { font-weight: 600; color: var(--ink); }
  .field .help { font-size: var(--text-sm); color: var(--text-muted); }
  /* Radio choices as full-width rows a thumb can hit, not the browser's 13px dots. */
  .field label:has(> input[type="radio"]) {
    display: flex; align-items: center; gap: var(--space-3); min-height: 44px; font-weight: 600; cursor: pointer;
  }
  .field input[type="radio"] { width: 22px; height: 22px; margin: 0; flex: none; accent-color: var(--surface-dark); }
  .field .error, .errorlist { font-size: var(--text-sm); color: var(--danger); font-weight: 600; list-style: none; padding: 0; }
  .input, .field input:not([type="checkbox"], [type="radio"]), .field select, .field textarea {
    width: 100%;
    min-height: var(--tap);
    padding: var(--space-2) var(--space-3);
    border: 1.5px solid var(--border-strong);
    border-radius: var(--radius);
    background: var(--surface);
    font-size: var(--text-md);
    color: var(--ink);
    transition: border-color var(--fast) var(--ease), box-shadow var(--fast) var(--ease);
  }
  .field textarea { min-height: 96px; resize: vertical; }
  /* Shown, but someone else owns it: the mobile number an admin sets, for instance. */
  .input:disabled, .field input:disabled, .field select:disabled, .field textarea:disabled {
    background: var(--surface-sunken);
    border-style: dashed;
    color: var(--text-muted);
    cursor: not-allowed;
  }
  .input:focus, .field input:focus, .field select:focus, .field textarea:focus {
    outline: none;
    border-color: var(--brand-dark-grey);
    box-shadow: 0 0 0 3px var(--lime-tint), 0 0 0 5px var(--brand-lime);
  }
  .field[aria-invalid="true"] input, .input[aria-invalid="true"] { border-color: var(--danger); }
  .search { position: relative; }
  .search svg { position: absolute; left: var(--space-3); top: 50%; translate: 0 -50%; width: 20px; height: 20px; color: var(--text-muted); }
  .search .input { padding-left: 2.6rem; }

  /* Quantity stepper: big +/- for gloved hands */
  .stepper {
    display: inline-grid;
    justify-self: start;
    min-height: var(--tap);
    grid-template-columns: var(--tap) 5.5rem var(--tap);
    border: 1.5px solid var(--border-strong);
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--surface);
  }
  .stepper button {
    border: 0;
    background: var(--surface-sunken);
    font-size: 1.5rem;
    line-height: 1;
    color: var(--ink);
  }
  .stepper button:active { background: var(--brand-light-grey); }
  .stepper input, .field .stepper input {
    width: auto;
    min-height: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    text-align: center;
    font-size: var(--text-lg);
    font-weight: 700;
    color: var(--ink);
    min-width: 0;
    -moz-appearance: textfield;
  }
  .stepper input::-webkit-inner-spin-button, .stepper input::-webkit-outer-spin-button { -webkit-appearance: none; }
  .stepper input:focus, .field .stepper input:focus { outline: none; box-shadow: inset 0 0 0 3px var(--brand-lime); }

  /* Messages (Django messages framework) */
  .messages { position: fixed; z-index: 40; left: var(--gutter); right: var(--gutter); bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + var(--space-3)); display: grid; gap: var(--space-2); list-style: none; padding: 0; }
  .toast {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius);
    background: var(--ink);
    color: #fff;
    box-shadow: var(--shadow-2);
    animation: toast-in var(--normal) var(--ease);
  }
  .toast svg { width: 20px; height: 20px; flex: none; margin-top: 2px; }
  .toast.success svg { color: var(--brand-lime); }
  .toast.error { background: var(--danger); }
  .toast button { margin-left: auto; background: none; border: 0; color: inherit; opacity: 0.8; padding: 0 var(--space-1); font-size: 1.25rem; line-height: 1; }
  @keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
  @media (min-width: 768px) {
    .messages { left: auto; right: var(--space-6); bottom: var(--space-6); width: 380px; }
  }

  /* Bottom sheet dialog (confirmations). Centred modal from tablet up. */
  dialog.sheet {
    margin: auto auto 0;
    width: 100%;
    max-width: 100%;
    border: 0;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    padding: var(--space-5) var(--gutter) calc(var(--space-5) + env(safe-area-inset-bottom));
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow-2);
  }
  dialog.sheet[open] { animation: sheet-up var(--normal) var(--ease); }
  dialog.sheet::backdrop { background: rgb(43 45 46 / 0.5); }
  dialog.sheet .actions { display: grid; gap: var(--space-2); margin-top: var(--space-5); }
  @keyframes sheet-up { from { transform: translateY(24px); opacity: 0; } }
  @media (min-width: 768px) {
    dialog.sheet { margin: auto; max-width: 440px; border-radius: var(--radius-lg); padding: var(--space-6); }
    dialog.sheet .actions { grid-auto-flow: column; justify-content: end; }
  }

  /* Empty state */
  .empty { text-align: center; padding: var(--space-7) var(--space-4); color: var(--text-muted); }
  .empty > svg { width: 48px; height: 48px; margin: 0 auto var(--space-3); color: var(--border-strong); }
  .empty h2 { font-size: var(--text-lg); margin-bottom: var(--space-2); }

  /* Skeleton (loading) */
  .skeleton { border-radius: var(--radius-sm); background: linear-gradient(90deg, var(--surface-sunken) 25%, var(--brand-light-grey) 50%, var(--surface-sunken) 75%) 0 0 / 200% 100%; animation: shimmer 1.2s linear infinite; min-height: 1em; }
  @keyframes shimmer { to { background-position: -200% 0; } }

  /* htmx: dim content while a request is in flight */
  .htmx-request.htmx-indicator, .htmx-request .htmx-indicator { opacity: 1; }
  .htmx-indicator { opacity: 0; transition: opacity var(--fast); }

  /* Page header with an action button on the right */
  .page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--space-3); }
  .page-head .page-title { margin-bottom: var(--space-4); }
  .page-head > .btn { flex: none; margin-top: var(--space-1); }
  /* Bottom margin for when the buttons wrap under the title: they mustn't touch what comes next. */
  .page-head-actions { flex: none; display: flex; flex-wrap: wrap; justify-content: flex-end; gap: var(--space-2); margin: var(--space-1) 0 var(--space-5); }
  /* Two or more buttons don't fit beside the title on a phone: they go underneath. */
  .page-head:has(.page-head-actions) { flex-wrap: wrap; }
  .page-head:has(.page-head-actions) .page-title { flex: 1 1 18rem; }
  @media (max-width: 47.99rem) {
    .page-head-actions { justify-content: flex-start; margin: 0 0 var(--space-5); }
  }

  /* Inline notices */
  .notice {
    display: flex;
    gap: var(--space-3);
    align-items: flex-start;
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius);
    background: var(--surface-sunken);
    border: 1px solid var(--border);
    color: var(--ink);
    margin-bottom: var(--space-4);
  }
  .notice svg { width: 20px; height: 20px; flex: none; margin-top: 2px; }
  .notice-warn { background: var(--warn-tint); border-color: #f0dca8; color: var(--warn); }
  .notice-warn strong { color: inherit; }

  /* Job comments: pink, like the old system's, so instructions for the fitters can't be missed. */
  .job-comments {
    background: var(--magenta-tint); color: var(--magenta-ink);
    border: 1px solid #f6b6d8; border-left: 6px solid var(--accent);
    border-radius: var(--radius-lg); padding: var(--space-4);
  }
  .job-comments-title { margin: 0 0 var(--space-2); font-size: 0.8rem; letter-spacing: 0.08em; text-transform: uppercase; color: inherit; }
  .job-comments .pre { margin: 0; font-size: 1.05rem; font-weight: 600; }
  .notice-error { background: var(--danger-tint); border-color: #f2c4c0; color: var(--danger); font-weight: 600; display: grid; }

  .divider { display: flex; align-items: center; gap: var(--space-3); color: var(--text-muted); font-size: var(--text-sm); margin: var(--space-5) 0; }
  .divider::before, .divider::after { content: ""; flex: 1; border-top: 1px solid var(--border); }

  .btn-sm { min-height: 40px; padding: 0 var(--space-3); font-size: var(--text-sm); }

  /* Filter chips (radio buttons) */
  .chips { display: flex; gap: var(--space-2); overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
  .chip input { position: absolute; opacity: 0; pointer-events: none; }
  .chip span {
    display: inline-flex; align-items: center; min-height: 40px; padding: 0 var(--space-4);
    border: 1.5px solid var(--border-strong); border-radius: var(--radius-pill);
    background: var(--surface); font-weight: 600; font-size: var(--text-sm); color: var(--ink); white-space: nowrap; cursor: pointer;
  }
  .chip input:checked + span { background: var(--surface-dark); border-color: var(--surface-dark); color: #fff; }
  .chip input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }

  .avatar {
    display: grid; place-items: center; flex: none; width: 40px; height: 40px; border-radius: 50%;
    background: var(--lime-tint); color: var(--ink); font-family: var(--font-heading); font-weight: 800; font-size: var(--text-lg);
  }
  .is-inactive .avatar { background: var(--surface-sunken); color: var(--text-muted); }
  .is-inactive .title { color: var(--text-muted); }

  .details { display: grid; gap: var(--space-3); }
  .details div { display: grid; grid-template-columns: 6.5rem 1fr; gap: var(--space-3); }
  .details dt { color: var(--text-muted); font-size: var(--text-sm); padding-top: 2px; }
  .details dd { margin: 0; color: var(--ink); }

  details.card summary { cursor: pointer; min-height: 32px; display: flex; align-items: center; list-style: none; }
  details.card summary::before { content: "+"; display: inline-grid; place-items: center; width: 24px; height: 24px; margin-right: var(--space-2); border-radius: 50%; background: var(--primary); color: var(--ink); font-weight: 700; }
  details.card[open] summary::before { content: "−"; }

  /* Box, then label; help and errors go under the label, not squeezed beside it. */
  .field-check { display: grid; grid-template-columns: 22px 1fr; align-items: start; column-gap: var(--space-3); row-gap: var(--space-1); }
  .field-check input { width: 22px; height: 22px; margin: 0.15em 0 0; accent-color: var(--brand-dark-grey); }
  .field-check .help, .field-check .error { grid-column: 2; }
  .field-check label { font-weight: 400; }

  .card.choice { display: flex; align-items: center; gap: var(--space-3); color: inherit; text-decoration: none; }
  .card.choice strong { display: block; }
  .card.choice .meta { font-size: var(--text-sm); color: var(--text-muted); }

  .table-wrap { overflow-x: auto; }
  .table { width: 100%; border-collapse: collapse; }
  .table th, .table td { text-align: left; padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--border); }
  img.qr { width: 200px; height: 200px; image-rendering: pixelated; }

  /* Stock */
  .stock-figure { flex: none; }
  .stock-hero { text-align: center; padding: var(--space-5) var(--space-4); }
  .stock-big { font-family: var(--font-heading); font-weight: 800; font-size: 3rem; line-height: 1.05; color: var(--ink); margin-top: var(--space-2); }
  .stock-sub { font-size: var(--text-lg); color: var(--text-muted); }
  .stock-hero.level-low { border-color: #f0dca8; background: linear-gradient(var(--warn-tint), var(--surface) 70%); }
  .stock-hero.level-out { border-color: #f2c4c0; background: linear-gradient(var(--danger-tint), var(--surface) 70%); }
  .qty { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); }
  .qty-unit { font-weight: 600; color: var(--text-muted); }
  .change { flex: none; min-width: 4.5rem; font-family: var(--font-heading); font-weight: 800; font-size: var(--text-lg); }
  .change.down { color: var(--ink); }
  .change.up { color: var(--ok); }
  fieldset.choice-list { border: 0; padding: 0; margin: 0; display: grid; gap: var(--space-2); }
  fieldset.choice-list legend { margin-bottom: var(--space-2); }
  .choice-row {
    display: flex; align-items: center; gap: var(--space-3); min-height: var(--tap);
    padding: var(--space-2) var(--space-3); border: 1.5px solid var(--border); border-radius: var(--radius); cursor: pointer;
  }
  .choice-row:has(input:checked) { border-color: var(--brand-dark-grey); background: var(--lime-tint); }
  .choice-row input { width: 20px; height: 20px; accent-color: var(--brand-dark-grey); flex: none; }
  a.chip { text-decoration: none; }
  a.chip span.on { background: var(--surface-dark); border-color: var(--surface-dark); color: #fff; }

  /* Material type editor */
  .mono, input.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 0.95rem; }
  code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 0.9em; background: var(--surface-sunken); padding: 0.05em 0.35em; border-radius: 4px; color: var(--ink); }
  .row-card { border: 1px solid var(--border); border-radius: var(--radius); padding: var(--space-3); display: grid; gap: var(--space-2); background: var(--surface); }
  .row-card[hidden] { display: none; }
  /* Phones: name (or material) on one line, its formula full width underneath. */
  .formula-line { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
  .formula-line > .field:first-child { flex: 1 1 0; }
  .formula-line > .field:last-child { flex: 1 1 100%; }
  @media (min-width: 768px) {
    .formula-line { flex-wrap: nowrap; }
    .formula-line > .field:first-child { flex: 0 0 34%; }
    .formula-line > .field:last-child { flex: 1 1 auto; }
  }
  .equals { font-weight: 700; color: var(--text-muted); }
  .row-foot { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2) var(--space-3); }
  .row-tools { display: flex; gap: var(--space-1); margin-left: auto; }
  .icon-btn { width: 40px; height: 40px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); color: var(--ink); font-size: 1rem; }
  .icon-btn:hover { background: var(--surface-sunken); }
  .mini-check { display: inline-flex; align-items: center; gap: var(--space-2); font-size: var(--text-sm); min-height: 40px; }
  .mini-check input { width: 20px; height: 20px; accent-color: var(--brand-dark-grey); }
  .mini-check.accept { font-weight: 600; color: var(--warn); }
  .result-list, .shown-list, .formula-list { list-style: none; padding: 0; display: grid; gap: var(--space-1); }
  .result-list .qty-n { display: inline-block; min-width: 3.5rem; font-family: var(--font-heading); font-weight: 800; color: var(--ink); }
  .shown-list { margin-top: var(--space-3); padding-top: var(--space-3); border-top: 1px dashed var(--border); font-size: var(--text-sm); }
  .formula-list li { overflow-wrap: anywhere; }
  .ok-text { color: var(--ok); font-weight: 600; }
  .warn-text { color: var(--warn); font-weight: 600; }
  .pre { white-space: pre-line; }
  .save-bar { position: sticky; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + var(--space-2)); z-index: 5; }
  @media (min-width: 768px) { .save-bar { bottom: var(--space-4); } }
  .notice-link { text-decoration: none; }

  /* Stock: one compact card per product, with take out (and add, for the office) on it. */
  .stock-cards { list-style: none; padding: 0; margin: 0; display: grid; gap: var(--space-2); }
  @media (min-width: 640px) { .stock-cards { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
  @media (min-width: 1100px) { .stock-cards { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
  .stock-card { display: grid; gap: var(--space-2); align-content: start; padding: var(--space-3); min-width: 0; }
  .stock-card-top { display: flex; align-items: flex-start; gap: var(--space-1); min-width: 0; }
  .stock-card-top .stock-card-head { flex: 1; }
  .stock-card-report {
    display: grid; place-items: center; width: 44px; height: 44px; flex: none; margin: -10px -8px -10px 0;
    border: 0; border-radius: var(--radius-sm); background: none; color: var(--text);
  }
  .stock-card-report:hover, .stock-card-report:focus-visible { background: var(--warn-tint); color: var(--warn); }
  .stock-card-report .icon { width: 22px; height: 22px; }
  .stock-card-head { display: flex; align-items: baseline; gap: var(--space-2); min-width: 0; color: var(--ink); text-decoration: none; }
  .stock-card-head .grow { min-width: 0; }
  .stock-card-head .title { font-weight: 700; }
  .stock-card-head .meta { font-size: var(--text-sm); }
  .stock-card-head .chev { width: 18px; height: 18px; color: var(--text-muted); flex: none; margin-left: auto; align-self: center; }
  .stock-card-figure { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); margin: 0; }
  .stock-card-qty { font-family: var(--font-heading); font-weight: 800; font-size: var(--text-lg); color: var(--ink); }
  .stock-card.level-out .stock-card-qty { color: var(--danger); }
  .stock-card-figure .meta { font-size: var(--text-sm); }
  .stock-card-actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(0, 1fr)); gap: var(--space-2); }
  .stock-card-actions .btn { min-width: 0; min-height: 44px; padding: 0 var(--space-3); font-size: var(--text-sm); }

  /* Buying */
  /* Tabs within a section (Buying): underlined, one row, like the old system's. Not pills:
     those are filters (All / Low / Out). */
  .subtabs {
    display: flex; gap: var(--space-1); margin-top: var(--space-3); border-bottom: 1.5px solid var(--border);
    overflow-x: auto; scrollbar-width: none;
  }
  .subtabs::-webkit-scrollbar { display: none; }
  .subtabs a {
    flex: none; display: inline-flex; align-items: center; min-height: 44px; padding: 0 var(--space-3);
    margin-bottom: -1.5px; border-bottom: 3px solid transparent;
    font-weight: 600; font-size: var(--text-sm); color: var(--text); text-decoration: none; white-space: nowrap;
  }
  .subtabs a:hover { color: var(--ink); border-bottom-color: var(--border-strong); }
  .subtabs a[aria-current="page"] { color: var(--ink); border-bottom-color: var(--ink); }
  .subtabs a:focus-visible { outline: 3px solid var(--focus); outline-offset: -3px; }
  .stock-cards h2 a { color: var(--ink); text-decoration: none; }
  .price-list { list-style: none; padding: 0; margin: 0; display: grid; gap: var(--space-2); }
  .price-list li { display: flex; align-items: baseline; gap: var(--space-1); padding: var(--space-2) 0; border-top: 1px solid var(--border); }
  .price-list li:first-child { border-top: 0; padding-top: 0; }
  .price-list .grow { flex: 1; min-width: 0; }
  .price-list li:not(.best) { color: var(--text-muted); }

  /* Jobs: the grab list. Big tick box, what it is and how many, then the stepper. */
  .grab-list { list-style: none; padding: 0; margin: 0; display: grid; gap: var(--space-2); }
  .grab-row {
    display: grid; grid-template-columns: auto 1fr; gap: var(--space-2) var(--space-3); align-items: start;
    padding: var(--space-3); border: 1.5px solid var(--border); border-radius: var(--radius); background: var(--surface);
  }
  .grab-row:has(.grab-tick:checked) { border-color: var(--brand-dark-grey); background: var(--lime-tint); }
  .grab-row.is-done { background: var(--surface-sunken); }
  .grab-row.is-done .title { color: var(--text-muted); }
  .grab-tick { width: 28px; height: 28px; margin: 2px 0 0; accent-color: var(--brand-dark-grey); grid-row: span 2; }
  .grab-body { display: grid; gap: var(--space-1); justify-items: start; min-width: 0; }
  .grab-body .title { font-weight: 700; color: var(--ink); cursor: pointer; }
  .grab-row > .grab-qty { grid-column: 2; }
  .grab-row:not(:has(.grab-tick)) { grid-template-columns: 1fr; }
  .grab-row:not(:has(.grab-tick)) > * { grid-column: 1; }
  .grab-unit { grid-column: 2; margin-top: calc(-1 * var(--space-1)); }
  @media (min-width: 768px) {
    .grab-row { grid-template-columns: auto 1fr auto auto; align-items: center; }
    .grab-tick { grid-row: auto; margin: 0; }
    .grab-row > .grab-qty, .grab-unit { grid-column: auto; margin: 0; }
  }
  fieldset.card { margin: 0; min-width: 0; }
  fieldset.card legend { padding: 0; float: left; width: 100%; }
  fieldset.card legend + * { clear: both; }
  /* Kiosk */
  .kiosk-done { margin-left: var(--space-3); display: flex; align-items: center; gap: var(--space-3); }
  .kiosk-timer {
    display: inline-flex; align-items: center; gap: var(--space-1); min-width: 4.5rem; justify-content: center;
    padding: 0.2rem var(--space-2); border-radius: var(--radius-pill); background: var(--surface-sunken);
    font-variant-numeric: tabular-nums; font-weight: 700; font-size: var(--text-sm); color: var(--ink);
  }
  .kiosk-timer svg { width: 16px; height: 16px; }
  .kiosk-timer.is-low { background: #fde6f2; color: var(--magenta-text); }
  @media (max-width: 767px) { .app-header.is-kiosk .scope-label { display: none; } }
  .kiosk-who { white-space: nowrap; font-weight: 600; font-size: var(--text-sm); color: var(--ink); }
  .kiosk-warning {
    position: fixed; inset: auto var(--space-4) calc(var(--tabbar-h) + env(safe-area-inset-bottom) + var(--space-4)); z-index: 50;
    max-width: 420px; margin: 0 auto; padding: var(--space-4); border-radius: var(--radius-lg);
    background: var(--surface-dark); color: var(--text-on-dark); box-shadow: var(--shadow-2);
  }
  @media (min-width: 768px) { .kiosk-warning { inset: auto var(--space-5) var(--space-5) calc(var(--rail-w) + var(--space-5)); } }
  .kiosk-warning[hidden] { display: none; }
  .kiosk-warning p { margin: 0 0 var(--space-3); }
  .people-grid { list-style: none; padding: 0; margin: 0; display: grid; gap: var(--space-3); grid-template-columns: repeat(auto-fit, minmax(140px, 180px)); justify-content: center; }
  .people-grid a {
    display: grid; justify-items: center; gap: var(--space-2); padding: var(--space-5) var(--space-3);
    border: 1.5px solid var(--border); border-radius: var(--radius-lg); background: var(--surface);
    color: var(--ink); text-decoration: none; font-weight: 700; text-align: center; min-height: 140px; align-content: center;
  }
  .people-grid a:hover, .people-grid a:focus-visible { border-color: var(--brand-dark-grey); background: var(--lime-tint); }
  .people-grid .avatar { width: 64px; height: 64px; font-size: var(--text-2xl); }
  .auth-panel.kiosk-panel { max-width: 720px; }
  .pin-dots { display: flex; justify-content: center; gap: var(--space-4); margin: var(--space-5) 0; }
  .pin-dots span { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--brand-dark-grey); }
  .pin-dots span.on { background: var(--brand-dark-grey); }
  .keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--space-3); max-width: 320px; margin: 0 auto; }
  .keypad button {
    min-height: 72px; border: 1.5px solid var(--border-strong); border-radius: var(--radius-lg); background: var(--surface);
    font-family: var(--font-heading); font-weight: 800; font-size: 1.75rem; color: var(--ink);
  }
  .keypad button:active { background: var(--brand-light-grey); }
  .keypad .key-quiet { font-family: inherit; font-size: var(--text-md); font-weight: 600; border-color: transparent; background: none; }
  form.pin-input-js .field { position: relative; height: 0; margin: 0; }
  .pin-forgot { margin-top: var(--space-6); }
  /* The store tablet: 8-inch, landscape, so 600-750px tall. Side by side, the keypad fits
     without scrolling: greeting and "forgotten" on the left, keypad on the right. */
  @media (orientation: landscape) and (min-width: 700px) and (max-height: 820px) {
    .auth-panel.kiosk-panel { max-width: 960px; padding-top: calc(var(--space-4) + env(safe-area-inset-top)); padding-bottom: var(--space-4); }
    .auth-panel.kiosk-panel .logo img { width: 150px; margin-bottom: var(--space-3); }
    .pin-layout {
      display: grid; grid-template-columns: 1fr 1fr; column-gap: var(--space-7); align-items: center;
      grid-template-areas: "intro pad" "forgot pad";
    }
    .pin-intro { grid-area: intro; align-self: end; }
    .pin-pad { grid-area: pad; }
    .pin-forgot { grid-area: forgot; align-self: start; margin-top: var(--space-3); }
    .pin-dots { margin: 0 0 var(--space-4); }
  }
  form.pin-input-js input.pin-input { position: absolute; inset: 0 auto auto 0; opacity: 0; pointer-events: none; width: 1px; min-height: 0; height: 1px; padding: 0; border: 0; }
  .size-pair { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-3); align-items: start; }
  .label-row { display: flex; align-items: baseline; justify-content: space-between; gap: var(--space-2); }
  .link-button { border: 0; background: none; padding: 0; color: var(--magenta-text); font: inherit; font-size: var(--text-sm); font-weight: 600; text-decoration: underline; cursor: pointer; }
  .type-grid { list-style: none; padding: 0; margin: 0; display: grid; gap: var(--space-2); grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .type-grid a {
    display: flex; flex-direction: column; justify-content: center; gap: var(--space-1); min-height: 72px;
    padding: var(--space-3); border: 1.5px solid var(--border); border-radius: var(--radius);
    background: var(--surface); color: var(--ink); text-decoration: none; font-weight: 700;
  }
  .type-grid a:hover { border-color: var(--brand-dark-grey); }
  .type-grid a .meta { font-weight: 400; font-size: var(--text-sm); color: var(--text-muted); }
  table.compare tr.diff td { background: var(--warn-tint); font-weight: 600; }
  .link-form { flex: 0 1 12rem; }
  .link-form select { min-height: 40px; }

  /* Error pages */
  .error-page { text-align: center; max-width: 420px; margin: var(--space-6) auto 0; }
  .error-code {
    font-family: var(--font-heading); font-weight: 800; font-size: 4.5rem; line-height: 1;
    color: var(--brand-lime); margin-bottom: var(--space-3); letter-spacing: -0.02em;
  }
  .auth-panel .left { text-align: left; }

  /* Signed-out pages (sign in, password reset, invites, offline): full logo on white */
  .auth {
    min-height: 100dvh;
    background: var(--surface);
  }
  .auth-panel {
    width: 100%;
    max-width: 420px;
    margin: 0 auto;
    padding: calc(var(--space-7) + env(safe-area-inset-top)) var(--gutter) var(--space-7);
  }
  .auth-panel .logo img { width: 220px; height: auto; margin: 0 auto var(--space-6); }

  /* Style guide swatches */
  .swatches { display: grid; gap: var(--space-3); grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .swatch .chip { height: 56px; }
  .swatch { border-radius: var(--radius); border: 1px solid var(--border); overflow: hidden; background: var(--surface); }
  .swatch .info { padding: var(--space-2) var(--space-3); font-size: var(--text-sm); }
  .swatch code { color: var(--text-muted); }
  .sw-lime { background: var(--brand-lime); }
  .sw-dark { background: var(--brand-dark-grey); }
  .sw-mid { background: var(--brand-mid-grey); }
  .sw-light { background: var(--brand-light-grey); }
  .sw-magenta { background: var(--brand-magenta); }
  .sw-ink { background: var(--ink); }
}

/* ---------------------------------------------------------------- utilities */
@layer utilities {
  .muted { color: var(--text-muted); }
  .stack-tight { margin: 0; padding-left: 1.25rem; display: grid; gap: var(--space-2); }
  .mt-2 { margin-top: var(--space-2); }
  .mb-4 { margin-bottom: var(--space-4); }
  .small { font-size: var(--text-sm); }
  .center { text-align: center; }
  .mt-0 { margin-top: 0 !important; }
  .mt-4 { margin-top: var(--space-4); }
  .mt-6 { margin-top: var(--space-6); }
  .visually-hidden {
    position: absolute !important; width: 1px; height: 1px; overflow: hidden;
    clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
  }
  .accent { color: var(--magenta-text); }
}

@layer components {
  /* Product order: drag by the handle, or the arrows (they work without JavaScript). */
  .sort-list { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--space-2); }
  .sort-row {
    display: flex; align-items: center; gap: var(--space-2); min-height: 56px;
    background: var(--surface); border: 1px solid var(--border, #e2e2e2); border-radius: var(--radius);
    padding: var(--space-1) var(--space-2);
  }
  .sort-name { flex: 1; min-width: 0; font-weight: 600; color: var(--ink); }
  .toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-2); }
  .sort-handle {
    display: grid; place-items: center; width: 44px; height: 44px; flex: none;
    border: 0; background: none; color: var(--text); cursor: grab; touch-action: none;
    border-radius: var(--radius-sm);
  }
  .sort-handle:active { cursor: grabbing; }
  .sort-arrows { display: flex; gap: var(--space-1); }
  .sort-arrows .icon-btn {
    display: grid; place-items: center; width: 44px; height: 44px; border-radius: var(--radius-sm);
    border: 1px solid var(--brand-light-grey); background: var(--surface); color: var(--ink);
  }
  .sort-arrows .icon-btn:disabled { opacity: 0.35; }
  .sort-row.is-dragging { box-shadow: 0 8px 24px rgb(0 0 0 / 0.18); border-color: var(--brand-lime); position: relative; z-index: 2; }
  .sort-row.is-dragging .sort-handle { cursor: grabbing; }
}

@layer components {
  /* Help: the ? in the header, and the guides. */
  .help-button {
    display: grid; place-items: center; width: 36px; height: 36px; margin-left: var(--space-2); flex: none;
    border-radius: 50%; border: 2px solid var(--brand-dark-grey); color: var(--ink);
    font-weight: 800; font-size: 1.05rem; line-height: 1; text-decoration: none;
  }
  .help-button:hover, .help-button:focus-visible { background: var(--lime-tint); border-color: var(--brand-lime); }
  .guide { max-width: 40rem; }
  .guide h2 { margin-top: var(--space-6); scroll-margin-top: 80px; }
  .guide h3 { margin-top: var(--space-4); }
  .guide p, .guide li { line-height: 1.55; }
  .guide p, .guide ol, .guide ul, .guide table { margin: var(--space-3) 0; }
  .guide ol, .guide ul { padding-left: 1.25rem; display: grid; gap: var(--space-2); }
  .guide table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
  .guide th, .guide td { text-align: left; vertical-align: top; padding: var(--space-2); border-bottom: 1px solid var(--border); }
  .guide th { font-weight: 700; color: var(--ink); }
  .guide .notice { margin: var(--space-4) 0; }
  .help-shot { margin: var(--space-4) auto; max-width: 300px; }
  .help-shot img {
    display: block; width: 100%; height: auto; border-radius: 18px;
    border: 1px solid var(--border); box-shadow: 0 6px 20px rgb(0 0 0 / 0.08);
  }
  .help-shots { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--space-4); margin: var(--space-4) 0; }
  .help-shots .help-shot { margin: 0; flex: 1 1 240px; max-width: 300px; }
  .help-shot figcaption { margin-top: var(--space-2); font-size: var(--text-sm); color: var(--text-muted); text-align: center; }
  .help-n {
    display: inline-grid; place-items: center; width: 1.45em; height: 1.45em; border-radius: 50%;
    background: var(--accent); color: #fff; font-size: 0.8em; font-weight: 800; vertical-align: 0.1em;
  }
  .guide-nav { display: flex; justify-content: space-between; gap: var(--space-3); margin-top: var(--space-6); }
  .help-snippet { font-size: var(--text-sm); color: var(--text-muted); }
}

/* Checks: one fieldset per question, big answer buttons, a problem panel that opens on a fail. */
@layer components {
  /* flow-root keeps the floated legend inside its own question. */
  .q { border: 0; margin: 0; padding: 0; min-width: 0; display: flow-root; }
  /* A fieldset's border would run through its legend, so the divider is a background line. */
  .q + .q { padding-top: var(--space-4); background: linear-gradient(var(--border), var(--border)) top / 100% 1px no-repeat; }
  /* Floated so the legend is an ordinary heading, not one sitting in the fieldset's edge. */
  .q-label { float: left; width: 100%; font-weight: 600; color: var(--ink); padding: 0; margin-bottom: var(--space-2); }
  .q-label + * { clear: both; }
  .q .help { font-size: var(--text-sm); color: var(--text-muted); margin: 0 0 var(--space-2); }
  .q .error { color: var(--danger); font-size: var(--text-sm); margin: var(--space-1) 0 0; }
  .q[aria-invalid="true"] .q-label { color: var(--danger); }

  .seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: var(--space-2); }
  .seg label { position: relative; }
  .seg input { position: absolute; opacity: 0; inset: 0; margin: 0; }
  .seg span {
    display: flex; align-items: center; justify-content: center; min-height: var(--tap);
    border: 2px solid var(--border-strong); border-radius: var(--radius); background: var(--surface);
    font-family: var(--font-heading); font-weight: 600; color: var(--ink); cursor: pointer;
  }
  .seg input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }
  .seg .seg-ok input:checked + span, .seg .seg-no input:checked + span { background: var(--ok-tint); border-color: var(--ok); color: var(--ok); }
  .seg .seg-defect input:checked + span, .seg .seg-damaged input:checked + span, .seg .seg-missing input:checked + span { background: var(--danger-tint); border-color: var(--danger); color: var(--danger); }
  .seg .seg-na input:checked + span, .seg .seg-yes input:checked + span { background: var(--surface-dark); border-color: var(--surface-dark); color: var(--text-on-dark); }
  .q[data-fail="yes"] .seg-yes input:checked + span { background: var(--danger-tint); border-color: var(--danger); color: var(--danger); }
  .q[data-fail="yes"] .seg-no input:checked + span { background: var(--ok-tint); border-color: var(--ok); color: var(--ok); }
  .q[data-fail="no"] .seg-no input:checked + span { background: var(--danger-tint); border-color: var(--danger); color: var(--danger); }
  .q[data-fail="no"] .seg-yes input:checked + span { background: var(--ok-tint); border-color: var(--ok); color: var(--ok); }

  /* Shown when the answer is a fail; always there without :has() support or when it has an error. */
  .q-problem { display: none; margin-top: var(--space-3); padding: var(--space-3); border-radius: var(--radius); background: var(--danger-tint); }
  .q-problem > * + * { margin-top: var(--space-3); }
  .q-problem.is-shown,
  .q-check:has(.seg-defect input:checked) .q-problem,
  .q[data-fail="yes"]:has(.seg-yes input:checked) .q-problem,
  .q[data-fail="no"]:has(.seg-no input:checked) .q-problem,
  .q-damage .q-problem,
  .q-ladder:has(.seg-damaged input:checked) .q-problem,
  .q-ladder:has(.seg-missing input:checked) .q-problem { display: block; }
  .q-damage .q-problem { background: none; padding: 0; }
  @supports not selector(:has(*)) { .q-problem { display: block; } }

  .reading { display: flex; align-items: center; gap: var(--space-2); max-width: 14rem; }
  .reading input { flex: 1; }
  .reading .unit { font-weight: 600; color: var(--text-muted); }

  .photo-btn { position: relative; overflow: hidden; }
  .photo-btn input[type="file"] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
  .photo-btn:focus-within { outline: 3px solid var(--focus); outline-offset: 2px; }
  .photo-btn.has-files { border-color: var(--ok); color: var(--ok); }

  .van-outline { display: block; width: 100%; max-width: 480px; color: var(--text-muted); touch-action: manipulation; cursor: crosshair; }
  .van-outline .van-label { font: 600 7px var(--font-body); fill: var(--text-muted); stroke: none; }
  .van-outline .mark { fill: var(--danger); stroke: var(--surface); stroke-width: 1.5; }

  .photos { display: flex; flex-wrap: wrap; gap: var(--space-2); margin-top: var(--space-2); }
  .photos img { width: 88px; height: 88px; object-fit: cover; border-radius: var(--radius-sm); border: 1px solid var(--border); }
  .answer.is-fail { background: var(--danger-tint); }
  .answer.is-warn { background: var(--warn-tint); }
  a.notice { text-decoration: none; }
  .notice-error strong { color: inherit; }
}

/* Ladders: the big status a scan opens on, and the tag preview. */
@layer components {
  .ladder-status { display: grid; gap: var(--space-1); padding: var(--space-4); border-radius: var(--radius); border: 2px solid; }
  .ladder-status strong { font-family: var(--font-heading); font-weight: 800; font-size: var(--text-2xl); letter-spacing: 0.02em; }
  .ladder-status.is-ok { background: var(--ok-tint); border-color: var(--ok); color: var(--ok); }
  .ladder-status.is-warn { background: var(--warn-tint); border-color: var(--warn); color: var(--warn); }
  .ladder-status.is-danger { background: var(--danger-tint); border-color: var(--danger); color: var(--danger); }
  .ladder-status.is-neutral { background: var(--surface-sunken); border-color: var(--border-strong); color: var(--text); }

  fieldset[data-layouts] { border: 0; padding: 0; margin: 0; min-width: 0; }
  /* Black only: on the office inkjet only the black is water resistant. */
  .tags { display: flex; flex-wrap: wrap; gap: var(--space-3); margin-top: var(--space-5); }
  .tag {
    display: flex; align-items: center; gap: 0.75rem; width: 16.5rem; height: 8.8rem; padding: 0.6rem 0.75rem;
    background: #fff; color: #000; border: 1px dashed var(--border-strong); border-radius: 8px; box-sizing: border-box;
  }
  .tag-qr { flex: none; width: 7rem; height: 7rem; }
  .tag-qr svg { width: 100%; height: 100%; display: block; }
  .tag-text { display: grid; align-content: center; gap: 0.15rem; min-width: 0; line-height: 1.05; }
  .tag-brand { font-family: var(--font-heading); font-weight: 600; font-size: 0.58rem; letter-spacing: 0.02em; text-transform: uppercase; white-space: nowrap; }
  .tag-company { display: block; font-weight: 600; text-transform: none; letter-spacing: 0; opacity: 0.85; }
  .tag-id { font-family: var(--font-heading); font-weight: 800; font-size: 2.3rem; letter-spacing: -0.01em; border-bottom: 0.18rem solid #000; padding-bottom: 0.1rem; }
  /* KIT-001 is longer than L-001: smaller, so it stays on one line. */
  .tag-id-long { font-size: 1.65rem; white-space: nowrap; }
  .tag-kind { font-size: 0.72rem; font-weight: 600; line-height: 1.15; margin-top: 0.15rem; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
  .tag-cta {
    justify-self: start; margin-top: 0.25rem; padding: 0.12rem 0.45rem; border-radius: 999px;
    background: #000; color: #fff; font-family: var(--font-heading); font-weight: 600; font-size: 0.66rem;
    letter-spacing: 0.04em; text-transform: uppercase; print-color-adjust: exact; -webkit-print-color-adjust: exact;
  }
}

/* Accident reports: injury questions show once someone is hurt; a printable record. */
@layer components {
  .choice-list { display: grid; gap: var(--space-2); }
  .injured-block .show-colleague, .injured-block .show-outsider, .injured-block .show-hurt { display: none; }
  .injured-block:has(input[name="injured"][value="colleague"]:checked) .show-colleague,
  .injured-block:has(input[name="injured"]:is([value="subcontractor"], [value="customer"], [value="public"]):checked) .show-outsider,
  .injured-block:has(input[name="injured"]:not([value="nobody"]):checked) .show-hurt { display: grid; }
  @supports not selector(:has(*)) { .injured-block .show-colleague, .injured-block .show-outsider, .injured-block .show-hurt { display: grid; } }
  .injured-block .errorlist, .injured-block .error { display: block; }
  .book-no { flex: none; min-width: 2.5rem; font-family: var(--font-heading); font-weight: 800; font-size: var(--text-xl); color: var(--ink); }
}
@media print {
  .app-header, .tabbar, .messages, .no-print, .save-bar { display: none !important; }
  .app, .app-main { display: block !important; padding: 0 !important; max-width: none !important; }
  body { background: #fff !important; }
  .card { box-shadow: none !important; border: 1px solid #999 !important; break-inside: avoid; }
}

/* Signatures: a pad to sign with a finger; the saved signature on a check. */
@layer components {
  .sig { display: grid; gap: var(--space-2); }
  .sig-pad-wrap { position: relative; border: 2px solid var(--border-strong); border-radius: var(--radius); background: #fff; }
  .sig-pad-wrap[hidden] { display: none; }
  .sig-pad { display: block; width: 100%; height: 150px; touch-action: none; cursor: crosshair; }
  .sig-line { position: absolute; left: var(--space-3); right: var(--space-3); bottom: 28px; border-top: 1px solid var(--border-strong); padding-top: 2px; font-size: var(--text-xs); color: var(--text-muted); pointer-events: none; }
  .sig-clear { position: absolute; top: var(--space-1); right: var(--space-1); }
  .sig.has-pad [data-sig-typed] { display: none; }
  .signed { display: grid; gap: var(--space-1); }
  .signed img { max-width: 260px; height: auto; border-bottom: 1px solid var(--border-strong); }
}

/* Policies and risk assessments. */
@layer components {
  .doc-body h2 { font-size: var(--text-lg); margin: var(--space-5) 0 var(--space-2); }
  .doc-body h2:first-child { margin-top: 0; }
  .doc-body ul { padding-left: 1.2rem; margin: 0 0 var(--space-3); }
  .doc-body p { margin: 0 0 var(--space-3); }
  .risk { display: inline-flex; align-items: baseline; gap: 0.25rem; padding: 0.1rem 0.5rem; border-radius: var(--radius-pill); font-family: var(--font-heading); font-weight: 800; }
  .risk small { font-family: var(--font-body); font-weight: 600; font-size: var(--text-xs); text-transform: uppercase; }
  .risk-low { background: var(--ok-tint); color: var(--ok); }
  .risk-medium { background: var(--warn-tint); color: var(--warn); }
  .risk-high { background: var(--danger-tint); color: var(--danger); }
  .hazard h3 { font-size: var(--text-md); }
  .sig-small { max-height: 36px; width: auto; }
  tr.unsigned td { color: var(--text-muted); }
}

/* Links that live in the side rail on wider screens and in a list on phones. */
@media (max-width: 767px) { .tab.rail-only { display: none !important; } }
@media (min-width: 768px) { .phone-only { display: none !important; } }

/* A talk read aloud: larger text. */
@layer components { .talk-points { font-size: var(--text-lg); } }

/* Help: the ? in the header (a 48px target round a smaller circle), and the guides. */
@layer components {
  .help-button { display: grid; place-items: center; width: 48px; height: 48px; margin-left: auto; flex: none; text-decoration: none; color: var(--ink); }
  .help-button span {
    display: grid; place-items: center; width: 34px; height: 34px; border-radius: 50%;
    border: 2px solid var(--brand-dark-grey); font-weight: 800; font-size: 1.05rem; line-height: 1;
  }
  .help-button:hover span, .help-button:focus-visible span { background: var(--lime-tint); border-color: var(--brand-lime); }
  .guide { max-width: 40rem; }
  .guide h2 { margin-top: var(--space-6); scroll-margin-top: 80px; }
  .guide h3 { margin-top: var(--space-4); }
  .guide p, .guide li { line-height: 1.55; }
  .guide p, .guide ol, .guide ul, .guide table { margin: var(--space-3) 0; }
  .guide ol, .guide ul { padding-left: 1.25rem; display: grid; gap: var(--space-2); }
  .guide table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
  .guide th, .guide td { text-align: left; vertical-align: top; padding: var(--space-2); border-bottom: 1px solid var(--border); }
  .guide th { font-weight: 700; color: var(--ink); }
  .guide .notice { margin: var(--space-4) 0; }
  .help-shot { margin: var(--space-4) auto; max-width: 300px; }
  .help-shot img {
    display: block; width: 100%; height: auto; border-radius: 18px;
    border: 1px solid var(--border); box-shadow: 0 6px 20px rgb(0 0 0 / 0.08);
  }
  .help-shot figcaption { margin-top: var(--space-2); font-size: var(--text-sm); color: var(--text-muted); text-align: center; }
  .help-n {
    display: inline-grid; place-items: center; width: 1.45em; height: 1.45em; border-radius: 50%;
    background: var(--accent); color: #fff; font-size: 0.8em; font-weight: 800; vertical-align: 0.1em;
  }
  .guide-nav { display: flex; justify-content: space-between; gap: var(--space-3); margin-top: var(--space-6); }
  .help-snippet { font-size: var(--text-sm); color: var(--text-muted); }
}
