:root {
  --bg: #f4f6f9; --panel: #fff; --text: #1d2330; --muted: #6b7385; --border: #e3e7ee;
  --primary: #c0151b; --primary-weak: #fde8e9; --side: #111827; --side-text: #c9cfdb;
  --ok: #138a4b; --ok-bg: #e3f6ec; --warn: #a86400; --warn-bg: #fff3dd;
  --bad: #c62828; --bad-bg: #fde8e8; --info: #1f5eff; --info-bg: #e8eeff; --gray-bg: #eef0f4;
  --radius: 10px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: 'Cairo', system-ui, sans-serif; background: var(--bg); color: var(--text); font-size: 14px; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); text-decoration: none; }

/* Layout */
.layout { display: flex; min-height: 100vh; }
.sidebar { width: 230px; background: var(--side); color: var(--side-text); padding: 16px 10px; flex-shrink: 0;
  position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { color: #fff; font-weight: 700; font-size: 17px; padding: 4px 10px 16px; display: flex; gap: 8px; align-items: center; }
.brand-mark { width: 34px; height: 34px; border-radius: 8px; background: #fff; padding: 3px; }
.login-logo { text-align: center; margin-bottom: 18px; }
.login-logo img { width: 100%; max-width: 320px; height: auto; }
.login-logo h1 { margin: 8px 0 0; font-size: 26px; color: #0f5132; }
.nav-group { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: #7c8496; padding: 14px 10px 4px; }
.nav a { display: block; color: var(--side-text); padding: 7px 12px; border-radius: 7px; margin: 1px 0; }
.nav a:hover { background: #1f2937; color: #fff; }
.nav a.active { background: var(--primary); color: #fff; }
.main { flex: 1; min-width: 0; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 24px;
  background: var(--panel); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
.topbar h1 { font-size: 18px; margin: 0; }
.topbar .right { display: flex; gap: 8px; align-items: center; }
.menu-btn { display: none; }
.content { padding: 20px 24px 40px; }

/* Controls */
.btn { border: 1px solid var(--border); background: var(--panel); padding: 7px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: #c5ccd8; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 3px 9px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
input, select, textarea { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; background: #fff; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-weak); border-color: var(--primary); }
textarea { min-height: 70px; resize: vertical; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }

.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar input, .toolbar select { max-width: 260px; }
.toolbar .spacer { flex: 1; }

/* Cards / tables */
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); }
.card h3 { margin: 0; padding: 12px 16px; font-size: 15px; border-bottom: 1px solid var(--border); }
.card .body { padding: 14px 16px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 12px; text-align: start; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: #fafbfc; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fafbff; }
td.num, th.num { text-align: end; font-variant-numeric: tabular-nums; }
td .row-actions { display: flex; gap: 4px; }
.empty { padding: 40px; text-align: center; color: var(--muted); }

.badge { display: inline-block; padding: 2px 10px; border-radius: 99px; font-size: 12px; font-weight: 600; background: var(--gray-bg); color: var(--muted); }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.info { background: var(--info-bg); color: var(--info); }

/* Dashboard */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.kpi .label { color: var(--muted); font-size: 13px; }
.kpi .value { font-size: 24px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi.alert .value { color: var(--bad); }
.grid2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 16px; margin-bottom: 16px; }
.chart { display: flex; align-items: flex-end; gap: 14px; height: 200px; padding: 10px 4px 0; }
.chart .m { flex: 1; display: flex; flex-direction: column; align-items: center; height: 100%; }
.chart .bars { flex: 1; display: flex; align-items: flex-end; gap: 4px; width: 100%; justify-content: center; }
.chart .bar { width: 40%; max-width: 26px; border-radius: 4px 4px 0 0; min-height: 2px; }
.chart .bar.r { background: var(--primary); } .chart .bar.e { background: #f0a020; }
.chart .lbl { font-size: 11px; color: var(--muted); margin-top: 6px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-inline-end: 5px; vertical-align: middle; }

/* Modal */
.overlay { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; overflow-y: auto; z-index: 50; }
.modal { background: var(--panel); border-radius: 12px; width: 100%; max-width: 720px; box-shadow: 0 20px 50px rgba(0,0,0,.2); }
.modal.wide { max-width: 980px; }
.modal header { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal header h2 { margin: 0; font-size: 17px; }
.modal .body { padding: 16px 18px; }
.modal footer { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); }
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px 14px; }
.form-grid .full { grid-column: 1 / -1; }
.lines { margin-top: 16px; }
.lines h4 { margin: 0 0 8px; font-size: 14px; }
.lines td { padding: 4px; border: 0; }
.lines th { background: none; padding: 4px; }
.lines .line-total { padding: 0 8px; }
.totals { text-align: end; margin-top: 8px; font-weight: 700; }
.err { color: var(--bad); background: var(--bad-bg); padding: 8px 12px; border-radius: 8px; margin-bottom: 12px; }

.detail-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px 18px; margin-bottom: 14px; }
.detail-grid .k { color: var(--muted); font-size: 12px; }
.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { padding: 6px 0 6px 0; padding-inline-start: 18px; position: relative; border-inline-start: 2px solid var(--border); margin-inline-start: 6px; }
.timeline li::before { content: ''; position: absolute; inset-inline-start: -7px; top: 12px; width: 10px; height: 10px; border-radius: 50%; background: var(--primary); }
.timeline .when { color: var(--muted); font-size: 12px; margin-inline-start: 8px; }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px;
  background: linear-gradient(135deg, #111827 0%, #7a0e12 100%); }
.login-box { background: #fff; border-radius: 14px; padding: 28px; width: 100%; max-width: 380px; }
.login-box h1 { margin: 0 0 18px; font-size: 22px; display: flex; gap: 10px; align-items: center; }
.login-box .field { margin-bottom: 12px; }
.login-box hr { border: 0; border-top: 1px solid var(--border); margin: 22px 0 16px; }
.lang-switch { position: fixed; top: 14px; inset-inline-end: 14px; }

#toast { position: fixed; bottom: 20px; inset-inline-start: 50%; transform: translateX(-50%); z-index: 100; }
[dir=rtl] #toast { transform: translateX(50%); }
#toast div { background: #111827; color: #fff; padding: 10px 18px; border-radius: 8px; margin-top: 6px; box-shadow: 0 6px 20px rgba(0,0,0,.2); }
#toast div.bad { background: var(--bad); }

@media (max-width: 900px) {
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  [dir=rtl] .sidebar { transform: translateX(100%); }
  .sidebar.open { transform: none !important; }
  .menu-btn { display: inline-block; }
  .grid2, .form-grid, .detail-grid { grid-template-columns: 1fr; }
  .content, .topbar { padding-inline: 16px; }
  .toolbar input, .toolbar select { max-width: none; }
}

@media print {
  .sidebar, .topbar, .toolbar { display: none !important; }
}

/* ---------- Logistics department ---------- */
.project-pick { width: auto; max-width: 280px; font-weight: 600; border-color: var(--primary); }
.page-intro { color: var(--muted); margin: 0 0 12px; }
.muted { color: var(--muted); font-size: 13px; }
.kpi .sub { color: var(--muted); font-size: 12px; margin-top: 4px; }
.kpi .value small { font-size: 13px; font-weight: 400; color: var(--muted); }
.kpis.two { grid-template-columns: repeat(2, 1fr); }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--panel); }
.seg button { border: 0; background: none; padding: 7px 14px; font: inherit; font-weight: 600; cursor: pointer; color: var(--text); }
.seg button.on { background: var(--primary); color: #fff; }
.hbars { display: grid; gap: 14px; }
.hbar { display: grid; grid-template-columns: 90px 1fr auto; gap: 12px; align-items: center; }
.hbar .track { height: 8px; background: #e7e5e4; border-radius: 4px; overflow: hidden; }
.hbar .track i { display: block; height: 100%; border-radius: 4px; }
.hbar .val { font-weight: 600; font-variant-numeric: tabular-nums; font-size: 13px; direction: ltr; unicode-bidi: isolate; }
.donut-wrap { display: flex; align-items: center; gap: 24px; flex-wrap: wrap; justify-content: center; }
.donut { width: 140px; height: 140px; border-radius: 50%; display: grid; place-items: center; position: relative; flex-shrink: 0; }
.donut::after { content: ''; position: absolute; inset: 22px; background: var(--panel); border-radius: 50%; }
.donut span { position: relative; z-index: 1; font-weight: 700; }
.legend-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; font-size: 14px; }
.legend-list i { display: inline-block; width: 11px; height: 11px; border-radius: 2px; margin-inline-end: 6px; vertical-align: middle; }
.cols { display: flex; align-items: flex-end; gap: 6px; height: 140px; padding-top: 8px; }
.cols .col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; }
.cols .bar { width: 100%; max-width: 26px; background: #e06a70; border-radius: 3px 3px 0 0; min-height: 2px; }
.cols span { font-size: 11px; color: var(--muted); margin-top: 4px; }
.attention { list-style: none; margin: 0; padding: 0; }
.attention li { display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--border); }
.attention li::before { content: '●'; color: #8a6d1c; margin-inline-end: 8px; }
.attention li b { flex: 1; }
tr.expander { cursor: pointer; }
tr.sub-rows > td { padding: 0 0 0 24px; background: #fafaf9; }
table.inner td { border-bottom: 1px dashed var(--border); font-size: 13px; }
#print-area { display: none; }
.print-head { display: flex; gap: 16px; align-items: center; margin-bottom: 16px; }
.print-head img { height: 48px; }
.print-head h1 { margin: 0; font-size: 20px; }
#print-area section { margin-bottom: 24px; }
#print-area h2 { font-size: 17px; border-bottom: 2px solid var(--primary); padding-bottom: 4px; }
@media print {
  body.printing .layout, body.printing #modal-root, body.printing #toast { display: none !important; }
  body.printing #print-area { display: block; }
  #print-area section { break-before: page; }
  #print-area section:first-of-type { break-before: auto; }
  .card, .kpi { break-inside: avoid; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}.mini-track { display: inline-block; width: 70px; height: 6px; background: #e7e5e4; border-radius: 3px; overflow: hidden; vertical-align: middle; margin-inline-end: 6px; }
.mini-track i { display: block; height: 100%; background: var(--primary); }
.attention.links li::before { color: var(--primary); }
.attention.links a { color: inherit; flex: 1; }
.attention li span { color: var(--muted); font-size: 13px; }
/* ---------- Project overview ---------- */
.daily-card { margin-bottom: 16px; }
.rates { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.rate b { font-size: 22px; margin-inline-start: 8px; }
.rate .dot { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-inline-end: 6px; }
.scurve { width: 100%; height: auto; }
.scurve text { font-size: 11px; fill: var(--muted); }
.scurve .grid { stroke: #e7e5e4; stroke-width: 1; }
.scope-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-top: 18px; }
.scope-head { display: flex; justify-content: space-between; margin-bottom: 8px; }
.scope-row { display: grid; grid-template-columns: 34px 1fr 52px; gap: 6px; align-items: center; font-size: 13px; margin: 5px 0; }
.scope-row .track { height: 6px; background: #e7e5e4; border-radius: 3px; overflow: hidden; }
.scope-row .track i { display: block; height: 100%; }
.scope-row b { text-align: end; font-variant-numeric: tabular-nums; }
.timeline-bar { position: relative; height: 130px; margin: 6px 10px; }
.tl-line, .tl-done { position: absolute; top: 64px; height: 4px; border-radius: 2px; left: 0; }
.tl-line { right: 0; background: #d6d3d1; }
.tl-done { background: var(--primary); }
.tl-today { position: absolute; top: 52px; height: 28px; border-left: 2px solid var(--primary); }
.tl-today span { position: absolute; top: 30px; left: -16px; color: var(--primary); font-weight: 700; font-size: 12px; }
.tl-ev { position: absolute; top: 59px; transform: translateX(-7px); }
.tl-ev i { display: block; width: 14px; height: 14px; border-radius: 50%; background: #4f7a5a; border: 2px solid #fff; }
.tl-ev.fc i { background: #fff; border: 2px solid var(--primary); }
.tl-ev.ipa i { background: #4f7a5a; }
.tl-label { position: absolute; left: -40px; width: 110px; font-size: 12px; line-height: 1.3; }
.tl-label span { display: block; color: var(--muted); }
.tl-ev.above .tl-label { bottom: 22px; }
.tl-ev.below .tl-label { top: 22px; }
.section-title { margin: 22px 0 10px; font-size: 13px; letter-spacing: .06em; color: var(--muted); }
.dept-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.dept-card { display: block; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; color: inherit; }
.dept-card:hover { border-color: var(--primary); }
.dept-title { font-weight: 700; margin-bottom: 8px; }
.dept-row { display: flex; justify-content: space-between; font-size: 13px; padding: 3px 0; color: var(--muted); }
.dept-row b { color: var(--text); }
@media (max-width: 900px) { .rates, .scope-grid { grid-template-columns: 1fr; } }
.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 4px 12px; max-height: 180px; overflow: auto; border: 1px solid var(--border); border-radius: 8px; padding: 8px; }
.checks label { display: flex; gap: 6px; align-items: center; font-weight: 400; margin: 0; }
.checks input { width: auto; }

.guide { margin-bottom: 12px; }
.guide summary { padding: 14px 16px; font-weight: 700; cursor: pointer; font-size: 16px; }
.guide ol { margin: 0; padding: 0 40px 16px; line-height: 1.9; }

.invoice-summary { display: flex; flex-wrap: wrap; gap: 8px 22px; background: var(--primary-weak); border-radius: 8px; padding: 10px 14px; }
tr.overdue td { background: #fff1f1; }
tr.overdue td:first-child { box-shadow: inset 3px 0 0 var(--bad); }

.kpi-link { display: block; color: inherit; text-decoration: none; }
.kpi-link:hover { border-color: var(--primary); }

/* Phones: tables scroll inside their card instead of widening the page; the top bar wraps. */
.grid2 > *, .main, .content { min-width: 0; }
.table-wrap { overflow-x: auto; }
@media (max-width: 700px) {
  .topbar { flex-wrap: wrap; gap: 8px; height: auto; padding-block: 8px; }
  .topbar > div:first-child { flex: 1 1 100%; flex-wrap: wrap; }
  .topbar h1 { font-size: 17px; }
  .topbar .right { flex: 1 1 100%; justify-content: flex-end; }
  .topbar .right > span { display: none; }
  .project-pick { flex: 1; max-width: none; min-width: 0; }
  .kpis { grid-template-columns: 1fr 1fr; gap: 8px; }
  .kpi { padding: 10px 12px; }
  .kpi .value { font-size: 20px; }
  .content { padding: 12px; }
}
/* ---------- Night theme ---------- */
:root[data-theme="dark"] {
  --bg: #0f1115; --panel: #181b21; --text: #e6e8ec; --muted: #9aa3b2; --border: #2a2f38;
  --primary: #e0343b; --primary-weak: #3a1c1e; --side: #0b0d10; --side-text: #b8c0cc;
  --ok: #4cc286; --ok-bg: #16301f; --warn: #e3a64a; --warn-bg: #33270f;
  --bad: #ff6b6b; --bad-bg: #3a1a1a; --info: #7aa2ff; --info-bg: #1b2440; --gray-bg: #22262e;
  color-scheme: dark;
}
:root[data-theme="dark"] input, :root[data-theme="dark"] select, :root[data-theme="dark"] textarea { background: #11141a; }
:root[data-theme="dark"] th { background: #1d2128; }
:root[data-theme="dark"] .login-box { background: var(--panel); }
:root[data-theme="dark"] .login-logo img { background: #fff; border-radius: 10px; padding: 8px; }
:root[data-theme="dark"] .hbar .track, :root[data-theme="dark"] .mini-track, :root[data-theme="dark"] .scope-row .track { background: #2a2f38; }
:root[data-theme="dark"] .scurve .grid { stroke: #2a2f38; }
:root[data-theme="dark"] .tl-line { background: #3a404b; }
:root[data-theme="dark"] .tl-ev i { border-color: var(--panel); }
:root[data-theme="dark"] .tl-ev.fc i { background: var(--panel); }
:root[data-theme="dark"] tr.sub-rows > td { background: #14171c; }
:root[data-theme="dark"] tr.overdue td { background: #2c1717; }
:root[data-theme="dark"] .donut::after { background: var(--panel); }
:root[data-theme="dark"] .modal, :root[data-theme="dark"] .card, :root[data-theme="dark"] .kpi { background: var(--panel); }
:root[data-theme="dark"] .push-banner { background: #2e2610; border-color: #4a3d16; }
.lang-switch { display: flex; gap: 6px; }
/* ---------- Activity log ---------- */
.act-list { list-style: none; margin: 0; padding: 0; }
.act-day { font-weight: 700; color: var(--muted); font-size: 13px; padding: 12px 0 6px; border-bottom: 1px solid var(--border); }
.act { display: flex; gap: 10px; align-items: flex-start; padding: 9px 0; border-bottom: 1px dashed var(--border); }
.act-time { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; min-width: 42px; direction: ltr; }
.act-icon { width: 22px; text-align: center; }
.act-body { flex: 1; min-width: 0; }
.act-ref { color: var(--primary); }
.act-details { font-size: 12.5px; margin-top: 2px; overflow-wrap: anywhere; }
.drop { display: flex; flex-direction: column; gap: 6px; align-items: center; justify-content: center; padding: 34px; border: 2px dashed var(--border); border-radius: var(--radius); cursor: pointer; text-align: center; }
.drop span { font-size: 16px; font-weight: 600; color: var(--primary); }
.drop.over, .drop:hover { border-color: var(--primary); background: var(--primary-weak); }

tr.hl td { background: var(--warn-bg); }

/* Assistant chat */
.asst { display: flex; flex-direction: column; height: calc(100vh - 190px); min-height: 380px; }
.asst-log { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.asst-msg { max-width: 85%; padding: 9px 13px; border-radius: 12px; line-height: 1.6; overflow-wrap: anywhere; }
.asst-msg.me { align-self: flex-start; background: var(--primary); color: #fff; white-space: pre-wrap; }
.asst-msg.bot { align-self: flex-end; background: var(--bg); border: 1px solid var(--border); }
.asst-msg table { margin: 6px 0; font-size: 13px; }
.asst-gap { height: 6px; }
.asst-sql { margin-top: 6px; font-size: 12px; color: var(--muted); }
.asst-sql pre { white-space: pre-wrap; direction: ltr; text-align: left; background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: 6px; margin: 4px 0; }
.asst-hints { display: flex; flex-wrap: wrap; gap: 8px; margin: auto; justify-content: center; }
.asst-form { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--border); align-items: flex-end; }
.asst-form textarea { flex: 1; resize: vertical; }
.asst-actions { display: flex; flex-direction: column; gap: 6px; }
