/* Ivory receivables — tokens
   ink     #16213A  text, top bar
   ground  #EEF0F3  page
   surface #FFFFFF  cards, tables
   brass   #A8834A  Ivory brand: primary actions, shop plates
   ledger  #C8102E  the red mark: arrears, bounced, overdue
   paid    #2F7D5B  cleared, settled
*/
:root {
  --ink: #16213a;
  --ink-2: #4a5468;
  --muted: #6f7889;
  --ground: #eef0f3;
  --surface: #ffffff;
  --line: #dde1e8;
  --brass: #a8834a;
  --brass-ink: #ffffff;
  --brass-soft: #f4ecdf;
  --ledger: #c8102e;
  --ledger-soft: #fbe9ec;
  --paid: #2f7d5b;
  --paid-soft: #e6f2ec;
  --amber: #9a6700;
  --amber-soft: #fff4d6;
  --radius: 10px;
  font-family: "Segoe UI", "Noto Sans Arabic", "Noto Sans", Tahoma, system-ui, sans-serif;
  color: var(--ink);
  background: var(--ground);
  -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; }
a { color: var(--brass); }
:focus-visible { outline: 2px solid var(--brass); outline-offset: 2px; }
.nowrap { white-space: nowrap; }
.num, td.amount, .amount { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }

