/* ==========================================================================
   Ledger UI — RTL Arabic accounting interface
   ========================================================================== */
:root {
  --ink: #1F2A37;
  --ink-soft: #536170;
  --paper: #F6F8F7;
  --panel: #FFFFFF;
  --ledger: #E7F0EA;
  --ledger-strong: #D4E6DA;
  --green: #1E6B52;
  --green-dark: #154D3B;
  --green-deep: #0F3A2C;
  --rule: #D3DDD8;
  --red: #A8262F;
  --red-bg: #FBECEC;
  --amber: #8A5A00;
  --amber-bg: #FFF5DD;
  --ok-bg: #E4F3EA;
  --focus: #2B7FD4;
  --radius: 6px;
  --font: "Segoe UI", "Noto Sans Arabic", "Noto Naskh Arabic", Tahoma, "Geeza Pro", sans-serif;
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.65;
  color: var(--ink);
  background: var(--paper);
}
a { color: var(--green); }
a:hover { color: var(--green-dark); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 3px; }
h1, h2, h3 { line-height: 1.3; margin: 0 0 .6em; }
h1 { font-size: 1.55rem; font-weight: 700; }
h2 { font-size: 1.15rem; font-weight: 700; }
h3 { font-size: 1rem; font-weight: 700; }
p { margin: 0 0 1em; max-width: 75ch; }
.num, td.num, th.num { font-variant-numeric: tabular-nums; text-align: left; direction: ltr; white-space: nowrap; }
.muted { color: var(--ink-soft); }
.small { font-size: .85rem; }
.ltr { direction: ltr; unicode-bidi: embed; }
.sr-only { position: absolute; top: 0; inset-inline-start: 0; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- App shell ---------- */
.shell { display: grid; grid-template-columns: 250px minmax(0, 1fr); min-height: 100vh; }
body { overflow-x: hidden; }
.sidebar {
  background: var(--green-deep);
  color: #DCEBE3;
  padding: 1.25rem 0;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: .6rem; padding: 0 1.25rem 1.1rem; border-bottom: 1px solid rgba(255,255,255,.08); margin-bottom: .75rem; }
.brand img { max-height: 36px; max-width: 120px; background: #fff; border-radius: 4px; padding: 2px; }
.brand strong { font-size: 1rem; color: #fff; display: block; line-height: 1.3; }
.brand span { font-size: .78rem; color: #9DBFAE; }
.nav-group { padding: .5rem 0; }
.nav-group > .nav-title { font-size: .78rem; color: #8CB09E; padding: .25rem 1.25rem; }
.sidebar a.nav-link {
  display: block; padding: .45rem 1.25rem; color: #DCEBE3; text-decoration: none;
  border-inline-start: 3px solid transparent;
}
.sidebar a.nav-link:hover { background: rgba(255,255,255,.05); color: #fff; }
.sidebar a.nav-link.active { background: rgba(255,255,255,.08); border-inline-start-color: #7FD1A8; color: #fff; font-weight: 600; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .7rem 1.75rem; background: var(--panel); border-bottom: 1px solid var(--rule);
}
.topbar .who { display: flex; align-items: center; gap: .9rem; font-size: .9rem; }
.topbar form { margin: 0; }
.content { padding: 1.5rem 1.75rem 3rem; max-width: 1280px; width: 100%; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem; }
.page-head h1 { margin: 0; }
.page-head .actions { display: flex; gap: .5rem; flex-wrap: wrap; }

.btn.menu-btn, .menu-backdrop { display: none; }

/* ---------- Panels ---------- */
.panel { background: var(--panel); border: 1px solid var(--rule); border-radius: var(--radius); padding: 1.25rem; margin-bottom: 1.25rem; }
.panel > h2:first-child { margin-top: 0; }
.panel-flush { padding: 0; overflow: hidden; }
.panel-flush > h2 { padding: 1rem 1.25rem 0; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1.25rem; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1rem; }

/* Dashboard figures: a ledger summary row, not a card grid */
.figures { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); border: 1px solid var(--rule); border-radius: var(--radius); background: var(--panel); margin-bottom: 1.25rem; }
.figure { padding: 1rem 1.25rem; border-inline-start: 1px solid var(--rule); }
.figure:first-child { border-inline-start: 0; }
.figure .label { color: var(--ink-soft); font-size: .85rem; }
.figure .value { font-size: 1.45rem; font-weight: 700; font-variant-numeric: tabular-nums; direction: ltr; text-align: right; }
.figure .value.neg { color: var(--red); }

/* ---------- Tables (ledger book) ---------- */
.table-wrap { overflow-x: auto; position: relative; }
table.ledger { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.ledger th {
  text-align: right; font-weight: 600; color: var(--ink-soft); background: var(--paper);
  border-bottom: 2px solid var(--rule); padding: .55rem .75rem; white-space: nowrap;
}
table.ledger td { padding: .5rem .75rem; border-bottom: 1px solid #E8EEEB; vertical-align: top; }
table.ledger tbody tr:nth-child(even) td { background: #FAFCFB; }
table.ledger tbody tr:hover td { background: var(--ledger); }
table.ledger tfoot td { font-weight: 700; border-top: 2px solid var(--ink); border-bottom: 3px double var(--ink); background: var(--panel); }
table.ledger td.actions { white-space: nowrap; text-align: left; }
table.ledger tr.group td { font-weight: 700; }
table.ledger tr.inactive td { color: #98A3AD; }
.tree-name { display: inline-block; }
.lvl-2 { padding-inline-start: 1.1rem; }
.lvl-3 { padding-inline-start: 2.2rem; }
.lvl-4 { padding-inline-start: 3.3rem; }
.lvl-5 { padding-inline-start: 4.4rem; }
.lvl-6 { padding-inline-start: 5.5rem; }
.lvl-7 { padding-inline-start: 6.6rem; }
.lvl-8 { padding-inline-start: 7.7rem; }
.empty { padding: 2rem 1.25rem; text-align: center; color: var(--ink-soft); }

/* ---------- Badges ---------- */
.badge { display: inline-block; font-size: .78rem; padding: .05rem .5rem; border-radius: 999px; border: 1px solid var(--rule); background: var(--paper); color: var(--ink-soft); white-space: nowrap; }
.badge.green { background: var(--ok-bg); border-color: #B7DCC6; color: var(--green-dark); }
.badge.red { background: var(--red-bg); border-color: #EDC3C6; color: var(--red); }
.badge.amber { background: var(--amber-bg); border-color: #EBD6A3; color: var(--amber); }

/* ---------- Forms ---------- */
label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: .3rem; }
.field { margin-bottom: 1rem; }
.field .hint { font-size: .82rem; color: var(--ink-soft); margin-top: .25rem; }
input[type=text], input[type=email], input[type=password], input[type=date], input[type=number], input[type=tel], input[type=search], select, textarea {
  width: 100%; font: inherit; color: var(--ink); background: #fff;
  border: 1px solid #BFCBC5; border-radius: var(--radius); padding: .5rem .65rem; min-height: 40px;
}
input:focus, select:focus, textarea:focus { border-color: var(--green); outline: 3px solid rgba(30,107,82,.18); outline-offset: 0; }
input[disabled], select[disabled] { background: var(--paper); color: var(--ink-soft); }
textarea { min-height: 80px; resize: vertical; }
.check { display: flex; align-items: center; gap: .5rem; font-weight: 500; }
.check input { width: 18px; height: 18px; accent-color: var(--green); }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0 1rem; }
.form-actions { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; margin-top: .5rem; }
.inline-form { display: inline; margin: 0; }
.filters { display: flex; gap: .6rem; flex-wrap: wrap; align-items: flex-end; margin-bottom: 1rem; }
.filters .field { margin: 0; min-width: 150px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: inherit; font-weight: 600; font-size: .9rem; line-height: 1.2;
  padding: .55rem 1rem; min-height: 40px; border-radius: var(--radius);
  border: 1px solid #BFCBC5; background: #fff; color: var(--ink); cursor: pointer; text-decoration: none;
}
.btn:hover { background: var(--paper); color: var(--ink); }
.btn-primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn-primary:hover { background: var(--green-dark); color: #fff; }
.btn-danger { color: var(--red); border-color: #E2B3B6; }
.btn-danger:hover { background: var(--red-bg); color: var(--red); }
.btn-link { border: 0; background: none; color: var(--green); padding: .2rem .35rem; min-height: 0; font-weight: 600; }
.btn-link:hover { background: none; text-decoration: underline; color: var(--green-dark); }
.btn-link.danger { color: var(--red); }
.btn-sm { padding: .3rem .65rem; min-height: 32px; font-size: .85rem; }
.btn-ghost-light { background: transparent; color: #DCEBE3; border-color: rgba(255,255,255,.25); }

/* ---------- Alerts ---------- */
.alert { border: 1px solid; border-radius: var(--radius); padding: .75rem 1rem; margin-bottom: 1rem; white-space: pre-line; }
.alert-success { background: var(--ok-bg); border-color: #B7DCC6; color: var(--green-dark); }
.alert-error { background: var(--red-bg); border-color: #EDC3C6; color: var(--red); }
.alert-warning { background: var(--amber-bg); border-color: #EBD6A3; color: var(--amber); }
.alert-info { background: #EAF2FB; border-color: #C3D8EE; color: #1D4F86; }

/* ---------- Journal sheet (the signature element) ---------- */
.sheet { border: 1px solid var(--rule); border-radius: var(--radius); background: #fff; overflow: hidden; }
.sheet table { width: 100%; min-width: 720px; border-collapse: collapse; }
.sheet th { background: var(--ledger); color: var(--green-deep); font-weight: 600; font-size: .88rem; padding: .5rem .5rem; text-align: right; border-bottom: 1px solid var(--ledger-strong); }
.sheet td { padding: .35rem .5rem; border-bottom: 1px solid #EDF2EF; vertical-align: middle; }
.sheet td.amount { width: 150px; background: #FBFDFC; border-inline-start: 1px solid #E3ECE7; }
.sheet td.amount input { text-align: left; direction: ltr; font-variant-numeric: tabular-nums; }
.sheet td.line-no { width: 36px; color: var(--ink-soft); text-align: center; font-variant-numeric: tabular-nums; }
.sheet td.remove { width: 44px; text-align: center; }
.sheet .line-account { min-width: 220px; }
.balance-strip {
  display: grid; grid-template-columns: 1fr repeat(3, auto); gap: 1.5rem; align-items: center;
  padding: .85rem 1rem; border-top: 2px solid var(--rule); background: var(--paper);
  font-variant-numeric: tabular-nums;
}
.balance-strip .tot { text-align: left; direction: ltr; }
.balance-strip .tot small { display: block; direction: rtl; text-align: right; color: var(--ink-soft); font-size: .78rem; }
.balance-strip .tot b { font-size: 1.1rem; }
.balance-strip .state { font-weight: 700; }
.balance-strip.is-balanced { background: var(--ok-bg); border-top-color: var(--green); }
.balance-strip.is-balanced .state { color: var(--green-dark); }
.balance-strip.is-off .state { color: var(--red); }
@media (prefers-reduced-motion: no-preference) {
  .sidebar { transition: transform .2s ease, visibility .2s; }
  .balance-strip { transition: background-color .25s ease, border-color .25s ease; }
}

.meta-list { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1.25rem; margin: 0; }
.meta-list dt { color: var(--ink-soft); }
.meta-list dd { margin: 0; }

/* ---------- Guest pages ---------- */
.guest { min-height: 100vh; display: grid; place-items: center; padding: 2rem 1rem;
  background: linear-gradient(180deg, var(--green-deep) 0, var(--green-deep) 210px, var(--paper) 210px); }
.guest-box { width: 100%; max-width: 440px; }
.guest-box.wide { max-width: 760px; }
.guest-head { color: #fff; text-align: center; margin-bottom: 1.25rem; }
.guest-head img { max-height: 56px; background: #fff; border-radius: 6px; padding: 4px; margin-bottom: .5rem; }
.guest-head h1 { margin: 0; font-size: 1.35rem; }
.guest-head p { margin: .2rem auto 0; color: #A9CDBB; font-size: .9rem; }
.guest .panel { padding: 1.5rem; }
.req-list { list-style: none; margin: 0; padding: 0; }
.req-list li { display: flex; justify-content: space-between; gap: 1rem; padding: .4rem 0; border-bottom: 1px solid #EDF2EF; }
.req-list .ok { color: var(--green); font-weight: 700; }
.req-list .bad { color: var(--red); font-weight: 700; }

/* ---------- 2FA ---------- */
.qr-box { display: flex; gap: 1.5rem; align-items: flex-start; flex-wrap: wrap; }
.qr-box svg { width: 200px; height: 200px; border: 1px solid var(--rule); border-radius: var(--radius); }
.secret { font-family: ui-monospace, Consolas, monospace; letter-spacing: .08em; direction: ltr; background: var(--paper); border: 1px dashed var(--rule); padding: .4rem .6rem; border-radius: 4px; word-break: break-all; display: inline-block; }
.codes { display: grid; grid-template-columns: repeat(2, max-content); gap: .4rem 2rem; font-family: ui-monospace, Consolas, monospace; direction: ltr; font-size: 1.05rem; margin: .75rem 0; }
.code-input { font-size: 1.5rem !important; letter-spacing: .4em; text-align: center; direction: ltr; font-variant-numeric: tabular-nums; }

/* ---------- Permission matrix ---------- */
.matrix th, .matrix td { text-align: center; }
.matrix th:first-child, .matrix td:first-child { text-align: right; }
.matrix .module-row td { background: var(--ledger) !important; font-weight: 700; text-align: right; color: var(--green-deep); }

.pager { display: flex; gap: .4rem; justify-content: center; padding: 1rem; }
.diff { font-family: ui-monospace, Consolas, monospace; font-size: .78rem; direction: ltr; text-align: left; white-space: pre-wrap; word-break: break-word; max-width: 420px; color: var(--ink-soft); margin: 0; }
details summary { cursor: pointer; color: var(--green); }

/* ---------- Responsive ---------- */
@media (max-width: 1000px) {
  .figures { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .figure:nth-child(3) { border-inline-start: 0; }
  .figure:nth-child(n+3) { border-top: 1px solid var(--rule); }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}
@media (max-width: 820px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; inset: 0 0 0 auto; width: 270px; z-index: 50; transform: translateX(100%); visibility: hidden; }
  .menu-toggle:checked ~ .shell .sidebar { transform: translateX(0); visibility: visible; }
  .menu-toggle:checked ~ .shell .menu-backdrop { display: block; position: fixed; inset: 0; z-index: 40; background: rgba(15,58,44,.45); }
  .btn.menu-btn { display: inline-flex; }
  .topbar-date { display: none; }
  .topbar { padding: .6rem 1rem; }
  .content { padding: 1rem 1rem 3rem; }
  .who .who-name { display: none; }
  .balance-strip { grid-template-columns: 1fr 1fr; }
  .balance-strip .state { grid-column: 1 / -1; }
}
@media (max-width: 520px) {
  .figures { grid-template-columns: 1fr; }
  .figure { border-inline-start: 0 !important; border-top: 1px solid var(--rule); }
  .figure:first-child { border-top: 0; }
}

@media print {
  .sidebar, .topbar, .page-head .actions, .filters, .no-print, .pager { display: none !important; }
  .shell { display: block; }
  .content { padding: 0; max-width: none; }
  .panel { border: 0; padding: 0; }
  body { background: #fff; font-size: 12px; }
}
.btn-block { width: 100%; }
.center { text-align: center; margin-inline: auto; }
.mt { margin-top: 1.25rem; }
.panel-pad { padding: 1rem 1.25rem; margin: 0; }
.logo-preview { max-height: 90px; max-width: 240px; border: 1px solid var(--rule); border-radius: var(--radius); padding: 4px; background: #fff; }
input[aria-invalid=true] { border-color: var(--red); background: var(--red-bg); }
