:root {
  color-scheme: light;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f8f6;
  --border: #e3e3df;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --nav-bg: #14213d;
  --nav-text: #d7dbe6;
  --primary: #1c5cab;
  --primary-hover: #184f95;
  --gold: #b07a00;
  --good: #0a7d0a;
  --good-bg: #e7f6e7;
  --bad: #c22f2f;
  --bad-bg: #fbeaea;
  --warn: #8a5a00;
  --warn-bg: #fff4dc;
  --info-bg: #e8f1fc;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --grid: #e9e9e5;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Arabic", Arial, sans-serif;
  font-size: 15px;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #111110;
    --surface: #1a1a19;
    --surface-2: #222220;
    --border: #2e2e2b;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #9a998f;
    --nav-bg: #0c1426;
    --primary: #3987e5;
    --primary-hover: #5598e7;
    --gold: #e0a72a;
    --good: #4cc44c;
    --good-bg: #13301a;
    --bad: #f07070;
    --bad-bg: #3a1818;
    --warn: #f0b84a;
    --warn-bg: #352a10;
    --info-bg: #142640;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --grid: #2b2b28;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { min-height: 100vh; -webkit-font-smoothing: antialiased; }
a { color: var(--primary); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--good); }
.neg { color: var(--bad); }

/* boot */
.boot { position: fixed; inset: 0; display: grid; place-items: center; }
.spinner { width: 34px; height: 34px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* brand */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: .2px; }
.brand-lg { font-size: 20px; margin-bottom: 6px; }
.logo { width: 26px; height: 26px; border-radius: 7px; background: linear-gradient(135deg, #eda100, #b07a00); position: relative; flex: none; }
.logo::after { content: ""; position: absolute; inset: 7px; border: 2px solid #fff; border-radius: 3px; transform: rotate(45deg); }

/* auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: radial-gradient(circle at 20% 10%, #1d3260, var(--nav-bg) 60%); }
.auth-card { width: 100%; max-width: 400px; background: var(--surface); border-radius: 16px; padding: 28px; box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.auth-card label { display: block; margin: 14px 0 0; font-weight: 600; font-size: 13.5px; }
.auth-card input { margin-top: 6px; }
.auth-card .btn-block { margin-top: 20px; }
.auth-switch { text-align: center; margin: 16px 0 0; }

/* forms */
input, select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 11px; min-height: 40px;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; }
.form-grid .full { grid-column: 1 / -1; }
.form-grid label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; color: var(--text-2); }
.form-grid .hint { font-weight: 400; color: var(--muted); font-size: 12px; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
.form-error { margin-top: 12px; background: var(--bad-bg); color: var(--bad); padding: 9px 12px; border-radius: 8px; font-size: 13.5px; }
.req::after { content: " *"; color: var(--bad); }

/* buttons */
.btn { font: inherit; font-weight: 600; font-size: 14px; border-radius: 8px; padding: 8px 14px; min-height: 38px; border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { color: var(--bad); }
.btn-ghost { background: transparent; border-color: transparent; color: inherit; }
.btn-sm { min-height: 30px; padding: 4px 10px; font-size: 13px; }
.btn-block { width: 100%; }
.icon-btn { background: none; border: 0; font-size: 18px; cursor: pointer; color: var(--muted); padding: 6px; }

/* layout */
.app { display: grid; grid-template-columns: 250px 1fr; min-height: 100vh; }
.sidebar { background: var(--nav-bg); color: var(--nav-text); padding: 18px 12px; display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.sidebar .brand { color: #fff; padding: 4px 10px 18px; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.sidebar nav a { color: var(--nav-text); text-decoration: none; padding: 9px 10px; border-radius: 8px; display: flex; gap: 10px; align-items: center; font-size: 14px; }
.sidebar nav a i { font-style: normal; width: 18px; text-align: center; opacity: .8; }
.sidebar nav a:hover { background: rgba(255,255,255,.07); }
.sidebar nav a.active { background: rgba(255,255,255,.14); color: #fff; font-weight: 600; }
.nav-group { font-size: 11px; text-transform: uppercase; letter-spacing: .8px; opacity: .55; margin: 14px 10px 4px; }
.sidebar-foot { border-top: 1px solid rgba(255,255,255,.1); padding: 12px 10px 0; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.sidebar-foot .muted { color: #9aa3b8; }
.sidebar-foot .btn { color: #fff; }
.main { min-width: 0; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 16px 28px; position: sticky; top: 0; z-index: 5; background: var(--bg); border-bottom: 1px solid var(--border); }
.topbar h1 { font-size: 20px; margin: 0; flex: 1; }
.menu-btn { display: none; background: none; border: 0; font-size: 22px; color: var(--text); cursor: pointer; }
.page { padding: 22px 28px 60px; max-width: 1400px; }
.scrim { display: none; }

.sync { display: flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--muted); }
.sync .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--good); }
.sync.saving .dot { background: #eda100; animation: pulse 1s infinite; }
.sync.error .dot { background: var(--bad); }
.sync.error { color: var(--bad); }
@keyframes pulse { 50% { opacity: .3; } }

/* cards */
.grid { display: grid; gap: 16px; }
.kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); min-width: 0; }
.card h3 { margin: 0 0 12px; font-size: 15px; display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.card h3 .sub { font-weight: 400; font-size: 12.5px; color: var(--muted); }
.kpi .label { font-size: 12.5px; color: var(--text-2); font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.kpi .value { font-size: 24px; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; letter-spacing: -.3px; overflow-wrap: anywhere; }
.kpi .foot { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.kpi.hero { background: linear-gradient(135deg, #14213d, #1c3a6e); color: #fff; border: 0; }
.kpi.hero .label, .kpi.hero .foot { color: #c8d3ea; }
.section { margin-top: 22px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 26px 0 12px; flex-wrap: wrap; }
.section-head h2 { font-size: 17px; margin: 0; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.toolbar input, .toolbar select { width: auto; min-height: 36px; padding: 6px 10px; }

/* chart */
.chart-box { position: relative; height: 280px; }
.chart-box.tall { height: 340px; }
.chart-box.short { height: 220px; }
.empty { padding: 28px 12px; text-align: center; color: var(--muted); font-size: 14px; }

/* tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .4px; color: var(--text-2); font-weight: 600; background: var(--surface-2); position: sticky; top: 0; }
td.r, th.r { text-align: right; }
tr:hover td { background: var(--surface-2); }
tfoot td { font-weight: 700; border-top: 2px solid var(--border); }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; }

/* badges & alerts */
.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; font-weight: 700; padding: 3px 8px; border-radius: 999px; text-transform: uppercase; letter-spacing: .4px; white-space: nowrap; }
.badge-live { background: var(--good-bg); color: var(--good); }
.badge-live::before { content: "●"; font-size: 9px; }
.badge-manual { background: var(--warn-bg); color: var(--warn); }
.badge-manual::before { content: "✎"; }
.badge-bad { background: var(--bad-bg); color: var(--bad); }
.badge-good { background: var(--good-bg); color: var(--good); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
.badge-info { background: var(--info-bg); color: var(--primary); }
.badge-neutral { background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border); }
.alerts { display: flex; flex-direction: column; gap: 8px; }
.alert { padding: 10px 14px; border-radius: 10px; font-size: 14px; display: flex; gap: 10px; align-items: flex-start; }
.alert b { white-space: nowrap; }
.alert-danger { background: var(--bad-bg); color: var(--bad); }
.alert-warning { background: var(--warn-bg); color: var(--warn); }
.alert-info { background: var(--info-bg); color: var(--primary); }

/* gold price panel */
.price-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.price-tile { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; }
.price-tile .k { font-weight: 700; color: var(--gold); font-size: 13px; }
.price-tile .p { font-size: 21px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.price-meta { font-size: 12.5px; color: var(--muted); margin-top: 12px; line-height: 1.6; }

/* debt cards */
.debt-card .top { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.debt-card .name { font-weight: 700; font-size: 15px; }
.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; border: 1px solid var(--border); margin: 10px 0 6px; }
.progress > span { display: block; height: 100%; background: var(--series-1); border-radius: 99px; }
.kv { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-top: 10px; }
.kv div { font-size: 12px; color: var(--muted); }
.kv b { display: block; font-size: 14px; color: var(--text); font-variant-numeric: tabular-nums; }
.card-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 14px; }

/* tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 18px; overflow-x: auto; }
.tabs button { font: inherit; font-size: 14px; background: none; border: 0; border-bottom: 2px solid transparent; padding: 9px 12px; cursor: pointer; color: var(--text-2); white-space: nowrap; }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

/* modal */
.modal { position: fixed; inset: 0; background: rgba(10,14,25,.55); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-box { background: var(--surface); border-radius: 14px; width: 100%; max-width: 640px; max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.modal-box.wide { max-width: 980px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-body { padding: 18px 20px 20px; overflow-y: auto; }

/* toasts */
.toasts { position: fixed; bottom: 18px; right: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: #1d2433; color: #fff; padding: 10px 14px; border-radius: 10px; font-size: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.25); max-width: 360px; }
.toast.err { background: #8f1f1f; }

/* responsive */
@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .cols-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; width: 270px; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  .app.menu-open .sidebar { transform: none; }
  .app.menu-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 30; }
  .menu-btn { display: block; }
  .topbar { padding: 12px 16px; }
  .topbar h1 { font-size: 17px; }
  .page { padding: 16px 14px 60px; }
  .cols-2, .cols-3 { grid-template-columns: 1fr; }
  .sync .sync-text { display: none; }
}
@media (max-width: 560px) {
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi { padding: 13px; }
  .kpi .value { font-size: 18px; }
  .form-grid { grid-template-columns: 1fr; }
  .price-grid { grid-template-columns: 1fr; }
  .price-tile .p { font-size: 18px; }
  .kv { grid-template-columns: 1fr 1fr; }
  .modal { padding: 0; place-items: end stretch; }
  .modal-box { max-height: 92vh; border-radius: 14px 14px 0 0; }
  th, td { padding: 8px 7px; }
}

/* print (reports) */
@media print {
  .sidebar, .topbar, .no-print, .tabs, .toasts { display: none !important; }
  .app { display: block; }
  .page { padding: 0; }
  .card { box-shadow: none; break-inside: avoid; }
  body { background: #fff; }
}
