:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --text: #1c2430;
  --muted: #6b7684;
  --line: #e3e7ec;
  --line-2: #eef1f4;
  --brand: #0f766e;
  --brand-dark: #0b5d57;
  --brand-soft: #e6f4f2;
  --side: #10302d;
  --side-text: #c9dcd9;
  --danger: #c53030;
  --danger-soft: #fdecec;
  --warn: #b45309;
  --warn-soft: #fff4e0;
  --ok: #15803d;
  --ok-soft: #e8f6ec;
  --info: #1d4ed8;
  --info-soft: #e8efff;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  font-family: "Segoe UI", Tahoma, "Noto Sans Arabic", Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); }
body { min-height: 100vh; }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 18px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 0 0 12px; }
.muted { color: var(--muted); }
small { font-size: 12px; }
bdi { unicode-bidi: isolate; }
.grow { flex: 1; }
.hidden { display: none !important; }
.pos { color: var(--ok); }
.neg { color: var(--danger); }
kbd { font: 11px/1 inherit; padding: 2px 5px; border-radius: 4px; background: rgba(255,255,255,.2); margin-inline-start: 6px; }

/* ---------- layout ---------- */
#app { display: flex; min-height: 100vh; }
.sidebar {
  width: 236px; flex: none; background: var(--side); color: var(--side-text);
  position: sticky; top: 0; height: 100vh; overflow-y: auto; display: flex; flex-direction: column;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 16px; color: #fff; }
.brand b { display: block; font-size: 15px; }
.brand small { color: var(--side-text); }
.logo {
  width: 38px; height: 38px; border-radius: 9px; background: var(--brand); color: #fff; display: grid;
  place-items: center; font-weight: 700; font-size: 19px; flex: none;
}
.logo.big { width: 56px; height: 56px; font-size: 28px; border-radius: 14px; margin: 0 auto 12px; }
nav { padding: 4px 10px 20px; }
nav .group { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: #7fa39e; padding: 16px 10px 6px; }
nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px; color: var(--side-text);
  text-decoration: none; margin-bottom: 2px;
}
nav a:hover { background: rgba(255,255,255,.06); color: #fff; text-decoration: none; }
nav a.active { background: var(--brand); color: #fff; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 10px 24px; background: var(--panel);
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20;
}
.topbar .quick { display: flex; gap: 8px; flex-wrap: wrap; }
#view { padding: 22px 24px 60px; max-width: 1400px; width: 100%; }
.banner { padding: 10px 24px; font-size: 13px; }
.banner.warn { background: var(--warn-soft); color: var(--warn); border-bottom: 1px solid #f5d9a8; }
.banner.danger { background: var(--danger); color: #fff; font-weight: 600; }
.only-mobile { display: none; }
.user-menu { position: relative; }
.dropdown {
  position: absolute; inset-inline-end: 0; top: calc(100% + 4px); background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.12); min-width: 190px; padding: 4px; z-index: 50;
}
.dropdown button { display: block; width: 100%; text-align: start; background: none; border: 0; padding: 9px 10px; border-radius: 6px; cursor: pointer; font: inherit; }
.dropdown button:hover { background: var(--bg); }

/* ---------- controls ---------- */
.ic { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[dir="rtl"] .flip { transform: scaleX(-1); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 36px; padding: 0 14px;
  border-radius: 8px; border: 1px solid var(--line); background: var(--panel); color: var(--text); font: inherit;
  font-weight: 500; cursor: pointer; white-space: nowrap; text-decoration: none !important;
}
.btn:hover { background: #f8fafb; border-color: #cfd6de; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--danger); border-color: #f1c4c4; }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--bg); }
.btn.sm { height: 28px; padding: 0 9px; font-size: 13px; }
.btn.block { width: 100%; margin-top: 8px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.icon-btn { display: inline-grid; place-items: center; width: 32px; height: 32px; border: 0; border-radius: 7px; background: transparent; cursor: pointer; color: var(--muted); }
.icon-btn:hover { background: var(--bg); color: var(--text); }
input, select, textarea {
  font: inherit; color: inherit; height: 36px; padding: 0 10px; border: 1px solid #d5dbe2; border-radius: 8px;
  background: #fff; width: 100%; min-width: 0;
}
textarea { height: auto; min-height: 70px; padding: 8px 10px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(15,118,110,.25); border-color: var(--brand); }
input[type=checkbox], input[type=radio] { width: 16px; height: 16px; flex: none; accent-color: var(--brand); }
input[type=number] { text-align: end; }
input.big { height: 44px; font-size: 16px; }
input[readonly] { background: var(--bg); }
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > span { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.field .hint { font-size: 12px; color: var(--muted); }
.chk { display: inline-flex; align-items: center; gap: 7px; cursor: pointer; white-space: nowrap; }
.row { display: flex; gap: 8px; align-items: center; }
.grid { display: grid; gap: 14px; }
.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: span 2; }
.span-all { grid-column: 1 / -1; }

/* ---------- page parts ---------- */
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin-bottom: 16px; }
.panel.flush { padding: 0; overflow: hidden; }
.panel h3 { display: flex; align-items: center; gap: 8px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar input[type=search] { max-width: 320px; }
.toolbar input[type=date], .toolbar select { width: auto; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; }
.tabs a { padding: 10px 14px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; text-decoration: none; }
.tabs a.active { color: var(--brand); border-bottom-color: var(--brand); font-weight: 600; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chips a { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel); color: var(--text); text-decoration: none; font-size: 13px; }
.chips a.active { background: var(--brand); border-color: var(--brand); color: #fff; }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }
.empty.err { color: var(--danger); }
.loading { padding: 40px; text-align: center; color: var(--muted); }

/* ---------- tables ---------- */
.tbl-wrap { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; }
.tbl th, .tbl td { padding: 9px 12px; text-align: start; border-bottom: 1px solid var(--line-2); vertical-align: middle; }
.tbl th { font-size: 12px; font-weight: 600; color: var(--muted); background: #f9fafb; white-space: nowrap; }
.tbl td.num, .tbl th.num { text-align: end; white-space: nowrap; }
.tbl tbody tr[data-href] { cursor: pointer; }
.tbl tbody tr[data-href]:hover td { background: #f6faf9; }
.tbl tfoot td { font-weight: 700; background: #f9fafb; border-top: 2px solid var(--line); }
.tbl tr.void td { color: var(--muted); text-decoration: line-through; }
.tbl tr.void td.keep { text-decoration: none; }
.tbl td .sub { display: block; color: var(--muted); font-size: 12px; }
.tbl input { height: 32px; }
.tbl .w-qty { width: 100px; }
.tbl .w-price { width: 120px; }
.tbl tr.group td { background: var(--bg); font-weight: 600; }
.tbl tr.total td { font-weight: 700; border-top: 2px solid var(--line); }
.tbl tr.grand td { font-weight: 700; font-size: 15px; background: var(--brand-soft); }
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--bg); color: var(--muted); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.info { background: var(--info-soft); color: var(--info); }
.badge.brand { background: var(--brand-soft); color: var(--brand); }

/* ---------- dashboard ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat {
  display: flex; flex-direction: column; gap: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); color: var(--text); text-decoration: none !important; border-top: 3px solid var(--line);
}
.stat span { color: var(--muted); font-size: 12.5px; }
.stat b { font-size: 20px; }
.stat .cur { font-size: 12px; color: var(--muted); font-weight: 500; }
a.stat[href]:hover { border-color: #cfd6de; }
.stat.c1 { border-top-color: var(--brand); }
.stat.c2 { border-top-color: #0891b2; }
.stat.c3 { border-top-color: #7c3aed; }
.stat.c4 { border-top-color: var(--ok); }
.stat.warn { border-top-color: var(--warn); }
.stat.warn b { color: var(--warn); }
.stat.neg { border-top-color: var(--danger); }
.stat.neg b { color: var(--danger); }
.dash-grid { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 16px; }
.bars { display: flex; align-items: flex-end; gap: 6px; height: 170px; padding-top: 10px; }
.bar { flex: 1; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 4px; }
.bar i { display: block; width: 100%; max-width: 34px; background: var(--brand); border-radius: 4px 4px 0 0; min-height: 2px; opacity: .85; }
.bar:hover i { opacity: 1; }
.bar small { color: var(--muted); font-size: 11px; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--line-2); }
.list li:last-child { border-bottom: 0; }
.list li .grow { min-width: 0; overflow: hidden; text-overflow: ellipsis; }

/* ---------- invoice editor ---------- */
.editor { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; align-items: start; }
.editor-side { position: sticky; top: 70px; }
.totals { width: 100%; border-collapse: collapse; }
.totals td { padding: 6px 0; }
.totals td:last-child { text-align: end; font-weight: 600; }
.totals tr.grand td { font-size: 20px; font-weight: 700; color: var(--brand); border-top: 2px solid var(--line); padding-top: 10px; }
.totals input { height: 32px; width: 110px; }
.party-info { margin-top: 10px; font-size: 13px; color: var(--muted); display: flex; gap: 16px; flex-wrap: wrap; }
.party-info b { color: var(--text); }
.warn-text { color: var(--warn); font-size: 12px; }

/* ---------- autocomplete ---------- */
.ac-wrap { position: relative; }
.ac-box {
  position: absolute; top: calc(100% + 4px); inset-inline: 0; background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; box-shadow: 0 10px 30px rgba(0,0,0,.14); max-height: 340px; overflow-y: auto; z-index: 60;
}
.ac-item { padding: 9px 12px; cursor: pointer; display: flex; gap: 10px; align-items: center; border-bottom: 1px solid var(--line-2); }
.ac-item.active, .ac-item:hover { background: var(--brand-soft); }
.ac-item .grow { min-width: 0; }
.ac-item small { color: var(--muted); }
.ac-empty { padding: 12px; color: var(--muted); }

/* ---------- modal ---------- */
.modal-wrap { position: fixed; inset: 0; background: rgba(15, 23, 32, .45); display: grid; place-items: center; z-index: 100; padding: 16px; }
.modal { background: var(--panel); border-radius: 12px; width: 100%; max-width: 560px; max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.modal.wide { max-width: 880px; }
.modal header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal header h3 { margin: 0; }
.modal-body { padding: 18px 20px; overflow-y: auto; }
.modal footer { display: flex; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--line); align-items: center; }

/* ---------- toasts ---------- */
#toasts { position: fixed; bottom: 20px; inset-inline-start: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 200; align-items: center; }
[dir="rtl"] #toasts { transform: translateX(50%); }
.toast { padding: 11px 18px; border-radius: 8px; background: #1f2937; color: #fff; box-shadow: 0 8px 24px rgba(0,0,0,.2); transition: opacity .4s; max-width: 520px; }
.toast.ok { background: var(--ok); }
.toast.err { background: var(--danger); }
.toast.out { opacity: 0; }

/* ---------- login ---------- */
.login { flex: 1; display: grid; place-items: center; padding: 20px; background: linear-gradient(135deg, #0f766e 0%, #10302d 100%); min-height: 100vh; }
.login-card { background: var(--panel); border-radius: 16px; padding: 32px; width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; text-align: center; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.login-card .field { text-align: start; }
.login-card h1 { font-size: 22px; }
.login-card p { margin: 0; }
.setup-box { background: var(--brand-soft); color: var(--brand-dark); border-radius: 8px; padding: 10px 12px; font-size: 13px; line-height: 1.7; }

/* ---------- detail pages ---------- */
.info-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px 20px; }
.info-grid div span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.big-balance { font-size: 26px; font-weight: 700; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 12px 0; border-bottom: 1px solid var(--line-2); display: flex; gap: 12px; }
.timeline li:last-child { border-bottom: 0; }
.timeline .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--brand); margin-top: 6px; flex: none; }
.timeline .meta { font-size: 12px; color: var(--muted); }
.timeline p { margin: 4px 0 0; white-space: pre-wrap; }
.note-form { display: grid; grid-template-columns: 130px minmax(0, 1fr) 160px auto; gap: 8px; align-items: start; margin-bottom: 14px; }
.note-form textarea { min-height: 36px; height: 36px; }

/* ---------- Excel import ---------- */
.step { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--brand); color: #fff; font-size: 13px; }
.drop { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 28px 16px; border: 2px dashed #c9d3dc; border-radius: 12px; cursor: pointer; text-align: center; color: var(--text); }
.drop:hover, .drop.over { border-color: var(--brand); background: var(--brand-soft); }
.drop .ic { width: 30px; height: 30px; color: var(--brand); }
.tbl tr.err-row td { background: #fff8f8; }
details summary { user-select: none; }

/* ---------- printable documents ---------- */
.doc { max-width: 900px; }
.doc-head { display: flex; justify-content: space-between; gap: 20px; border-bottom: 2px solid var(--brand); padding-bottom: 14px; margin-bottom: 16px; }
.doc-head h2 { margin: 0 0 4px; font-size: 20px; }
.doc-head .co small { display: block; color: var(--muted); }
.doc-title { text-align: end; }
.doc-title h2 { color: var(--brand); }
.doc-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 16px; }
.doc-meta .box { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; }
.doc-meta .box span { display: block; font-size: 12px; color: var(--muted); }
.doc .tbl th { background: var(--brand-soft); color: var(--brand-dark); }
.doc-totals { display: flex; justify-content: flex-end; margin-top: 12px; }
.doc-totals table { width: 320px; border-collapse: collapse; }
.doc-totals td { padding: 6px 10px; border-bottom: 1px solid var(--line-2); }
.doc-totals td:last-child { text-align: end; font-weight: 600; }
.doc-totals tr.grand td { background: var(--brand); color: #fff; font-size: 16px; font-weight: 700; }
.doc-foot { margin-top: 24px; padding-top: 12px; border-top: 1px dashed var(--line); text-align: center; color: var(--muted); font-size: 12.5px; }
.doc { position: relative; }
.stamp { position: absolute; top: 40%; inset-inline-start: 25%; transform: rotate(-18deg); border: 4px solid var(--danger); color: var(--danger); font-size: 42px; font-weight: 800; padding: 6px 24px; border-radius: 10px; opacity: .35; pointer-events: none; }
.sign-row { display: flex; justify-content: space-between; margin-top: 50px; }
.sign-row div { width: 40%; border-top: 1px solid var(--text); padding-top: 6px; text-align: center; font-size: 12px; }

@media screen { #print-root { display: none; } }
@media print {
  @page { margin: 12mm; }
  body { background: #fff; }
  #app, #toasts, .modal-wrap { display: none !important; }
  #print-root { display: block; }
  .panel { box-shadow: none; border: 0; padding: 0; }
  .doc .tbl th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .doc-totals tr.grand td, .tbl tr.grand td { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .no-print { display: none !important; }
}

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .editor { grid-template-columns: 1fr; }
  .editor-side { position: static; }
  .dash-grid { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .sidebar { position: fixed; inset-inline-start: 0; top: 0; z-index: 90; transform: translateX(-100%); transition: transform .2s; }
  [dir="rtl"] .sidebar { transform: translateX(100%); }
  .sidebar.open { transform: none !important; box-shadow: 0 0 40px rgba(0,0,0,.4); }
  .only-mobile { display: inline-grid; }
  #view { padding: 16px; }
  .topbar { padding: 8px 16px; }
  .g4, .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .note-form { grid-template-columns: 1fr; }
  .doc-meta { grid-template-columns: 1fr; }
  .topbar .quick .btn span { display: none; }
}
@media (max-width: 520px) {
  .g4, .g3, .g2 { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  .user-menu small { display: none; }
}
