/* تصميم إدارة مدارج العلم: لوحة عمل هادئة بلون زمردي، أرقام واضحة، جداول قابلة للتمرير على الجوال */
:root {
  --bg: #eef2f1;
  --surface: #ffffff;
  --surface-2: #f4f8f7;
  --ink: #12201e;
  --muted: #5a6b68;
  --line: #d5dfdc;
  --accent: #0a6a60;
  --accent-ink: #ffffff;
  --accent-soft: #d9efeb;
  --good: #18794e;
  --good-soft: #dff3e8;
  --warn: #a45a00;
  --warn-soft: #fbeed8;
  --bad: #b3261e;
  --bad-soft: #fbe5e3;
  --shadow: 0 8px 28px rgba(10, 40, 36, .18);
  --f-display: "Reem Kufi", "IBM Plex Sans Arabic", Tahoma, sans-serif;
  --f-body: "IBM Plex Sans Arabic", system-ui, Tahoma, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1716; --surface: #16211f; --surface-2: #1b2927; --ink: #e6efed; --muted: #93a6a2;
    --line: #2a3b38; --accent: #4cc2b2; --accent-ink: #06201c; --accent-soft: #17403b;
    --good: #58c98e; --good-soft: #163626; --warn: #e5a54b; --warn-soft: #3a2c14; --bad: #f0857d; --bad-soft: #3b1d1b;
    --shadow: 0 8px 28px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1716; --surface: #16211f; --surface-2: #1b2927; --ink: #e6efed; --muted: #93a6a2;
  --line: #2a3b38; --accent: #4cc2b2; --accent-ink: #06201c; --accent-soft: #17403b;
  --good: #58c98e; --good-soft: #163626; --warn: #e5a54b; --warn-soft: #3a2c14; --bad: #f0857d; --bad-soft: #3b1d1b;
  --shadow: 0 8px 28px rgba(0, 0, 0, .5);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--f-body); font-size: 15px; line-height: 1.6; }
h1, h2, h3 { font-family: var(--f-display); font-weight: 600; margin: 0; text-wrap: balance; }
h1 { font-size: 1.5rem; line-height: 1.3; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; display: inline-block; }
.skip { position: absolute; inset-inline-start: 8px; top: -60px; background: var(--accent); color: var(--accent-ink); padding: 8px 14px; border-radius: 8px; z-index: 100; }
.skip:focus { top: 8px; }
.muted { color: var(--muted); }
.hint { font-size: .8rem; color: var(--muted); }

/* الشريط العلوي */
.topbar { position: sticky; top: 0; z-index: 30; background: var(--surface); border-bottom: 1px solid var(--line); }
.topbar-in { max-width: 1180px; margin-inline: auto; padding: 6px 16px; display: flex; align-items: center; gap: 14px; }
.brand { display: flex; flex-direction: column; line-height: 1.2; flex: none; }
.brand b { font-family: var(--f-display); font-size: 1.1rem; color: var(--accent); }
.brand span { font-size: .72rem; color: var(--muted); letter-spacing: .04em; }
#nav { display: flex; gap: 2px; overflow-x: auto; flex: 1; min-width: 0; scrollbar-width: none; }
#nav::-webkit-scrollbar { display: none; }
#nav a { white-space: nowrap; padding: 8px 12px; border-radius: 8px; text-decoration: none; color: var(--muted); font-weight: 500; }
#nav a:hover { background: var(--surface-2); color: var(--ink); }
#nav a[aria-current="page"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.sync { display: flex; align-items: center; gap: 4px; flex: none; }
.sync-state { font-size: .75rem; color: var(--muted); white-space: nowrap; }
.icon-btn { border: 0; background: transparent; width: 38px; height: 38px; border-radius: 8px; font-size: 1.15rem; color: var(--muted); line-height: 1; }
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn.spin { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(-360deg); } }
@media (max-width: 760px) {
  .topbar-in { flex-wrap: wrap; row-gap: 0; }
  .brand span { display: none; }
  #nav { order: 3; flex-basis: 100%; padding-bottom: 4px; }
  .sync { margin-inline-start: auto; }
  .sync-state { display: none; }
}

