/* Navy + gold. Navy = structure and text, gold = the one accent (main action, active nav), never text on light. */
:root {
  --bg: #F5F3EE; --card: #FFFFFF; --overlay: #FFFFFF; --sunken: #FAF8F4;
  --text: #14213D; --muted: #5C6478; --line: rgb(20 33 61 / .1); --field-line: #8A8F9C;
  --link: #1F3A6E; --focus: #1F3A6E;
  --btn: #14213D; --btn-ink: #FFFFFF;
  --gold: #D4AF37; --gold-ink: #14213D; --gold-soft: rgb(212 175 55 / .16); --gold-deep: #9A7B1C;
  --side: #14213D; --side-ink: #C9D1E3; --side-line: rgb(255 255 255 / .08); --side-active: #D4AF37; --side-active-ink: #14213D; --side-hover: rgb(255 255 255 / .06);
  --neg: #B42318; --neg-ink: #FFFFFF; --pos: #067647; --warn: #8A5300;
  --shadow-l1: 0 1px 2px rgb(20 33 61 / .04); --shadow-l3: 0 8px 24px rgb(20 33 61 / .07); --scrim: rgb(11 18 32 / .5);
  --vp-wide: 1280px; --hit: 44px; --side-w: 240px;
  --dur: 200ms; --ease-out: cubic-bezier(.2, 0, 0, 1);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0B1220; --card: #111A2E; --overlay: #16213A; --sunken: #0E1628;
    --text: #E8ECF4; --muted: #9AA6BD; --line: rgb(232 236 244 / .1); --field-line: #5F6E8E;
    --link: #9DB8F0; --focus: #E0B84C;
    --btn: #E8ECF4; --btn-ink: #0B1220;
    --gold: #E0B84C; --gold-ink: #1A1405; --gold-soft: rgb(224 184 76 / .14); --gold-deep: #E0B84C;
    --side: #0E1628; --side-ink: #9AA6BD; --side-line: rgb(232 236 244 / .08); --side-active: rgb(224 184 76 / .14); --side-active-ink: #E0B84C; --side-hover: rgb(255 255 255 / .04);
    --neg: #F87171; --neg-ink: #1A0505; --pos: #4ADE80; --warn: #E0B84C;
    --shadow-l1: inset 0 1px 0 rgb(255 255 255 / .04); --shadow-l3: inset 0 1px 0 rgb(255 255 255 / .06); --scrim: rgb(0 0 0 / .6);
  }
}
/* Vazirmatn, self-hosted (SIL OFL 1.1, see fonts/OFL.txt) — only the two weights the UI uses */
@font-face { font-family: Vazirmatn; src: url(/fonts/Vazirmatn-Regular.woff2) format("woff2"); font-weight: 400; font-display: swap; }
@font-face { font-family: Vazirmatn; src: url(/fonts/Vazirmatn-SemiBold.woff2) format("woff2"); font-weight: 600; font-display: swap; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.55 Vazirmatn, "Segoe UI", Tahoma, sans-serif; font-variant-numeric: tabular-nums; }
h2, h3, h4 { margin: 0 0 8px; line-height: 1.25; font-weight: 600; }
h2 { font-size: 24px; } h3 { font-size: 18px; } h4 { font-size: 16px; }
b, strong { font-weight: 600; }
p { margin: 0 0 8px; }
a { color: var(--link); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.muted { color: var(--muted); font-size: 14px; }
small { font-size: 12px; }
.error { color: var(--neg); }
.neg { color: var(--neg); } .pos { color: var(--pos); } .warn { color: var(--warn); }
.ico { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.ico.flip { transform: scaleX(-1); }

/* ---------- chrome: navy sidebar (desktop) / top bar + bottom tab bar (phone) ---------- */
#side { position: fixed; inset-block: 0; inset-inline-start: 0; width: var(--side-w); background: var(--side); color: var(--side-ink);
  display: flex; flex-direction: column; padding: 16px 12px; padding-top: max(16px, env(safe-area-inset-top)); z-index: 20; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 12px; padding: 4px 8px 16px; margin-bottom: 8px; border-bottom: 1px solid var(--side-line); color: #fff; }
.brand img { border-radius: 8px; }
.brand strong { font-size: 16px; }
#nav { display: flex; flex-direction: column; gap: 4px; }
#nav a { display: flex; align-items: center; gap: 12px; min-height: var(--hit); padding: 0 12px; border-radius: 8px; color: var(--side-ink); text-decoration: none; }
#nav a[aria-current] { background: var(--side-active); color: var(--side-active-ink); font-weight: 600; }
#who { margin-top: auto; padding: 16px 8px 0; border-top: 1px solid var(--side-line); display: grid; gap: 8px; font-size: 14px; }
.who-name { color: #fff; font-weight: 600; }
.who-role { color: var(--side-ink); }
#who select { background: var(--side-hover); color: #fff; border-color: var(--side-line); }
#who select option { color: #14213D; background: #fff; }
#who button.link { color: var(--side-ink); justify-content: flex-start; gap: 8px; padding: 0 4px; }
.topbar { display: none; }
#tabbar { display: none; }
body:not(.auth) { padding-inline-start: var(--side-w); }
body.auth #side, body.auth .topbar, body.auth #tabbar { display: none !important; }
main { max-width: var(--vp-wide); margin: 0 auto; padding: 24px; }
main[aria-busy] { opacity: .6; }

@media (hover: hover) {
  #nav a:not([aria-current]):hover { background: var(--side-hover); color: #fff; }
  tbody tr:hover { background: var(--sunken); }
  button.primary:hover, .btn.primary:hover { filter: brightness(1.05); }
}

/* ---------- surfaces ---------- */
.card { background: var(--card); border: 1px solid var(--line); box-shadow: var(--shadow-l1); border-radius: 12px; padding: 16px; margin-bottom: 16px; }
.card.narrow { max-width: 400px; margin: 48px auto; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.crumbs { font-size: 14px; color: var(--muted); }

/* auth: navy canvas, white card, gold mark */
body.auth { background: var(--side); }
body.auth main { min-height: 100dvh; display: grid; place-items: center; }
.auth-card { text-align: center; width: min(400px, 100%); margin: 0; padding: 32px 24px; }
.auth-card img { border-radius: 12px; margin-bottom: 8px; }
.auth-card form { text-align: start; display: grid; gap: 12px; margin-top: 16px; }

/* ---------- controls ---------- */
label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); min-height: var(--hit); }
input, select, textarea { font: inherit; font-size: 16px; color: var(--text); background: var(--card); border: 1px solid var(--field-line); border-radius: 8px; padding: 8px 12px; min-height: var(--hit);
  min-width: 0; max-width: 100%; } /* a long <option> or a file input must not widen its container */
label { min-width: 0; }
.version { font-size: 12px; color: var(--muted); margin: 0; }
#who .version { color: var(--side-ink); }
input[type=checkbox] { width: 20px; height: 20px; min-height: 0; accent-color: var(--btn); }
input[type=range] { padding: 0; border: 0; background: none; accent-color: var(--gold-deep); }
[aria-invalid=true] { border-color: var(--neg); }
.field-error { color: var(--neg); font-size: 14px; }
button, .btn { font: inherit; font-size: 16px; min-height: var(--hit); padding: 0 16px; border-radius: 8px; border: 1px solid transparent; background: var(--btn); color: var(--btn-ink);
  cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
button.primary, .btn.primary { background: var(--gold); color: var(--gold-ink); font-weight: 600; }
button:disabled { opacity: .55; cursor: default; }
button.secondary, .btn.secondary { background: var(--card); color: var(--text); border-color: var(--field-line); }
button.danger { background: var(--neg); color: var(--neg-ink); }
button.link { background: none; color: var(--link); padding: 0 8px; }
button.link.danger { color: var(--neg); }
a.link { display: inline-flex; align-items: center; min-height: var(--hit); padding: 0 8px; }
.row-actions { display: flex; align-items: center; gap: 4px; }
button:disabled.danger, button:disabled.primary { opacity: .45; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: end; margin: 8px 0; }
.range label { flex: 1 1 140px; }

/* ---------- lists ---------- */
.toolbar { display: flex; gap: 12px; flex-wrap: wrap; align-items: end; margin-bottom: 12px; }
.search { flex: 1 1 240px; }
.filters summary { display: inline-flex; align-items: center; min-height: var(--hit); padding: 0 16px; border: 1px solid var(--field-line); border-radius: 8px; cursor: pointer; }
.filters[open] summary { border-color: var(--text); }
.filters form { margin-top: 8px; }
.chips { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; font-size: 14px; }
.chip { border: 1px solid var(--field-line); border-radius: 8px; padding: 4px 8px; }
.pager { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 12px; font-size: 14px; color: var(--muted); }
.scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: start; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 400; background: var(--sunken); }
tbody tr:last-child td { border-bottom: 0; }
td a { font-weight: 600; color: var(--link); }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 12px; overflow-x: auto; }
.tabs button { background: none; color: var(--muted); border-radius: 0; border-bottom: 2px solid transparent; }
.tabs button[aria-selected=true] { color: var(--text); border-bottom-color: var(--gold); font-weight: 600; }
.feed { display: flex; flex-direction: column; }
.news { border-bottom: 1px solid var(--line); padding: 12px 0; }
.news:last-child { border-bottom: 0; }
.news p { white-space: pre-line; max-width: 70ch; }
.receipt { display: flex; gap: 12px; border-bottom: 1px solid var(--line); padding: 12px 0; }
.receipt img { width: 96px; height: 128px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); background: var(--sunken); }
.receipt.pending { border-inline-start: 3px solid var(--gold); padding-inline-start: 12px; }
.status { font-size: 14px; color: var(--muted); }
.empty { text-align: center; padding: 32px 16px; }
.empty p { color: var(--muted); }

