:root {
  --paper: #f3f6f4;
  --surface: #ffffff;
  --ink: #14262b;
  --muted: #5d6d70;
  --line: #d9e1de;
  --line-strong: #b9c6c2;
  --green: #0b6e4f;
  --green-deep: #0a3d30;
  --green-soft: #e1f0e9;
  --amber: #9a5f08;
  --amber-soft: #fbefd6;
  --red: #b3261e;
  --red-soft: #fbe8e6;
  --blue: #1f5f8b;
  --blue-soft: #e4eff7;
  --radius: 6px;
  --font: "Segoe UI", "Noto Sans Arabic", "Noto Kufi Arabic", Tahoma, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.55;
  color: var(--ink);
  background: var(--paper);
}
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.3; }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }
.ltr { direction: ltr; unicode-bidi: isolate; text-align: left; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--green); outline-offset: 2px; }

/* الهيكل */
.shell { display: grid; grid-template-columns: 216px 1fr; min-height: 100vh; }
.side {
  background: var(--green-deep);
  color: #dbe9e3;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 16px 16px; border-bottom: 1px solid rgba(255,255,255,.1); }
.brand-mark { width: 30px; height: 30px; fill: #6fd3a9; flex: none; }
.brand-name { font-weight: 700; font-size: 15px; color: #fff; }
.brand-sub { font-size: 12px; color: #9fbdb2; }
.nav { display: flex; flex-direction: column; padding: 12px 8px; gap: 2px; flex: 1; overflow: auto; }
.nav a, .nav-soon { padding: 8px 12px; border-radius: var(--radius); text-decoration: none; color: inherit; }
.nav a:hover { background: rgba(255,255,255,.08); }
.nav a.active { background: #6fd3a9; color: var(--green-deep); font-weight: 700; }
.nav-group { margin: 14px 12px 4px; font-size: 12px; color: #8fb0a4; }
.nav-soon { color: #7f9d92; cursor: default; }
.side-foot { padding: 12px 16px; font-size: 12px; color: #9fbdb2; border-top: 1px solid rgba(255,255,255,.1); }

.main { padding: 22px 26px 40px; min-width: 0; outline: none; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.page-head h1 { font-size: 22px; font-weight: 700; }
.page-head .spacer { flex: 1; }

/* الأزرار */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--line-strong); background: var(--surface);
  padding: 7px 14px; border-radius: var(--radius); cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--ink); }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; font-weight: 600; }
.btn.primary:hover { background: #095c42; }
.btn.danger { color: var(--red); border-color: #e3b5b1; }
.btn.danger:hover { background: var(--red-soft); border-color: var(--red); }
.btn.small { padding: 3px 9px; font-size: 13px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--paper); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
kbd {
  font: 11px/1 var(--font); padding: 2px 5px; border-radius: 4px;
  background: rgba(0,0,0,.12); direction: ltr; unicode-bidi: isolate;
}
.btn:not(.primary) kbd { background: var(--paper); border: 1px solid var(--line); color: var(--muted); }

/* عدادات تعمل كفلاتر */
.counters { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0; margin-bottom: 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.counter { all: unset; box-sizing: border-box; cursor: pointer; padding: 12px 16px; border-inline-start: 1px solid var(--line);
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.counter:first-child { border-inline-start: 0; }
.counter:hover { background: var(--paper); }
.counter[aria-pressed="true"] { background: var(--green-soft); box-shadow: inset 0 -3px 0 var(--green); }
.counter:focus-visible { outline: 2px solid var(--green); outline-offset: -2px; }
.counter b { font-size: 22px; font-weight: 700; }
.counter.warn b { color: var(--amber); }
.counter.bad b { color: var(--red); }

/* شريط الأدوات */
.toolbar { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.toolbar .search { flex: 1 1 320px; position: relative; }
.toolbar .search input { width: 100%; padding-left: 44px; }
.toolbar .search kbd { position: absolute; left: 10px; top: 50%; translate: 0 -50%;
  background: var(--paper); border: 1px solid var(--line); color: var(--muted); }

input[type="text"], input[type="search"], input[type="number"], input[type="date"], input[type="tel"], select, textarea {
  border: 1px solid var(--line-strong); border-radius: var(--radius); background: var(--surface);
  padding: 7px 10px; min-width: 0; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--green); outline-offset: -1px; border-color: var(--green); }
input:disabled { background: var(--paper); color: var(--muted); border-color: var(--line); }
input.invalid, select.invalid { border-color: var(--red); background: var(--red-soft); }
.toolbar select { width: auto; }

/* الجدول */
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: auto; }
table.grid { width: 100%; border-collapse: collapse; }
.grid th { position: sticky; top: 0; background: #eaf0ed; text-align: start; font-weight: 600; font-size: 13px;
  padding: 9px 12px; border-bottom: 1px solid var(--line-strong); white-space: nowrap; }
.grid td { padding: 8px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.grid tbody tr:last-child td { border-bottom: 0; }
.grid tbody tr { cursor: pointer; }
.grid tbody tr:hover, .grid tbody tr:focus-visible { background: #f2f8f5; outline: none; }
.grid tbody tr:focus-visible td:first-child { box-shadow: inset 3px 0 0 var(--green); }
.grid .end { text-align: end; }
.item-name { font-weight: 600; }
.item-sub { font-size: 12px; color: var(--muted); }
tr.inactive .item-name { color: var(--muted); text-decoration: line-through; }

.tag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 99px; white-space: nowrap; }
.tag.ok { background: var(--green-soft); color: var(--green); }
.tag.warn { background: var(--amber-soft); color: var(--amber); }
.tag.bad { background: var(--red-soft); color: var(--red); }
.tag.info { background: var(--blue-soft); color: var(--blue); }
.tag.off { background: #e9eded; color: var(--muted); }

.pager { display: flex; align-items: center; gap: 10px; margin-top: 10px; color: var(--muted); }
.pager .spacer { flex: 1; }

.empty { padding: 44px 20px; text-align: center; }
.empty h2 { font-size: 17px; margin-bottom: 6px; }
.empty p { margin: 0 0 14px; color: var(--muted); }

/* بطاقة المادة (لوحة جانبية) */
.scrim { position: fixed; inset: 0; background: rgba(10, 30, 25, .38); z-index: 20; }
.drawer {
  position: fixed; top: 0; bottom: 0; inset-inline-end: 0; width: min(780px, 100vw); z-index: 21;
  background: var(--paper); display: flex; flex-direction: column;
  box-shadow: 0 0 40px rgba(10, 30, 25, .25);
  animation: slide .16s ease-out;
}
@keyframes slide { from { translate: -24px 0; opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .drawer { animation: none; } }
.drawer-head { display: flex; align-items: center; gap: 10px; padding: 14px 20px; background: var(--surface); border-bottom: 1px solid var(--line); }
.drawer-head h2 { font-size: 18px; }
.drawer-head .code { color: var(--muted); }
.drawer-head .spacer { flex: 1; }
.drawer-body { flex: 1; overflow: auto; padding: 16px 20px 24px; }
.drawer-foot { display: flex; gap: 8px; padding: 12px 20px; background: var(--surface); border-top: 1px solid var(--line); }
.drawer-foot .spacer { flex: 1; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 12px; }
.card > h3 { font-size: 14px; font-weight: 700; margin-bottom: 10px; }
.fields { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 10px 12px; }
.field { grid-column: span 4; display: flex; flex-direction: column; gap: 3px; }
.field.w2 { grid-column: span 2; } .field.w3 { grid-column: span 3; }
.field.w6 { grid-column: span 6; } .field.w8 { grid-column: span 8; } .field.w12 { grid-column: span 12; }
.field > label, .field > .label { font-size: 12.5px; color: var(--muted); }
.field .req { color: var(--red); }
.field .hint { font-size: 12px; color: var(--muted); }
.field .err { font-size: 12px; color: var(--red); }
.with-add { display: flex; gap: 6px; }
.with-add .btn { padding: 0 10px; flex: none; }

.checks { display: flex; flex-wrap: wrap; gap: 6px 18px; }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--green); }

/* تركيبة السعر */
.price-build { display: grid; grid-template-columns: 1fr auto 1fr auto 1fr auto 1.15fr; align-items: stretch; gap: 0; margin-top: 12px; }
.price-build .step { border: 1px solid var(--line); background: var(--paper); padding: 8px 12px; border-radius: var(--radius); }
.price-build .step small { display: block; font-size: 12px; color: var(--muted); }
.price-build .step b { font-size: 16px; }
.price-build .op { display: grid; place-items: center; padding: 0 8px; color: var(--muted); font-size: 16px; }
.price-build .step.total { background: var(--green-soft); border-color: #a9d3c0; }
.price-build .step.total b { color: var(--green); }
.price-note { margin-top: 8px; font-size: 12.5px; }
.price-note.warn { color: var(--amber); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--paper); border: 1px solid var(--line);
  border-radius: 99px; padding: 2px 4px 2px 10px; }
.chip button { all: unset; cursor: pointer; width: 18px; height: 18px; border-radius: 50%; text-align: center; line-height: 18px; color: var(--muted); }
.chip button:hover { background: var(--red-soft); color: var(--red); }

.mini { width: 100%; border-collapse: collapse; }
.mini th { text-align: start; font-size: 12.5px; color: var(--muted); font-weight: 500; padding: 4px 8px; border-bottom: 1px solid var(--line); }
.mini td { padding: 6px 8px; border-bottom: 1px solid var(--line); }
.mini tr:last-child td { border-bottom: 0; }
.mini tr.excluded td { color: var(--muted); text-decoration: line-through; }
.mini tr.excluded td:last-child { text-decoration: none; }
.mini .end { text-align: end; }

/* التعريفات */
.defs { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; align-items: start; }
.def-list { list-style: none; margin: 10px 0 0; padding: 0; max-height: 320px; overflow: auto; }
.def-list li { display: flex; align-items: center; gap: 8px; padding: 5px 2px; border-bottom: 1px solid var(--line); }
.def-list li:last-child { border-bottom: 0; }
.def-list .spacer { flex: 1; }
.add-row { display: flex; gap: 6px; }

/* الحوار الصغير */
dialog.ask { border: 1px solid var(--line-strong); border-radius: 8px; padding: 18px 20px; width: min(420px, 92vw); color: var(--ink); }
dialog.ask::backdrop { background: rgba(10, 30, 25, .4); }
dialog.ask h3 { font-size: 16px; margin-bottom: 10px; }
dialog.ask p { margin: 0 0 12px; }
dialog.ask .actions { display: flex; gap: 8px; justify-content: flex-start; margin-top: 14px; }

/* التنبيهات */
#toasts { position: fixed; top: 14px; left: 50%; translate: -50% 0; z-index: 60; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { border-radius: var(--radius); max-width: 440px; box-shadow: 0 6px 20px rgba(10, 30, 25, .16); }

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: static; height: auto; }
  .nav { flex-direction: row; flex-wrap: wrap; }
  .nav-group, .nav-soon, .side-foot { display: none; }
  .main { padding: 16px 12px 32px; }
  .counters { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .counter:nth-child(3) { border-inline-start: 0; }
  .counter:nth-child(n+3) { border-top: 1px solid var(--line); }
  .field, .field.w2, .field.w3 { grid-column: span 6; }
  .field.w6, .field.w8 { grid-column: span 12; }
  .price-build { grid-template-columns: 1fr 1fr; gap: 6px; }
  .price-build .op { display: none; }
  .defs { grid-template-columns: 1fr; }
  .hide-sm { display: none; }
}

/* اختيار المادة: بحث مع قائمة منسدلة */
.picker { position: relative; }
.picker input { width: 100%; font-size: 15px; padding: 9px 12px; }
.picker-list { position: absolute; z-index: 15; inset-inline: 0; top: calc(100% + 4px); background: var(--surface);
  border: 1px solid var(--line-strong); border-radius: var(--radius); box-shadow: 0 10px 28px rgba(10, 30, 25, .16);
  max-height: 340px; overflow: auto; margin: 0; padding: 4px; list-style: none; }
.picker-list li { display: flex; align-items: baseline; gap: 10px; padding: 7px 10px; border-radius: 4px; cursor: pointer; }
.picker-list li.active, .picker-list li:hover { background: var(--green-soft); }
.picker-list .grow { flex: 1; min-width: 0; }
.picker-list .empty-row { color: var(--muted); cursor: default; }
.picker-list .empty-row:hover { background: none; }

/* محرر المستند (شراء) */
.doc-head { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 10px 12px; }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.seg button { all: unset; cursor: pointer; padding: 6px 14px; font-size: 13.5px; }
.seg button + button { border-inline-start: 1px solid var(--line-strong); }
.seg button[aria-pressed="true"] { background: var(--green); color: #fff; font-weight: 600; }
.seg button:focus-visible { outline: 2px solid var(--green); outline-offset: -2px; }
.lines { width: 100%; border-collapse: collapse; }
.lines th { text-align: start; font-size: 12.5px; color: var(--muted); font-weight: 500; padding: 6px 6px; border-bottom: 1px solid var(--line-strong); white-space: nowrap; }
.lines td { padding: 6px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
.lines input, .lines select { padding: 6px 8px; }
.lines .w-qty { width: 78px; } .lines .w-money { width: 96px; } .lines .w-pct { width: 64px; } .lines .w-date { width: 142px; }
.lines .end { text-align: end; }
.lines .line-total { font-weight: 700; padding-top: 12px; white-space: nowrap; }
.lines .x { all: unset; cursor: pointer; color: var(--muted); width: 26px; height: 30px; text-align: center; border-radius: 4px; }
.lines .x:hover { background: var(--red-soft); color: var(--red); }
.doc-foot { display: grid; grid-template-columns: 1fr 340px; gap: 16px; align-items: start; margin-top: 12px; }
.totals { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; }
.totals .row { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 4px 0; }
.totals .row.grand { border-top: 1px solid var(--line-strong); margin-top: 6px; padding-top: 9px; font-size: 18px; font-weight: 700; }
.totals .row.grand b { color: var(--green); }
.totals .inline { display: flex; gap: 6px; }
.totals .inline input { width: 90px; } .totals .inline select { width: auto; }

/* شاشة البيع */
.pos { display: grid; grid-template-columns: minmax(0, 1fr) 330px; gap: 16px; align-items: start; }
.pos-tabs { display: flex; gap: 6px; margin-bottom: 10px; flex-wrap: wrap; align-items: center; }
.pos-tab { all: unset; cursor: pointer; padding: 5px 12px; border: 1px solid var(--line-strong); border-radius: 99px; background: var(--surface); font-size: 13px; }
.pos-tab[aria-selected="true"] { background: var(--green-deep); color: #fff; border-color: var(--green-deep); }
.pos-tab:focus-visible { outline: 2px solid var(--green); outline-offset: 2px; }
.pos-side { position: sticky; top: 16px; display: flex; flex-direction: column; gap: 10px; }
.pos-total { background: var(--green-deep); color: #fff; border-radius: 8px; padding: 14px 16px; }
.pos-total small { color: #9fd9c2; }
.pos-total .amount { font-size: 44px; font-weight: 700; line-height: 1.1; letter-spacing: -.5px; }
.pos-total .sub { display: flex; justify-content: space-between; font-size: 13px; color: #cfe9de; margin-top: 4px; }
.pay-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pay-grid .btn { justify-content: center; padding: 11px 8px; font-weight: 600; }
.pay-grid .btn.wide { grid-column: 1 / -1; }
.pos-empty { padding: 46px 16px; text-align: center; color: var(--muted); }
.today { display: flex; gap: 18px; flex-wrap: wrap; color: var(--muted); font-size: 13px; }
.today b { color: var(--ink); }
.unit-toggle { margin-top: 4px; }
.unit-toggle .seg button { padding: 2px 9px; font-size: 12px; }

/* عرض المستند */
.kv { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px 14px; }
.kv div small { display: block; color: var(--muted); font-size: 12px; }

@media (max-width: 1000px) {
  .pos, .doc-foot { grid-template-columns: 1fr; }
  .pos-side { position: static; }
  .kv { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* الإشعارات بأنواعها */
.toast { display: flex; align-items: flex-start; gap: 10px; padding: 10px 12px 10px 8px; background: var(--surface); color: var(--ink);
  border: 1px solid var(--line-strong); border-inline-start: 5px solid var(--blue); min-width: 260px; animation: toast-in .16s ease-out; }
@keyframes toast-in { from { translate: 0 8px; opacity: 0; } }
.toast-icon { flex: none; width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: 13px; color: #fff; background: var(--blue); }
.toast-text { flex: 1; }
.toast-close { all: unset; cursor: pointer; color: var(--muted); padding: 0 4px; font-size: 16px; line-height: 1; }
.toast-close:hover { color: var(--ink); }
.toast.success { border-inline-start-color: var(--green); background: #f1faf5; } .toast.success .toast-icon { background: var(--green); }
.toast.warning { border-inline-start-color: #d08a0c; background: #fff9ea; } .toast.warning .toast-icon { background: #d08a0c; }
.toast.error { border-inline-start-color: var(--red); background: #fff3f1; color: var(--ink); } .toast.error .toast-icon { background: var(--red); }

/* تأكيد الحفظ */
.check { flex: none; width: 30px; height: 30px; border-radius: 50%; background: var(--green); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 17px; }
.check.big { width: 56px; height: 56px; font-size: 30px; margin: 0 auto 10px; }
.saved-banner { display: flex; gap: 12px; align-items: center; background: #f1faf5; border: 1px solid #a9d3c0; border-radius: var(--radius); padding: 12px 14px; margin-bottom: 12px; }
.saved-card { text-align: center; padding: 26px 18px 22px; border-bottom: 1px solid var(--line); background: #f1faf5; }
.saved-card h2 { font-size: 20px; color: var(--green); }
.saved-facts { display: flex; justify-content: center; gap: 28px; margin: 14px 0 16px; flex-wrap: wrap; }
.saved-facts small { display: block; color: var(--muted); font-size: 12.5px; }
.saved-facts b { font-size: 17px; }
.saved-actions { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; }

/* شريط أدوات المستند والتنقل */
.doc-toolbar { display: flex; align-items: center; gap: 8px; padding: 8px 20px; background: #eaf0ed; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.doc-nav { display: flex; align-items: center; gap: 6px; }
.doc-toolbar .sep { width: 1px; align-self: stretch; background: var(--line-strong); margin: 0 4px; }

/* أزرار القائمة الجانبية والتنبيهات */
.side-tools { display: flex; flex-direction: column; gap: 4px; padding: 8px; border-top: 1px solid rgba(255,255,255,.1); }
.side-btn { all: unset; box-sizing: border-box; cursor: pointer; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 7px 12px; border-radius: var(--radius); color: #dbe9e3; }
.side-btn:hover { background: rgba(255,255,255,.08); }
.side-btn:focus-visible { outline: 2px solid #6fd3a9; outline-offset: -2px; }
.side-btn kbd { background: rgba(255,255,255,.14); color: #dbe9e3; }
.badge { min-width: 20px; padding: 0 6px; height: 20px; border-radius: 99px; background: #d08a0c; color: #fff; font-size: 12px; font-weight: 700; display: inline-grid; place-items: center; }
.badge.bad { background: #d6453b; }
.alert-list { list-style: none; margin: 0; padding: 0; }
.alert-list li { display: flex; align-items: baseline; gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--line); }
.alert-list li:last-child { border-bottom: 0; }
.alert-list .grow { flex: 1; }
.help-list { margin: 0; padding-inline-start: 20px; }
.help-list li { margin-bottom: 6px; }
kbd.key { font-size: 12.5px; padding: 3px 8px; background: var(--paper); border: 1px solid var(--line-strong); color: var(--ink); }

/* الطباعة: معاينة ثم ورقة */
dialog.print-preview { width: min(860px, 96vw); }
.preview-scroll { max-height: 62vh; overflow: auto; background: #dfe5e3; padding: 14px; border-radius: var(--radius); }
.paper { background: #fff; color: #000; margin: 0 auto; font-size: 12px; line-height: 1.45; }
.paper.thermal { width: 72mm; padding: 4mm 3mm; }
.paper.a4 { width: 190mm; padding: 12mm; font-size: 13px; }
.paper .p-head { text-align: center; border-bottom: 1px dashed #000; padding-bottom: 6px; margin-bottom: 6px; }
.paper .p-name { font-size: 16px; font-weight: 700; }
.paper .p-row { display: flex; justify-content: space-between; gap: 8px; }
.paper .p-row.strong { font-weight: 700; font-size: 15px; border-top: 1px solid #000; margin-top: 4px; padding-top: 4px; }
.paper .p-lines { width: 100%; border-collapse: collapse; margin: 6px 0; }
.paper .p-lines th { text-align: start; border-bottom: 1px solid #000; padding: 2px 3px; font-weight: 700; }
.paper .p-lines td { padding: 2px 3px; border-bottom: 1px dotted #999; vertical-align: top; }
.paper .p-totals { margin-inline-start: auto; }
.paper.a4 .p-totals { width: 70mm; }
.paper .p-foot { text-align: center; margin-top: 8px; border-top: 1px dashed #000; padding-top: 6px; }
#print-root { display: none; }
@media print {
  body > *:not(#print-root) { display: none !important; }
  #print-root { display: block; }
  #print-root .paper { margin: 0; }
  body { background: #fff; }
  body[data-print="thermal"] { width: 72mm; }
}
@media (max-width: 900px) { .side-tools { flex-direction: row; } }

/* فحص السعر */
.price-card { background: var(--green-soft); border: 1px solid #a9d3c0; border-radius: var(--radius); padding: 12px 14px; }
.price-big { font-size: 38px; font-weight: 700; color: var(--green); line-height: 1.2; }

/* شريط الأزرار الموحد: جديد، تعديل، حذف، حفظ، تراجع */
.crud { display: inline-flex; background: var(--surface); border: 1px solid var(--line-strong); border-radius: var(--radius); overflow: hidden; }
.crud-btn { all: unset; box-sizing: border-box; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; padding: 7px 13px; font-size: 13.5px; font-weight: 600; white-space: nowrap; }
.crud-btn + .crud-btn { border-inline-start: 1px solid var(--line); }
.crud-btn svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.crud-btn:hover:not(:disabled) { background: var(--paper); }
.crud-btn:focus-visible { outline: 2px solid var(--green); outline-offset: -2px; }
.crud-btn:disabled { color: #a7b3b0; cursor: not-allowed; }
.crud-new:not(:disabled) { color: var(--blue); }
.crud-edit:not(:disabled) { color: var(--amber); }
.crud-delete:not(:disabled) { color: var(--red); }
.crud-save:not(:disabled) { color: #fff; background: var(--green); }
.crud-save:hover:not(:disabled) { background: #095c42; }
.crud-btn kbd { background: rgba(0,0,0,.08); color: inherit; }
.crud-save:not(:disabled) kbd { background: rgba(255,255,255,.22); }
.crud-bar { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.editing-note { background: var(--amber-soft); color: var(--amber); border: 1px solid #ecd9a8; border-radius: var(--radius); padding: 6px 12px; font-size: 13px; }

/* شعار الصيدلية */
.brand-logo { width: 40px; height: 40px; object-fit: contain; background: #fff; border-radius: 8px; padding: 3px; flex: none; }
.logo-field { display: flex; align-items: center; gap: 12px; }
.logo-preview { width: 84px; height: 84px; border: 1px dashed var(--line, #cfd8d4); border-radius: 10px; display: grid; place-items: center; background: #fff; overflow: hidden; font-size: 12px; }
.logo-preview img { max-width: 100%; max-height: 100%; object-fit: contain; }
.logo-actions { display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }
.paper .p-logo { display: block; margin: 0 auto 6px; max-height: 70px; max-width: 60%; object-fit: contain; }
.paper.a4 .p-logo { max-height: 90px; }
.paper .p-contact { font-size: 11px; }

/* المساعد الذكي ولوحة المؤشرات */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); margin-bottom: 12px; overflow: hidden; }
.kpi { padding: 12px 16px; border-inline-start: 1px solid var(--line); display: flex; flex-direction: column; gap: 2px; }
.kpi small { color: var(--muted); font-size: 12.5px; }
.kpi b { font-size: 20px; }
.kpi span { font-size: 12px; color: var(--muted); }
.kpi.warn b { color: var(--amber); } .kpi.bad b { color: var(--red); }
.dash-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; align-items: start; }
.dash-grid .card { margin-bottom: 12px; }
@media (max-width: 900px) { .dash-grid { grid-template-columns: 1fr; } }
.bars { display: flex; align-items: flex-end; gap: 6px; height: 150px; padding-top: 6px; }
.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: 28px; background: var(--green); border-radius: 3px 3px 0 0; }
.bar span { font-size: 11px; color: var(--muted); }
button.link { all: unset; cursor: pointer; color: var(--green-deep); text-decoration: underline; text-decoration-color: var(--line-strong); text-underline-offset: 3px; }
button.link:hover { color: var(--green); } button.link:focus-visible { outline: 2px solid var(--green); }
.chat-quick { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chat-thread { display: flex; flex-direction: column; gap: 8px; max-height: 360px; overflow: auto; margin-bottom: 10px; }
.chat-msg { padding: 8px 12px; border-radius: 8px; white-space: pre-wrap; line-height: 1.7; max-width: 92%; }
.chat-msg.user { background: var(--green-soft); align-self: flex-start; }
.chat-msg.assistant { background: var(--paper); border: 1px solid var(--line); align-self: flex-end; }
.chat-msg.wait { color: var(--muted); } .chat-msg.error { background: var(--red-soft); border-color: transparent; color: var(--red); }
.chat-box { display: flex; gap: 8px; align-items: flex-end; }
.chat-box textarea { flex: 1; resize: vertical; font: inherit; padding: 8px 10px; border: 1px solid var(--line-strong); border-radius: var(--radius); }
.ai-body { margin-top: 10px; line-height: 1.7; }
.ai-dialog .ai-body { max-height: 62vh; overflow: auto; }
.ai-wait { color: var(--muted); } .ai-error { color: var(--red); background: var(--red-soft); padding: 8px 12px; border-radius: var(--radius); }
.ai-note { font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); padding-top: 8px; margin-top: 12px; }
.ai-summary { padding: 8px 12px; border-radius: var(--radius); font-weight: 600; }
.ai-summary.ok { background: var(--green-soft); color: var(--green-deep); } .ai-summary.warn { background: var(--amber-soft); color: var(--amber); }
.ai-sec { margin-top: 10px; } .ai-sec h4 { font-size: 13.5px; margin: 0 0 4px; } .ai-sec ul { margin: 0; padding-inline-start: 20px; } .ai-sec p { margin: 0; }
.ai-sec.warn h4 { color: var(--red); }
.ai-interaction { border-inline-start: 4px solid var(--blue); padding: 6px 12px; margin-top: 10px; background: var(--paper); }
.ai-interaction.bad { border-color: var(--red); } .ai-interaction.warn { border-color: var(--amber); }
.ai-interaction p { margin: 4px 0 0; } .ai-pair { display: flex; gap: 8px; align-items: center; } .ai-advice { font-weight: 600; }
.ai-suggest { padding: 6px 0; border-top: 1px solid var(--line); } .ai-stock { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.ai-chip { background: var(--green-soft); border-radius: 99px; padding: 2px 10px; font-size: 12.5px; }
.ai-invoice-head { display: flex; flex-wrap: wrap; gap: 18px; margin-bottom: 8px; }
tr.ai-unmatched td { background: var(--amber-soft); }

/* ===== تحديث التصميم: أيقونات، عمق أنعم، وزوايا ألين ===== */
:root { --radius: 10px; --shadow: 0 1px 2px rgba(16, 40, 34, .06), 0 4px 14px rgba(16, 40, 34, .05); --paper: #eef3f1; }
.ico { width: 17px; height: 17px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.btn.small .ico { width: 15px; height: 15px; }
.shell { grid-template-columns: 232px 1fr; }
.side { background: linear-gradient(180deg, #0c4a39 0%, #072e24 100%); box-shadow: -2px 0 18px rgba(7, 46, 36, .18); }
.brand { padding: 20px 16px 18px; }
.nav { padding: 14px 10px; gap: 3px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 9px; transition: background .12s; }
.nav a .ico { width: 19px; height: 19px; opacity: .85; }
.nav a.active { background: linear-gradient(90deg, #7fe0b6, #5fcf9f); box-shadow: 0 3px 10px rgba(0, 0, 0, .22); }
.nav a.active .ico { opacity: 1; }
.side-btn { display: flex; align-items: center; gap: 10px; }
.main { padding: 24px 28px 44px; }
.page-head h1 { font-size: 24px; letter-spacing: -.2px; }
.btn { padding: 8px 14px; border-radius: 9px; box-shadow: 0 1px 1px rgba(16, 40, 34, .05); transition: background .12s, border-color .12s, box-shadow .12s, transform .05s; }
.btn:hover { border-color: var(--green); box-shadow: 0 2px 8px rgba(11, 110, 79, .12); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn.primary { background: linear-gradient(180deg, #0e8660, #0b6e4f); box-shadow: 0 2px 8px rgba(11, 110, 79, .28); }
.btn.primary:hover { background: linear-gradient(180deg, #0c7a57, #095c42); }
.btn.small { padding: 4px 10px; border-radius: 8px; }
.card, .table-wrap, .counters, .kpis { border-radius: var(--radius); box-shadow: var(--shadow); border-color: #e1e8e5; }
table.grid thead th { background: #f4f8f6; }
table.grid tbody tr:hover { background: #f3faf6; }
input:not([type=checkbox]):not([type=radio]):not([type=file]), select, textarea { border-radius: 8px; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--green); box-shadow: 0 0 0 3px rgba(11, 110, 79, .15); }
.crud { border-radius: 10px; box-shadow: var(--shadow); overflow: hidden; background: var(--surface); }
.crud-btn { padding: 9px 15px; }
.crud-new:not(:disabled) svg, .crud-save:not(:disabled) svg { stroke: var(--green); }
.crud-edit:not(:disabled) svg { stroke: var(--blue); }
.crud-delete:not(:disabled) svg { stroke: var(--red); }
.crud-cancel:not(:disabled) svg { stroke: var(--amber); }
.crud-save:not(:disabled) { background: var(--green-soft); color: var(--green-deep); }
.kpi { position: relative; padding: 14px 18px; }
.kpi b { font-size: 22px; letter-spacing: -.3px; }
.kpi::before { content: ''; position: absolute; inset-inline-start: 0; top: 14px; bottom: 14px; width: 3px; border-radius: 3px; background: var(--green); opacity: .5; }
.kpi.warn::before { background: var(--amber); opacity: 1; } .kpi.bad::before { background: var(--red); opacity: 1; }
.bar i { background: linear-gradient(180deg, #19a377, #0b6e4f); border-radius: 5px 5px 0 0; }
dialog.ask { border: 0; border-radius: 14px; box-shadow: 0 20px 60px rgba(7, 46, 36, .3); }
.drawer { box-shadow: 0 0 50px rgba(7, 46, 36, .25); }
.tag { font-weight: 600; }
/* التقارير */
.rep { display: grid; grid-template-columns: 230px 1fr; gap: 14px; align-items: start; }
.rep-menu { background: var(--surface); border: 1px solid #e1e8e5; border-radius: var(--radius); box-shadow: var(--shadow); padding: 8px; position: sticky; top: 12px; max-height: calc(100vh - 24px); overflow: auto; }
.rep-group { font-size: 12px; color: var(--muted); margin: 10px 8px 4px; font-weight: 700; }
.rep-item { all: unset; box-sizing: border-box; display: block; width: 100%; padding: 7px 10px; border-radius: 8px; cursor: pointer; font-size: 13.5px; }
.rep-item:hover { background: var(--paper); } .rep-item.active { background: var(--green-soft); color: var(--green-deep); font-weight: 700; }
.rep-item:focus-visible { outline: 2px solid var(--green); }
.rep-filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; margin-bottom: 10px; }
.rep-field { display: flex; flex-direction: column; gap: 3px; font-size: 12.5px; color: var(--muted); }
.rep-table tfoot td { font-weight: 700; background: #f4f8f6; border-top: 2px solid var(--line-strong); padding: 8px 10px; }
.rep-main { min-width: 0; }
@media (max-width: 900px) { .rep { grid-template-columns: 1fr; } .rep-menu { position: static; max-height: none; } }
.rep-paper table { width: 100%; border-collapse: collapse; font-size: 11px; } .rep-paper th, .rep-paper td { border-bottom: 1px solid #ccc; padding: 3px 5px; text-align: start; }

/* الجرد */
.stk-summary { display: flex; flex-wrap: wrap; gap: 22px; padding: 12px 4px; }
.neg { color: var(--red); font-weight: 700; } .pos { color: var(--green); font-weight: 700; }
.crud-bar .spacer { flex: 1; }

/* الصندوق */
.cash-grid { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 12px; align-items: start; }
@media (max-width: 1000px) { .cash-grid { grid-template-columns: 1fr; } }
.paper-like .p-row { display: flex; justify-content: space-between; padding: 5px 0; border-bottom: 1px dashed var(--line); }
.paper-like .p-row.strong { font-weight: 700; font-size: 16px; border-bottom: 0; border-top: 2px solid var(--ink); margin-top: 4px; padding-top: 8px; }

/* ملصقات الباركود */
.label { box-sizing: border-box; padding: 1.5mm 2mm; background: #fff; color: #000; display: flex; flex-direction: column; align-items: center; justify-content: space-between; overflow: hidden; text-align: center; line-height: 1.15; page-break-after: always; break-after: page; }
.label-pharmacy { font-size: 7pt; } .label-name { font-size: 7.5pt; font-weight: 700; text-align: center; max-height: 2.4em; overflow: hidden; width: 100%; }
.label-bars { flex: 1; min-height: 0; width: 92%; display: flex; } .label-bars .barcode { width: 100%; height: 100%; }
.label-code { font-size: 6.5pt; letter-spacing: .5px; } .label-foot { display: flex; justify-content: space-between; width: 100%; font-size: 8pt; gap: 6px; }
.label-preview { display: grid; place-items: center; padding: 14px; background: var(--paper); border-radius: var(--radius); margin-top: 10px; }
.label-preview .label { border: 1px solid var(--line-strong); zoom: 1.6; }

/* الدخول والمستخدمون */
.login { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(160deg, #0c4a39, #072e24); padding: 20px; }
.login-card { background: #fff; border-radius: 18px; padding: 34px 32px; width: min(380px, 100%); display: flex; flex-direction: column; gap: 12px; box-shadow: 0 30px 80px rgba(0, 0, 0, .35); text-align: center; }
.login-card h1 { font-size: 21px; } .login-card label { display: flex; flex-direction: column; gap: 4px; text-align: start; font-size: 13px; color: var(--muted); }
.login-card input { padding: 10px 12px; border: 1px solid var(--line-strong); font-size: 15px; } .login-card .btn { justify-content: center; padding: 11px; font-size: 15px; margin-top: 6px; }
.login-logo { max-height: 90px; max-width: 70%; object-fit: contain; margin: 0 auto; }
.login-mark { width: 62px; height: 62px; border-radius: 16px; margin: 0 auto; display: grid; place-items: center; background: var(--green-soft); color: var(--green); font-size: 40px; font-weight: 700; line-height: 1; }
.perm-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 6px 14px; margin: 10px 0; }
.user-form { background: var(--paper); border-radius: var(--radius); padding: 14px; margin-top: 8px; }

/* المحاسبة */
.bs-head { margin: 14px 0 4px; font-size: 13.5px; color: var(--green-deep); }

/* الفوترة الوطنية */
.einv-panel { display: flex; gap: 14px; align-items: center; padding: 12px 14px; margin: 10px 0; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.einv-qr { width: 120px; flex: none; text-align: center; } .einv-qr small { display: none; }
.qr { width: 100%; height: auto; display: block; }
.paper .p-qr { width: 34mm; margin: 8px auto 4px; text-align: center; } .paper .p-qr small { display: block; font-size: 9px; margin-top: 2px; }
.paper.a4 .p-qr { width: 38mm; }

/* من لا يملك صلاحية رؤية الكلفة: تُخفى أعمدة وحقول الكلفة والربح */
.no-cost .cost-col, .no-cost [data-field="cost_fils"] { display: none !important; }

.label-qr { justify-content: center; } .label-qr .qr { width: auto; height: 100%; }

/* ===== الجوال والأجهزة اللوحية ===== */
.topbar { display: none; }
.side-scrim { position: fixed; inset: 0; background: rgba(7, 46, 36, .45); z-index: 39; }
.side-btn.net, .net-pill { font-weight: 700; }
.side-btn.net.off, .net-pill.off { color: #ffd9a8; } .side-btn.net.bad, .net-pill.bad { color: #ffb4ad; } .side-btn.net.wait, .net-pill.wait { color: #bfe9d6; }
.net-pill { font-size: 12.5px; padding: 3px 10px; border-radius: 99px; background: rgba(255, 255, 255, .12); white-space: nowrap; }

@media (max-width: 900px) {
  .topbar { display: flex; align-items: center; gap: 10px; position: sticky; top: 0; z-index: 38; height: 52px; padding: 0 12px; background: #0b4a39; color: #fff; box-shadow: 0 2px 10px rgba(7, 46, 36, .25); }
  .topbar-name { font-weight: 700; font-size: 16px; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .menu-btn { all: unset; box-sizing: border-box; width: 44px; height: 44px; display: grid; place-items: center; cursor: pointer; border-radius: 10px; }
  .menu-btn:focus-visible { outline: 2px solid #7fe0b6; }
  .menu-btn svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
  .shell { display: block; min-height: 0; }
  /* القائمة الجانبية تنزلق من اليمين فوق المحتوى */
  .side { position: fixed; top: 0; bottom: 0; inset-inline-start: 0; width: min(290px, 84vw); height: auto; z-index: 40; transform: translateX(105%); transition: transform .2s ease; overflow: auto; }
  .side.open { transform: none; }
  .nav { flex-direction: column; flex-wrap: nowrap; }
  .nav a { padding: 12px 14px; font-size: 15.5px; }
  .side-tools { flex-direction: column; }
  .side-foot { display: block; }
  .main { padding: 14px 10px 90px; }
  .page-head { flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
  .page-head h1 { font-size: 20px; }
  .page-head .spacer { display: none; }
  /* أهداف اللمس وحجم الخط الذي يمنع تكبير iOS عند الكتابة */
  input:not([type=checkbox]):not([type=radio]), select, textarea { font-size: 16px; min-height: 42px; }
  .btn { min-height: 42px; } .btn.small { min-height: 36px; padding: 6px 12px; }
  .crud-bar { overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 4px; }
  .crud { flex: none; } .crud-btn { padding: 10px 13px; } .crud-btn kbd, .btn kbd { display: none; }
  .seg { max-width: 100%; overflow-x: auto; flex-wrap: nowrap; } .seg button { white-space: nowrap; flex: none; }
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  table.grid { min-width: 560px; } table.grid.rep-table { min-width: 620px; }
  .hide-sm { display: none !important; }
  .toolbar, .rep-filters { gap: 6px; } .rep-filters > input:not([type=checkbox]), .rep-filters > select, .rep-filters > textarea { flex: 1 1 140px; min-width: 0; width: auto !important; }
  .rep-field { flex: 1 1 140px; } .rep-field input, .rep-field select { width: 100%; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); } .kpi { padding: 10px 12px; } .kpi b { font-size: 18px; }
  .counters { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fields { grid-template-columns: repeat(6, minmax(0, 1fr)); }
  .field, .field.w2, .field.w3, .field.w6, .field.w8, .field.w12 { grid-column: span 6; }
  /* البيع: المجموع وأزرار الدفع ثابتة أسفل الشاشة */
  .pos { display: block; }
  .pos-side { position: static; margin-top: 12px; }
  .pos-total { position: sticky; bottom: 0; z-index: 5; border-radius: 12px 12px 0 0; }
  .pay-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; } .pay-grid .btn { min-height: 48px; font-size: 15px; justify-content: center; }
  .lines { min-width: 0; } table.lines { min-width: 520px; }
  .picker input { font-size: 16px; min-height: 46px; }
  /* النوافذ والبطاقات المنزلقة بعرض الشاشة */
  .drawer { width: 100vw !important; max-width: 100vw; border-radius: 0; }
  .drawer-head { flex-wrap: wrap; } .doc-toolbar { overflow-x: auto; flex-wrap: nowrap; } .doc-toolbar > * { flex: none; }
  dialog.ask { width: 96vw !important; max-width: 96vw; padding: 14px; max-height: 92vh; overflow: auto; }
  dialog.ask .actions { flex-wrap: wrap; } dialog.ask .actions .btn { flex: 1 1 auto; justify-content: center; }
  .kv { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .cash-grid, .dash-grid, .rep { grid-template-columns: 1fr; display: grid; }
  .rep-menu { position: static; max-height: 220px; }
  .einv-panel { flex-direction: column; align-items: flex-start; }
  .saved-actions { flex-wrap: wrap; } .saved-actions .btn { flex: 1 1 45%; justify-content: center; }
  .chat-box { flex-direction: column; align-items: stretch; }
  .bars { height: 110px; }
  #toasts { left: 8px; right: 8px; translate: none; align-items: stretch; } .toast { max-width: none; }
  .login-card { padding: 26px 20px; }
}
@media (max-width: 520px) {
  .kpis, .counters { grid-template-columns: 1fr 1fr; }
  .perm-grid { grid-template-columns: 1fr; }
  .page-head h1 { width: 100%; }
}

@media (max-width: 900px) { #toasts { top: 60px; } }

@media (max-width: 900px) {
  /* شبكة التعريفات عمود واحد، وبطاقاتها لا تتجاوز عرض الشاشة */
  .defs { display: grid; grid-template-columns: minmax(0, 1fr) !important; }
  .defs > * { grid-column: 1 / -1 !important; min-width: 0; }
  .card { min-width: 0; overflow-x: auto; }
  .logo-field { flex-wrap: wrap; }
  /* القائمة المغلقة لا تُرى ولا تُلمس حتى لو اتسعت الصفحة */
  .side:not(.open) { visibility: hidden; }
  .side { transition: transform .2s ease, visibility .2s; }
  html, body { overflow-x: hidden; }
}
