:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --text: #1f2933;
  --muted: #65717e;
  --border: #dde3e9;
  --primary: #0f766e;
  --primary-dark: #0b5952;
  --danger: #b42318;
  --danger-bg: #fdecea;
  --ok: #067647;
  --ok-bg: #e7f6ee;
  --warn: #9a5b00;
  --warn-bg: #fff4e0;
  --info-bg: #e8f1fb;
  --radius: 10px;
  color-scheme: light;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, "Segoe UI", Tahoma, "Noto Sans Arabic", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.6;
  padding-bottom: env(safe-area-inset-bottom);
}

a { color: var(--primary); }

h1 { font-size: 1.35rem; margin: 0 0 .75rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 1rem 0 .4rem; color: var(--muted); }

.muted { color: var(--muted); }
.small { font-size: .875rem; }

/* Top bar & navigation */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: .5rem;
  padding: .6rem 1rem; padding-top: calc(.6rem + env(safe-area-inset-top));
  background: var(--primary); color: #fff;
}
.brand { color: #fff; font-weight: 700; text-decoration: none; }
.topbar-logout { display: flex; align-items: center; gap: .5rem; margin: 0; }
.topbar-user { font-size: .9rem; opacity: .9; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .btn-ghost { color: #fff; border-color: rgba(255,255,255,.5); background: transparent; }

/* Grouped menu: a few pills in one row; each group opens a panel under the bar (full width on a phone). */
.mainnav {
  position: relative; z-index: 9;
  display: flex; flex-wrap: wrap; gap: .25rem;
  padding: .4rem .75rem; background: var(--surface); border-bottom: 1px solid var(--border);
}
.mainnav .navlink, .navgroup > summary {
  display: inline-block; padding: .4rem .75rem; border-radius: 999px; text-decoration: none; white-space: nowrap;
  color: var(--text); cursor: pointer; list-style: none; user-select: none;
}
.navgroup > summary::-webkit-details-marker { display: none; }
.navgroup > summary::after { content: "▾"; font-size: .75em; margin-inline-start: .35rem; color: var(--muted); }
.navgroup[open] > summary { background: var(--bg); }
.mainnav .navlink:hover, .navgroup > summary:hover { background: var(--bg); }
.mainnav .is-current { color: var(--primary); font-weight: 700; }
.navmenu {
  position: absolute; top: 100%; inset-inline: .5rem; max-width: 420px;
  display: grid; gap: .1rem; padding: .4rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 8px 24px rgba(15, 23, 42, .12);
}
@media (max-width: 480px) {
  .mainnav { gap: 0; padding: .35rem .25rem; justify-content: space-between; }
  .mainnav .navlink, .navgroup > summary { padding: .4rem .35rem; font-size: .9rem; }
  .navgroup > summary::after { margin-inline-start: .15rem; }
}
.navmenu a { padding: .55rem .75rem; border-radius: 8px; text-decoration: none; color: var(--text); }
.navmenu a:hover, .navmenu a:focus { background: var(--bg); }
.alert-warn { background: var(--warn-bg); color: var(--warn); border-color: #f0c78a; }
.neg { color: var(--danger); }
.balance-card > summary { cursor: pointer; }
.balance-card[open] > summary { margin-bottom: .6rem; }

.container { max-width: 860px; margin: 0 auto; padding: 1rem; }

/* Cards & layout */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem; margin-bottom: 1rem;
}
.card-narrow { max-width: 420px; margin: 1.5rem auto; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head h1 { margin: 0; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.tile {
  display: flex; flex-direction: column; gap: .25rem; padding: 1rem; text-decoration: none; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.tile span { color: var(--muted); font-size: .9rem; }
.tile:hover { border-color: var(--primary); }

.info-row, .actions-row { display: flex; flex-wrap: wrap; gap: .5rem 1.25rem; align-items: center; }
.actions-row { margin-top: .75rem; }
.actions-row form { margin: 0; }

/* Forms */
.form label { display: block; margin: .75rem 0 .3rem; font-weight: 600; }
.form label.checkbox { font-weight: 400; }
input[type="text"], input[type="password"], input[type="search"], input[type="date"], input[type="month"], select {
  width: 100%; padding: .65rem .75rem; font: inherit;
  border: 1px solid var(--border); border-radius: 8px; background: #fff; color: var(--text);
}
input:focus, select:focus, button:focus-visible, a:focus-visible { outline: 3px solid rgba(15,118,110,.3); outline-offset: 1px; }
input.has-error { border-color: var(--danger); }
.field-error { color: var(--danger); margin: .3rem 0 0; font-size: .9rem; }
.hint { color: var(--muted); font-size: .85rem; margin: .3rem 0 0; }

.password-field { display: flex; gap: .5rem; align-items: center; }
.password-field input { flex: 1; }

label.checkbox { display: flex; align-items: center; gap: .5rem; margin: .5rem 0; cursor: pointer; }
label.checkbox input { width: 1.15rem; height: 1.15rem; flex-shrink: 0; }

.perms { border: 1px solid var(--border); border-radius: var(--radius); margin: 1rem 0; padding: .5rem 1rem 1rem; }
.perms legend { font-weight: 700; padding: 0 .4rem; }
.templates { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; margin-top: .5rem; }

.searchbar, .filters { display: flex; flex-wrap: wrap; gap: .5rem; align-items: flex-end; margin-bottom: 1rem; }
.searchbar input { flex: 1; min-width: 180px; }
.filters label { flex: 1 1 150px; font-size: .9rem; margin: 0; }
.filters label select, .filters label input { margin-top: .25rem; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .3rem;
  padding: .6rem 1rem; font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text);
  min-height: 44px;
}
.btn:hover { border-color: var(--primary); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-warn { background: var(--warn-bg); border-color: #f0c78a; color: var(--warn); }
.btn-ghost { background: transparent; }
.btn-small { padding: .3rem .7rem; min-height: 34px; font-size: .875rem; }
.btn-block { width: 100%; margin-top: 1.25rem; }

/* Alerts & badges */
.alert { padding: .75rem 1rem; border-radius: 8px; margin-bottom: 1rem; border: 1px solid transparent; }
.alert-error { background: var(--danger-bg); color: var(--danger); border-color: #f5c2bd; }
.alert-success { background: var(--ok-bg); color: var(--ok); border-color: #b5e3c8; }
.alert-info { background: var(--info-bg); color: #1d4f91; border-color: #c4d9f2; }

.perm-label { flex: 1; }
.badge { white-space: nowrap; flex-shrink: 0; display: inline-block; padding: .05rem .55rem; border-radius: 999px; font-size: .8rem; font-weight: 600; }
.badge-ok { background: var(--ok-bg); color: var(--ok); }
.badge-off { background: #eceff2; color: var(--muted); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
.badge-soon { background: var(--info-bg); color: #1d4f91; font-weight: 400; }

.chips { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: .4rem; }
.chips li { background: var(--bg); border: 1px solid var(--border); border-radius: 999px; padding: .15rem .7rem; font-size: .9rem; }

/* Lists (mobile-first cards) */
.list { display: flex; flex-direction: column; gap: .6rem; }
.list-item {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: .75rem 1rem; display: flex; flex-direction: column; gap: .4rem;
}
.list-item.is-inactive { opacity: .7; }
.list-main { display: flex; flex-wrap: wrap; gap: .25rem .75rem; align-items: baseline; justify-content: space-between; }
.list-meta { display: flex; flex-wrap: wrap; gap: .3rem .9rem; align-items: center; }
.list-actions { display: flex; gap: .4rem; flex-wrap: wrap; }

.details { margin: .2rem 0 0; padding-inline-start: 1.1rem; }
.details .details { margin: 0; }

.pager { display: flex; justify-content: center; align-items: center; gap: 1rem; margin: 1rem 0; }

/* Distributors: tables, figures, wizard */
input.num { text-align: left; font-variant-numeric: tabular-nums; max-width: 10rem; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -.25rem; }
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th, .table td { padding: .45rem .5rem; border-bottom: 1px solid var(--border); text-align: right; vertical-align: top; }
.table th { color: var(--muted); font-weight: 600; white-space: nowrap; }
.table td input.num { width: 6.5rem; padding: .45rem .5rem; }
.table tfoot td, .table tr.subtotal td { font-weight: 700; background: var(--bg); }
.qv { white-space: nowrap; font-variant-numeric: tabular-nums; }
.table tr.is-changed td { background: var(--warn-bg); }

/* On phones the balance table becomes one card per lot, each figure labelled. */
@media (max-width: 640px) {
  .table-stack thead { display: none; }
  .table-stack, .table-stack tbody, .table-stack tfoot, .table-stack tr, .table-stack td { display: block; width: 100%; }
  .table-stack tr { border: 1px solid var(--border); border-radius: 8px; margin-bottom: .6rem; padding: .3rem .1rem; }
  .table-stack td { border: 0; padding: .2rem .6rem; display: flex; justify-content: space-between; gap: .75rem; }
  .table-stack td:first-child { display: block; border-bottom: 1px solid var(--border); padding-bottom: .4rem; margin-bottom: .2rem; }
  .table-stack td[data-label]::before { content: attr(data-label); color: var(--muted); }
}

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: .6rem; margin: .75rem 0; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .6rem .8rem; display: flex; flex-direction: column; gap: .15rem; }
.stat span { color: var(--muted); font-size: .85rem; }
.stat strong { font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.stat-main { border-color: var(--primary); }
.stat-main strong { color: var(--primary); font-size: 1.2rem; }
.stat-warn { border-color: #f0c78a; background: var(--warn-bg); }

.nowrap { white-space: nowrap; }

/* Collections, targets, remittances */
select.multi { min-height: 5.5rem; }
.inline-form { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; margin: 0; }
.inline-form select { width: auto; min-width: 10rem; padding: .35rem .5rem; }
.form label.plain { display: inline; font-weight: 400; margin: 0; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.steps { list-style: none; display: flex; gap: .4rem; flex-wrap: wrap; padding: 0; margin: 0 0 1rem; }
.steps li { padding: .2rem .7rem; border-radius: 999px; background: var(--surface); border: 1px solid var(--border); font-size: .875rem; color: var(--muted); }
.steps li.is-current { background: var(--primary); border-color: var(--primary); color: #fff; }

@media (min-width: 700px) {
  .list-item { flex-direction: row; align-items: center; justify-content: space-between; flex-wrap: wrap; }
  .audit-item { flex-direction: column; align-items: stretch; }
}

/* Training site (phase 9): a fixed banner on every page and a different colour, so it is never mistaken for the real site. */
.env-training { --primary: #b45309; --primary-dark: #92400e; --env-banner-h: 2rem; }
.env-banner {
  position: sticky; top: 0; z-index: 12;
  height: calc(var(--env-banner-h) + env(safe-area-inset-top)); padding: env(safe-area-inset-top) .5rem 0;
  line-height: var(--env-banner-h); text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  background: #facc15; color: #1f1300; font-weight: 700; font-size: .95rem; border-bottom: 2px solid #92400e;
}
.env-training .topbar { top: calc(var(--env-banner-h) + env(safe-area-inset-top)); padding-top: .6rem; }
@media print {
  .env-banner { position: static; height: auto; padding: 0; border: 1px solid #000; background: #fff; color: #000; margin-bottom: 4mm; }
}

/* Reports: printing. Only the report itself goes on paper. */
.stat a { align-self: flex-start; }
@media print {
  .topbar, .mainnav, .no-print, .alert-success { display: none !important; }
  body { background: #fff; color: #000; font-size: 11pt; }
  .container { max-width: none; margin: 0; padding: 0; }
  .card, .stat { box-shadow: none; break-inside: avoid; }
  .table-wrap { overflow: visible; margin: 0; }
  .table { font-size: 9.5pt; }
  .table thead { display: table-header-group; }
  .table tr { break-inside: avoid; }
  a { color: inherit; text-decoration: none; }
  h1 { font-size: 15pt; }
  h2 { font-size: 12pt; break-after: avoid; }
  @page { margin: 12mm; }
}

/* Phase 11: install the app. A slim strip under the top bar (same colour family, amber on training) and a block on the login page. */
[data-install][hidden], [data-install] [hidden] { display: none !important; }
.install { display: flex; align-items: center; flex-wrap: wrap; gap: .5rem; min-width: 0; }
.install-bar { padding: .35rem 1rem; background: var(--primary-dark); color: #fff; font-size: .9rem; }
.install-login { margin-top: 1rem; padding-top: .75rem; border-top: 1px solid var(--border); }
.install-btn { font-weight: 700; }
.install-bar .install-btn { padding: .3rem .9rem; min-height: 34px; background: #fff; border-color: #fff; color: var(--primary-dark); }
.install-login .install-btn { flex: 1 1 auto; background: var(--primary); border-color: var(--primary); color: #fff; }
.install-btn::before { content: "⬇"; }
.install-ios { margin: 0; flex: 1 1 12rem; min-width: 0; }
.install-help { flex: 1 1 12rem; min-width: 0; }
.install-help > summary { cursor: pointer; text-decoration: underline; width: max-content; max-width: 100%; }
.install-login .install-help > summary { color: var(--primary); font-size: .9rem; }
.install-help ul { margin: .4rem 0 0; padding-inline-start: 1.2rem; font-size: .875rem; line-height: 1.6; overflow-wrap: anywhere; }
.install-login .install-help ul { color: var(--muted); }
.install-close {
  margin-inline-start: auto; min-width: 36px; min-height: 36px; padding: 0;
  border: 0; border-radius: 8px; background: transparent; color: inherit; font: inherit; cursor: pointer; opacity: .85;
}
.install-close:hover, .install-close:focus-visible { opacity: 1; background: rgba(255,255,255,.15); }
.install-login .install-close { color: var(--muted); }
.install-login .install-close:hover { background: var(--bg); }
@media print {
  .install { display: none !important; }
}

/* Phase 10: opening balances preview */
.opening-row h3 { margin: 0 0 .5rem; font-size: 1.05rem; }
.opening-critical { border-color: #f5c2bd; }
.opening-row ul.alert { padding-inline-start: 2rem; }