/* KPI tiles */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--card); border: 1px solid var(--line); box-shadow: var(--shadow-l1); border-radius: 12px; padding: 16px; display: flex; flex-direction: column; gap: 4px; color: var(--text); text-decoration: none; }
.stat > span:not(.stat-ico) { font-size: 14px; color: var(--muted); }
.stat b { font-size: 22px; }
.stat-ico { display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: 8px; background: var(--gold-soft); color: var(--gold-deep); margin-bottom: 4px; }
.sheet-section { margin: 8px 0 0; padding-top: 16px; border-top: 1px solid var(--line); }
.hint { border-inline-start: 3px solid var(--gold); }
.settings { max-width: 640px; }
.settings form { display: grid; gap: 12px; margin-top: 8px; }
.settings-status { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--line); }
/* Jalali calendar popup (fixed, opened by any input[data-date]) */
input[data-date] { cursor: pointer; }
#jcal { position: fixed; z-index: 500; width: 324px; max-width: calc(100vw - 16px); padding: 8px; background: var(--overlay); color: var(--text);
  border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow-l3); }
.jc-head { display: flex; align-items: center; gap: 4px; margin-bottom: 4px; }
.jc-head b { flex: 1; text-align: center; font-size: 16px; }
.jc-head button, .jc-grid button { background: none; color: var(--text); padding: 0; min-width: var(--hit); }
.jc-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 0; }
.jc-dn { display: grid; place-items: center; height: 32px; font-size: 12px; color: var(--muted); }
.jc-grid button { min-height: var(--hit); border-radius: 8px; font-size: 14px; }
.jc-grid button.today { box-shadow: inset 0 0 0 1px var(--gold-deep); }
.jc-grid button.sel { background: var(--btn); color: var(--btn-ink); font-weight: 600; }
.jc-foot { display: flex; justify-content: space-between; border-top: 1px solid var(--line); margin-top: 4px; padding-top: 4px; }
@media (hover: hover) { .jc-head button:hover, .jc-grid button:not(.sel):hover { background: var(--sunken); } }
.skeleton div { height: 96px; border-radius: 12px; background: var(--line); margin-bottom: 16px; }