/* ---- top bar ---- */
.topbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--ink); color: #fff;
  position: sticky; top: 0; z-index: 20;
}
.topbar a, .topbar button { color: #fff; }
.topbar .brand { font-weight: 700; letter-spacing: .02em; text-decoration: none; }
.topbar .brand::before { content: ""; display: inline-block; width: 10px; height: 10px; background: var(--brass); border-radius: 2px; margin-inline-end: 8px; vertical-align: 1px; }
.topbar nav { display: flex; gap: 4px; flex-wrap: wrap; }
.topbar nav a { text-decoration: none; padding: 6px 10px; border-radius: 6px; opacity: .8; }
.topbar nav a[aria-current="page"], .topbar nav a:hover { opacity: 1; background: rgba(255,255,255,.1); }
.topbar .spacer { flex: 1; }
.topbar button { background: transparent; border-color: rgba(255,255,255,.3); }

main { padding: 16px; max-width: 1200px; margin-inline: auto; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.page-head h1 { margin: 0; }
.page-head .spacer { flex: 1; }

/* ---- surfaces ---- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.card + .card, section.card { margin-top: 14px; }
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-wrap .card { width: 100%; max-width: 380px; }
h1 { font-size: 1.35rem; margin: 0 0 16px; }
h2 { font-size: 1.05rem; margin: 0 0 12px; }
h3 { font-size: .95rem; margin: 18px 0 8px; color: var(--ink-2); }
.muted { color: var(--muted); }
.small-text { font-size: .85rem; }

/* ---- forms ---- */
label { display: block; font-size: .86rem; color: var(--ink-2); margin: 12px 0 4px; }
label .req { color: var(--ledger); }
input, select, textarea {
  width: 100%; padding: 10px 12px; font: inherit; font-size: 16px;
  border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink);
}
textarea { min-height: 70px; resize: vertical; }
input[type=checkbox], input[type=radio] { width: auto; }
.hint { font-size: .8rem; color: var(--muted); margin-top: 4px; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 16px 16px; margin: 0 0 14px; background: var(--surface); }
legend { font-weight: 600; padding: 0 6px; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 0 14px; align-items: start; }
.grid-form .wide { grid-column: 1 / -1; }
.segmented { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.segmented label { margin: 0; display: inline-flex; align-items: center; gap: 6px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; background: #fff; color: var(--ink); }
.segmented input { margin: 0; }
.segmented label:has(input:checked) { border-color: var(--brass); background: var(--brass-soft); }

button {
  font: inherit; cursor: pointer; border-radius: 8px; padding: 9px 14px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
}
button.primary { background: var(--brass); border-color: var(--brass); color: var(--brass-ink); font-weight: 600; }
button.danger { color: var(--ledger); border-color: #f0c4cb; }
button.ok { color: var(--paid); border-color: #bfdccd; font-weight: 600; }
button.block { width: 100%; margin-top: 18px; }
button.small { padding: 5px 10px; font-size: .84rem; }
button:disabled { opacity: .6; cursor: progress; }
.linklike { background: none; border: none; color: var(--brass); padding: 0; text-decoration: underline; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.msg { margin-top: 12px; padding: 10px 12px; border-radius: 8px; font-size: .92rem; }
.msg.error { background: var(--ledger-soft); color: var(--ledger); }
.msg.ok { background: var(--paid-soft); color: var(--paid); }

/* ---- tables ---- */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 12px; border-bottom: 1px solid var(--line); text-align: start; white-space: nowrap; vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
th { font-size: .78rem; color: var(--muted); font-weight: 600; background: #f8f9fb; position: sticky; top: 0; }
td.amount, th.amount { text-align: end; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: #fafbfc; }
tr.voided td { color: var(--muted); text-decoration: line-through; }
tr.voided td.keep { text-decoration: none; }

/* Signature: the shop plate — brass unit number like the plates on pavilion stalls. */
.plate {
  display: inline-block; min-width: 3.2em; text-align: center; font-weight: 700; letter-spacing: .04em;
  padding: 3px 8px; border-radius: 4px; background: var(--brass); color: #fff;
  box-shadow: inset 0 -2px 0 rgba(0,0,0,.18), inset 0 1px 0 rgba(255,255,255,.25);
  font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate;
}
.plate.lg { font-size: 1.4rem; padding: 6px 14px; }
.zone { font-size: .76rem; color: var(--muted); display: block; margin-top: 2px; }

/* The red mark. */
.owed { color: var(--ledger); font-weight: 700; }
.credit { color: var(--paid); font-weight: 600; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .76rem; background: var(--line); color: var(--ink-2); white-space: nowrap; }
.badge.red { background: var(--ledger-soft); color: var(--ledger); }
.badge.green { background: var(--paid-soft); color: var(--paid); }
.badge.amber { background: var(--amber-soft); color: var(--amber); }
.badge.brass { background: var(--brass-soft); color: #7a5c2c; }

/* ---- filters ---- */
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; margin-bottom: 12px; }
.filters > div { min-width: 150px; }
.filters label { margin-top: 0; }
.filters .search { flex: 1; min-width: 200px; }

/* ---- tenancy header ---- */
.summary { display: grid; grid-template-columns: auto 1fr auto; gap: 16px; align-items: center; }
.summary .title { font-size: 1.25rem; font-weight: 700; }
.summary .bal { text-align: end; }
.summary .bal .label { font-size: .8rem; color: var(--muted); }
.summary .bal .value { font-size: 1.6rem; font-weight: 700; }
.facts { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 12px; font-size: .9rem; color: var(--ink-2); }
.facts b { color: var(--ink); font-weight: 600; }
@media (max-width: 640px) {
  .summary { grid-template-columns: auto 1fr; }
  .summary .bal { grid-column: 1 / -1; text-align: start; }
}

/* ---- dialogs ---- */
dialog { border: none; border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 24px); box-shadow: 0 20px 60px rgba(22,33,58,.3); }
dialog::backdrop { background: rgba(22,33,58,.45); }
dialog form { padding: 20px; }
dialog h2 { margin-bottom: 4px; }
dialog .actions { margin-top: 18px; justify-content: flex-end; }

/* ---- file picker ---- */
.file-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 6px; }
.file-row .name { font-size: .85rem; color: var(--ink-2); }
.thumb { width: 56px; height: 56px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); }

.empty { padding: 28px; text-align: center; color: var(--muted); }

/* ---- phone: tables become cards ---- */
@media (max-width: 720px) {
  main { padding: 12px; }
  table.cards thead { display: none; }
  table.cards, table.cards tbody, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards tr { border-bottom: 1px solid var(--line); padding: 10px 12px; }
  table.cards td { border: none; padding: 2px 0; white-space: normal; text-align: start; }
  table.cards td[data-label]::before { content: attr(data-label) " "; color: var(--muted); font-size: .78rem; }
  table.cards td.amount { text-align: start; }
  table.cards td:empty { display: none; }
}

[hidden] { display: none !important; }

@media (prefers-reduced-motion: no-preference) {
  dialog[open] { animation: rise .16s ease-out; }
  @keyframes rise { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }
}

/* ---- register grid ---------------------------------------------------- */
.grid-wrap { overflow: auto; max-height: calc(100vh - 230px); border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table.grid { border-collapse: separate; border-spacing: 0; width: max-content; min-width: 100%; }
table.grid th, table.grid td { border-bottom: 1px solid var(--line); padding: 7px 10px; background: var(--surface); }
table.grid thead th { position: sticky; z-index: 2; background: #f8f9fb; }
table.grid thead tr.groups th { top: 0; height: 34px; font-size: .76rem; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-2); border-inline-start: 1px solid var(--line); }
table.grid thead tr.cols th { top: 34px; }
/* No. · Name · Rent stay pinned while the rest scrolls sideways. */
table.grid .pin { position: sticky; z-index: 1; }
table.grid thead .pin { z-index: 3; }
table.grid .pin-1 { inset-inline-start: 0; width: 96px; min-width: 96px; }
table.grid .pin-2 { inset-inline-start: 96px; width: 190px; min-width: 190px; max-width: 190px; overflow: hidden; text-overflow: ellipsis; }
table.grid .pin-3 { inset-inline-start: 286px; width: 104px; min-width: 104px; border-inline-end: 2px solid var(--line); }
table.grid tbody tr:hover td { background: #fafbfc; }
table.grid tfoot td { position: sticky; bottom: 0; background: #f3f4f7; font-weight: 700; border-top: 2px solid var(--line); z-index: 1; }
table.grid tfoot td.pin { z-index: 2; }
table.grid .grp-start { border-inline-start: 1px solid var(--line); }
.grp-toggle { background: none; border: none; padding: 0 4px; font: inherit; color: inherit; cursor: pointer; text-transform: inherit; letter-spacing: inherit; }
.grp-toggle::before { content: "▾ "; }
.grp-toggle[aria-expanded="false"]::before { content: "▸ "; }
th.collapsed, td.collapsed { width: 26px; min-width: 26px; padding: 0 !important; background: repeating-linear-gradient(45deg, #f3f4f7, #f3f4f7 4px, #eceef2 4px, #eceef2 8px) !important; }

/* Week cells: the red mark. */
th.wk { font-size: .72rem; text-align: center; min-width: 64px; }
th.wk.now { color: var(--brass); }
td.wk { text-align: center; padding: 4px 3px; }
.wc { display: inline-block; min-width: 56px; padding: 3px 4px; border-radius: 5px; font-size: .74rem; line-height: 1.15; font-variant-numeric: tabular-nums; }
.wc b { display: block; font-size: .7rem; }
.wc.paid { color: var(--paid); }
.wc.paid::before { content: "✓"; font-weight: 700; }
.wc.late { border: 1.5px solid var(--ledger); color: var(--ledger); }
.wc.missing { background: var(--ledger); color: #fff; font-weight: 600; }
.wc.shortfall { background: var(--ledger-soft); color: var(--ledger); border: 1.5px solid var(--ledger); font-weight: 600; }
.wc.due { background: var(--amber-soft); color: var(--amber); }
.legend { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; font-size: .8rem; color: var(--muted); margin: 8px 0 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.late-badge { display: inline-block; min-width: 1.6em; text-align: center; border-radius: 999px; padding: 1px 6px; font-size: .78rem; font-weight: 700; background: var(--ledger-soft); color: var(--ledger); }
.nav-count { display: inline-block; min-width: 1.4em; padding: 0 5px; margin-inline-start: 4px; border-radius: 999px; background: var(--ledger); color: #fff; font-size: .72rem; text-align: center; }

/* Phone: cards with the last weeks as chips. */
.phone-list { display: none; }
@media (max-width: 720px) {
  .grid-wrap { display: none; }
  .phone-list { display: block; }
  .pcard { display: grid; grid-template-columns: auto 1fr auto; gap: 4px 10px; padding: 12px; border-bottom: 1px solid var(--line); background: var(--surface); }
  .pcard .nm { font-weight: 600; }
  .pcard .chips { grid-column: 1 / -1; }
  .pcard .money { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: .85rem; color: var(--ink-2); }
}

/* ---- ledger grid (Abu Tamim's layout, decision 22) ---- */
.ledger-bar { align-items: end; }
.ledger-bar .spacer { flex: 1; }
.ledger-bar .switch { display: inline-flex; align-items: center; gap: 6px; margin: 0; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink); cursor: pointer; }
.ledger-bar .switch:has(input:checked) { border-color: var(--brass); background: var(--brass-soft); }
.ledger-bar .as-of { font-size: .82rem; color: var(--muted); padding-bottom: 9px; }
table.grid.ledger thead tr.r1 th { top: 0; height: 32px; }
table.grid.ledger thead tr.r2 th { top: 32px; height: 32px; }
table.grid.ledger thead tr.r3 th { top: 64px; }
table.grid.ledger th.band { text-align: center; font-size: .76rem; color: var(--ink-2); border-inline-start: 1px solid var(--line); }
table.grid.ledger th.month { text-align: center; }
table.grid.ledger .pin-2 { width: 230px; min-width: 230px; max-width: 230px; white-space: normal; }
table.grid.ledger .pin-3 { inset-inline-start: 326px; text-align: end; }
.month-toggle { background: none; border: none; padding: 0 4px; font: inherit; font-weight: 600; color: var(--ink-2); cursor: pointer; }
.month-toggle .pm { display: inline-block; width: 18px; height: 18px; line-height: 16px; margin-inline-start: 6px; border: 1px solid var(--line); border-radius: 4px; color: var(--brass); text-align: center; }
td.mcell.paid { background: var(--paid-soft); }
td.mcell.short { background: var(--ledger-soft); }
.mc { display: inline-block; min-width: 64px; font-size: .78rem; font-weight: 600; line-height: 1.25; font-variant-numeric: tabular-nums; }
.mc small { display: block; font-weight: 400; font-size: .72rem; }
.mc.paid { color: var(--paid); }
.mc.short { color: var(--ledger); }
.mc.future { color: var(--muted); font-weight: 400; }
.wc.future { color: var(--muted); }
table.grid.ledger .wc.missing::before { content: "× "; }
/* A shop paid a month or more ahead: one cell over all its weeks; the label stays in view past the pinned columns. */
table.grid.ledger td.paid-ahead { background: var(--paid-soft); text-align: start; }
table.grid.ledger td.paid-ahead .pa { position: sticky; inset-inline-start: 446px; color: var(--paid); font-size: .85rem; white-space: nowrap; }
table.grid.ledger td.paid-ahead .pa b { font-size: .95rem; }
table.grid.ledger td.paid-ahead .pa small { margin-inline-start: 10px; color: var(--muted); }
.badges { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 3px; }
.grid-note { font-size: .8rem; color: var(--muted); margin: 6px 0 0; }

/* ---- reports ------------------------------------------------------------ */
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.tabs button[aria-selected="true"] { background: var(--ink); color: #fff; border-color: var(--ink); }
.summary-boxes { display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0; }
.sbox { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; min-width: 150px; }
.sbox .k { font-size: .78rem; color: var(--muted); }
.sbox .v { font-size: 1.2rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.sbox table { width: auto; }
.sbox td { padding: 2px 8px 2px 0; border: none; font-size: .88rem; }
tfoot.totals td { font-weight: 700; background: #f3f4f7; border-top: 2px solid var(--line); }
.print-head { display: none; }

@media print {
  @page { size: A4 landscape; margin: 10mm; }
  body { background: #fff; font-size: 10.5px; }
  header.topbar, .no-print, .tabs, .filters, .msg { display: none !important; }
  main { padding: 0; max-width: none; }
  .print-head { display: block; border-bottom: 2px solid var(--brass); padding-bottom: 6px; margin-bottom: 8px; }
  .print-head .co { font-weight: 700; font-size: 14px; }
  .print-head .meta { color: #444; font-size: 10px; }
  .table-wrap { border: none; overflow: visible; }
  table.cards thead { display: table-header-group; }
  table.cards, table.cards tbody, table.cards tr, table.cards td { display: revert; }
  th, td { padding: 4px 6px; white-space: normal; }
  tr { break-inside: avoid; }
  a { color: inherit; text-decoration: none; }
  .plate { box-shadow: none; padding: 1px 5px; }
  .sbox { border-color: #999; }
  .grid-wrap { overflow: visible; max-height: none; border: none; }
  table.grid .pin, table.grid thead th, table.grid tfoot td, table.grid.ledger td.paid-ahead .pa { position: static; }
  .legend, .grid-note, .month-toggle .pm { display: none; }
}
