:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --ink: #0f172a;
  --muted: #5b6577;
  --line: #e2e7ee;
  --line-strong: #cfd6e0;
  --brand: #2f4aa0;
  --brand-ink: #ffffff;
  --brand-soft: #e8edfa;
  --brand-glow: #c7d3f5;
  --good: #15803d;
  --good-soft: #e7f6ec;
  --warn: #b45309;
  --warn-soft: #fdf3e2;
  --bad: #b91c1c;
  --bad-soft: #fdeceb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 1px 1px rgba(15, 23, 42, 0.03);
  --font: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@view-transition { navigation: auto; }
::view-transition-old(root), ::view-transition-new(root) { animation-duration: 120ms; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 var(--font);
  font-variant-numeric: tabular-nums;
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.25; margin: 0; letter-spacing: -0.01em; }
h1 { font-size: 22px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
p { margin: 0 0 12px; }
small, .muted { color: var(--muted); }
.nowrap { white-space: nowrap; }
code { font-family: var(--mono); font-size: 0.92em; }

/* ---- shell ---- */
.shell { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: 100vh; }
.side {
  background: #151c35;
  color: #d5dbef;
  padding: 16px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 16px; color: #fff; font-weight: 650; }
.brand img { width: 34px; height: 34px; border-radius: 9px; }
.brand span { line-height: 1.15; font-size: 14px; }
.brand small { display: block; color: #a3aed6; font-weight: 400; font-size: 11px; }
.nav { display: flex; flex-direction: column; gap: 1px; }
.nav .group { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: #8792bd; padding: 14px 10px 4px; }
.nav a {
  color: #d5dbef;
  padding: 7px 10px;
  border-radius: 7px;
  display: flex;
  align-items: center;
  gap: 9px;
}
.nav a:hover { background: #222c52; text-decoration: none; color: #fff; }
.nav a.on { background: var(--brand-glow); color: #0f1a3d; font-weight: 600; }
.nav svg { width: 16px; height: 16px; flex: none; opacity: 0.85; }
.side form { margin: 0; }
.side .who { border-top: 1px solid #27315a; margin-top: 14px; padding: 12px 10px 0; font-size: 13px; }
.side .who a { color: #d5dbef; }
.linkbtn { background: none; border: 0; padding: 0; color: #a3aed6; cursor: pointer; font: inherit; }
.linkbtn:hover { color: #fff; text-decoration: underline; }

main { padding: 24px 32px 64px; width: 100%; min-width: 0; }
.appbar, .tabbar { display: none; }

/* ---- page header ---- */
.head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.head p { margin: 4px 0 0; color: var(--muted); }
.crumbs { font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---- surfaces ---- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card > .pad, .pad { padding: 16px; }
.card + .card, .card + .grid, .grid + .card, .grid + .grid { margin-top: 16px; }
.grid > * { margin-top: 0 !important; }
.card-h { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 1000px) { .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 640px) { .g2, .g3, .g4 { grid-template-columns: 1fr; } }

.stat { padding: 14px 16px; }
.stat .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
.stat .value { font-size: 22px; font-weight: 650; margin-top: 2px; letter-spacing: -0.01em; }
.stat .sub { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.pos { color: var(--good); }
.neg { color: var(--bad); }

dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; margin: 0; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; font-weight: 500; }

/* ---- tables ---- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; background: #fafbfc; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: #fafcfd; }
td.num, th.num { text-align: right; white-space: nowrap; }
tfoot td { font-weight: 650; border-top: 1px solid var(--line-strong); border-bottom: 0; background: #fafbfc; }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }

/* ---- badges ---- */
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: #eef1f5; color: #3b4658; white-space: nowrap; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.brand { background: var(--brand-soft); color: var(--brand); }

/* ---- forms ---- */
form { margin: 0; }
.form { display: grid; gap: 14px; grid-template-columns: minmax(0, 1fr); }
.grid > *, .fields > * { min-width: 0; }
.fields { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); }
label { display: grid; gap: 4px; font-weight: 550; font-size: 13px; }
label .hint { font-weight: 400; color: var(--muted); font-size: 12px; }
/* Fields side by side share their rows (label, control, anything under it), so a label that
   wraps to two lines no longer pushes its box below the boxes next to it. */
@supports (grid-template-rows: subgrid) {
  .fields > * { grid-row: span 3; }
  .fields > label:not(.check) { grid-template-rows: subgrid; row-gap: 4px; align-content: start; }
  .fields > label:not(.check) > span:first-child { align-self: end; }
  .fields > :not(label) { align-self: end; }
  .fields > label.check { align-self: end; padding-bottom: 8px; }
}
.span2 { grid-column: span 2; }
.span-all { grid-column: 1 / -1; }
@media (max-width: 640px) { .span2 { grid-column: auto; } }
input, select, textarea {
  font: inherit;
  color: inherit;
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #fff;
  min-height: 38px;
}
textarea { min-height: 70px; resize: vertical; }
input, select, textarea { min-width: 0; max-width: 100%; }
input[type='date'], input[type='month'], input[type='time'] { display: block; }
/* iPhone Safari gives date fields a fixed intrinsic width and centred text; make them behave like other inputs */
@supports (-webkit-touch-callout: none) {
  input[type='date'], input[type='month'], input[type='time'] { -webkit-appearance: none; appearance: none; text-align: left; line-height: 1.4; }
  input::-webkit-date-and-time-value { text-align: left; margin: 0; min-height: 1.4em; }
}
fieldset.reveal { border: 1px solid var(--line); border-radius: 12px; padding: 12px 16px 16px; margin: 0; background: #f8fafb; min-width: 0; display: grid; gap: 12px; }
fieldset.reveal legend { padding: 0 6px; font-weight: 650; }
fieldset.reveal .check { margin: 0; }
fieldset.reveal:not(:has(input[name='mode']:checked)) > .fields { display: none; }
.rm-text { display: none; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-glow); outline-offset: 0; border-color: var(--brand); }
input[type='checkbox'], input[type='radio'] { width: auto; min-height: 0; padding: 0; flex: none; accent-color: var(--brand); }
input[readonly] { background: #f6f8fa; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: inherit;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  min-height: 38px;
  white-space: nowrap;
}
.btn:hover { text-decoration: none; background: #f6f8fa; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { background: #1f3478; }
.btn.danger { color: var(--bad); border-color: #efc4c1; }
.btn.danger:hover { background: var(--bad-soft); }
.btn.sm { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.alert { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; border: 1px solid; }
.alert.error { background: var(--bad-soft); border-color: #f2c5c2; color: #8c1616; }
.alert.ok { background: var(--good-soft); border-color: #bfe5cb; color: #135f30; }
.copy-row { display: flex; gap: 6px; margin-top: 10px; flex-wrap: wrap; }
.copy-row input { flex: 1 1 260px; min-height: 32px; font-family: var(--mono); font-size: 12.5px; background: #fff; color: var(--ink); }
.alert-link { font-weight: 650; color: inherit; text-decoration: underline; margin-left: 4px; }
.alert.info { background: var(--brand-soft); border-color: #c3cdee; color: #1f3478; }

/* workflow steps */
.steps { list-style: none; margin: 0 0 16px; padding: 14px 16px; display: grid; grid-template-columns: repeat(auto-fit, minmax(112px, 1fr)); gap: 12px; }
.steps li { display: flex; gap: 10px; align-items: flex-start; position: relative; }
.steps .dot { flex: none; width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: 12px; background: #eef1f5; color: var(--muted); border: 1px solid var(--line-strong); }
.steps li.done .dot { background: var(--good); border-color: var(--good); color: #fff; }
.steps li.current .dot { background: var(--brand); border-color: var(--brand); color: #fff; box-shadow: 0 0 0 4px var(--brand-soft); }
.steps strong { display: block; font-size: 13.5px; }
.steps li.todo strong { color: var(--muted); }
.steps small { display: block; color: var(--muted); font-size: 12px; margin-bottom: 6px; }
.steps .btn { margin-top: 2px; white-space: normal; text-align: left; line-height: 1.25; }
.steps li, .steps li > div { min-width: 0; }
.step-link { font-size: 12px; }

.checklist { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.checklist li { display: flex; gap: 10px; align-items: flex-start; }
.checklist .dot { flex: none; width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: 12px; background: #eef1f5; color: var(--muted); }
.checklist li.done .dot { background: var(--good); color: #fff; }
.checklist li.done strong { text-decoration: line-through; color: var(--muted); }
.checklist li.current .dot { background: var(--brand); color: #fff; }
.checklist small { display: block; color: var(--muted); }

/* clickable table rows */
tr[data-href] { cursor: pointer; }

/* pickers with "+ New" */
.select-add { display: flex; gap: 6px; align-items: stretch; min-width: 0; }
.select-add select { flex: 1; min-width: 0; }
.select-add .quick { padding: 0 10px; color: var(--brand); font-weight: 650; flex: none; }
.select-add .quick.sm { min-height: 0; padding: 0 9px; font-size: 16px; }

/* quick-add dialog */
dialog.quick-dialog { border: 0; border-radius: 14px; padding: 0; width: min(760px, 96vw); max-height: 92vh; box-shadow: 0 20px 60px rgba(15, 23, 42, 0.35); overflow: hidden; }
dialog.quick-dialog::backdrop { background: rgba(15, 20, 40, 0.45); }
dialog.quick-dialog header { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--line); }
dialog.quick-dialog iframe { display: block; width: 100%; height: 360px; max-height: calc(92vh - 54px); border: 0; background: var(--bg); }
body.embed { background: var(--bg); }
body.embed main { padding: 16px; }
body.embed .crumbs, body.embed .head .actions, body.embed .head h1, body.embed a.btn.ghost { display: none; }
body.embed .head { margin-bottom: 10px; }
body.embed .head:not(:has(p)) { display: none; }
/* inside the dialog: no app bar or tab bar, so the save bar sits at the bottom */
body.embed { --tabbar-h: 0px; }
body.embed .appbar { display: none !important; }

/* line-item editors */
.lines td { padding: 6px 8px; vertical-align: middle; }
.lines input, .lines select { min-width: 90px; }
.lines .rm { color: var(--bad); }
.lines td.w-item { min-width: 200px; }
.lines td.w-batch { min-width: 180px; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: end; margin-bottom: 14px; }
.filters label { min-width: 150px; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 14px; }
.tabs a { padding: 6px 12px; border-radius: 999px; background: #fff; border: 1px solid var(--line); color: var(--ink); font-weight: 550; font-size: 13px; }
.tabs a.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.tabs a:hover { text-decoration: none; }

.bar { height: 6px; border-radius: 3px; background: #e8edf2; overflow: hidden; min-width: 80px; }
.bar > i { display: block; height: 100%; background: var(--brand); }
.bar.done > i { background: var(--good); }

/* chart */
.only-narrow { display: none; }
@media (max-width: 700px) { .only-wide { display: none; } .only-narrow { display: block; } }
.chart { width: 100%; height: auto; display: block; }
.chart .axis { stroke: var(--line); }
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }

/* auth pages */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: radial-gradient(circle at 20% 0%, #e3e9fa 0, transparent 50%), var(--bg); }
.auth .card { width: 100%; max-width: 380px; }
.auth .logo { display: flex; flex-direction: column; align-items: center; gap: 8px; margin-bottom: 18px; text-align: center; }
.auth .logo img { width: 72px; height: 72px; border-radius: 50%; }
.auth .switch { text-align: center; margin-top: 14px; font-size: 13px; display: grid; gap: 6px; }

.thumbs { display: flex; flex-wrap: wrap; gap: 10px; }
.thumb { width: 140px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: #fff; font-size: 12px; }
.thumb a.img { display: block; aspect-ratio: 4 / 3; background: #f1f4f7 center / cover no-repeat; }
.thumb .file-icon { display: grid; place-items: center; aspect-ratio: 4 / 3; background: #f1f4f7; font-weight: 700; color: var(--muted); }
.thumb .meta { padding: 6px 8px; display: flex; justify-content: space-between; gap: 4px; align-items: center; }
.thumb .meta span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---- New / More screens ---- */
.tile-group, .menu-group { margin-bottom: 22px; }
.tile-group h2, .menu-group h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin: 0 0 8px 2px; }
.tiles { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
.tile { display: flex; gap: 12px; align-items: center; padding: 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; color: var(--ink); box-shadow: var(--shadow); }
.tile:hover { text-decoration: none; border-color: var(--brand); }
.tile .ti { flex: none; width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; background: var(--brand-soft); color: var(--brand); }
.tile svg { width: 22px; height: 22px; }
.tile strong { display: block; font-size: 15px; }
.tile small { display: block; font-size: 12.5px; line-height: 1.3; }
.profile-row { display: flex; gap: 12px; align-items: center; padding: 14px 16px; margin-bottom: 22px; color: var(--ink); }
.profile-row:hover { text-decoration: none; }
.profile-row small { display: block; }
.avatar { width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; background: var(--brand); color: #fff; font-weight: 700; font-size: 18px; flex: none; }
.menu-list a { display: flex; align-items: center; gap: 12px; padding: 14px 16px; color: var(--ink); border-bottom: 1px solid var(--line); font-weight: 550; }
.menu-list a:last-child { border-bottom: 0; }
.menu-list a:hover { text-decoration: none; background: #fafcfd; }
.menu-list a::after { content: '›'; margin-left: auto; color: var(--muted); font-size: 20px; line-height: 1; }
.menu-list svg { width: 20px; height: 20px; color: var(--brand); flex: none; }

/* ======== Phones & tablets: app-style shell ======== */
@media (max-width: 1024px) {
  :root { --tabbar-h: 62px; --safe-b: env(safe-area-inset-bottom, 0px); --radius: 14px; }
  body { font-size: 15px; }
  .shell { grid-template-columns: minmax(0, 1fr); }
  .side { display: none; }
  main { display: flex; flex-direction: column; padding: 0 16px calc(var(--tabbar-h) + var(--safe-b) + 28px); }
  main > * { flex: none; }
  a, button { -webkit-tap-highlight-color: transparent; }

  /* sticky title bar with back button */
  .appbar {
    order: -1;
    display: flex;
    align-items: center;
    gap: 4px;
    position: sticky;
    top: 0;
    z-index: 15;
    margin: 0 -16px 14px;
    padding: calc(env(safe-area-inset-top, 0px) + 6px) 12px 6px;
    min-height: 54px;
    background: rgba(255, 255, 255, 0.94);
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--line);
  }
  .appbar .back { width: 42px; height: 42px; margin-left: -6px; display: grid; place-items: center; border-radius: 50%; color: var(--ink); flex: none; }
  .appbar .back:active { background: #eef2f6; }
  .appbar .back svg { width: 26px; height: 26px; }
  .appbar-title { font-weight: 700; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .head h1, .crumbs { display: none; }
  .head { margin-bottom: 14px; }
  .head > div:first-child:not(:has(p)) { display: none; }
  .head .actions { width: 100%; gap: 8px; }
  .head .actions .btn { min-height: 40px; padding: 8px 14px; font-size: 14.5px; }
  /* a single main action spans the width; several become compact buttons */
  .head .actions:has(> :only-child) > *, .head .actions:has(> :only-child) .btn { flex: 1 1 auto; min-height: 46px; }

  /* bottom tab bar */
  .tabbar {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    background: rgba(255, 255, 255, 0.97);
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
    border-top: 1px solid var(--line);
    padding-bottom: var(--safe-b);
    box-shadow: 0 -4px 18px rgba(15, 23, 42, 0.06);
  }
  .tabbar a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; height: var(--tabbar-h); color: #6b7585; font-size: 11.5px; font-weight: 600; }
  .tabbar a:hover { text-decoration: none; }
  .tabbar .ti { display: grid; place-items: center; width: 52px; height: 28px; border-radius: 14px; transition: background 0.15s; }
  .tabbar svg { width: 23px; height: 23px; }
  .tabbar a.on { color: var(--brand); }
  .tabbar a.on .ti { background: var(--brand-soft); }
  .tabbar a.new .ti { width: 48px; height: 48px; margin-top: -18px; border-radius: 16px; background: var(--brand); color: #fff; box-shadow: 0 6px 16px rgba(47, 74, 160, 0.35); }
  .tabbar a.new.on .ti { background: #1f3478; }

  /* touch-sized controls; 16px text stops iPhones zooming into inputs */
  input, select, textarea { font-size: 16px; min-height: 46px; border-radius: 10px; padding: 10px 12px; }
  input[type='checkbox'], input[type='radio'] { width: 22px; height: 22px; min-height: 0; padding: 0; }
  .btn { min-height: 46px; padding: 10px 16px; border-radius: 10px; font-size: 15px; }
  .btn.sm { min-height: 38px; padding: 6px 12px; font-size: 14px; }
  label { font-size: 14px; }
  .select-add .quick { min-width: 46px; }
  .select-add .quick.sm { font-size: 20px; }

  /* the save button stays reachable above the tab bar on long forms */
  .form > .btn-row:last-child {
    position: sticky;
    bottom: calc(var(--tabbar-h) + var(--safe-b));
    z-index: 5;
    margin: 4px -16px -16px;
    padding: 12px 16px;
    background: rgba(255, 255, 255, 0.97);
    border-top: 1px solid var(--line);
    border-radius: 0 0 var(--radius) var(--radius);
  }
  .form > .btn-row:last-child .btn.primary { flex: 1 1 auto; }

  /* scrollable pills and compact filters */
  .tabs { flex-wrap: nowrap; overflow-x: auto; margin: 0 -16px 14px; padding: 0 16px 2px; scrollbar-width: none; }
  .tabs::-webkit-scrollbar { display: none; }
  .tabs a { flex: none; padding: 9px 15px; font-size: 14px; }
  .filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
  .filters label { min-width: 0; }
  .filters > .btn { grid-column: 1 / -1; }

  .card { border-radius: var(--radius); }
  .tile:active, .menu-list a:active, tr[data-href]:active td, table.cards tr[data-href]:active { background: #eef1fa; }
  .stat .value { font-size: 20px; }
  th, td { padding: 8px 9px; }
  th { font-size: 11px; letter-spacing: 0.03em; }
}

/* List tables become cards whenever their card is narrow (phones, and half-width panels on tablets):
   first column is the title, the rest are label/value pairs. */
.card { container-type: inline-size; }
@container (max-width: 480px) {
  .table-wrap:has(table.cards) { overflow: visible; }
  table.cards thead { display: none; }
  table.cards, table.cards tbody, table.cards tfoot, table.cards tr { display: block; width: 100%; }
  table.cards tr { padding: 12px 16px; border-bottom: 1px solid var(--line); }
  table.cards tbody tr:last-child { border-bottom: 0; }
  table.cards td { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 3px 0; border: 0; text-align: right; background: none !important; }
  table.cards td::before { content: attr(data-label); flex: none; color: var(--muted); font-size: 13px; font-weight: 500; text-align: left; }
  table.cards td:first-child { display: block; padding: 0 0 6px; text-align: left; font-weight: 650; font-size: 15.5px; }
  table.cards td:first-child::before { display: none; }
  table.cards td:first-child .muted, table.cards td:first-child small { display: block; font-size: 13px; font-weight: 500; }
  table.cards td:empty { display: none; }
  table.cards tfoot tr { background: #fafbfc; border-top: 1px solid var(--line-strong); }
  table.cards .bar { flex: 1; max-width: 160px; margin-top: 8px; }
}

/* ======== Phones ======== */
@media (max-width: 700px) {
  .g2, .g3 { grid-template-columns: 1fr; }
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .grid { gap: 12px; }
  .stat { padding: 12px 14px; }
  .stat .label { font-size: 11px; }
  .stat .value { font-size: 18px; }
  .fields { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  label .hint { display: block; margin-top: 1px; }
  dl.facts { grid-template-columns: 1fr; gap: 0; }
  dl.facts dd { margin-bottom: 8px; }
  .steps { grid-template-columns: 1fr 1fr; }

  /* quick-add opens as a full-screen sheet */
  dialog.quick-dialog { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; }
  dialog.quick-dialog iframe { height: calc(100dvh - 54px) !important; max-height: none; }

}

/* ======== Phones & portrait tablets: line-item editors ======== */
@media (max-width: 820px) {
  /* line-item editors: one card per line, label above each input */
  .lines thead { display: none; }
  .lines tbody, .lines tfoot { display: block; }
  .lines tbody tr { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; padding: 14px; margin-bottom: 10px; background: #f8fafb; border: 1px solid var(--line); border-radius: 12px; }
  .lines tbody td { display: block; min-width: 0; padding: 0; border: 0; text-align: left; background: none !important; }
  .lines tbody td::before { content: attr(data-label); display: block; margin-bottom: 4px; font-size: 14px; font-weight: 550; color: var(--ink); }
  .lines tbody td:first-child, .lines tbody td:has(.select-add) { grid-column: 1 / -1; }
  .lines tbody td[data-unit-out], .lines tbody td.unit-static, .lines tbody td:empty { display: none; }
  .lines tbody td.w-item, .lines tbody td.w-batch { min-width: 0; grid-column: 1 / -1; }
  .lines tbody td:has([data-amount]), .lines tbody td[data-amount] { grid-column: 1 / -1; display: flex; justify-content: space-between; align-items: baseline; padding-top: 10px; border-top: 1px dashed var(--line-strong); font-weight: 650; }
  .lines tbody td[data-amount]::before { margin: 0; font-weight: 500; color: var(--muted); }
  .lines tbody td[data-amount]:empty { display: none; }
  .lines tbody td:has(.rm) { grid-column: 1 / -1; display: flex; justify-content: flex-end; margin: -6px -6px -8px 0; }
  .lines tbody td:has(.rm)::before { display: none; }
  .lines .rm { min-height: 36px; padding: 4px 8px; font-size: 14px; font-weight: 600; }
  .lines .rm-text { display: inline; }
  .lines input, .lines select { min-width: 0; background: #fff; }
  .lines input[readonly] { background: #eef1f5; color: var(--muted); }
  .lines tfoot tr { display: flex; justify-content: space-between; padding: 4px 2px 0; font-size: 16px; }
  .lines tfoot td { border: 0; background: none; padding: 0; }
  .lines tfoot td:empty { display: none; }
}

/* print (packing list) */
.print-only { display: none; }
@media print {
  body { background: #fff; font-size: 12px; }
  .side, .tabbar, .appbar, .no-print { display: none !important; }
  .shell { display: block; }
  main { padding: 0; max-width: none; }
  .card { box-shadow: none; border-color: #bbb; }
  .print-only { display: block; }
}

/* printable documents (pink slip, invoice) */
.doc-head { display: flex; justify-content: space-between; gap: 16px; flex-wrap: wrap; align-items: flex-start; padding-bottom: 14px; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
.doc-title { font-size: 18px; font-weight: 700; letter-spacing: 0.02em; text-transform: uppercase; margin-bottom: 6px; }
.doc-pink { border-top: 6px solid #f2a7c3; }
.doc-pink .doc-title { color: #b03a6a; }
.doc-parties { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; margin-bottom: 14px; }
.doc-parties .muted { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
.doc-sign { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 40px 24px; margin-top: 48px; }
.doc-sign div { flex: 0 1 220px; border-top: 1px solid var(--line-strong); padding-top: 6px; text-align: center; color: var(--muted); }
.doc .table-wrap { overflow-x: auto; }
@media print {
  .doc { border: 0; padding: 0; }
}

/* groups of checkboxes / radios (partner types, user locations) */
fieldset.choices { border: 0; margin: 0; padding: 0; min-width: 0; }
fieldset.choices legend { padding: 0; margin-bottom: 8px; font-weight: 600; font-size: 13px; }
.choices .opts { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 6px 12px; }
.choices .check { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); margin: 0; }
.choices .check:has(input:checked) { border-color: var(--brand); background: var(--brand-soft); }

/* spacing between stacked blocks inside the cards of a multi-card form */
form.form > .card > .pad > * + *, form.form > .card > .pad > .table-wrap + .btn { margin-top: 14px; }
form.form > .card > .pad > p { margin-bottom: 0; }

details > summary { cursor: pointer; font-weight: 600; color: var(--brand); padding: 2px 0; }
details[open] > summary { margin-bottom: 10px; }

/* processing: live yield summary */
.yield { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.yield > div { background: var(--bg); border-radius: 10px; padding: 10px 12px; display: grid; gap: 2px; }
.yield span { font-size: 12px; color: var(--muted); }
.yield strong { font-size: 18px; }
.yield small { color: var(--muted); font-size: 12px; min-height: 1em; }
[data-y="warn"]:not([hidden]) { color: var(--bad); font-weight: 600; }
.stock-hint { display: block; margin-top: 3px; color: var(--muted); font-size: 12px; min-height: 1em; }

/* in-page "are you sure?" dialog */
dialog.ask-dialog { border: 0; border-radius: 14px; padding: 20px; width: min(420px, 92vw); box-shadow: 0 20px 60px rgba(15, 23, 42, 0.35); }
dialog.ask-dialog::backdrop { background: rgba(15, 20, 40, 0.45); }
dialog.ask-dialog p { margin: 0 0 18px; font-size: 15px; line-height: 1.5; }
dialog.ask-dialog .btn-row { justify-content: flex-end; }
.btn.danger-solid { background: var(--bad); border-color: var(--bad); color: #fff; }
/* filters apply on change when scripts run, so their button is only needed without JS */
.js .filters .auto-apply { display: none; }
.btn svg { width: 18px; height: 18px; flex: none; }
.btn:has(svg) { display: inline-flex; align-items: center; justify-content: center; gap: 8px; }

/* ======== Krishvedi ERP additions ======== */
/* notifications bell: top-right on desktop, in the app bar on phones */
.bell { position: fixed; top: 14px; right: 18px; z-index: 40; width: 40px; height: 40px; display: grid; place-items: center; border-radius: 50%; background: var(--surface); border: 1px solid var(--line); color: var(--ink); box-shadow: var(--shadow); }
.bell:hover { text-decoration: none; background: #f6f8fa; }
.bell svg { width: 20px; height: 20px; }
.count { position: absolute; top: -4px; right: -4px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--bad); color: #fff; font-size: 11px; font-weight: 700; line-height: 18px; text-align: center; font-style: normal; }
.tabbar .ti { position: relative; }
.tabbar .count { top: -2px; right: 4px; }
main > .head:first-of-type, main > .appbar + .head { padding-right: 52px; }
@media (max-width: 1024px) {
  .bell { top: calc(env(safe-area-inset-top, 0px) + 8px); right: 10px; width: 38px; height: 38px; box-shadow: none; }
  main > .head:first-of-type, main > .appbar + .head { padding-right: 0; }
  .appbar { padding-right: 56px; }
}

.side-search { padding: 0 6px 10px; }
.side-search input { min-height: 34px; padding: 6px 10px; border-radius: 8px; border: 1px solid #2f3a66; background: #1c2546; color: #fff; font-size: 13px; }
.side-search input::placeholder { color: #8792bd; }

/* "How this works" guides */
details.guide { background: #f2f8ff; border: 1px solid #cfe0f5; border-radius: var(--radius); padding: 10px 14px; margin-bottom: 16px; }
details.guide > summary { color: #1f4f86; display: flex; align-items: center; gap: 8px; list-style: none; }
details.guide > summary::-webkit-details-marker { display: none; }
details.guide[open] > summary { margin-bottom: 6px; }
.guide-i { width: 20px; height: 20px; border-radius: 50%; background: #1f4f86; color: #fff; display: grid; place-items: center; font-size: 12px; font-weight: 700; flex: none; }
.guide-body { color: #23384f; font-size: 13.5px; }
.guide-body p:last-child, .guide-body ol:last-child, .guide-body ul:last-child { margin-bottom: 0; }
.guide-body ol, .guide-body ul { margin: 4px 0 8px; padding-left: 20px; }
.guide-body li + li { margin-top: 3px; }
.tip { color: var(--muted); font-size: 12.5px; margin: 8px 0 0; }
.warn-box { border-left: 4px solid var(--warn); background: var(--warn-soft); margin-bottom: 16px; }
.info-box { border-left: 4px solid var(--brand); background: var(--brand-soft); margin-bottom: 16px; }
.bad-box { border-left: 4px solid var(--bad); background: var(--bad-soft); margin-bottom: 16px; }

/* pills with counts */
.pill-n { display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 9px; background: #eef1f5; color: var(--muted); font-size: 11.5px; font-weight: 650; }
.tabs a.on .pill-n { background: rgba(255,255,255,.2); color: #fff; }

/* history */
ol.timeline { list-style: none; margin: 0; padding: 12px 16px 12px 30px; position: relative; }
ol.timeline::before { content: ''; position: absolute; left: 20px; top: 18px; bottom: 18px; width: 2px; background: var(--line); }
ol.timeline li { position: relative; padding: 4px 0 12px; }
ol.timeline li::before { content: ''; position: absolute; left: -14px; top: 9px; width: 10px; height: 10px; border-radius: 50%; background: var(--brand); border: 2px solid #fff; box-shadow: 0 0 0 1px var(--line); }
ol.timeline small { color: var(--muted); }
.tl-reason { font-style: italic; color: #3b4658; margin-top: 2px; }
.tl-changes { margin: 4px 0 0; padding-left: 16px; font-size: 12.5px; color: #3b4658; }
.tl-changes s { color: var(--muted); }

/* reason pop-over (reject / cancel / reopen) */
details.reason-pop { position: relative; display: inline-block; }
details.reason-pop > summary { list-style: none; }
details.reason-pop > summary::-webkit-details-marker { display: none; }
details.reason-pop[open] > summary { margin-bottom: 0; }
details.reason-pop .reason-form { position: absolute; z-index: 20; top: calc(100% + 6px); left: 0; width: min(360px, 86vw); box-shadow: 0 12px 30px rgba(15,23,42,.18); display: grid; gap: 10px; }
.head .actions details.reason-pop .reason-form { left: auto; right: 0; }
@media (max-width: 700px) { details.reason-pop { display: block; } details.reason-pop > summary { width: 100%; } details.reason-pop .reason-form { position: static; width: 100%; margin-top: 8px; } }

/* tasks */
.task-list a { align-items: center; }
.task-n { min-width: 34px; height: 34px; border-radius: 10px; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; font-size: 15px; flex: none; }
.task-list small, .menu-list small { display: block; color: var(--muted); font-weight: 400; font-size: 12.5px; }
.menu-list a.unread strong::before { content: '● '; color: var(--bad); }

/* classification tree */
ul.tree, ul.tree ul { list-style: none; margin: 0; padding-left: 18px; }
ul.tree { padding-left: 0; }
ul.tree ul { border-left: 1px dashed var(--line-strong); margin-left: 6px; }
.tree-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 4px 0; }
.tree-add { display: flex; gap: 6px; padding: 4px 0 8px; max-width: 460px; }
.tree-add input { min-height: 32px; padding: 4px 8px; }

/* permission matrix */
.perm-matrix td, .perm-matrix th { padding: 6px 10px; }
.perm-matrix .perm-group td { background: #f4f6f8; font-weight: 700; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.perm-matrix input { width: 20px; height: 20px; }

/* values ever edited on a job card show light red */
.edited, td.edited input, input.edited { background: #fdecec !important; }

/* hover card for items */
.peek { position: absolute; z-index: 60; background: #151c35; color: #e9ecf7; padding: 10px 12px; border-radius: 10px; font-size: 12.5px; line-height: 1.45; max-width: 320px; box-shadow: 0 10px 30px rgba(0,0,0,.25); pointer-events: none; }
.peek strong { display: block; color: #fff; margin-bottom: 2px; }

/* live figures box (mass balance, totals) */
.live { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.live > div { background: var(--bg); border-radius: 10px; padding: 10px 12px; }
.live span { display: block; font-size: 12px; color: var(--muted); }
.live strong { font-size: 17px; }
.live .bad { color: var(--bad); }
.live .good { color: var(--good); }

/* labels (one per box) */
.labels { display: grid; grid-template-columns: repeat(auto-fill, minmax(90mm, 1fr)); gap: 6mm; }
.labels .label { border: 1px dashed #999; border-radius: 4px; padding: 3mm 4mm; height: 46mm; overflow: hidden; display: grid; align-content: start; gap: 1mm; font-size: 11px; page-break-inside: avoid; break-inside: avoid; background: #fff; }
.labels .label .l-item { font-size: 14px; font-weight: 700; line-height: 1.2; }
.labels .label .l-big { font-size: 16px; font-weight: 700; }
.labels .label .l-row { display: flex; justify-content: space-between; gap: 6px; }
@media print {
  .labels { grid-template-columns: repeat(2, 100mm); gap: 0; }
  .labels .label { width: 100mm; height: 50mm; border: 0; border-radius: 0; padding: 4mm 5mm; page-break-after: always; break-after: page; }
  .labels.a4 { grid-template-columns: repeat(2, 1fr); gap: 2mm; }
  .labels.a4 .label { width: auto; height: 38mm; page-break-after: auto; break-after: auto; border: 1px dashed #bbb; }
  .bell, details.guide { display: none !important; }
}
.doc-table td, .doc-table th { padding: 6px 8px; font-size: 13px; }
.kpi-link { color: inherit; display: block; }
.kpi-link:hover { text-decoration: none; }
.kpi-link:hover .stat { border-color: var(--brand); }
.stage-chain { display: flex; gap: 8px; flex-wrap: wrap; padding: 12px 16px; }
.stage-chain a { display: grid; gap: 2px; padding: 8px 12px; border-radius: 10px; border: 1px solid var(--line); background: #fff; color: var(--ink); min-width: 150px; font-size: 13px; }
.stage-chain a.on { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.stage-chain a:hover { text-decoration: none; border-color: var(--brand); }
.stage-chain small { color: var(--muted); }
.row-red td { background: #fff5f5; }
td.red, .red { color: var(--bad); font-weight: 650; }
.green { color: var(--good); font-weight: 650; }
.flow-strip { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 13px; color: var(--muted); margin: -6px 0 14px; }
.flow-strip b { color: var(--ink); font-weight: 600; }

/* a document number with its date underneath */
td small.muted { display: block; }
.spec-flag { margin-left: 6px; }
details.reason-pop .reason-form { text-align: left; white-space: normal; font-weight: 400; }
/* required fields and inline validation */
label:has(> input:required, > select:required, > textarea:required) > span:first-child::after { content: ' *'; color: var(--bad); font-weight: 600; }
.field-error { display: block; color: var(--bad); font-size: 12.5px; margin-top: 4px; font-weight: 500; }
label.invalid input, label.invalid select, label.invalid textarea, input.invalid { border-color: var(--bad); box-shadow: 0 0 0 3px color-mix(in srgb, var(--bad) 15%, transparent); }
.progress { height: 6px; border-radius: 99px; background: var(--line, #e5e7eb); margin: 4px 0 12px; overflow: hidden; }
.progress::before { content: ''; display: block; height: 100%; width: var(--p); background: var(--brand); border-radius: inherit; }
details.done-steps { margin-top: 8px; }
details.done-steps summary { cursor: pointer; font-size: 13px; }
@media (max-width: 520px) {
  .tiles { grid-template-columns: 1fr 1fr; gap: 8px; }
  .tile { flex-direction: column; align-items: flex-start; gap: 8px; padding: 12px; }
  .tile strong { font-size: 14px; line-height: 1.25; }
}
/* a card right after a form (or its buttons) needs the same gap as between cards */
form + .card, .btn-row + .card, .card + form, .card + .btn-row { margin-top: 16px; }
.form-inline { display: flex; gap: 10px; align-items: end; flex-wrap: wrap; }
.form-inline label { display: grid; gap: 4px; min-width: 220px; }
.form-inline label > span { font-size: 13px; font-weight: 600; }
/* a form card placed inside another card loses its own frame */
.card .card:not(.reason-form):not(dialog) { border: 0; box-shadow: none; padding: 0; background: transparent; }
@media (min-width: 900px) { table.lines td:has(select[data-item]) { min-width: 300px; } table.lines span[data-item-wrap] select { min-width: 250px; } }
.btn.on { border-color: var(--brand); color: var(--brand); background: var(--brand-soft); }
@media (min-width: 900px) { .table-wrap > table:not(.lines) > tbody > tr > td:first-child { min-width: 170px; } }
/* A line field that doesn't apply to the choice made on that line. */
.na { opacity: 0.35; pointer-events: none; }
/* Long button labels wrap on phones instead of widening the page. */
@media (max-width: 480px) {
  .btn { white-space: normal; max-width: 100%; }
}

/* Pack types on the item form and item page. */
ul.plain { list-style: none; margin: 0; padding: 0; }
.pack-uom { display: flex; gap: 6px; flex-wrap: wrap; }
.pack-uom select, .pack-uom input { min-width: 8em; }
.ratio { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.ratio input { width: 7em; }
/* A row's unit picker next to its quantity. */
select[data-unit], select[data-pack-pick], select[data-type-filter] { min-width: 9em; }

/* Quick add: "+ New" shows only for kinds the user may create (<body data-can-add>), never in filter bars. */
body:not([data-can-add~="vendor"]) .quick[data-kind="vendor"],
body:not([data-can-add~="customer"]) .quick[data-kind="customer"],
body:not([data-can-add~="transporter"]) .quick[data-kind="transporter"],
body:not([data-can-add~="processor"]) .quick[data-kind="processor"],
body:not([data-can-add~="party"]) .quick[data-kind="party"],
body:not([data-can-add~="RM"]) .quick[data-kind="RM"],
body:not([data-can-add~="PM"]) .quick[data-kind="PM"],
body:not([data-can-add~="SFG"]) .quick[data-kind="SFG"],
body:not([data-can-add~="FG"]) .quick[data-kind="FG"],
body:not([data-can-add~="BYP"]) .quick[data-kind="BYP"],
body:not([data-can-add~="CON"]) .quick[data-kind="CON"],
body:not([data-can-add~="item"]) .quick[data-kind="item"],
body:not([data-can-add~="site"]) .quick[data-kind="site"],
body:not([data-can-add~="store"]) .quick[data-kind="store"],
body:not([data-can-add~="floor"]) .quick[data-kind="floor"],
body:not([data-can-add~="hsn"]) .quick[data-kind="hsn"],
body:not([data-can-add~="reason"]) .quick[data-kind="reason"],
.filters .quick { display: none; }

/* Type to search over a long <select> (app.js): the select stays underneath for the form. */
.combo { position: relative; display: block; flex: 1; min-width: 0; }
.combo-native { position: absolute; inset: 0; opacity: 0; pointer-events: none; }
.combo-input { padding-right: 28px; background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%235b6577' stroke-width='2.5'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") no-repeat right 10px center; text-overflow: ellipsis; }
.combo.invalid .combo-input { border-color: var(--bad); }
.combo-list { position: fixed; z-index: 60; top: 0; left: 0; width: max-content; max-width: min(92vw, 520px); max-height: 300px; overflow: auto; margin: 0; padding: 4px; list-style: none; background: var(--surface); border: 1px solid var(--line-strong); border-radius: 8px; box-shadow: 0 8px 24px rgba(15, 23, 42, 0.14); }
.combo-list li { padding: 8px 10px; border-radius: 6px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.combo-list li[aria-selected='true'] { font-weight: 650; }
.combo-list li.active { background: var(--brand-soft); }
.combo-list li.combo-note { color: var(--muted); cursor: default; font-size: 13px; white-space: normal; }
@media (max-width: 640px) { .combo-list { max-width: calc(100vw - 32px); } .combo-list li { padding: 11px 10px; } }

/* ---- Textile ---- */
[hidden] { display: none !important; }
.big-out { display: block; font-size: 20px; font-weight: 700; padding: 6px 0; }
.inline-form { display: flex; gap: 8px; align-items: end; flex-wrap: wrap; margin: 0; }
.inline-form label { margin: 0; }
details.more { margin: 8px 0 12px; }
details.more > summary { cursor: pointer; color: var(--brand); font-weight: 600; padding: 6px 0; }
details.more[open] > summary { margin-bottom: 8px; }
fieldset.checks { border: 0; padding: 0; margin: 0 0 12px; display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; }
fieldset.checks legend { font-size: 13px; color: var(--muted); margin-bottom: 6px; width: 100%; }
.l-barcode { height: 12mm; }
.l-barcode svg { width: 100%; height: 100%; display: block; }
.tile strong.red { color: var(--bad); }
.live strong { font-variant-numeric: tabular-nums; }
@media print { .l-barcode { height: 12mm; } }
@media (max-width: 700px) { section.doc { overflow-x: auto; } .doc-head { flex-wrap: wrap; gap: 10px; } .doc-table { min-width: 100%; } }
body.embed [data-again] { display: none; }
