:root {
  --paper: #F5F1E8;
  --card: #FFFDF8;
  --ink: #1C2321;
  --ink-2: #4A5550;
  --line: #DDD5C4;
  --brand: #1E3A34;
  --brand-2: #2F5D52;
  --late: #B42318;
  --late-bg: #FDECEA;
  --soon: #A15C07;
  --soon-bg: #FEF3DC;
  --ok: #0B7A4B;
  --ok-bg: #E4F4EA;
  --vacant: #6B7280;
  --vacant-bg: #EEECE7;
  --radius: 16px;
  --tap: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: 'Tajawal', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size: 19px;
  line-height: 1.6;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: inherit; }
[hidden] { display: none !important; }

#offline {
  position: sticky; top: 0; z-index: 20;
  background: var(--soon); color: #fff;
  padding: calc(10px + var(--safe-top)) 16px 10px;
  text-align: center; font-weight: 500; font-size: 17px;
}

main { max-width: 960px; margin: 0 auto; padding: 0 16px calc(48px + var(--safe-bottom)); }
.loading { padding: 30vh 0; text-align: center; color: var(--ink-2); }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: calc(14px + var(--safe-top)) 0 14px;
}
.topbar h1 { font-size: 26px; margin: 0; flex: 1; font-weight: 800; line-height: 1.3; }
.topbar .sub { display: block; font-size: 16px; font-weight: 500; color: var(--ink-2); }
.icon-btn {
  min-width: var(--tap); height: var(--tap);
  border: 1px solid var(--line); background: var(--card);
  border-radius: 14px; font-size: 17px; font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 0 14px; cursor: pointer; text-decoration: none;
}
.icon-btn svg { width: 24px; height: 24px; }

/* ---------- Summary tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 12px; }
.tile {
  border-radius: var(--radius); padding: 14px 14px 12px; border: 2px solid transparent;
  background: var(--card); text-align: start; cursor: pointer; display: block; width: 100%;
}
.tile .n { font-size: 40px; font-weight: 800; line-height: 1.1; display: block; }
.tile .t { font-size: 16px; font-weight: 700; display: block; }
.tile.late { background: var(--late-bg); color: var(--late); border-color: #F4C7C2; }
.tile.soon { background: var(--soon-bg); color: var(--soon); border-color: #F5DCA8; }
.tile.ok { background: var(--ok-bg); color: var(--ok); border-color: #BFE3CD; }
.tile.zero { background: var(--card); color: var(--ink-2); border-color: var(--line); }

.collected {
  display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 4px 12px;
  background: var(--brand); color: #fff; border-radius: var(--radius);
  padding: 14px 18px; margin-bottom: 24px;
}
.collected b { font-size: 24px; }

/* ---------- Sections ---------- */
h2 { font-size: 22px; margin: 28px 0 12px; font-weight: 800; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 28px 0 12px; }
.section-head h2 { margin: 0; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 14px; }
.card h3 { margin: 0 0 10px; font-size: 20px; }

/* ---------- Alerts ---------- */
.alert {
  display: flex; align-items: center; gap: 12px;
  border-radius: var(--radius); padding: 14px 16px; margin-bottom: 10px;
  border-inline-start: 8px solid; background: var(--card); border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line); border-inline-end: 1px solid var(--line);
}
.alert.late { border-inline-start-color: var(--late); background: var(--late-bg); }
.alert.soon { border-inline-start-color: var(--soon); background: var(--soon-bg); }
.alert .body { flex: 1; min-width: 0; text-decoration: none; display: block; }
.alert .who { font-weight: 800; font-size: 20px; display: block; }
.alert .where { color: var(--ink-2); font-size: 16px; display: block; }
.alert .why { font-weight: 700; display: block; }
.alert .why.late { color: var(--late); }
.alert .why.soon { color: var(--soon); }