/* ---------- overlays: slide-over sheet + confirm dialog ---------- */
dialog { padding: 0; border: 1px solid var(--line); border-radius: 12px; background: var(--overlay); box-shadow: var(--shadow-l3); color: var(--text); width: min(420px, calc(100vw - 32px)); }
dialog::backdrop { background: var(--scrim); }
dialog form { display: flex; flex-direction: column; max-height: inherit; }
.sheet-head { padding: 20px 20px 0; }
.sheet-body { padding: 16px 20px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; overflow-y: auto; }
.sheet-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px; border-top: 1px solid var(--line); background: var(--sunken); }
dialog.sheet { margin: 0; inset-block: 0; inset-inline-start: auto; inset-inline-end: 0; height: 100dvh; max-height: 100dvh; width: min(440px, 100vw); max-width: 100vw; /* UA default is 100% - 2em */ border-radius: 0; border-block: 0; border-inline-end: 0; }
dialog.sheet form { height: 100%; }
dialog.sheet .sheet-body { flex: 1; align-content: start; }
dialog.sheet[open] { animation: sheet-in var(--dur) var(--ease-out); }
@keyframes sheet-in { from { transform: translateX(-100%); } }

#toast { position: fixed; inset: auto auto 16px 50%; margin: 0; border: 1px solid var(--line); transform: translateX(-50%) translateY(200%); background: var(--text); color: var(--bg); padding: 12px 16px; border-radius: 8px;
  visibility: hidden; transition: transform var(--dur) ease-in, visibility 0s var(--dur); max-width: calc(100% - 32px); z-index: 400; }
