:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-2: #f8faf9;
  --border: #e2e7e5;
  --text: #17201d;
  --muted: #64716c;
  --accent: #d9520a;
  --accent-ink: #ffffff;
  --accent-soft: #fff0e6;
  --navy: #0b1f3a;
  --c-delivered: #2a6fdb;
  --c-returned: #e8890c;
  --c-revenue: #d9520a;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn: #a15c07;
  --warn-soft: #fdf3e2;
  --ok: #157f3c;
  --ok-soft: #e5f5eb;
  --info: #1d5fb8;
  --info-soft: #e7effb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 20, .06), 0 2px 8px rgba(16, 24, 20, .04);
  --font: "Segoe UI", Tahoma, "Noto Sans Arabic", "Noto Sans", system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101513; --surface: #171d1b; --surface-2: #1c2422; --border: #2a3431; --text: #e6ece9; --muted: #95a39e;
    --accent: #ff8a3d; --accent-ink: #2a1203; --accent-soft: #3a2314; --navy: #e6ece9; --c-delivered: #4a86e8; --c-returned: #c97f0a; --c-revenue: #ff8a3d; --danger: #f97066; --danger-soft: #3a1b18;
    --warn: #f5b041; --warn-soft: #36290f; --ok: #4ac27a; --ok-soft: #16301f; --info: #73a6f0; --info-soft: #172741;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 15px; line-height: 1.5; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0 0 .6em; line-height: 1.25; }
h1 { font-size: 1.45rem; } h2 { font-size: 1.15rem; } h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }
.ltr { direction: ltr; unicode-bidi: isolate; }
.hidden { display: none !important; }

/* Layout */
.shell { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.sidebar { background: var(--surface); border-inline-end: 1px solid var(--border); padding: 16px 12px; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 16px; }
.brand-logo { width: 40px; height: 42px; flex: none; }
.brand-name { font-weight: 900; font-size: 1.25rem; letter-spacing: 1.5px; text-transform: uppercase; color: var(--navy); line-height: 1.1; }
.brand-tag { font-size: .68rem; font-weight: 700; letter-spacing: 1px; color: var(--accent); }
.auth-card .brand-logo, .track-box .brand-logo { width: 72px; height: 75px; }
.auth-card .brand-name, .track-box .brand-name { font-size: 2.2rem; }
.auth-card .brand-tag, .track-box .brand-tag { font-size: .8rem; }
.courier-pick { padding: 4px 8px; font-size: .88rem; max-width: 170px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 8px; color: var(--text); margin-bottom: 2px; }
.nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav .sep { height: 1px; background: var(--border); margin: 10px 4px; }
.main { min-width: 0; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
.topbar .spacer { flex: 1; }
.menu-btn { display: none; }
.content { padding: 24px; max-width: 1400px; }
@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; width: 260px; z-index: 20; transform: translateX(110%); transition: transform .2s; box-shadow: var(--shadow); }
  [dir="ltr"] .sidebar { transform: translateX(-110%); }
  .sidebar.open { transform: none !important; }
  .menu-btn { display: inline-flex; }
  .content { padding: 14px; }
  .topbar { padding: 10px 14px; }
}

