:root {
  --brand: #1565c0;
  --brand-dark: #0d47a1;
  --brand-soft: #e3f0fd;
  --accent: #00a389;
  --accent-soft: #e0f5f1;
  --warn: #e08600;
  --warn-soft: #fff4e0;
  --danger: #d32f2f;
  --danger-soft: #fdecec;
  --bg: #f4f6fa;
  --card: #ffffff;
  --text: #1b2430;
  --muted: #667085;
  --line: #e3e8ef;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 2px 8px rgba(16, 24, 40, .06);
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Noto Sans Arabic", Tahoma, Arial, sans-serif;
  --nav-h: 64px;
  --side-w: 230px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font); font-size: 16px; line-height: 1.45; color: var(--text); background: var(--bg);
  -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; } h2 { font-size: 18px; } h3 { font-size: 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.right { text-align: end; }
.center { text-align: center; }
.hidden { display: none !important; }
.splash { display: grid; place-items: center; min-height: 100vh; color: var(--muted); font-size: 18px; }

/* ---------- layout ---------- */
.shell { min-height: 100vh; }
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; background: var(--brand); color: #fff; min-height: 56px;
}
.topbar h1 { font-size: 18px; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .icon-btn { color: #fff; }
.topbar .icon-btn:hover { background: rgba(255, 255, 255, .15); }
.main { padding: 16px; padding-bottom: calc(var(--nav-h) + 24px + env(safe-area-inset-bottom)); max-width: 1100px; margin: 0 auto; }

.bottomnav {
  position: fixed; bottom: 0; inset-inline: 0; z-index: 30; display: grid; grid-template-columns: repeat(5, 1fr);
  background: #fff; border-top: 1px solid var(--line); height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
}
.bottomnav a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: 12px; font-weight: 600;
}
.bottomnav a svg { width: 24px; height: 24px; }
.bottomnav a.active { color: var(--brand); }
.bottomnav a.sell span.bubble {
  display: grid; place-items: center; width: 46px; height: 46px; border-radius: 50%; background: var(--accent); color: #fff;
  margin-top: -18px; box-shadow: 0 4px 12px rgba(0, 163, 137, .35);
}

.sidebar { display: none; }
@media (min-width: 900px) {
  .shell { display: grid; grid-template-columns: var(--side-w) 1fr; }
  .sidebar {
    display: flex; flex-direction: column; gap: 2px; position: sticky; top: 0; height: 100vh; overflow-y: auto;
    background: #fff; border-inline-end: 1px solid var(--line); padding: 14px 10px;
  }
  .sidebar .brand { display: flex; align-items: center; gap: 10px; padding: 6px 10px 16px; font-weight: 700; font-size: 17px; }
  .sidebar .brand img { width: 34px; height: 34px; }
  .sidebar a {
    display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px; color: var(--text); font-weight: 500;
  }
  .sidebar a svg { width: 20px; height: 20px; color: var(--muted); }
  .sidebar a:hover { background: var(--bg); }
  .sidebar a.active { background: var(--brand-soft); color: var(--brand-dark); }
  .sidebar a.active svg { color: var(--brand); }
  .sidebar .sep { height: 1px; background: var(--line); margin: 8px 6px; }
  .sidebar .grow { flex: 1; }
  .bottomnav { display: none; }
  .main { padding: 24px; padding-bottom: 40px; }
  .topbar { background: var(--bg); color: var(--text); border-bottom: 1px solid var(--line); }
  .topbar .icon-btn { color: var(--text); }
  .topbar .icon-btn:hover { background: #e9edf3; }
  .topbar .only-mobile { display: none; }
}

/* ---------- components ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.card + .card, .card + .section, .section + .card, .section + .section { margin-top: 14px; }
.section-title { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 20px 2px 10px; }
.section-title h2 { font-size: 16px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.row { display: flex; align-items: center; gap: 10px; }
.row.wrap { flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 600px) { .grid2 { grid-template-columns: 1fr 1fr; } }
.grow { flex: 1; min-width: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 48px; padding: 10px 18px;
  border-radius: 12px; border: 1px solid transparent; background: var(--brand); color: #fff; font-weight: 600; cursor: pointer;
  text-align: center; transition: background .15s, transform .05s;
}
.btn:active { transform: scale(.98); }
.btn:hover { background: var(--brand-dark); }
.btn svg { width: 20px; height: 20px; flex: none; }
.btn.green { background: var(--accent); } .btn.green:hover { background: #008a74; }
.btn.light { background: #fff; color: var(--text); border-color: var(--line); } .btn.light:hover { background: var(--bg); }
.btn.ghost { background: transparent; color: var(--brand); } .btn.ghost:hover { background: var(--brand-soft); }
.btn.danger { background: #fff; color: var(--danger); border-color: #f3c4c4; } .btn.danger:hover { background: var(--danger-soft); }
.btn.block { width: 100%; }
.btn.big { min-height: 56px; font-size: 18px; }
.btn.sm { min-height: 38px; padding: 6px 12px; font-size: 14px; border-radius: 10px; }
.btn[disabled] { opacity: .55; pointer-events: none; }
.icon-btn {
  display: inline-grid; place-items: center; width: 42px; height: 42px; border-radius: 50%; border: 0; background: transparent;
  cursor: pointer; color: var(--text); flex: none;
}
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn:hover { background: var(--bg); }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 14px; font-weight: 600; color: #344054; }
.field label .opt { font-weight: 400; color: var(--muted); font-size: 12px; }
.input, select.input, textarea.input {
  width: 100%; min-height: 48px; padding: 10px 14px; border: 1px solid #cfd6e0; border-radius: 12px; background: #fff; outline: none;
  font-size: 16px;
}
textarea.input { min-height: 80px; resize: vertical; }
.input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(21, 101, 192, .15); }
.input.big { font-size: 20px; min-height: 56px; font-weight: 600; }
.searchbox { position: relative; }
.searchbox svg { position: absolute; inset-inline-start: 14px; top: 50%; transform: translateY(-50%); width: 20px; height: 20px; color: var(--muted); }
.searchbox .input { padding-inline-start: 44px; }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; cursor: pointer; }
.check input { width: 22px; height: 22px; accent-color: var(--brand); }

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: #e9edf3; border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { min-height: 44px; border: 0; border-radius: 9px; background: transparent; font-weight: 600; color: var(--muted); cursor: pointer; }
.seg button.on { background: #fff; color: var(--brand-dark); box-shadow: var(--shadow); }
.seg.green button.on { color: var(--accent); }

.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 700; background: #eef2f6; color: #475467; white-space: nowrap; }
.badge.INV, .badge.FINAL { background: var(--brand-soft); color: var(--brand-dark); }
.badge.QTN { background: #f1e8fd; color: #6a1b9a; }
.badge.SO, .badge.OPEN { background: var(--warn-soft); color: #9a5b00; }
.badge.RET { background: #fde7ef; color: #ad1457; }
.badge.CANCELLED { background: var(--danger-soft); color: var(--danger); }
.badge.CONVERTED { background: var(--accent-soft); color: #00796b; }
.badge.CASH { background: var(--accent-soft); color: #00796b; }
.badge.CREDIT { background: var(--warn-soft); color: #9a5b00; }

/* tiles on home */
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
@media (min-width: 700px) { .tiles { grid-template-columns: repeat(4, 1fr); } }
.tile {
  display: flex; flex-direction: column; align-items: flex-start; gap: 10px; padding: 16px; min-height: 110px; border-radius: var(--radius);
  background: #fff; box-shadow: var(--shadow); color: var(--text); font-weight: 700; font-size: 16px; border: 0; cursor: pointer; text-align: start;
}
.tile .ico { display: grid; place-items: center; width: 44px; height: 44px; border-radius: 12px; }
.tile .ico svg { width: 24px; height: 24px; }
.tile.blue .ico { background: var(--brand-soft); color: var(--brand); }
.tile.green .ico { background: var(--accent-soft); color: var(--accent); }
.tile.purple .ico { background: #f1e8fd; color: #7b1fa2; }
.tile.orange .ico { background: var(--warn-soft); color: var(--warn); }
.tile.pink .ico { background: #fde7ef; color: #c2185b; }
.tile.teal .ico { background: #e0f2f1; color: #00897b; }
.tile:hover { box-shadow: 0 4px 16px rgba(16, 24, 40, .1); }
.tile.primary { background: linear-gradient(135deg, var(--accent), #00796b); color: #fff; grid-column: span 2; min-height: 96px; flex-direction: row; align-items: center; font-size: 20px; }
.tile.primary .ico { background: rgba(255, 255, 255, .2); color: #fff; width: 56px; height: 56px; }
@media (min-width: 700px) { .tile.primary { grid-column: span 2; } }

.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
@media (min-width: 700px) { .stats { grid-template-columns: repeat(4, 1fr); } }
.stat { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; }
.stat .label { font-size: 13px; color: var(--muted); font-weight: 600; }
.stat .value { font-size: 22px; font-weight: 700; margin-top: 4px; }
.stat .sub { font-size: 12px; color: var(--muted); }
.stat.hero { background: var(--brand); color: #fff; } .stat.hero .label, .stat.hero .sub { color: rgba(255, 255, 255, .85); }

/* lists */
.list { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.list-item {
  display: flex; align-items: center; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--line); color: var(--text);
  cursor: pointer; min-height: 64px; width: 100%; background: #fff; border-inline: 0; border-top: 0; text-align: start;
}
.list-item:last-child { border-bottom: 0; }
.list-item:hover { background: #f8fafc; }
.list-item .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-item .sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-item .end { text-align: end; flex: none; }
.avatar {
  display: grid; place-items: center; width: 42px; height: 42px; border-radius: 50%; background: var(--brand-soft); color: var(--brand-dark);
  font-weight: 700; flex: none; font-size: 15px;
}
.avatar.walkin { background: var(--accent-soft); color: #00796b; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }
.money-pos { color: var(--danger); font-weight: 700; }
.money-neg { color: var(--accent); font-weight: 700; }

/* tables */
.table-wrap { overflow-x: auto; background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); }
table.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th { text-align: start; font-size: 12px; color: var(--muted); font-weight: 700; padding: 10px 12px; background: #f8fafc; border-bottom: 1px solid var(--line); white-space: nowrap; }
.tbl td { padding: 10px 12px; border-bottom: 1px solid var(--line); }
.tbl tr:last-child td { border-bottom: 0; }
.tbl tfoot td { font-weight: 700; background: #f8fafc; }
.tbl .num { text-align: end; }
.tbl tr.click { cursor: pointer; } .tbl tr.click:hover td { background: #f8fafc; }

/* document editor */
.doc-customer { display: flex; align-items: center; gap: 12px; }
.line-card { border: 1px solid var(--line); border-radius: 12px; padding: 12px; background: #fff; }
.line-card + .line-card { margin-top: 10px; }
.line-card .line-top { display: flex; align-items: flex-start; gap: 8px; }
.line-card .line-name { font-weight: 600; flex: 1; }
.line-card .line-fields { display: grid; grid-template-columns: 1.2fr 1fr 1fr; gap: 8px; margin-top: 10px; }
.line-card .line-fields .field label { font-size: 12px; }
.line-card .line-fields .input { min-height: 44px; padding: 8px 10px; }
.line-card .line-total { margin-top: 8px; display: flex; justify-content: space-between; font-size: 14px; color: var(--muted); }
.line-card .line-total b { color: var(--text); font-size: 16px; }
.stepper { display: flex; align-items: stretch; border: 1px solid #cfd6e0; border-radius: 12px; overflow: hidden; background: #fff; min-height: 44px; }
.stepper button { width: 40px; border: 0; background: #f1f4f8; font-size: 20px; font-weight: 700; cursor: pointer; color: var(--brand-dark); }
.stepper input { width: 100%; min-width: 0; border: 0; text-align: center; font-weight: 700; font-size: 16px; outline: none; }
.totals { display: grid; gap: 6px; }
.totals .t-row { display: flex; justify-content: space-between; font-size: 15px; }
.totals .t-row.grand { font-size: 22px; font-weight: 800; color: var(--brand-dark); border-top: 1px dashed var(--line); padding-top: 8px; margin-top: 4px; }
.savebar {
  position: sticky; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); z-index: 10; margin: 16px -16px -16px; padding: 12px 16px;
  background: rgba(255, 255, 255, .96); border-top: 1px solid var(--line); display: flex; gap: 10px; align-items: center;
  backdrop-filter: blur(6px);
}
.savebar .sum { flex: 1; }
.savebar .sum .lbl { font-size: 12px; color: var(--muted); }
.savebar .sum .val { font-size: 20px; font-weight: 800; }
@media (min-width: 900px) { .savebar { bottom: 0; margin: 16px -24px -40px; padding: 14px 24px; } }

details.more > summary { cursor: pointer; color: var(--brand); font-weight: 600; padding: 8px 0; list-style: none; }
details.more > summary::-webkit-details-marker { display: none; }
details.more > summary::before { content: '+ '; }
details.more[open] > summary::before { content: '− '; }

/* modal */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 100; background: rgba(15, 23, 42, .45); display: flex; align-items: flex-end; justify-content: center;
  animation: fade .15s;
}
.modal {
  background: #fff; width: 100%; max-width: 640px; max-height: 92vh; border-radius: 18px 18px 0 0; display: flex; flex-direction: column;
  animation: up .2s ease-out; padding-bottom: env(safe-area-inset-bottom);
}
@media (min-width: 700px) { .modal-backdrop { align-items: center; } .modal { border-radius: 18px; max-height: 86vh; } }
.modal-head { display: flex; align-items: center; gap: 8px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.modal-head h2 { flex: 1; }
.modal-body { padding: 16px; overflow-y: auto; flex: 1; }
.modal-foot { padding: 12px 16px; border-top: 1px solid var(--line); display: flex; gap: 10px; }
.modal-foot .btn { flex: 1; }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(30px); opacity: .5; } }

/* toast */
#toast {
  position: fixed; z-index: 200; left: 50%; transform: translateX(-50%) translateY(-20px); top: calc(12px + env(safe-area-inset-top));
  background: #1b2430; color: #fff; padding: 12px 18px; border-radius: 12px; max-width: min(92vw, 480px); font-weight: 600;
  opacity: 0; pointer-events: none; transition: all .2s; box-shadow: 0 6px 20px rgba(0, 0, 0, .2);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { background: var(--danger); }
#toast.ok { background: var(--accent); }

/* login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: linear-gradient(160deg, var(--brand) 0%, #0b3d91 55%, var(--bg) 55.1%); }
.login-card { width: 100%; max-width: 400px; background: #fff; border-radius: 20px; padding: 28px 22px; box-shadow: 0 10px 40px rgba(0, 0, 0, .15); }
.login-card .logo { display: flex; justify-content: center; margin-bottom: 12px; }
.login-card .logo img { max-height: 64px; max-width: 200px; }
.login-card h1 { text-align: center; margin-bottom: 4px; }
.login-card p { text-align: center; margin: 0 0 20px; }
.lang-switch { position: fixed; top: 14px; inset-inline-end: 14px; background: rgba(255, 255, 255, .2); color: #fff; border: 0; border-radius: 999px; padding: 8px 14px; font-weight: 600; cursor: pointer; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 15px; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; font-weight: 600; word-break: break-word; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; }
.actions .btn { flex: 1 1 140px; }
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; margin-bottom: 14px; }
.filters .field { flex: 1 1 140px; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 12px; }
.chip { flex: none; padding: 8px 16px; border-radius: 999px; border: 1px solid var(--line); background: #fff; font-weight: 600; cursor: pointer; font-size: 14px; color: var(--muted); }
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.balance-box { text-align: center; padding: 18px; border-radius: var(--radius); }
.balance-box .val { font-size: 30px; font-weight: 800; }
.balance-box.owes { background: var(--danger-soft); color: var(--danger); }
.balance-box.clear { background: var(--accent-soft); color: #00796b; }
.logo-preview { max-height: 80px; max-width: 220px; border: 1px dashed var(--line); border-radius: 10px; padding: 6px; background: #fff; }

/* ---------- print (A4 invoice / receipt) ---------- */
#print-root { display: none; }
.doc-paper { background: #fff; color: #111; font-size: 12px; width: 100%; }
.doc-paper .hdr { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px; border-bottom: 3px solid var(--brand); padding-bottom: 10px; }
.doc-paper .hdr .en { text-align: left; } .doc-paper .hdr .ar { text-align: right; direction: rtl; }
.doc-paper .hdr .cname { font-size: 18px; font-weight: 800; color: var(--brand-dark); }
.doc-paper .hdr img { max-height: 70px; max-width: 160px; }
.doc-paper .title { text-align: center; margin: 12px 0; }
.doc-paper .title h2 { font-size: 18px; letter-spacing: .02em; }
.doc-paper .title .ar { direction: rtl; font-size: 16px; font-weight: 700; }
.doc-paper .boxes { display: grid; grid-template-columns: 1.3fr 1fr; gap: 10px; margin-bottom: 10px; }
.doc-paper .box { border: 1px solid #9aa7b8; border-radius: 8px; padding: 8px 10px; }
.doc-paper .box .r { display: grid; grid-template-columns: 1fr 1.6fr 1fr; gap: 6px; padding: 3px 0; border-bottom: 1px dotted #d0d7e2; }
.doc-paper .box .r:last-child { border-bottom: 0; }
.doc-paper .box .r .l { font-weight: 700; } .doc-paper .box .r .a { direction: rtl; text-align: right; font-weight: 700; }
.doc-paper table.lines { width: 100%; border-collapse: collapse; }
.doc-paper table.lines th { background: #e8eef7; border: 1px solid #9aa7b8; padding: 5px; font-size: 11px; }
.doc-paper table.lines th .a { display: block; direction: rtl; font-weight: 700; }
.doc-paper table.lines td { border-inline: 1px solid #9aa7b8; padding: 5px; vertical-align: top; }
.doc-paper table.lines tbody tr:last-child td { border-bottom: 1px solid #9aa7b8; }
.doc-paper table.lines .n { text-align: right; white-space: nowrap; }
.doc-paper .foot { display: grid; grid-template-columns: 1.3fr 1fr; gap: 10px; margin-top: 10px; }
.doc-paper .sumbox .r { display: flex; justify-content: space-between; padding: 4px 8px; border-bottom: 1px solid #d0d7e2; }
.doc-paper .sumbox .r.g { background: #e8eef7; font-weight: 800; font-size: 14px; }
.doc-paper .sumbox .r .a { direction: rtl; }
.doc-paper .qr img { width: 120px; height: 120px; }
.doc-paper .sign { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-top: 26px; }
.doc-paper .sign div { border-top: 1px solid #555; padding-top: 4px; text-align: center; }
.doc-paper .note { margin-top: 8px; font-size: 11px; color: #333; white-space: pre-wrap; }
.doc-paper .cancelled-stamp { color: var(--danger); border: 3px solid var(--danger); display: inline-block; padding: 4px 16px; font-weight: 800; font-size: 20px; transform: rotate(-8deg); }
.paper-preview { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; overflow-x: auto; }
.paper-preview .doc-paper { min-width: 640px; }
@media (max-width: 700px) {
  .paper-preview { padding: 8px; }
  .paper-preview .doc-paper { zoom: .5; }
}

@media print {
  @page { size: A4; margin: 10mm; }
  body { background: #fff; }
  body.printing #app, body.printing #modal-root, body.printing #toast { display: none !important; }
  body.printing #print-root { display: block; }
  .doc-paper { font-size: 11px; }
  .doc-paper table.lines th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .doc-paper .sumbox .r.g, .doc-paper .hdr { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