#banner { background: var(--warn-soft); border-bottom: 1px solid var(--warn); padding: 8px 16px; font-size: .9rem; text-align: center; }
#banner.bad { background: var(--bad-soft); border-color: var(--bad); }

main { max-width: 1180px; margin-inline: auto; padding: 18px 16px 60px; display: flex; flex-direction: column; gap: 18px; outline: 0; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px 16px; }
.page-head p { color: var(--muted); font-size: .9rem; margin-top: 2px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

/* أزرار */
.btn { border: 1px solid var(--line); background: var(--surface); border-radius: 9px; padding: 8px 16px; font-weight: 500; min-height: 40px; }
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--bad); }
.btn.danger-solid { background: var(--bad); color: #fff; border-color: var(--bad); }
.btn.small { padding: 4px 12px; min-height: 32px; font-size: .88rem; }
.btn.busy { position: relative; color: transparent !important; }
.btn.busy::after { content: ""; position: absolute; inset: 0; margin: auto; width: 16px; height: 16px; border: 2px solid var(--accent-ink); border-top-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }
.btn:not(.primary).busy::after { border-color: var(--accent); border-top-color: transparent; }

/* بطاقات ولوحات */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 16px; display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.panel > h2 { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.grid { display: grid; gap: 16px; min-width: 0; }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 340px), 1fr)); }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; min-width: 0; }
.kpi small { display: block; color: var(--muted); font-size: .82rem; }
.kpi b { display: block; font-family: var(--f-display); font-size: 1.45rem; font-weight: 600; line-height: 1.35; overflow-wrap: anywhere; }
.kpi .sub { color: var(--muted); font-size: .78rem; }
.kpi.owe b { color: var(--bad); }
.kpi.credit b { color: var(--good); }
.kpi.main b { color: var(--accent); }

.empty-state { text-align: center; padding: 28px 16px; color: var(--muted); display: flex; flex-direction: column; gap: 8px; align-items: center; }
.empty-state b { color: var(--ink); font-size: 1.05rem; }

.badge { display: inline-block; padding: 1px 10px; border-radius: 999px; font-size: .78rem; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.badge.good { background: var(--good-soft); color: var(--good); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.badge.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

.bal small { font-size: .75rem; color: var(--muted); }
.bal.owe { color: var(--bad); font-weight: 600; }
.bal.credit { color: var(--good); font-weight: 600; }
.bal.zero { color: var(--muted); }
.pos { color: var(--good); }
.neg { color: var(--bad); }

/* الجداول */
.dt { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.dt-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }
.dt-search { flex: 1 1 200px; max-width: 360px; min-height: 40px; border: 1px solid var(--line); border-radius: 9px; padding: 6px 12px; background: var(--surface); }
.dt-search:focus { border-color: var(--accent); outline: 0; }
.dt-count { color: var(--muted); font-size: .82rem; }
.table-scroll { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: .92rem; }
th, td { padding: 9px 12px; text-align: start; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { background: var(--surface-2); color: var(--muted); font-weight: 600; font-size: .8rem; white-space: nowrap; position: sticky; top: 0; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--ink); }
th.sorted { color: var(--accent); }
th.sorted::after { content: " " attr(data-dir); font-size: .65rem; }
tbody tr:last-child td { border-bottom: 0; }
tr.clickable { cursor: pointer; }
tr.clickable:hover { background: var(--surface-2); }
tr.dim td { color: var(--muted); }
tr.voided td { text-decoration: line-through; color: var(--muted); }
td.num-col, th.num-col { text-align: end; }
.empty-cell { text-align: center; color: var(--muted); padding: 24px; }
tfoot td { background: var(--surface-2); font-weight: 600; border-top: 2px solid var(--line); border-bottom: 0; }

/* النماذج */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 170px), 1fr)); gap: 14px; }
.fld { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.fld.wide { grid-column: 1 / -1; }
.fld > label { font-size: .82rem; color: var(--muted); font-weight: 500; }
.req { color: var(--bad); }
input[type=text], input[type=number], input[type=date], input[type=time], input[type=search], input[type=password], input[type=url], select, textarea {
  width: 100%; min-width: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 9px; padding: 8px 12px; min-height: 42px;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); outline: 0; box-shadow: 0 0 0 3px var(--accent-soft); }