#toast.show { transform: translateX(-50%); visibility: visible; transition: transform var(--dur) var(--ease-out), visibility 0s; }
#toast.err { background: var(--neg); color: var(--neg-ink); }

/* ---------- phone ---------- */
@media (max-width: 767.98px) {
  body:not(.auth) { padding-inline-start: 0; padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  .topbar { display: flex; align-items: center; gap: 8px; position: sticky; top: 0; z-index: 10; padding: 8px 16px; padding-top: max(8px, env(safe-area-inset-top));
    background: var(--side); color: #fff; }
  .topbar img { border-radius: 6px; }
  #side { display: none; inset-block-end: calc(64px + env(safe-area-inset-bottom)); width: 100%; }
  body.menu-open #side { display: flex; }
  #tabbar { display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; inset-inline: 0; bottom: 0; z-index: 30; background: var(--card);
    border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
  #tabbar a, #tabbar button { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-height: 64px; padding: 4px 2px;
    font-size: 12px; color: var(--muted); text-decoration: none; background: none; border: 0; border-radius: 0; }
  #tabbar a span, #tabbar button span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
  #tabbar [aria-current], #tabbar [aria-expanded=true] { color: var(--text); font-weight: 600; box-shadow: inset 0 3px 0 var(--gold); }
  main { padding: 16px 12px; }
  #toast { bottom: calc(76px + env(safe-area-inset-bottom)); }
}
@media (prefers-reduced-motion: reduce) {
  #toast { transition: none; }
  dialog.sheet[open] { animation: none; }
}
.invoice { display: block; max-width: 100%; max-height: 70vh; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 12px; }
#print-head { display: none; }

/* ---------- print / "Save as PDF": A4, black on white whatever the screen theme, every matching row ---------- */
@page { size: A4; margin: 12mm; }
@media print {
  :root { --bg: #fff; --card: #fff; --sunken: #f3f3f3; --text: #000; --muted: #444; --line: #bbb; --neg: #a40000; --pos: #05620e; }
  body, body:not(.auth) { background: #fff; color: #000; padding: 0; font-size: 11pt; }
  #side, .topbar, #tabbar, dialog, button, #toast, #jcal, .toolbar, .pager, .range, .chips, .crumbs, .page-head .actions, .row-actions,
  .stat-ico, .no-print, .empty button { display: none !important; }
  #print-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 16px; padding-bottom: 6px; margin-bottom: 10px; border-bottom: 2px solid #000; }
  #print-head b { font-size: 14pt; flex-basis: 100%; }
  main { max-width: none; padding: 0; }
  .card, .stat { border: 1px solid #bbb; box-shadow: none; break-inside: avoid; }
  .stats { grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); }
  .scroll { overflow: visible; }
  table { font-size: 10pt; } th, td { white-space: normal; padding: 4px 6px; }
  thead { display: table-header-group; } tr { break-inside: avoid; }
  a { color: #000; text-decoration: none; }
  tr[data-off-page][hidden] { display: table-row !important; }
  .feed [data-off-page][hidden] { display: flex !important; }
  .invoice { max-height: none; break-inside: avoid; }
}
