/* Layout: dark right-hand sidebar (RTL), light work area with cards. Teal accent, red/green/amber for money state. */
:root {
  --bg: #eceff3;
  --surface: #ffffff;
  --surface-2: #f4f6f9;
  --ink: #14202b;
  --muted: #5a6b7b;
  --line: #d8dfe6;
  --accent: #14506b;
  --accent-ink: #ffffff;
  --accent-soft: #dcebf2;
  --owed: #b23a24;
  --owed-soft: #f8e3dd;
  --paid: #1b7448;
  --paid-soft: #dcf0e5;
  --part: #946200;
  --part-soft: #f6ead0;
  --side: #14202b;
  --side-ink: #cbd6e0;
  --side-active: #1d3a4c;
  --shadow: 0 1px 2px rgba(20, 32, 43, .06);
  --font-display: "Noto Kufi Arabic", "Segoe UI", Tahoma, sans-serif;
  --font-body: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1318; --surface: #151e27; --surface-2: #1b2631; --ink: #e7edf2; --muted: #93a4b3; --line: #2a3947;
    --accent: #6db8da; --accent-ink: #0a1a23; --accent-soft: #1a3341; --owed: #ff8f78; --owed-soft: #3a1f1a;
    --paid: #6fd6a0; --paid-soft: #173226; --part: #f0c36a; --part-soft: #3a2e14; --side: #0a1015; --side-active: #17303f;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0d1318; --surface: #151e27; --surface-2: #1b2631; --ink: #e7edf2; --muted: #93a4b3; --line: #2a3947;
  --accent: #6db8da; --accent-ink: #0a1a23; --accent-soft: #1a3341; --owed: #ff8f78; --owed-soft: #3a1f1a;
  --paid: #6fd6a0; --paid-soft: #173226; --part: #f0c36a; --part-soft: #3a2e14; --side: #0a1015; --side-active: #17303f;
  --shadow: none; color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font-family: var(--font-body); font-size: 15px; line-height: 1.6; text-align: start; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
h1, h2, h3 { font-family: var(--font-display); margin: 0; line-height: 1.4; text-wrap: balance; }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; display: inline-block; }
.muted, .sub { color: var(--muted); font-size: 13px; }
.boot { padding: 60px 20px; text-align: center; color: var(--muted); }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 250px minmax(0, 1fr); min-height: 100vh; }
.side { background: var(--side); color: var(--side-ink); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { padding: 18px 18px 14px; border-bottom: 1px solid rgba(255, 255, 255, .08); display: flex; gap: 12px; align-items: center; }
.brand img { max-height: 36px; max-width: 60px; object-fit: contain; background: #fff; border-radius: 4px; padding: 2px; }
.brand b { font-family: var(--font-display); color: #fff; font-size: 16px; line-height: 1.35; overflow-wrap: anywhere; }
.brand small { display: block; color: var(--side-ink); font-size: 12px; opacity: .8; }
.nav { padding: 10px 10px 20px; display: flex; flex-direction: column; gap: 2px; }
.nav h4 { margin: 14px 10px 4px; font-size: 12px; font-weight: 500; letter-spacing: .04em; color: #7f93a3; font-family: var(--font-body); }
.nav a { display: block; padding: 8px 12px; border-radius: 6px; color: var(--side-ink); text-decoration: none; }
.nav a:hover { background: rgba(255, 255, 255, .06); color: #fff; }
.nav a[aria-current="page"] { background: var(--side-active); color: #fff; font-weight: 600; }
.main { min-width: 0; display: flex; flex-direction: column; }
.top { position: sticky; top: 0; z-index: 20; background: var(--surface); border-bottom: 1px solid var(--line); padding: 10px 20px; display: flex; align-items: center; gap: 12px; }
.top h1 { font-size: 18px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.burger { display: none; background: none; border: 1px solid var(--line); border-radius: 6px; padding: 4px 10px; font-size: 18px; line-height: 1.2; }
.umenu { position: relative; }
.umenu > button { background: var(--surface-2); border: 1px solid var(--line); border-radius: 99px; padding: 5px 14px; display: flex; gap: 8px; align-items: center; }
.umenu .pop { position: absolute; left: 0; top: calc(100% + 6px); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; min-width: 200px; box-shadow: 0 8px 24px rgba(0, 0, 0, .18); padding: 6px; z-index: 30; }
.umenu .pop button { display: block; width: 100%; text-align: start; background: none; border: 0; border-radius: 6px; padding: 8px 10px; }
.umenu .pop button:hover { background: var(--surface-2); }
.umenu .pop .who { padding: 8px 10px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.view { padding: 20px; display: flex; flex-direction: column; gap: 16px; max-width: 1400px; width: 100%; margin: 0 auto; }
.scrim-nav { display: none; }

/* ---------- surfaces ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); }
.card-b { padding: 16px 18px; }
.card-h { padding: 12px 18px; border-bottom: 1px solid var(--line); display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.card-h h2 { font-size: 16px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.sp { justify-content: space-between; }
.grow { flex: 1 1 auto; min-width: 0; }
.grid { display: grid; gap: 12px; }
.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)); }
.span2 { grid-column: 1 / -1; }

/* ---------- controls ---------- */
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > label { font-size: 13px; color: var(--muted); font-weight: 500; }
.field .hint { font-size: 12px; color: var(--muted); }
input[type=text], input[type=search], input[type=number], input[type=date], input[type=password], input[type=tel], input[type=month], select, textarea {
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); background: var(--surface); }
input.ltr, .ltr { direction: ltr; text-align: start; }
html.ltr input.ltr, html.ltr .ltr { text-align: left; }
textarea { resize: vertical; }
input[type=checkbox] { width: 17px; height: 17px; accent-color: var(--accent); }
.check { display: flex; gap: 8px; align-items: center; }
.btn { border: 1px solid var(--line); background: var(--surface); border-radius: 6px; padding: 8px 14px; font-weight: 500; white-space: nowrap; text-decoration: none; color: inherit; display: inline-block; text-align: center; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--owed); border-color: var(--owed); }
.btn.sm { padding: 3px 10px; font-size: 13px; }
.btn.lg { padding: 12px 20px; font-size: 16px; }
.btn[disabled] { opacity: .55; cursor: not-allowed; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 4px 13px; font-size: 13px; }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.pill { display: inline-block; font-size: 12px; font-weight: 600; padding: 1px 9px; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.pill.owed { background: var(--owed-soft); color: var(--owed); }
.pill.paid { background: var(--paid-soft); color: var(--paid); }
.pill.part { background: var(--part-soft); color: var(--part); }
.pill.info { background: var(--accent-soft); color: var(--accent); }
.t-owed { color: var(--owed); } .t-paid { color: var(--paid); } .t-part { color: var(--part); }
.err { color: var(--owed); font-size: 13px; margin: 0; min-height: 1.2em; }
.banner { background: var(--accent-soft); border: 1px solid var(--accent); border-radius: 8px; padding: 10px 16px; display: flex; gap: 12px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.banner.warn { background: var(--part-soft); border-color: var(--part); }
.empty { padding: 44px 20px; text-align: center; color: var(--muted); }
.empty strong { display: block; font-family: var(--font-display); color: var(--ink); font-size: 17px; margin-bottom: 4px; }

/* ---------- tables ---------- */
.tbl-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; }
.tbl th { background: var(--surface-2); color: var(--muted); font-weight: 600; font-size: 13px; text-align: start; padding: 9px 14px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.tbl td { padding: 9px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl tbody tr.click { cursor: pointer; }
.tbl tbody tr.click:hover td { background: var(--surface-2); }
.tbl .n { text-align: end; white-space: nowrap; }
.tbl th.n { text-align: end; }
.tbl tfoot td { font-weight: 700; background: var(--surface-2); }
.tbl .acts { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.tbl tr.void td { opacity: .55; text-decoration: line-through; }
.pager { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 18px; border-top: 1px solid var(--line); background: var(--surface-2); border-radius: 0 0 8px 8px; }

/* ---------- stat tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 14px 16px; min-width: 0; }
.tile .lab { font-size: 13px; color: var(--muted); }
.tile .val { font-family: var(--font-display); font-weight: 700; font-size: 21px; overflow-wrap: anywhere; }
.tile .val small { font-family: var(--font-body); font-weight: 400; font-size: 12px; color: var(--muted); margin-inline-start: 4px; }
.tile .sub { margin-top: 2px; }

/* ---------- charts / bars ---------- */
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.legend i { display: inline-block; width: 11px; height: 11px; border-radius: 2px; margin-inline-end: 6px; vertical-align: -1px; }
.scrollx { overflow-x: auto; }
.chart { display: block; width: 100%; min-width: 520px; height: auto; direction: ltr; }
.chart text { fill: var(--muted); font-family: var(--font-body); font-size: 11px; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.bars { display: flex; flex-direction: column; gap: 10px; }
.brow { display: grid; grid-template-columns: minmax(90px, 1.2fr) 2fr minmax(80px, 1fr); gap: 10px; align-items: center; }
.btrack { background: var(--surface-2); border-radius: 3px; height: 14px; overflow: hidden; }
.bfill { height: 100%; background: var(--accent); border-radius: 3px; }
.bfill.hot { background: var(--owed); }
.bv { text-align: end; font-weight: 600; }

/* ---------- modal / toast / combo ---------- */
.scrim { position: fixed; inset: 0; background: rgba(8, 14, 20, .55); display: flex; align-items: flex-start; justify-content: center; padding: 20px 14px; overflow-y: auto; z-index: 100; }
.modal { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; width: 100%; max-width: 560px; margin-block: auto; display: flex; flex-direction: column; }
.modal.wide { max-width: 860px; }
.modal.xl { max-width: 1060px; }
.modal-h { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.modal-h h2 { font-size: 17px; }
.modal-b { padding: 16px 18px; display: flex; flex-direction: column; gap: 14px; }
.modal-f { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; gap: 8px; flex-wrap: wrap; background: var(--surface-2); border-radius: 0 0 10px 10px; }
.x { background: none; border: 0; font-size: 22px; line-height: 1; color: var(--muted); padding: 2px 8px; }
#toasts { position: fixed; bottom: calc(18px + env(safe-area-inset-bottom, 0px)); left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 300; align-items: center; pointer-events: none; }
.toast { background: var(--ink); color: var(--bg); padding: 8px 18px; border-radius: 99px; font-size: 14px; box-shadow: 0 4px 16px rgba(0, 0, 0, .25); }
.toast.bad { background: var(--owed); color: #fff; }
.combo { position: relative; }
.combo ul { list-style: none; margin: 4px 0 0; padding: 4px; position: absolute; inset-inline: 0; top: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(0, 0, 0, .2); max-height: 280px; overflow-y: auto; z-index: 60; }
.combo li { padding: 7px 10px; border-radius: 6px; cursor: pointer; display: flex; justify-content: space-between; gap: 10px; }
.combo li.on, .combo li:hover { background: var(--accent-soft); }
.combo li .s { color: var(--muted); font-size: 12px; white-space: nowrap; }
.combo li.none { color: var(--muted); cursor: default; }
.combo li.none:hover { background: none; }

/* ---------- login ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth .card { width: 100%; max-width: 440px; }
.auth h1 { font-size: 22px; }

/* ---------- invoice form ---------- */
.inv-lines input { padding: 6px 8px; }
.inv-lines td { padding: 6px 8px; }
.inv-lines .qty { width: 90px; }
.inv-lines .prc { width: 120px; }
.inv-lines .dsc { width: 100px; }
.sumbox { display: flex; flex-direction: column; gap: 8px; }
.sumbox .l { display: flex; justify-content: space-between; gap: 10px; align-items: center; }
.sumbox .l.total { font-family: var(--font-display); font-weight: 700; font-size: 19px; border-top: 1px solid var(--line); padding-top: 8px; }
.sumbox .l input, .sumbox .l select { width: 120px; }
.inv-grid { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 16px; align-items: start; }

/* ---------- POS ---------- */
.pos { display: grid; grid-template-columns: minmax(0, 1fr) 390px; gap: 14px; align-items: start; }
.pos-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; padding: 14px; }
.pcard { background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; text-align: start; display: flex; flex-direction: column; gap: 4px; min-height: 92px; }
.pcard:hover { border-color: var(--accent); background: var(--accent-soft); }
.pcard .nm { font-weight: 600; overflow-wrap: anywhere; line-height: 1.35; }
.pcard .pr { color: var(--accent); font-weight: 700; margin-top: auto; }
.pcard.out { opacity: .5; }
.cart { position: sticky; top: 70px; }
.cart-lines { max-height: 340px; overflow-y: auto; }
.cline { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 10px; padding: 9px 16px; border-bottom: 1px solid var(--line); }
.cline .nm { font-weight: 500; overflow-wrap: anywhere; }
.qty-ctl { display: inline-flex; align-items: center; gap: 4px; }
.qty-ctl button { width: 28px; height: 28px; border-radius: 6px; border: 1px solid var(--line); background: var(--surface-2); padding: 0; line-height: 1; }
.qty-ctl input { width: 58px; text-align: center; padding: 4px; }

/* ---------- documents (screen preview + print) ---------- */
.doc { background: #fff; color: #14202b; font-family: var(--font-body); text-align: start; line-height: 1.7; font-size: 14px; padding: 26px; }
.doc .n { direction: ltr; unicode-bidi: isolate; display: inline-block; font-variant-numeric: tabular-nums; }
.doc .top { position: static; background: none; border: 0; border-bottom: 3px solid #14506b; padding: 0 0 12px; display: flex; justify-content: space-between; gap: 18px; flex-wrap: wrap; }
.doc .who { display: flex; gap: 12px; align-items: center; }
.doc .lg { max-height: 64px; max-width: 140px; object-fit: contain; }
.doc .co { font-family: var(--font-display); font-weight: 700; font-size: 20px; line-height: 1.4; }
.doc .mu { color: #5a6b7b; font-size: 13px; }
.doc .ttl { font-family: var(--font-display); font-weight: 700; font-size: 19px; color: #14506b; }
.doc .box { margin: 14px 0; padding: 10px 14px; border: 1px solid #cfd6dd; border-radius: 4px; }
.doc table { width: 100%; border-collapse: collapse; margin: 10px 0; }
.doc th { background: #eef2f5; font-weight: 600; font-size: 13px; }
.doc th, .doc td { border: 1px solid #cfd6dd; padding: 6px 9px; text-align: start; }
.doc td.a, .doc th.a { text-align: end; }
.doc .tot td { font-weight: 700; }
.doc .rem td { font-weight: 700; font-size: 15px; background: #f7ece9; }
.doc .sign { display: flex; justify-content: space-between; gap: 30px; margin-top: 44px; }
.doc .sign div { flex: 1; border-top: 1px solid #14202b; padding-top: 6px; text-align: center; font-size: 13px; }
.doc .foot { margin-top: 20px; text-align: center; color: #5a6b7b; font-size: 13px; }
.doc.r80 { width: 76mm; padding: 2mm; font-size: 12px; line-height: 1.5; margin: 0 auto; }
.doc.r80 .top { display: block; text-align: center; border-bottom: 1px dashed #000; padding-bottom: 6px; }
.doc.r80 .who { justify-content: center; }
.doc.r80 .co { font-size: 15px; }
.doc.r80 table { margin: 6px 0; }
.doc.r80 th, .doc.r80 td { border: 0; border-bottom: 1px dashed #999; padding: 2px 3px; font-size: 12px; }
.doc.r80 .sign { display: none; }
.preview { background: #dfe5ea; padding: 14px; border-radius: 8px; overflow: auto; max-height: 62vh; }
.preview .doc { max-width: 780px; margin: 0 auto; border: 1px solid #cfd6dd; }
.preview .doc.r80 { max-width: 80mm; }

#print-root { display: none; }
@media print {
  body.printing > *:not(#print-root) { display: none !important; }
  body.printing { background: #fff !important; }
  body.printing #print-root { display: block !important; }
  #print-root .doc { padding: 0; border: 0; }
  #print-root .doc.r80 { padding: 0; }
  #print-root .doc table { page-break-inside: auto; }
  #print-root .doc tr { page-break-inside: avoid; }
}

/* ---------- responsive ---------- */
@media (max-width: 1000px) {
  .pos { grid-template-columns: 1fr; }
  .cart { position: static; }
  .inv-grid { grid-template-columns: 1fr; }
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; inset-block: 0; inset-inline-start: 0; width: 270px; transform: translateX(100%); transition: transform .2s; z-index: 80; }
  body.nav-open .side { transform: none; }
  body.nav-open .scrim-nav { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .5); z-index: 70; }
  .burger { display: block; }
  .view { padding: 14px; }
  .top { padding: 8px 14px; }
  .g2, .g3 { grid-template-columns: 1fr; }
}
@media (max-width: 520px) {
  .g4 { grid-template-columns: 1fr; }
  .brow { grid-template-columns: 1fr 1fr; }
  .brow .btrack { grid-column: 1 / -1; order: 3; }
  .umenu .uname { display: none; }
}
@media (prefers-reduced-motion: reduce) { .side { transition: none; } }

/* i18n / LTR support */
.langsel { border: 1px solid var(--line); background: var(--surface); color: inherit; border-radius: 6px; padding: 6px 8px; }
.langrow { padding: 4px 6px 8px; }
.langrow .langsel { width: 100%; }
html.ltr .side { }
@media (max-width: 860px) { html.ltr .side { transform: translateX(-100%); } html.ltr body.nav-open .side { transform: none; } }
html.ltr .umenu .pop { left: auto; right: 0; }
.umenu .pop .langsel { width: 100%; }
.brandmark { direction: ltr; text-align: center; font-weight: 700; font-size: 34px; letter-spacing: .5px; color: var(--accent, #14506b); line-height: 1; margin-bottom: -4px; }
.brandmark { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.brandmark img { display: block; border-radius: 16px; }
.brand img[src$="logo.svg"] { background: none; padding: 0; border-radius: 9px; max-height: 36px; width: 36px; }
.devfoot { text-align: center; font-size: 12.5px; color: var(--muted); padding: 18px 12px 22px; margin-top: auto; }
.devfoot a { color: var(--accent); text-decoration: none; font-weight: 600; }
.devfoot a:hover { text-decoration: underline; }
.auth .devfoot { margin-top: 14px; padding: 6px; }
@media print { .devfoot { display: none; } }