input:disabled, select:disabled { background: var(--surface-2); color: var(--muted); }
input[type=number], input[type=time] { direction: ltr; text-align: end; }
input.invalid, select.invalid { border-color: var(--bad); }
.hint.lock { color: var(--warn); }
.form-error { background: var(--bad-soft); color: var(--bad); border-radius: 8px; padding: 8px 12px; font-size: .88rem; }
.form-note { background: var(--warn-soft); border-radius: 8px; padding: 8px 12px; font-size: .86rem; }
fieldset { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px 12px; margin: 0; min-width: 0; }
legend { padding-inline: 6px; color: var(--muted); font-size: .82rem; font-weight: 600; }
.days-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; }

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 4px; }
.seg label { position: relative; min-width: 0; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.seg span { display: block; text-align: center; padding: 8px 6px; border-radius: 7px; font-weight: 500; font-size: .9rem; line-height: 1.35; }
.seg small { display: block; font-size: .72rem; opacity: .8; font-weight: 400; }
.seg input:checked + span { background: var(--accent); color: var(--accent-ink); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }

.stepper { display: grid; grid-template-columns: 38px minmax(44px, 1fr) 38px; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: 9px; overflow: hidden; width: 140px; max-width: 100%; }
.stepper button { border: 0; background: var(--surface-2); height: 40px; font-size: 1.2rem; font-weight: 600; color: var(--accent); }
.stepper button:hover { background: var(--accent-soft); }
.stepper input { width: 100%; min-width: 0; text-align: center !important; border: 0 !important; border-radius: 0; background: transparent; font-weight: 700; height: 40px; min-height: 40px; box-shadow: none !important; -moz-appearance: textfield; padding: 0; }
.stepper input::-webkit-outer-spin-button, .stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper.sm { width: 112px; grid-template-columns: 30px 1fr 30px; }
.stepper.sm button, .stepper.sm input { height: 34px; min-height: 34px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { position: relative; }
.chip input { position: absolute; opacity: 0; inset: 0; cursor: pointer; margin: 0; }
.chip span { display: block; padding: 8px 14px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface); font-weight: 500; }
.chip input:checked + span { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* النوافذ */
.backdrop { position: fixed; inset: 0; z-index: 50; background: rgba(8, 20, 18, .55); display: flex; align-items: flex-start; justify-content: center; padding: 4vh 12px 12px; overflow-y: auto; }
.modal { background: var(--surface); border-radius: 14px; width: 100%; max-width: 560px; box-shadow: var(--shadow); display: flex; flex-direction: column; margin-block: auto; }
.modal.wide { max-width: 860px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.modal-body { padding: 16px; display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.modal-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); background: var(--surface-2); border-radius: 0 0 14px 14px; }
.confirm-text { line-height: 1.8; }

#toast { position: fixed; inset-inline: 16px; bottom: calc(18px + env(safe-area-inset-bottom, 0px)); margin-inline: auto; width: fit-content; max-width: min(100%, 560px); background: var(--ink); color: var(--bg); padding: 10px 20px; border-radius: 12px; font-size: .9rem; opacity: 0; transform: translateY(10px); transition: opacity .2s, transform .2s; pointer-events: none; z-index: 80; }
#toast.show { opacity: 1; transform: none; }
#toast.bad { background: var(--bad); color: #fff; }
#toast.good { background: var(--good); color: #fff; }

/* الخطوات والحاسبة */
.steps { margin: 0; padding: 0; list-style: none; counter-reset: st; display: flex; flex-direction: column; }
.steps li { counter-increment: st; display: grid; grid-template-columns: 28px 1fr auto; gap: 4px 10px; padding: 12px 0; border-top: 1px solid var(--line); align-items: baseline; }
.steps li:first-child { border-top: 0; padding-top: 0; }
.steps li::before { content: counter(st); width: 24px; height: 24px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-weight: 700; font-size: .8rem; display: grid; place-items: center; align-self: start; }
.steps .t { font-weight: 600; }
.steps .r { font-weight: 700; color: var(--accent); white-space: nowrap; }
.steps .f { grid-column: 2 / -1; color: var(--muted); font-size: .88rem; overflow-wrap: anywhere; }
.steps .f .num { color: var(--ink); }
.dates { grid-column: 2 / -1; display: flex; flex-wrap: wrap; gap: 5px; margin-top: 4px; }
.dates span { font-size: .75rem; padding: 2px 8px; border-radius: 6px; background: var(--accent-soft); line-height: 1.5; }
.dates span.off { background: transparent; border: 1px dashed var(--line); color: var(--muted); text-decoration: line-through; }
.band { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; display: grid; grid-template-columns: repeat(3, 1fr); }
.band > div { padding: 14px; min-width: 0; }
.band > div + div { border-inline-start: 1px solid var(--line); }
.band small { color: var(--muted); display: block; font-size: .8rem; }
.band b { font-family: var(--f-display); font-size: 1.4rem; font-weight: 600; line-height: 1.3; overflow-wrap: anywhere; }
.band .main b { color: var(--accent); }
.status { font-size: .85rem; border-radius: 8px; padding: 6px 12px; background: var(--accent-soft); }
.status.warn { background: var(--warn-soft); }
.status.bad { background: var(--bad-soft); color: var(--bad); }
.cols-calc { display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; align-items: start; }
@media (min-width: 900px) { .cols-calc { grid-template-columns: minmax(0, 5fr) minmax(0, 6fr); } }
.group { display: flex; flex-direction: column; gap: 10px; }
.group + .group { border-top: 1px solid var(--line); padding-top: 16px; }
.q { font-weight: 600; }
.adj { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px 12px; }
.adj + .adj { border-top: 1px dashed var(--line); padding-top: 10px; }
.adj .txt { display: flex; flex-direction: column; min-width: 0; flex: 1 1 150px; }
.adj .txt b { font-weight: 600; }

/* الشهر */
.month-row-input { width: 74px; min-height: 34px; padding: 4px 8px; text-align: center !important; }
.month-table td { white-space: nowrap; }
.month-table td.wrap { white-space: normal; min-width: 150px; }
.sticky-foot { position: sticky; bottom: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 16px; display: flex; flex-wrap: wrap; gap: 10px 20px; align-items: center; justify-content: space-between; box-shadow: var(--shadow); z-index: 5; }

.statement .debit { color: var(--bad); }
.statement .credit { color: var(--good); }

.kv { display: flex; flex-direction: column; gap: 2px; margin: 0; }
.kv > div { display: flex; justify-content: space-between; gap: 12px; padding-block: 4px; border-bottom: 1px dashed var(--line); }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 500; }
.list-plain { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.list-plain li { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 9px 0; border-top: 1px solid var(--line); }
.list-plain li:first-child { border-top: 0; }
.list-plain button.link { background: none; border: 0; padding: 0; text-align: start; color: var(--accent); font-weight: 600; }
.list-plain button.link:hover { text-decoration: underline; }
.code { direction: ltr; text-align: left; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .82rem; background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; overflow-x: auto; white-space: pre; }
.loading { padding: 60px 16px; text-align: center; color: var(--muted); }

@media (max-width: 560px) {
  .band { grid-template-columns: 1fr; }
  .band > div + div { border-inline-start: 0; border-top: 1px solid var(--line); }
  .band > div { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
  .modal-actions .btn { flex: 1 1 auto; }
}
@media (prefers-reduced-motion: reduce) { * { animation-duration: .001s !important; transition-duration: .001s !important; } }

/* تنقل الأشهر */
.monthnav { display: flex; align-items: center; gap: 4px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 4px; }
.monthnav button { border: 0; background: transparent; width: 40px; height: 40px; border-radius: 8px; font-size: 1.2rem; color: var(--ink); }
.monthnav button:hover { background: var(--accent-soft); }
.monthnav .lbl { min-width: 138px; text-align: center; font-family: var(--f-display); font-weight: 600; font-size: 1.05rem; }
td .num, th .num { white-space: nowrap; }
.month-table th, .month-table td { padding: 8px 8px; }
.stepper.sm { width: 104px; }
