:root {
  --navy: #1f4e78; --blue: #2e75b6; --sky: #9dc3e6; --pale: #ddebf7; --paper: #f2f7fc;
  --orange: #c55a11; --green: #548235; --red: #c00000; --amber: #ffeb9c; --amber-ink: #7a5b00;
  --ink: #1b2733; --muted: #667788; --line: #c5d3e0; --bg: #e9eff5; --white: #fff;
  --radius: 6px;
  --font: "Segoe UI", Tahoma, "Noto Naskh Arabic", "Noto Sans Arabic", "DejaVu Sans", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); font-size: 14px; color: var(--ink); background: var(--bg); line-height: 1.5; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--blue); }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 1px; }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: plaintext; text-align: center; }

/* ---------- الهيكل ---------- */
.shell { display: grid; grid-template-columns: 236px 1fr; grid-template-rows: 52px 1fr; height: 100vh; }
.topbar { grid-column: 1 / -1; background: var(--navy); color: #fff; display: flex; align-items: center; gap: 14px; padding: 0 16px; }
.topbar h1 { font-size: 16px; margin: 0; font-weight: 600; flex: 1; }
.topbar .who { font-size: 13px; opacity: .92; }
.topbar button { background: transparent; border: 1px solid rgba(255,255,255,.45); color: #fff; border-radius: var(--radius); padding: 4px 10px; }
.topbar button:hover { background: rgba(255,255,255,.14); }
.topbar .undo { background: var(--amber); color: var(--amber-ink); border-color: var(--amber); font-weight: 600; }
.menu-toggle { display: none; font-size: 18px; padding: 2px 10px !important; }
.side { background: #fff; border-left: 1px solid var(--line); overflow-y: auto; padding: 10px 0 20px; }
.side h3 { margin: 14px 16px 4px; font-size: 12px; color: var(--muted); font-weight: 600; }
.side a { display: block; padding: 7px 18px; color: var(--ink); text-decoration: none; border-right: 4px solid transparent; }
.side a:hover { background: var(--paper); }
.side a.on { background: var(--pale); border-right-color: var(--blue); color: var(--navy); font-weight: 600; }
.main { overflow: auto; padding: 18px 22px 40px; }
.page-title { display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap; margin: 0 0 14px; }
.page-title h2 { margin: 0; font-size: 20px; color: var(--navy); }
.page-title .sub { color: var(--muted); font-size: 13px; }
.spacer { flex: 1; }

/* ---------- عناصر عامة ---------- */
.card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.card h3 { margin: 0 0 10px; font-size: 15px; color: var(--navy); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.btn { background: var(--blue); color: #fff; border: 1px solid var(--blue); border-radius: var(--radius); padding: 6px 14px; }
.btn:hover { background: var(--navy); border-color: var(--navy); }
.btn.alt { background: #fff; color: var(--navy); border-color: var(--sky); }
.btn.alt:hover { background: var(--pale); }
.btn.green { background: var(--green); border-color: var(--green); }
.btn.red { background: var(--red); border-color: var(--red); }
.btn.sm { padding: 2px 9px; font-size: 12px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
input[type=text], input[type=number], input[type=date], input[type=password], input[type=search], select, textarea {
  border: 1px solid var(--line); background: #fff; border-radius: 4px; padding: 5px 8px; min-width: 0; }
input:disabled, select:disabled { background: #f0f3f6; }
label.f { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
label.f input, label.f select { font-size: 14px; color: var(--ink); }
.muted { color: var(--muted); }
.pill { display: inline-block; padding: 0 8px; border-radius: 10px; font-size: 12px; background: var(--pale); color: var(--navy); }
.empty { padding: 28px; text-align: center; color: var(--muted); }
.err { color: var(--red); } .ok { color: var(--green); } .warn { color: var(--orange); }
.note { background: var(--amber); color: var(--amber-ink); padding: 8px 12px; border-radius: var(--radius); font-size: 13px; }
.note.blue { background: var(--pale); color: var(--navy); }
.note.red { background: #fde8e8; color: var(--red); }

table.t { border-collapse: collapse; width: 100%; background: #fff; }
table.t th { background: var(--navy); color: #fff; font-weight: 600; padding: 6px 8px; font-size: 13px; position: sticky; top: 0; z-index: 1; white-space: nowrap; }
table.t td { border: 1px solid #dde6ee; padding: 4px 8px; }
table.t tbody tr:nth-child(even) { background: #fafcfe; }
table.t tbody tr:hover { background: var(--paper); }
table.t td.c, table.t th.c { text-align: center; }
.tablewrap { max-height: calc(100vh - 250px); overflow: auto; border: 1px solid var(--line); border-radius: var(--radius); background: #fff; }
.tablewrap.short { max-height: 420px; }
.pager { display: flex; gap: 8px; align-items: center; margin-top: 8px; }

/* ---------- الدخول ---------- */
.login { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(160deg, var(--navy), #163a59); }
.login .box { width: 360px; background: #fff; border-radius: 10px; padding: 26px; }
.login img { width: 100%; margin-bottom: 8px; }
.login h2 { margin: 6px 0 14px; color: var(--navy); font-size: 18px; text-align: center; }
.login label.f { margin-bottom: 10px; }

/* ---------- لوحة التحكم ---------- */
.filters { display: flex; gap: 14px; align-items: end; flex-wrap: wrap; margin-bottom: 14px; }
.filters select, .filters input { min-width: 220px; background: var(--pale); border-color: var(--sky); }
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 14px; }
.kpi { background: var(--paper); border: 1px solid var(--sky); border-radius: var(--radius); padding: 10px 14px; text-align: center; }
.kpi .l { font-size: 13px; color: #555; }
.kpi .v { font-size: 32px; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; }
.kpi .s { font-size: 11.5px; color: var(--muted); font-style: italic; }
.kpi.navy .v { color: var(--navy); } .kpi.red .v { color: var(--red); } .kpi.amber .v { color: #b8860b; } .kpi.blue .v { color: var(--blue); } .kpi.grey .v { color: #7f7f7f; }
.charts { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 14px; }
.chart h4 { margin: 0 0 6px; font-size: 14px; color: var(--navy); }
svg.chart text { font-family: var(--font); }

/* ---------- النماذج الورقية ---------- */
.sheet { background: #fff; border: 1px solid var(--line); box-shadow: 0 1px 6px rgba(31,78,120,.12); max-width: 900px; margin: 0 auto; padding: 14px 22px 22px; }
.sheet .lh { width: 100%; display: block; }
.sheet .meta { display: flex; justify-content: space-between; align-items: center; margin: 8px 0; font-weight: 600; }
.sheet .meta input[type=date] { font-weight: 600; }
.sheet .ttl { text-align: center; font-size: 16px; font-weight: 700; margin: 6px 0 10px; }
table.form { width: 100%; border-collapse: collapse; }
table.form th { background: #d9d9d9; border: 1px solid #333; padding: 4px; font-size: 13px; }
table.form td { border: 1px solid #333; padding: 0; height: 30px; position: relative; }
table.form td.n { text-align: center; background: #d9d9d9; width: 34px; font-size: 13px; }
table.form td input { width: 100%; border: 0; border-radius: 0; padding: 5px 7px; background: transparent; height: 100%; }
table.form td input:focus { background: #fffbe0; outline: 2px solid var(--blue); outline-offset: -2px; }
table.form td.st { border: 0; padding: 0 8px; width: 1%; white-space: nowrap; font-size: 12px; background: transparent; }
table.form td.st.bad { color: var(--red); } table.form td.st.good { color: var(--green); } table.form td.st.alert { color: var(--orange); }
table.form tr.rowbad td:not(.st):not(.n) { background: #fff1f1; }
.sheet .signs { display: flex; justify-content: space-between; margin-top: 14px; padding: 0 30px; }
.sheet .signs div { text-align: center; min-width: 150px; }
.sheet .signs img { height: 56px; display: block; margin: 0 auto; }
.formbar { display: flex; gap: 10px; align-items: center; justify-content: center; margin: 14px 0; flex-wrap: wrap; }
.statuspane { max-width: 900px; margin: 12px auto 0; }
.statuscol { width: 280px; }

/* ---------- القائمة المنسدلة المخصصة ---------- */
.combo { position: relative; width: 100%; height: 100%; }
.combo-list { position: fixed; z-index: 50; background: #fff; border: 1px solid var(--blue); box-shadow: 0 6px 18px rgba(0,0,0,.18); max-height: 260px; overflow-y: auto; min-width: 240px; border-radius: 4px; }
.combo-list div { padding: 5px 10px; cursor: pointer; display: flex; gap: 10px; justify-content: space-between; border-bottom: 1px solid #f0f3f6; }
.combo-list div.on { background: var(--pale); }
.combo-list div small { color: var(--muted); white-space: nowrap; }
.combo-list .none { color: var(--muted); cursor: default; }

/* ---------- نوافذ وإشعارات ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(15,30,45,.5); display: grid; place-items: center; z-index: 100; padding: 16px; }
.modal { background: #fff; border-radius: 10px; width: min(560px, 100%); max-height: 90vh; overflow: auto; padding: 18px; }
.modal.wide { width: min(860px, 100%); }
.modal h3 { margin: 0 0 10px; color: var(--navy); }
.modal .actions { display: flex; gap: 8px; justify-content: flex-start; margin-top: 14px; flex-wrap: wrap; }
.modal ul { margin: 6px 0; padding-inline-start: 22px; } .modal li { margin: 2px 0; }
#toasts { position: fixed; bottom: 18px; left: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast { background: var(--navy); color: #fff; padding: 9px 14px; border-radius: var(--radius); box-shadow: 0 4px 14px rgba(0,0,0,.25); max-width: 420px; }
.toast.err { background: var(--red); } .toast.ok { background: var(--green); color: #fff; }

/* ---------- حالات الرصيد ---------- */
.st-need { color: var(--red); font-weight: 600; } .st-near { color: #b8860b; font-weight: 600; } .st-ok { color: var(--green); } .st-idle { color: #7f7f7f; } .st-none { color: #aaa; }
.tabs { display: flex; gap: 4px; border-bottom: 2px solid var(--sky); margin-bottom: 12px; flex-wrap: wrap; }
.tabs button { background: transparent; border: 0; padding: 7px 14px; border-radius: 6px 6px 0 0; color: var(--navy); }
.tabs button.on { background: var(--navy); color: #fff; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { background: var(--pale); color: var(--navy); border-radius: 12px; padding: 1px 10px; font-size: 12.5px; display: inline-flex; gap: 6px; align-items: center; }
.chip button { border: 0; background: none; color: var(--red); padding: 0; font-size: 14px; line-height: 1; }
.badge-perm { display: inline-block; margin: 2px; }

@media (max-width: 960px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; top: 52px; bottom: 0; right: 0; width: 250px; z-index: 40; transform: translateX(100%); transition: transform .15s; box-shadow: -4px 0 14px rgba(0,0,0,.2); }
  .side.open { transform: none; }
  .menu-toggle { display: inline-block; }
  .kpis { grid-template-columns: repeat(2, 1fr); } .charts, .grid2, .grid3 { grid-template-columns: 1fr; }
  .main { padding: 12px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
@media print { .side, .topbar, #toasts { display: none !important; } .shell { display: block; height: auto; } .main { overflow: visible; padding: 0; } body { background: #fff; } }

.sheet { overflow-x: auto; }
.sheet.wide { max-width: 1240px; }
.sheet.wide .lh, .sheet.wide .meta, .sheet.wide .ttl { max-width: 920px; margin-inline-end: auto; }
table.form td.st { white-space: normal; min-width: 230px; max-width: 330px; font-size: 12px; line-height: 1.35; padding: 2px 8px; }
table.form td { min-width: 60px; }
table.form td.n { min-width: 0; }
.formbar .muted { margin-inline-start: 8px; }
table.form { table-layout: fixed; }
table.form th.statuscol { width: 290px; background: transparent; border: 0; }
@media (max-width: 960px) {
  .filters > * { flex: 1 1 150px; }
  .filters select, .filters input { min-width: 0; width: 100%; }
  .page-title h2 { font-size: 18px; }
  .modal { padding: 14px; }
}
.grid2 > *, .grid3 > *, .charts > *, .kpis > * { min-width: 0; }
@media (max-width: 960px) {
  .topbar { gap: 8px; padding: 0 10px; }
  .topbar h1 { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
  .topbar .who { display: none; }
  .topbar button { padding: 3px 8px; font-size: 12.5px; white-space: nowrap; }
}
.shell > * { min-width: 0; }

/* ---------- نماذج الجوال: كل سطر بطاقة ---------- */
.mobonly { display: none; }
@media (max-width: 720px) {
  .mobonly { display: block; }
  .sheet { padding: 8px; border: 0; box-shadow: none; background: transparent; }
  .sheet .lh { display: none; }
  .sheet .meta { margin: 0 0 8px; }
  .sheet .ttl { font-size: 15px; margin: 0 0 8px; }
  table.form, table.form tbody { display: block; table-layout: auto; }
  table.form thead { display: none; }
  table.form tr { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 8px; background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px 8px; margin-bottom: 10px; }
  table.form tr.rowbad { border-color: #e6a0a0; }
  table.form tr.mobhide { display: none; }
  table.form td { display: block; border: 0; height: auto; padding: 0; min-width: 0; }
  table.form td::before { content: attr(data-l); display: block; font-size: 11.5px; color: var(--muted); margin-bottom: 2px; }
  table.form td.n { grid-column: 1 / -1; width: auto; background: transparent; text-align: right; font-weight: 700; color: var(--navy); padding: 0; }
  table.form td.n::before { content: 'سطر '; display: inline; margin: 0; font-size: inherit; color: inherit; }
  table.form td.c-item, table.form td.c-receiver { grid-column: 1 / -1; }
  table.form td input { border: 1px solid var(--line); border-radius: 6px; height: 40px; background: #fff; font-size: 16px; }
  table.form tr.rowbad td:not(.st):not(.n) { background: transparent; }
  table.form td.st { grid-column: 1 / -1; width: auto; min-width: 0; max-width: none; padding: 0; font-size: 12.5px; white-space: normal; }
  table.form td.st:empty { display: none; }
  table.form td.st::before { display: none; }
  .formbar { position: sticky; bottom: 0; background: var(--bg); padding: 8px 4px; margin: 6px 0 0; z-index: 5; justify-content: stretch; }
  .formbar .btn { flex: 1 1 auto; }
}

/* إصلاح: أسماء الرسوم البيانية كانت تُقصّ في الواجهة العربية (RTL) */
svg.chart { direction: ltr; }

/* قائمة اقتراحات البحث في لوحة التحكم */
.sugg { position: absolute; top: 100%; left: 0; right: 0; z-index: 50; display: none; background: #fff; border: 1px solid #c9d6e2; border-radius: 8px; box-shadow: 0 6px 18px rgba(0,0,0,.18); max-height: 320px; overflow: auto; margin-top: 2px; }
.sugg-item { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 11px 12px; border-bottom: 1px solid #eef2f6; cursor: pointer; font-size: 14px; color: #1b2733; }
.sugg-item:hover, .sugg-item:active { background: #e8f0f8; }
.sugg-item .sb { color: #667788; font-size: 12px; white-space: nowrap; }
.sugg-more { padding: 8px 12px; font-size: 12px; color: #667788; }
