:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #eef2f7;
  --text: #13213a;
  --muted: #5f6d84;
  --line: #dde3ec;
  --brand: #0f2a4a;
  --brand-2: #1d5c96;
  --accent: #0b8a6f;
  --danger: #c0392b;
  --warn: #b7791f;
  --ok: #0b8a6f;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(15, 42, 74, .06), 0 4px 14px rgba(15, 42, 74, .06);
  --tabbar-h: 64px;
  font-family: Vazirmatn, "Vazir", "IRANSans", Tahoma, "Segoe UI", system-ui, -apple-system, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1424;
    --surface: #13213a;
    --surface-2: #1a2b48;
    --text: #e8eef7;
    --muted: #9db0cc;
    --line: #26395a;
    --brand: #0a1a30;
    --brand-2: #5aa2e6;
    --accent: #33c4a2;
    --danger: #ef6b5b;
    --warn: #e8b04b;
    --ok: #33c4a2;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.7;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand-2); text-decoration: none; }
bdi { unicode-bidi: isolate; }

/* Header */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--brand);
  color: #fff;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 12px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.topbar h1 { font-size: 17px; margin: 0; font-weight: 700; }
.topbar .who { font-size: 12px; opacity: .8; }
.topbar .brand { display: flex; align-items: center; gap: 10px; }
.topbar .logo {
  width: 32px; height: 32px; border-radius: 9px; background: #fff; color: var(--brand);
  display: grid; place-items: center; font-weight: 800; font-size: 15px;
}
.icon-btn {
  background: rgba(255, 255, 255, .12); border: 0; color: #fff; border-radius: 10px;
  padding: 6px 12px; font-size: 13px; cursor: pointer;
}