/* Components */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin-bottom: 16px; }
.card > h2:first-child, .card > h3:first-child { margin-top: 0; }
.grid { display: grid; gap: 14px; }
.stats { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: .85rem; }
.stat .value { font-size: 1.5rem; font-weight: 700; margin-top: 2px; }
.stat.accent { border-color: var(--accent); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.between { justify-content: space-between; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { margin: 0; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 8px; padding: 8px 14px; font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.ok { color: var(--ok); border-color: var(--ok); }
.btn.warn { color: var(--warn); border-color: var(--warn); }
.btn.small { padding: 4px 10px; font-size: .85rem; }
.btn.big { padding: 12px 16px; font-size: 1rem; flex: 1; }
.btn.ghost { border-color: transparent; background: transparent; }

label.field { display: flex; flex-direction: column; gap: 4px; font-size: .88rem; color: var(--muted); }
label.field > span.req::after { content: " *"; color: var(--danger); }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; min-width: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
textarea { min-height: 80px; resize: vertical; }
input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--accent); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px 16px; }
.form-grid .wide { grid-column: 1 / -1; }
.check { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: .95rem; }

.filters { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; margin-bottom: 12px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 12px; text-align: start; border-bottom: 1px solid var(--border); vertical-align: top; white-space: nowrap; }
th { background: var(--surface-2); color: var(--muted); font-size: .82rem; font-weight: 600; position: sticky; top: 0; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }
td.wrap { white-space: normal; min-width: 180px; }
tfoot td { font-weight: 700; background: var(--surface-2); }

.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: .8rem; font-weight: 600; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.badge.new { background: var(--info-soft); color: var(--info); border-color: transparent; }
.badge.received { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge.out_for_delivery { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.delivered { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.postponed { background: var(--warn-soft); color: var(--warn); border-color: var(--warn); }
.badge.returned { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge.cancelled { background: var(--surface-2); color: var(--muted); text-decoration: line-through; }
.badge.off { background: var(--danger-soft); color: var(--danger); border-color: transparent; }

.bulkbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; padding: 10px 12px; background: var(--accent-soft); border-radius: var(--radius); margin-bottom: 10px; }
.pager { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-top: 10px; flex-wrap: wrap; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { position: relative; padding-inline-start: 22px; padding-bottom: 14px; }
.timeline li::before { content: ""; position: absolute; inset-inline-start: 5px; top: 7px; width: 9px; height: 9px; border-radius: 50%; background: var(--accent); }
.timeline li::after { content: ""; position: absolute; inset-inline-start: 9px; top: 18px; bottom: 0; width: 1px; background: var(--border); }
.timeline li:last-child::after { display: none; }

dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; overflow-wrap: anywhere; }
.two-col { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; }
@media (max-width: 900px) { .two-col { grid-template-columns: 1fr; } }

/* Courier cards */
.dcard { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; box-shadow: var(--shadow); }
.dcard .top { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.dcard .amount { font-size: 1.3rem; font-weight: 700; }
.dcard .actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }

/* Modal */
.overlay { position: fixed; inset: 0; background: rgba(10, 15, 13, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--surface); border-radius: 14px; width: min(560px, 100%); max-height: calc(100vh - 32px); overflow-y: auto; padding: 20px; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.modal.wide { width: min(900px, 100%); }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }

#toasts { position: fixed; bottom: 16px; inset-inline-start: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); max-width: 380px; }
.toast.error { background: var(--danger); color: #fff; }

/* Auth & public */
.center-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card { width: min(400px, 100%); }
.auth-card .brand { justify-content: center; }
.track-box { width: min(620px, 100%); }
.scan-input { font-size: 1.3rem; padding: 12px; direction: ltr; text-align: center; letter-spacing: 1px; }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.error-list { color: var(--danger); margin: 8px 0; padding-inline-start: 20px; max-height: 220px; overflow-y: auto; }

/* Printing */
#print-area { display: none; }
@media print {
  @page { size: A4; margin: 8mm; }
  body { background: #fff; color: #000; }
  #app, #toasts, .overlay { display: none !important; }
  #print-area { display: block; }
  .waybill { border: 1.5px solid #000; border-radius: 6px; padding: 8px 10px; height: 136mm; margin-bottom: 6mm; page-break-inside: avoid; break-inside: avoid; display: flex; flex-direction: column; font-size: 12.5px; color: #000; }
  .waybill:nth-child(2n) { page-break-after: always; break-after: page; }
  .statement { color: #000; font-size: 12px; }
  .statement table th { background: #eee; color: #000; position: static; }
  .statement th, .statement td { border: 1px solid #999; padding: 4px 6px; white-space: normal; }
}
.waybill .wb-head { display: flex; justify-content: space-between; align-items: center; border-bottom: 1.5px solid #000; padding-bottom: 6px; }
.waybill .wb-brand { display: flex; align-items: center; gap: 8px; }
.waybill .wb-brand img { width: 34px; height: 36px; }
.waybill .wb-company { font-size: 18px; font-weight: 900; letter-spacing: 1px; text-transform: uppercase; }
.waybill .wb-barcode { text-align: center; padding: 6px 0 2px; }
.waybill .wb-barcode svg { height: 54px; width: 80%; }
.waybill .wb-track { font-size: 18px; font-weight: 800; letter-spacing: 2px; text-align: center; direction: ltr; }
.waybill .wb-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0; border: 1px solid #000; margin-top: 6px; }
.waybill .wb-grid > div { padding: 5px 7px; border-bottom: 1px solid #000; }
.waybill .wb-grid > div:nth-child(odd) { border-inline-end: 1px solid #000; }
.waybill .wb-label { font-size: 10.5px; color: #444; }
.waybill .wb-val { font-weight: 700; font-size: 13.5px; }
.waybill .wb-cod { margin-top: auto; border: 2px solid #000; padding: 6px 10px; display: flex; justify-content: space-between; align-items: center; font-size: 15px; font-weight: 800; }
.waybill .wb-cod .big { font-size: 24px; }
.waybill .wb-foot { display: flex; justify-content: space-between; font-size: 10.5px; margin-top: 4px; }

/* ---------- Navigation groups ---------- */
.nav-group { font-size: .72rem; font-weight: 700; letter-spacing: .5px; color: var(--muted); text-transform: uppercase; padding: 14px 12px 4px; }
.nav-group:first-child { padding-top: 0; }

/* ---------- Alerts panel ---------- */
.alerts .all-good { color: var(--ok); font-weight: 600; padding: 6px 0; }
.alert-group { border-top: 1px solid var(--border); padding: 8px 0; }
.alert-group summary { cursor: pointer; font-weight: 600; list-style: none; display: flex; align-items: center; gap: 8px; }
.alert-group summary::-webkit-details-marker { display: none; }
.alert-group > a { display: flex; align-items: center; gap: 8px; font-weight: 600; color: var(--text); }
.alert-count { min-width: 28px; height: 24px; padding: 0 8px; border-radius: 999px; background: var(--warn-soft); color: var(--warn); display: inline-grid; place-items: center; font-size: .82rem; font-weight: 700; }
.alert-list { margin: 6px 0 0; padding-inline-start: 36px; color: var(--text); font-size: .92rem; }
.alert-list li { margin: 3px 0; }

/* ---------- Reports ---------- */
.range-bar { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; justify-content: space-between; }
.seg { display: inline-flex; flex-wrap: wrap; gap: 4px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; }
.seg-btn { border: 0; background: transparent; color: var(--muted); font: inherit; font-weight: 600; padding: 6px 12px; border-radius: 7px; cursor: pointer; }
.seg-btn.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.kpis { margin-bottom: 16px; }
.kpi .hint { color: var(--muted); font-size: .76rem; margin-top: 4px; line-height: 1.35; }
.delta { font-size: .8rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.delta.good { background: var(--ok-soft); color: var(--ok); }
.delta.bad { background: var(--danger-soft); color: var(--danger); }
.insights ul { margin: 0 0 10px; padding-inline-start: 20px; }
.insights li { margin: 6px 0; line-height: 1.6; }
.chart-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 1000px) { .chart-grid { grid-template-columns: 1fr; } }
.chart { position: relative; direction: ltr; }
.chart-svg { width: 100%; height: auto; display: block; }
.chart-svg .grid { stroke: var(--border); stroke-width: 1; }
.chart-svg .axis { stroke: var(--muted); stroke-width: 1; }
.chart-svg .tick { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.chart-svg .hit:hover { fill: rgba(127, 127, 127, .08); }
.chart-tip { position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: .82rem; box-shadow: 0 4px 16px rgba(0,0,0,.15); min-width: 150px; z-index: 2; }
.legend { display: flex; gap: 16px; font-size: .85rem; color: var(--muted); margin-bottom: 6px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-inline-end: 6px; vertical-align: middle; }
.rate.good { color: var(--ok); font-weight: 600; }
.rate.bad { color: var(--danger); font-weight: 600; }
.pl table { max-width: 520px; }
.pl td { white-space: normal; }
.pl tr.sum td { font-weight: 700; border-top: 2px solid var(--border); }
.pl tr.sub td { color: var(--muted); font-size: .9rem; padding-inline-start: 28px; }
.pl tr.total td { font-weight: 800; font-size: 1.1rem; }
.pl tr.total.pos td { color: var(--ok); }
.pl tr.total.neg td { color: var(--danger); }

/* ---------- Expenses ---------- */
.hbar { margin-bottom: 10px; font-size: .9rem; }
.hbar-track { height: 8px; background: var(--surface-2); border-radius: 4px; margin-top: 4px; overflow: hidden; }
.hbar-fill { height: 100%; background: var(--accent); border-radius: 4px; }

/* ---------- Delivery / proof / exchange ---------- */
.exchange-note { display: inline-block; background: var(--info-soft); color: var(--info); border-radius: 6px; padding: 2px 8px; font-weight: 600; margin-top: 4px; }
.sig-pad { width: 100%; height: 150px; border: 1px dashed var(--border); border-radius: 8px; background: #fff; touch-action: none; }
.pod-preview { max-width: 100%; max-height: 200px; border-radius: 8px; }
.pod-row { display: flex; gap: 12px; flex-wrap: wrap; }
.pod-img { max-width: 220px; max-height: 220px; border-radius: 8px; border: 1px solid var(--border); background: #fff; }
.btn.wa { color: #128c4a; border-color: #128c4a; }
.warn-stat { border-color: var(--danger); }
.warn-stat .value { color: var(--danger); }

/* ---------- Integration ---------- */
.copy-line { margin: 10px 0; }
.copy-line code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; font-size: .8rem; overflow-wrap: anywhere; flex: 1; }
pre.code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 12px; overflow-x: auto; font-size: .8rem; text-align: left; }
.steps p { line-height: 1.7; }
.warn-text { color: var(--warn); font-weight: 600; }
.sticky-save { position: sticky; bottom: 0; background: var(--bg); padding: 12px 0; }

/* ---------- Route sheet (print) ---------- */
.manifest .wb-head { display: flex; justify-content: space-between; align-items: center; border-bottom: 1.5px solid #000; padding-bottom: 6px; margin-bottom: 8px; }
.manifest .wb-brand { display: flex; align-items: center; gap: 8px; }
.manifest .wb-brand img { width: 34px; height: 36px; }
.manifest .wb-company { font-size: 18px; font-weight: 900; }
.manifest-foot { display: flex; justify-content: space-between; gap: 12px; margin-top: 14px; font-size: 13px; flex-wrap: wrap; }