.all-good {
  background: var(--ok-bg); color: var(--ok); border-radius: var(--radius);
  padding: 22px; text-align: center; font-weight: 800; font-size: 21px;
}

/* ---------- Buildings ---------- */
.buildings { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.building { text-decoration: none; display: block; }
.building .name { font-size: 22px; font-weight: 800; margin: 0; }
.building .meta { color: var(--ink-2); font-size: 16px; margin-bottom: 12px; }
.doors { display: flex; flex-wrap: wrap; gap: 8px; }
.door {
  min-width: 48px; height: 48px; padding: 0 8px; border-radius: 10px;
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 17px; border: 2px solid;
}
.door.ok { background: var(--ok-bg); color: var(--ok); border-color: #A8D9BB; }
.door.late { background: var(--late); color: #fff; border-color: var(--late); }
.door.soon { background: var(--soon-bg); color: var(--soon); border-color: #EBC67F; }
.door.vacant { background: var(--vacant-bg); color: var(--vacant); border-color: #D3D0C8; border-style: dashed; }

.legend { display: flex; flex-wrap: wrap; gap: 8px 16px; font-size: 15px; color: var(--ink-2); margin-top: 12px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 16px; height: 16px; border-radius: 4px; display: inline-block; }

/* ---------- Apartment rows ---------- */
.row {
  display: flex; align-items: center; gap: 14px; text-decoration: none;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; margin-bottom: 10px;
}
.row .door { flex: none; }
.row .info { flex: 1; min-width: 0; }
.row .info b { display: block; font-size: 20px; }
.row .info small { display: block; color: var(--ink-2); font-size: 16px; }
.row .chev { color: var(--ink-2); font-size: 26px; }

.chip { display: inline-block; padding: 2px 12px; border-radius: 999px; font-size: 16px; font-weight: 700; }
.chip.late { background: var(--late); color: #fff; }
.chip.soon { background: var(--soon-bg); color: var(--soon); }
.chip.ok { background: var(--ok-bg); color: var(--ok); }
.chip.vacant { background: var(--vacant-bg); color: var(--vacant); }

/* ---------- Apartment page ---------- */
.tenant-head { padding: 4px 0 12px; }
.tenant-head .tname { font-size: 30px; font-weight: 800; margin: 0; line-height: 1.3; }
.tenant-head .tplace { color: var(--ink-2); }
.contact { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 14px 0; }

.banner { border-radius: var(--radius); padding: 14px 18px; font-weight: 800; font-size: 20px; margin-bottom: 14px; }
.banner.late { background: var(--late); color: #fff; }
.banner.soon { background: var(--soon-bg); color: var(--soon); }
.banner.ok { background: var(--ok-bg); color: var(--ok); }
.banner.vacant { background: var(--vacant-bg); color: var(--vacant); }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 0; }
.kv dt { color: var(--ink-2); }
.kv dd { margin: 0; font-weight: 700; }

.dues { list-style: none; margin: 0 0 14px; padding: 0; }
.dues li {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 12px 0; border-bottom: 1px solid var(--line);
}
.dues li:last-child { border-bottom: 0; }
.dues .d { font-weight: 700; }
.dues small { display: block; color: var(--ink-2); font-size: 15px; font-weight: 400; }

.progress { height: 14px; background: var(--vacant-bg); border-radius: 999px; overflow: hidden; margin: 12px 0 6px; }
.progress span { display: block; height: 100%; background: var(--brand-2); border-radius: 999px; }
.progress.soon span { background: var(--soon); }
.progress.late span { background: var(--late); }
.big-left { font-size: 24px; font-weight: 800; }

.bill-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 8px; }
.muted { color: var(--ink-2); }
.danger-zone { display: grid; gap: 10px; margin-top: 24px; }

/* ---------- Buttons ---------- */
.btn {
  min-height: var(--tap); padding: 10px 20px; border-radius: 14px;
  border: 2px solid var(--brand); background: var(--brand); color: #fff;
  font-weight: 800; font-size: 19px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; text-decoration: none;
}
.btn.block { width: 100%; }
.btn.ghost { background: var(--card); color: var(--brand); }
.btn.ok { background: var(--ok); border-color: var(--ok); }
.btn.late { background: var(--late); border-color: var(--late); }
.btn.danger { background: var(--card); color: var(--late); border-color: #E9B4AE; }
.btn.small { min-height: 46px; font-size: 17px; padding: 6px 14px; }
.btn.wa { background: #128C4B; border-color: #128C4B; }
.btn svg { width: 22px; height: 22px; flex: none; }
.btn:disabled { opacity: .5; }
.btn:active, .icon-btn:active, .tile:active { transform: scale(.98); }

/* ---------- Forms ---------- */
label.f { display: block; margin-bottom: 16px; }
label.f > span { display: block; font-weight: 700; margin-bottom: 6px; }
label.f > small { display: block; color: var(--ink-2); font-size: 15px; margin-top: 4px; }
input, select, textarea {
  width: 100%; min-height: var(--tap); padding: 10px 14px;
  border: 2px solid #C9C0AD; border-radius: 12px; background: #fff; font-size: 19px;
}
input:focus, select:focus, textarea:focus { outline: 3px solid #9BC7B8; border-color: var(--brand-2); }
input[type="date"] { direction: rtl; text-align: start; }
input[type="tel"], input[inputmode="decimal"], input[inputmode="numeric"] { direction: ltr; text-align: right; }
textarea { min-height: 96px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-error { background: var(--late-bg); color: var(--late); border-radius: 12px; padding: 10px 14px; font-weight: 700; margin-bottom: 14px; }
fieldset { border: 0; padding: 0; margin: 0 0 8px; }
legend { font-weight: 800; font-size: 19px; color: var(--brand); margin-bottom: 8px; padding: 0; }
.choice { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px; }
.choice label { position: relative; }
.choice input { position: absolute; opacity: 0; pointer-events: none; }
.choice span {
  display: flex; align-items: center; justify-content: center; min-height: var(--tap);
  border: 2px solid #C9C0AD; border-radius: 12px; background: #fff; font-weight: 700; cursor: pointer;
}
.choice input:checked + span { border-color: var(--brand); background: var(--brand); color: #fff; }

/* ---------- Bottom sheet dialog ---------- */
dialog#sheet {
  border: 0; padding: 0; width: 100%; max-width: 640px; max-height: 92vh;
  margin: auto auto 0; border-radius: 22px 22px 0 0; background: var(--paper); color: var(--ink);
}
@media (min-width: 700px) { dialog#sheet { margin: auto; border-radius: 22px; } }
dialog#sheet::backdrop { background: rgba(20, 28, 25, .55); }
.sheet-head {
  position: sticky; top: 0; background: var(--paper); z-index: 1;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 18px 20px 12px; border-bottom: 1px solid var(--line);
}
.sheet-head h2 { margin: 0; font-size: 22px; }
.sheet-body { padding: 18px 20px calc(24px + var(--safe-bottom)); }
.sheet-foot { display: grid; gap: 10px; margin-top: 8px; }

/* ---------- Login ---------- */
.login { max-width: 440px; margin: 0 auto; padding-top: calc(10vh + var(--safe-top)); }
.login .mark { width: 88px; height: 88px; border-radius: 22px; display: block; margin: 0 auto 16px; }
.login h1 { text-align: center; font-size: 30px; margin: 0 0 4px; }
.login p.lead { text-align: center; color: var(--ink-2); margin: 0 0 28px; }

#toast {
  position: fixed; inset-inline: 16px; bottom: calc(20px + var(--safe-bottom)); z-index: 50;
  max-width: 520px; margin: 0 auto; background: var(--ink); color: #fff;
  padding: 14px 18px; border-radius: 14px; font-weight: 700; text-align: center;
}

.help ol { padding-inline-start: 24px; margin: 8px 0 0; }
.help li { margin-bottom: 6px; }

@media (max-width: 520px) {
  body { font-size: 18px; }
  .tiles { gap: 8px; }
  .tile { padding: 12px 10px 10px; }
  .tile .n { font-size: 34px; }
  .tile .t { font-size: 15px; }
  .topbar h1 { font-size: 23px; }
  .two { grid-template-columns: 1fr; }
}

/* Keep labels on one line; let the row wrap under the text on narrow phones. */
.chip, .btn.small { white-space: nowrap; }
.dues li { flex-wrap: wrap; }
.dues li > .d { flex: 1 1 160px; }
.choice.three { grid-template-columns: repeat(3, 1fr); }

/* ================= Rent book (paper ledger) ================= */
.tabs { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 12px; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: none; display: inline-flex; align-items: center; gap: 8px;
  min-height: 52px; padding: 0 18px; border-radius: 14px; border: 2px solid var(--line);
  background: var(--card); font-weight: 800; font-size: 18px; text-decoration: none; cursor: pointer; white-space: nowrap;
}
.tab.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.tab.add { border-style: dashed; color: var(--brand); }
.tab i {
  font-style: normal; background: var(--late); color: #fff; border-radius: 999px;
  min-width: 26px; height: 26px; display: inline-flex; align-items: center; justify-content: center; font-size: 15px;
}

.book-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin: 8px 0 10px; }
.book-head h2 { margin: 0; font-size: 26px; }
.link { border: 0; background: none; color: var(--brand-2); font-weight: 700; text-decoration: underline; cursor: pointer; padding: 8px 0; }

.paper {
  background: #fff; border: 1px solid var(--line); border-radius: 12px;
  padding: 14px; margin-bottom: 18px; box-shadow: 0 1px 0 rgba(0,0,0,.03);
}
.paper-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.paper-head h3 { margin: 0; font-size: 21px; }
.year { display: flex; align-items: center; gap: 6px; }
.year b { font-size: 20px; min-width: 56px; text-align: center; }
.year button {
  width: 46px; height: 46px; border-radius: 12px; border: 2px solid var(--line); background: var(--card);
  font-size: 24px; font-weight: 800; cursor: pointer; line-height: 1;
}

.sheet-wrap { overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch; border: 1px solid #E6DFD0; border-radius: 8px; }
.ledger { border-collapse: separate; border-spacing: 0; font-size: 17px; min-width: 100%; }
.ledger th, .ledger td {
  border-bottom: 1px solid #E6DFD0; border-inline-start: 1px solid #EFE9DD;
  padding: 0; text-align: center; vertical-align: middle; background: #fff;
}
.ledger thead th {
  background: var(--paper); font-size: 15px; font-weight: 700; color: var(--ink-2);
  padding: 8px 6px; border-bottom: 2px solid #CFC5B0; white-space: nowrap;
}
.ledger thead th.now { color: var(--brand); background: #E5EFEA; }
.ledger td.now { background: #F6FAF8; }
.ledger .who {
  position: sticky; inset-inline-start: 0; z-index: 1; text-align: start;
  border-inline-end: 2px solid #CFC5B0; min-width: 116px; max-width: 160px;
}
.ledger thead .who { z-index: 2; }
.ledger .who button, .ledger td > button:not(.cell) {
  display: block; width: 100%; min-height: 60px; padding: 8px 10px; border: 0; background: none;
  text-align: inherit; cursor: pointer; font: inherit;
}
.ledger .who b { display: block; font-size: 19px; color: var(--brand); }
.ledger .who span { display: block; font-size: 16px; line-height: 1.35; overflow: hidden; text-overflow: ellipsis; }
.ledger td.rent { padding: 6px 10px; font-weight: 800; white-space: nowrap; }
.ledger small { display: block; font-size: 13px; font-weight: 500; color: var(--ink-2); white-space: nowrap; }
.ledger td.contract button { font-weight: 700; font-size: 15px; white-space: nowrap; }
.ledger td.contract.soon { background: var(--soon-bg); }
.ledger td.contract.soon small { color: var(--soon); font-weight: 800; }
.ledger td.contract.late { background: var(--late-bg); }
.ledger td.contract.late small { color: var(--late); font-weight: 800; }
.ledger tfoot th, .ledger tfoot td { background: var(--paper); font-weight: 800; font-size: 15px; padding: 10px 6px; border-bottom: 0; }
.ledger td.m { padding: 5px; min-width: 66px; }
.ledger td.period button { white-space: nowrap; font-size: 15px; }

.cell {
  width: 54px; height: 50px; border-radius: 10px; border: 2px solid #D9D1C0; background: #fff;
  font-size: 24px; font-weight: 800; cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
  padding: 0; line-height: 1;
}
.cell.paid { background: var(--ok); border-color: var(--ok); color: #fff; }
.cell.covered { background: var(--ok-bg); border-color: #A8D9BB; color: var(--ok); }
.cell.late { background: var(--late); border-color: var(--late); color: #fff; }
.cell.open { background: #fff; }
.cell.off { background: #F3F0EA; border-color: #EEEAE2; cursor: default; }
.cell:active { transform: scale(.94); }

.key span.k { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.key { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; font-size: 15px; color: var(--ink-2); margin: 12px 0 4px; }
.key .cell { width: 28px; height: 26px; font-size: 15px; border-radius: 6px; cursor: default; }
.hint { font-size: 15px; color: var(--ink-2); margin: 0 0 12px; }

#toast { display: flex; align-items: center; justify-content: center; gap: 14px; }
#toast .undo {
  background: #fff; color: var(--ink); border: 0; border-radius: 10px; padding: 8px 14px; font-weight: 800; cursor: pointer;
}

/* ================= Simple list ================= */
.btn.big { min-height: 68px; font-size: 22px; margin: 4px 0 8px; }
h3.group { font-size: 17px; color: var(--ink-2); margin: 18px 4px 8px; font-weight: 700; }
.tenant {
  display: block; width: 100%; text-align: start; cursor: pointer;
  background: var(--card); border: 1px solid var(--line); border-inline-start: 7px solid var(--line);
  border-radius: 14px; padding: 12px 16px; margin-bottom: 8px; font: inherit; color: inherit;
}
.tenant.ok { border-inline-start-color: var(--ok); }
.tenant.late { border-inline-start-color: var(--late); background: var(--late-bg); }
.tenant.soon { border-inline-start-color: var(--soon); }
.tenant.vacant { border-style: dashed; color: var(--vacant); }
.t-name { display: block; font-size: 20px; font-weight: 800; }
.t-name small { font-size: 16px; font-weight: 500; color: var(--ink-2); }
.t-rent { display: block; font-weight: 700; }
.t-rent.late { color: var(--late); }
.t-rent.ok { color: var(--ok); }
.t-sub { display: block; font-size: 16px; color: var(--ink-2); }
.t-sub.soon { color: var(--soon); font-weight: 700; }
.t-sub.late { color: var(--late); font-weight: 700; }

.log { background: #fff; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; margin-bottom: 12px; }
.log-row {
  display: grid; grid-template-columns: auto 1fr auto auto; align-items: center; gap: 10px;
  padding: 10px 14px; border-bottom: 1px solid #EEE8DC;
}
.log-row:last-child { border-bottom: 0; }
.l-date { font-size: 14px; color: var(--ink-2); width: 74px; line-height: 1.3; }
.l-main b { display: block; }
.l-main span { display: block; font-size: 16px; color: var(--ink-2); }
.l-amount { font-weight: 800; white-space: nowrap; }
.l-del { width: 40px; height: 40px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); cursor: pointer; color: var(--ink-2); }
p.q { margin: 0 0 8px; font-weight: 700; }
