:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e3e6eb;
  --text: #1b1f24;
  --muted: #69717d;
  --accent: #1f6f5c;
  --accent-soft: #e3f1ed;
  --pos: #18794e;
  --neg: #c0392b;
  --warn: #b7791f;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05);
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111418;
    --surface: #1a1e23;
    --surface-2: #22272e;
    --border: #2d333b;
    --text: #e6e9ed;
    --muted: #9aa3ae;
    --accent: #4fb89a;
    --accent-soft: #1d3530;
    --pos: #4cc38a;
    --neg: #f07167;
    --warn: #e0b04f;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  --bg: #111418; --surface: #1a1e23; --surface-2: #22272e; --border: #2d333b;
  --text: #e6e9ed; --muted: #9aa3ae; --accent: #4fb89a; --accent-soft: #1d3530;
  --pos: #4cc38a; --neg: #f07167; --warn: #e0b04f; --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  /* Same font on every device (closest free match to Apple's SF Arabic) */
  font-family: "Noto Sans Arabic", sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  min-height: 100vh;
  font-size: 15px;
}
.num, .kpi-value, input[type=number] { font-variant-numeric: tabular-nums; }

/* sidebar */
.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--surface);
  border-inline-end: 1px solid var(--border);
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 6px 20px; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 10px;
  background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 20px;
}
.brand-title { font-weight: 700; font-size: 17px; }
.brand-sub { color: var(--muted); font-size: 13px; }
nav { display: flex; flex-direction: column; gap: 2px; }
nav a {
  color: var(--text); text-decoration: none;
  padding: 10px 12px; border-radius: 8px; font-weight: 500;
}
nav a:hover { background: var(--surface-2); }
nav a.active { background: var(--accent-soft); color: var(--accent); }

main { flex: 1; min-width: 0; padding: 20px 28px 40px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.topbar h1 { margin: 0; font-size: 22px; }
.month-bar { display: flex; align-items: center; gap: 8px; }
.month-label { position: relative; display: flex; align-items: center; cursor: pointer; padding: 6px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); min-width: 130px; justify-content: center; }
.month-label input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; }
.btn.icon { font-size: 18px; line-height: 1; padding: 6px 12px; }
.month-range { margin: -10px 0 14px; }
.closed-banner { background: color-mix(in srgb, var(--warn) 14%, transparent); color: var(--warn); border: 1px solid color-mix(in srgb, var(--warn) 35%, transparent); border-radius: 10px; padding: 10px 14px; margin-bottom: 16px; font-weight: 500; }
.closed-banner a { color: inherit; }
.badge.closed { background: var(--surface-2); color: var(--muted); }
.badge.open { background: color-mix(in srgb, var(--pos) 15%, transparent); color: var(--pos); }
tr.current-month td { background: var(--accent-soft); }
#monthsBody a { color: var(--text); font-weight: 600; text-decoration: none; }
#monthsBody a:hover { color: var(--accent); }
fieldset[disabled], form.locked { opacity: .55; pointer-events: none; }

/* cards */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 18px; margin-bottom: 16px;
  box-shadow: var(--shadow); min-width: 0;
}
.card.narrow { max-width: 480px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h2 { margin: 0; font-size: 16px; }
.grid-2 { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; }
.grid-2 > .card { margin-bottom: 0; }
.grid-2 + .grid-2 { margin-top: 16px; }
.grid-2:last-child { margin-bottom: 16px; }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.kpi-label { color: var(--muted); font-size: 13px; margin-bottom: 6px; }
.kpi-value { font-size: 24px; font-weight: 700; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.chart-box { position: relative; height: 280px; }

.payroll-mini { display: flex; flex-direction: column; gap: 12px; }
.payroll-mini > div:not(.progress) { display: flex; justify-content: space-between; }
.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .3s; }

.breakdown { display: flex; flex-direction: column; gap: 10px; }
.breakdown-row { display: grid; grid-template-columns: 110px 1fr auto; gap: 10px; align-items: center; font-size: 14px; }
.breakdown-bar { height: 10px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.breakdown-bar > div { height: 100%; background: var(--neg); opacity: .8; border-radius: 99px; }

/* tables */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 10px 6px; border-bottom: 1px solid var(--border); text-align: start; white-space: nowrap; }
.table th { color: var(--muted); font-weight: 500; font-size: 13px; }
.table td.note { white-space: normal; color: var(--muted); }
.table tfoot td { font-weight: 700; border-bottom: none; }
.table .num { text-align: end; }
.table.compact td { padding: 8px 4px; font-size: 14px; }
.table input[type=number] { width: 84px; padding: 6px 8px; }
.empty { text-align: center; color: var(--muted); padding: 24px !important; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }

.badge { display: inline-block; padding: 2px 10px; border-radius: 99px; font-size: 12px; font-weight: 600; }
.badge.income, .badge.paid, .badge.active { background: color-mix(in srgb, var(--pos) 15%, transparent); color: var(--pos); }
.badge.expense { background: color-mix(in srgb, var(--neg) 15%, transparent); color: var(--neg); }
.badge.pending, .badge.inactive { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }

/* forms */
input, select, button { font: inherit; color: inherit; }
input, select {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.form-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.form-row label, .form-col label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.form-row label.grow { flex: 1; min-width: 180px; }
.form-row label.check { flex-direction: row; align-items: center; gap: 6px; padding-bottom: 10px; color: var(--text); }
.form-col { display: flex; flex-direction: column; gap: 12px; }
.form-actions { display: flex; gap: 8px; }

.btn {
  border: 1px solid transparent; border-radius: 8px; padding: 8px 16px;
  cursor: pointer; font-weight: 600; text-decoration: none; text-align: center;
  background: var(--surface-2);
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn.danger { color: var(--neg); background: transparent; border-color: var(--border); }
.btn:hover { filter: brightness(.96); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* login */
.login-body { align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; padding: 24px; }
.login-card .brand { padding: 0 0 6px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.login-error { color: var(--neg); font-size: 14px; }
.sidebar-user { margin-top: 24px; padding: 12px 6px 0; border-top: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 14px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 8px;
  font-weight: 500; z-index: 10; max-width: calc(100% - 32px);
}
.toast.error { background: var(--neg); color: #fff; }

@media (max-width: 1000px) {
  .grid-2 { grid-template-columns: 1fr; }
  .grid-2 > .card { margin-bottom: 0; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 720px) {
  body { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; padding: 12px 16px; border-inline-end: none; border-bottom: 1px solid var(--border); }
  .brand { padding-bottom: 10px; }
  nav { flex-direction: row; overflow-x: auto; }
  nav a { white-space: nowrap; }
  main { padding: 16px; }
  .kpi-value { font-size: 20px; }
  .form-row label { flex: 1 1 140px; }
}