/* Layout */
main { padding: 16px; padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px); max-width: 900px; margin: 0 auto; }
.section-title { display: flex; align-items: center; justify-content: space-between; margin: 18px 0 10px; }
.section-title h2 { font-size: 16px; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

/* Cards */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow); margin-bottom: 10px;
}
.card.tap { cursor: pointer; }
.card.tap:active { transform: scale(.995); }
.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
@media (min-width: 640px) { .stats { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; box-shadow: var(--shadow); }
.stat .label { font-size: 12px; color: var(--muted); }
.stat .value { font-size: 22px; font-weight: 800; margin-top: 2px; }
.stat.danger .value { color: var(--danger); }
.stat.warn .value { color: var(--warn); }
.stat.wide { grid-column: 1 / -1; }

.item-head { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.item-title { font-weight: 700; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.amount { font-weight: 800; font-variant-numeric: tabular-nums; }
.amount .rial { display: block; font-weight: 400; font-size: 12px; color: var(--muted); }
.debt { color: var(--danger); }
.credit { color: var(--ok); }

/* Badges */
.badge { display: inline-block; font-size: 12px; padding: 1px 9px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.danger { background: rgba(192, 57, 43, .12); color: var(--danger); }
.badge.warn { background: rgba(183, 121, 31, .14); color: var(--warn); }
.badge.ok { background: rgba(11, 138, 111, .13); color: var(--ok); }
.badge.info { background: rgba(29, 92, 150, .13); color: var(--brand-2); }

/* Forms */
label { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 4px; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=search], select, textarea {
  width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 11px;
  padding: 11px 12px; font-size: 16px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--brand-2); }
textarea { min-height: 84px; resize: vertical; }
.ltr { direction: ltr; text-align: left; }
.date-picker { display: grid; grid-template-columns: 1.2fr 1fr 1fr; gap: 6px; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: transparent; padding: 6px 14px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.seg button.on { background: var(--surface); box-shadow: var(--shadow); font-weight: 700; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; border-radius: 11px; padding: 11px 16px; font-size: 15px; font-weight: 700;
  background: var(--brand-2); color: #fff; cursor: pointer;
}
.btn.block { width: 100%; }
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.ok { background: var(--accent); }
.btn.danger { background: var(--danger); }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: default; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }

/* Tabs (bottom bar) */
.tabbar {
  position: fixed; bottom: 0; right: 0; left: 0; z-index: 10;
  background: var(--surface); border-top: 1px solid var(--line);
  display: flex; justify-content: space-around;
  padding-bottom: env(safe-area-inset-bottom);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-decoration: none; color: var(--muted); font-size: 11px; gap: 2px;
}
.tabbar a .ico { font-size: 20px; line-height: 1; }
.tabbar a.on { color: var(--brand-2); font-weight: 700; }

/* Filters */
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 10px; scrollbar-width: none; }
.chips button { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 5px 13px; font-size: 13px; white-space: nowrap; cursor: pointer; }
.chips button.on { background: var(--brand-2); border-color: var(--brand-2); color: #fff; }

/* Sheet (modal) */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(5, 12, 24, .45); z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  background: var(--bg); width: 100%; max-width: 640px; max-height: 92vh; overflow-y: auto;
  border-radius: 18px 18px 0 0; padding: 16px 16px calc(env(safe-area-inset-bottom) + 20px);
}
.sheet h3 { margin: 0 0 6px; font-size: 17px; }
@media (min-width: 700px) { .sheet-backdrop { align-items: center; } .sheet { border-radius: 18px; } }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: linear-gradient(160deg, var(--brand) 0%, #173f6b 100%); }
.login-card { background: var(--surface); width: 100%; max-width: 380px; border-radius: 18px; padding: 24px 20px; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.login-card .logo-lg { width: 56px; height: 56px; border-radius: 15px; background: var(--brand); color: #fff; display: grid; place-items: center; font-weight: 800; font-size: 24px; margin: 0 auto 8px; }
.login-card h1 { text-align: center; font-size: 20px; margin: 0; }
.login-card p { text-align: center; margin: 2px 0 14px; }

.empty { text-align: center; color: var(--muted); padding: 28px 12px; }
.error-box { background: rgba(192, 57, 43, .1); color: var(--danger); border-radius: 11px; padding: 10px 12px; font-size: 14px; margin-top: 10px; }
.loading { text-align: center; color: var(--muted); padding: 30px; }

#toast {
  position: fixed; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 14px); right: 50%; transform: translateX(50%);
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 30; max-width: calc(100% - 32px);
}
#toast.show { opacity: .95; }

table.list { width: 100%; border-collapse: collapse; font-size: 14px; }
table.list td { padding: 8px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.list tr:last-child td { border-bottom: 0; }

/* ---------- printable documents (always paper colours, in light and dark mode) ---------- */
.doc-page { max-width: 820px; margin: 0 auto; padding: 12px; }
main.doc-page { padding-bottom: 40px; }
.doc { position: relative; background: #fff; color: #1b2433; border-radius: 12px; padding: 22px 20px; box-shadow: 0 2px 16px rgba(0,0,0,.12);
  font-size: 14px; line-height: 1.75; overflow: hidden; border-top: 6px solid #2e8b3e; }
.doc-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; border-bottom: 2px solid #f2b51c; padding-bottom: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.doc-logo { width: 210px; max-width: 55%; height: auto; }
.doc-title { text-align: left; }
.doc-title h2 { margin: 0 0 4px; font-size: 18px; color: #2e8b3e; text-align: right; }
.doc-no { font-weight: 700; }
.doc table { width: 100%; border-collapse: collapse; margin: 10px 0; }
.doc-info th, .doc-info td { border: 1px solid #d9dee6; padding: 6px 8px; text-align: right; vertical-align: top; }
.doc-info th { background: #f4f7f2; color: #44506a; font-weight: 600; white-space: nowrap; width: 1%; }
.doc-items th { background: #2e8b3e; color: #fff; padding: 7px 8px; font-weight: 600; text-align: right; }
.doc-items td { border-bottom: 1px solid #e3e7ee; padding: 7px 8px; vertical-align: top; }
.doc-items td.c { text-align: center; }
.doc-items .n { text-align: left; direction: ltr; white-space: nowrap; font-variant-numeric: tabular-nums; }
.doc-items tfoot td { padding: 6px 8px; border-bottom: 1px dashed #e3e7ee; }
.doc-items tfoot tr.strong td { font-weight: 700; border-top: 2px solid #c9d1dc; }
.doc-items tfoot tr.doc-payable td { background: #fff6dc; font-weight: 800; font-size: 15px; border-bottom: 2px solid #f2b51c; }
.doc-sub { font-size: 12px; color: #6a7489; }
.doc-words { margin: 8px 0; padding: 8px 10px; background: #f4f7f2; border-radius: 8px; }
.doc-notes, .doc-paid { margin: 8px 0; font-size: 13px; }
.doc-bank { margin: 12px 0; border: 1.5px solid #2e8b3e; border-radius: 10px; padding: 10px 12px; }
.doc-bank-title { font-weight: 700; color: #2e8b3e; margin-bottom: 6px; }
.doc-bank-row { display: flex; align-items: center; gap: 8px; padding: 4px 0; border-bottom: 1px dashed #e3e7ee; flex-wrap: wrap; }
.doc-bank-label { color: #44506a; min-width: 6.5em; }
.doc-bank-val { font-weight: 700; letter-spacing: .5px; font-variant-numeric: tabular-nums; flex: 1; word-break: break-all; }
.doc-copy { border: 1px solid #2e8b3e; background: #fff; color: #2e8b3e; border-radius: 8px; padding: 3px 10px; font: inherit; font-size: 12px; cursor: pointer; }
.doc-foot { margin-top: 14px; border-top: 2px solid #f2b51c; padding-top: 10px; text-align: center; font-size: 13px; color: #44506a; }
.doc-slogan { color: #2e8b3e; font-weight: 700; margin-bottom: 4px; }
.doc-contacts { display: flex; justify-content: center; gap: 14px; flex-wrap: wrap; }
.doc-void { position: absolute; top: 40%; left: 50%; transform: translate(-50%, -50%) rotate(-24deg); font-size: 64px; font-weight: 900; color: rgba(192, 57, 43, .18);
  border: 6px solid rgba(192, 57, 43, .18); padding: 0 24px; border-radius: 12px; pointer-events: none; white-space: nowrap; }
.doc-actions { margin-top: 12px; }
@media (max-width: 480px) { .doc { padding: 16px 12px; font-size: 13px; } .doc-title { text-align: right; } .doc-logo { max-width: 70%; } }
@page { size: A4; margin: 10mm; }
@media print {
  body { background: #fff !important; }
  .topbar, .tabbar, .no-print, #toast, .sheet-backdrop { display: none !important; }
  main#view, .doc-page { padding: 0 !important; margin: 0 !important; max-width: none; }
  .doc { box-shadow: none; border-radius: 0; padding: 0; border-top: 0; }
  .doc-items th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .doc-items tfoot tr.doc-payable td, .doc-info th, .doc-words { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
.receipt-text { white-space: pre-wrap; background: var(--surface-2); border-radius: 8px; padding: 6px 8px; margin-top: 6px; color: var(--text); }
.doc-pay { margin-top: 8px; }
.doc-pay-title { font-weight: 700; margin-top: 10px; }
.doc-bank-val { white-space: nowrap; word-break: normal; letter-spacing: 0; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 14px; }
@media (max-width: 480px) {
  .doc-bank-row { display: grid; grid-template-columns: 1fr auto; row-gap: 2px; }
  .doc-bank-label { grid-column: 1 / -1; font-size: 12px; }
  .doc-bank-val { font-size: 13.5px; }
}
