:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --text: #17201d;
  --muted: #5d6b66;
  --line: #e1e7e4;
  --brand: #0f766e;
  --brand-soft: #e6f4f2;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn: #b54708;
  --warn-soft: #fff4e5;
  --ok: #067647;
  --ok-soft: #e7f6ee;
  --radius: 12px;
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1413;
    --card: #182120;
    --text: #e8eeec;
    --muted: #93a39e;
    --line: #2a3533;
    --brand: #2dd4bf;
    --brand-soft: #123a36;
    --danger: #f97066;
    --danger-soft: #3b1714;
    --warn: #fdb022;
    --warn-soft: #3a2a10;
    --ok: #47cd89;
    --ok-soft: #10301f;
  }
}
html[lang='ur'] {
  --font: 'Noto Nastaliq Urdu', 'Inter', serif;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  display: flex;
  min-height: 100vh;
}
html[lang='ur'] body { line-height: 2.1; font-size: 15px; }
/* Numbers, CNICs and phones stay left-to-right and in Latin digits */
.ltr, input[type='tel'], input[type='number'], .num { direction: ltr; unicode-bidi: isolate; font-family: 'Inter', system-ui, sans-serif; }

a { color: var(--brand); text-decoration: none; }
h1, h2, h3 { margin: 0; font-weight: 600; }
h2 { font-size: 17px; }

/* ---------- Layout ---------- */
.sidebar {
  width: 230px;
  flex-shrink: 0;
  background: var(--card);
  border-inline-end: 1px solid var(--line);
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; padding: 0 8px 22px; }
.logo { width: 32px; height: 32px; border-radius: 9px; background: var(--brand); color: #fff; display: grid; place-items: center; font-family: 'Inter', sans-serif; }
#nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: 9px; color: var(--muted); font-weight: 500; margin-bottom: 2px;
}
#nav a:hover { background: var(--bg); color: var(--text); }
#nav a.active { background: var(--brand-soft); color: var(--brand); }
#nav svg, .bottom-nav svg { width: 19px; height: 19px; flex-shrink: 0; }

main { flex: 1; min-width: 0; padding: 22px 28px 40px; max-width: 1100px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.topbar h1 { font-size: 22px; }

.bottom-nav { display: none; }

@media (max-width: 760px) {
  .sidebar { display: none; }
  main { padding: 14px 16px 96px; }
  .topbar h1 { font-size: 19px; }
  .bottom-nav {
    display: flex; position: fixed; inset: auto 0 0 0; z-index: 10;
    background: var(--card); border-top: 1px solid var(--line);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .bottom-nav a {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
    font-size: 11px; color: var(--muted); padding: 4px 0; line-height: 1.4;
  }
  html[lang='ur'] .bottom-nav a { font-size: 10px; line-height: 1.8; }
  .bottom-nav a.active { color: var(--brand); }
}

/* ---------- Components ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }

.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.stat .label { color: var(--muted); font-size: 13px; }
.stat .value { font-size: 24px; font-weight: 700; font-family: 'Inter', sans-serif; margin-top: 2px; }

.btn, .pill {
  font: inherit; font-weight: 500; cursor: pointer; border-radius: 9px;
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  padding: 8px 14px; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.btn:hover, .pill:hover { border-color: var(--muted); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.primary { color: #062a26; } }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.pill { border-radius: 999px; padding: 6px 14px; }
.btn:disabled { opacity: .5; cursor: default; }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; align-items: center; }
.toolbar .search { flex: 1; min-width: 200px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; background: var(--card); }
.seg button { font: inherit; border: 0; background: none; padding: 7px 12px; cursor: pointer; color: var(--muted); }
.seg button.on { background: var(--brand-soft); color: var(--brand); font-weight: 600; }

input, select, textarea {
  font: inherit; width: 100%; padding: 9px 11px; border-radius: 9px;
  border: 1px solid var(--line); background: var(--card); color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: transparent; }
textarea { min-height: 80px; resize: vertical; }
label { display: block; font-weight: 500; font-size: 14px; margin-bottom: 4px; }
.help { color: var(--muted); font-size: 13px; margin-top: 3px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field { margin-bottom: 14px; }
@media (max-width: 600px) { .grid2 { grid-template-columns: 1fr; gap: 0; } }

.phone-row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.phone-row input[type='tel'] { flex: 1; }
.check { display: inline-flex; align-items: center; gap: 6px; font-weight: 400; margin: 0; white-space: nowrap; }
.check input { width: auto; }

.unit-picks { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 8px; }
.unit-pick { border: 1px solid var(--line); border-radius: 9px; padding: 8px 10px; display: flex; gap: 8px; align-items: flex-start; cursor: pointer; font-weight: 400; }
.unit-pick:has(input:checked) { border-color: var(--brand); background: var(--brand-soft); }
.unit-pick input { width: auto; margin-top: 4px; }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; }
.row {
  display: flex; align-items: center; gap: 14px; padding: 12px 4px; border-bottom: 1px solid var(--line);
  color: inherit;
}
.row:last-child { border-bottom: 0; }
a.row:hover { background: var(--bg); }
.row .main { flex: 1; min-width: 0; }
.row .title { font-weight: 600; }
.row .sub { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .end { text-align: end; flex-shrink: 0; }
.avatar { width: 38px; height: 38px; border-radius: 50%; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; font-weight: 700; flex-shrink: 0; font-family: 'Inter', sans-serif; }

.tag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--bg); color: var(--muted); border: 1px solid var(--line); }
.tag.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.tag.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.tag.bad { background: var(--danger-soft); color: var(--danger); border-color: transparent; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: start; padding: 9px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 13px; color: var(--muted); font-weight: 500; }
tr:last-child td { border-bottom: 0; }
.table-wrap { overflow-x: auto; }

dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; margin: 0; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; overflow-wrap: anywhere; }
@media (max-width: 500px) { dl.facts { grid-template-columns: 1fr; gap: 0; } dl.facts dd { margin-bottom: 8px; } }

.phone-links { display: flex; flex-wrap: wrap; gap: 6px; }
.phone-links a { display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; font-size: 13px; }
.phone-links a { white-space: nowrap; }
.phone-links svg { width: 14px; height: 14px; }
.phone-links a.wa { border-color: #25d366; color: #128c3e; }
@media (prefers-color-scheme: dark) { .phone-links a.wa { color: #4ade80; } }

.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.spacer { flex: 1; }

/* Modal */
dialog { border: 0; border-radius: 14px; padding: 0; width: min(520px, calc(100vw - 32px)); background: var(--card); color: var(--text); }
dialog::backdrop { background: rgb(0 0 0 / .45); }
#modal-form { padding: 20px; }
#modal-form h2 { margin-bottom: 16px; }
#modal-form .actions { justify-content: flex-end; margin-top: 6px; }

#toast {
  position: fixed; inset-inline-start: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; opacity: 0;
  transition: opacity .2s, transform .2s; pointer-events: none; z-index: 50;
}
html[dir='rtl'] #toast { transform: translateX(50%) translateY(20px); }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
html[dir='rtl'] #toast.show { transform: translateX(50%) translateY(0); }
#toast.err { background: var(--danger); color: #fff; }
@media (max-width: 760px) { #toast { bottom: 90px; } }

.lang-choice { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; max-width: 420px; }
.lang-choice button { font-size: 18px; padding: 18px; justify-content: center; }
.lang-choice button.on { border: 2px solid var(--brand); background: var(--brand-soft); color: var(--brand); }
.lang-choice button:nth-child(2) { font-family: 'Noto Nastaliq Urdu', serif; }

.report li { margin-bottom: 4px; }
.errors { color: var(--danger); font-size: 13px; }

/* Floor-grouped tenant list */
.floor-card { padding: 14px 16px 6px; }
.floor-card .card-head { margin-bottom: 4px; }
.unit-badge {
  min-width: 92px; padding: 4px 10px; border-radius: 8px; background: var(--brand-soft); color: var(--brand);
  font-weight: 600; font-size: 13px; text-align: center; flex-shrink: 0;
}
.row .end.actions { flex-wrap: nowrap; }

/* Unit rows in tenant form */
.unit-row { display: grid; grid-template-columns: 1.3fr 1fr .8fr 1fr auto; gap: 8px; align-items: end; margin-bottom: 10px; }
label.mini { font-size: 12px; font-weight: 400; color: var(--muted); margin-bottom: 2px; }
@media (max-width: 600px) {
  .unit-row { grid-template-columns: 1fr 1fr; padding: 10px; border: 1px solid var(--line); border-radius: 10px; }
  .unit-row .u-del { grid-column: 1 / -1; justify-self: end; }
  .unit-badge { min-width: 76px; }
}
@media (max-width: 600px) { .row .sub { white-space: normal; } }

/* Photos */
img.avatar { object-fit: cover; background: var(--bg); }
.avatar.sm { width: 34px; height: 34px; font-size: 13px; }
.avatar.lg { width: 84px; height: 84px; font-size: 28px; }
.avatar.lg svg { width: 34px; height: 34px; }
.profile-head { display: flex; align-items: center; gap: 16px; margin-bottom: 16px; }
.photo-pick { display: flex; align-items: center; gap: 16px; margin-bottom: 18px; }
.file-btn { margin: 0; font-weight: 500; }
.thumbs { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 8px; }
.thumb {
  position: relative; width: 104px; height: 78px; border-radius: 9px; overflow: hidden;
  border: 1px solid var(--line); background: var(--bg); display: grid; place-items: center;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb .pdf { font-weight: 700; color: var(--danger); font-family: 'Inter', sans-serif; }
.thumb button {
  position: absolute; top: 4px; inset-inline-end: 4px; width: 24px; height: 24px; border-radius: 50%;
  border: 0; background: rgb(0 0 0 / .6); color: #fff; cursor: pointer; font-size: 12px; line-height: 1;
}

/* Staff rows in tenant form */
.staff-row { display: grid; grid-template-columns: 1.3fr 1fr 1.3fr 1fr 1fr; gap: 8px; align-items: end; margin-bottom: 10px; }
@media (max-width: 760px) {
  .staff-row { grid-template-columns: 1fr 1fr; padding: 10px; border: 1px solid var(--line); border-radius: 10px; }
}
@media (max-width: 600px) { .row .avatar.sm { display: none; } }
.staff-row .s-wide { grid-column: 1 / -1; }
.staff-row { padding: 12px; border: 1px solid var(--line); border-radius: 10px; }
.staff-row textarea { min-height: 54px; }
.staff-card { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin-bottom: 10px; }
.staff-card .card-head { margin-bottom: 8px; }
.staff-row .s-del { grid-column: 1 / -1; justify-self: end; }

/* ---------- Phase 2: rent, ledger, receipts ---------- */
.section-title { font-size: 14px; color: var(--muted); font-weight: 500; margin: 4px 0 8px; }
.month-bar { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
.month-bar input[type='month'] { width: auto; }
.month-bar .month-name { margin-inline-start: 6px; }
.unit-badges { display: flex; flex-direction: column; gap: 4px; flex-shrink: 0; }
.rent-row .main { color: inherit; }
.rent-end { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.balance-card { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.balance-card .big { font-size: 28px; font-weight: 700; margin: 2px 0 4px; }
.numcol { text-align: end; white-space: nowrap; }
.nowrap { white-space: nowrap; }
table.ledger tr.payment td { background: var(--ok-soft); }
table.ledger tr.payment td:first-child { border-start-start-radius: 6px; border-end-start-radius: 6px; }
.wa-btn { border-color: #25d366; color: #128c3e; }
@media (prefers-color-scheme: dark) { .wa-btn { color: #4ade80; } }
.wa-btn svg { width: 16px; height: 16px; }
@media (max-width: 600px) {
  .rent-row { flex-wrap: wrap; }
  .rent-row .main { flex-basis: calc(100% - 110px); }
  .rent-end { flex-direction: row; width: 100%; justify-content: space-between; align-items: center; }
  .unit-badges { flex-direction: row; flex-wrap: wrap; }
}

/* Receipt: always printed on white */
.receipt {
  background: #fff; color: #111; border: 1px solid var(--line); border-radius: 12px;
  padding: 28px; max-width: 640px; margin: 0 auto;
}
.r-head { display: flex; justify-content: space-between; gap: 16px; border-bottom: 2px solid #0f766e; padding-bottom: 14px; margin-bottom: 16px; }
.r-building { font-size: 20px; font-weight: 700; color: #0f766e; }
.r-muted { color: #555; font-size: 13px; }
.r-title { text-align: end; font-weight: 600; }
.r-no { font-size: 18px; color: #0f766e; }
.r-facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; margin: 0 0 18px; }
.r-facts dt { color: #555; }
.r-facts dd { margin: 0; }
.r-amount { display: flex; justify-content: space-between; align-items: center; background: #e6f4f2; border-radius: 10px; padding: 14px 16px; font-size: 16px; }
.r-amount b { font-size: 24px; color: #0f766e; }
.r-balance { margin-top: 10px; text-align: end; color: #333; }
.r-sign { margin-top: 48px; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; color: #555; font-size: 13px; }
.r-sign div { width: 180px; border-bottom: 1px solid #999; }
@media (max-width: 500px) { .receipt { padding: 18px; } .r-head { flex-direction: column; } .r-title { text-align: start; } }

@media print {
  body { background: #fff; display: block; }
  .sidebar, .bottom-nav, .topbar, .no-print, #toast { display: none !important; }
  main { padding: 0; max-width: none; }
  .receipt { border: 0; max-width: none; padding: 0; }
}
.stat .value { white-space: nowrap; font-size: clamp(18px, 2.4vw, 24px); }

/* ---------- Phase 3: dashboard, chart, reports ---------- */
.dash-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.dash-grid .dash-wide { grid-column: 1 / -1; }
@media (max-width: 900px) { .dash-grid { grid-template-columns: 1fr; } }
.dash-grid .row { padding: 9px 2px; }

/* Billed vs collected: blue + aqua, validated for colour-blind separation in light and dark */
:root { --billed: #2a78d6; --collected: #1baf7a; --grid: #e6ebe9; }
@media (prefers-color-scheme: dark) { :root { --billed: #3987e5; --collected: #199e70; --grid: #26302e; } }
.chart-legend { display: flex; gap: 16px; font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.chart-legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-inline-end: 6px; }
.sw.billed { background: var(--billed); }
.sw.collected { background: var(--collected); }
.chart { position: relative; height: 200px; direction: ltr; padding-left: 40px; }
.chart-grid { position: absolute; inset: 0 0 24px 40px; display: flex; flex-direction: column; justify-content: space-between; }
.chart-grid div { border-top: 1px solid var(--grid); position: relative; height: 0; }
.chart-grid span { position: absolute; left: -40px; top: -8px; width: 34px; text-align: right; font: 11px 'Inter', sans-serif; color: var(--muted); }
.chart-cols { position: absolute; inset: 0 0 0 40px; display: flex; }
.chart-col { flex: 1; display: flex; flex-direction: column; align-items: center; position: relative; outline: none; cursor: default; }
.chart-col .bars { flex: 1; width: 100%; display: flex; justify-content: center; align-items: flex-end; gap: 2px; padding-bottom: 0; margin-bottom: 24px; }
.chart-col .bar { width: min(24px, 30%); border-radius: 4px 4px 0 0; min-height: 0; }
.chart-col .bar.billed { background: var(--billed); }
.chart-col .bar.collected { background: var(--collected); }
.chart-x { position: absolute; bottom: 0; font-size: 12px; color: var(--muted); line-height: 20px; }
.chart-col:hover, .chart-col:focus-visible { background: var(--bg); border-radius: 6px; }
.chart-tip {
  display: none; position: absolute; bottom: calc(100% - 30px); left: 50%; transform: translateX(-50%); z-index: 5;
  background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; white-space: nowrap;
  box-shadow: 0 4px 14px rgb(0 0 0 / .12); font-size: 13px; color: var(--text);
}
.chart-col:hover .chart-tip, .chart-col:focus .chart-tip { display: block; }
.chart-col:first-child .chart-tip { left: 0; transform: none; }
.chart-col:last-child .chart-tip { left: auto; right: 0; transform: none; }

.report-tabs a { padding: 7px 14px; color: var(--muted); }
.report-tabs a.on { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
label.inline { display: inline-flex; align-items: center; gap: 6px; margin: 0; font-weight: 400; }
label.inline input { width: auto; }
tr.total-row td { font-weight: 700; border-top: 2px solid var(--line); }
.rent-end .actions { flex-wrap: nowrap; }

/* ---------- Phase 4: login ---------- */
body.locked .sidebar, body.locked main, body.locked .bottom-nav { display: none; }
#auth { position: fixed; inset: 0; z-index: 40; background: var(--bg); display: grid; place-items: center; padding: 16px; overflow-y: auto; }
#auth[hidden] { display: none; }
.auth-card { width: min(400px, 100%); background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 24px; }
.auth-card .brand { padding: 0 0 18px; }
.auth-card .brand .pill { margin-inline-start: auto; font-size: 13px; }
.auth-card h1 { font-size: 20px; margin-bottom: 14px; }
.auth-submit { width: 100%; justify-content: center; padding: 11px; font-size: 16px; }
.auth-error { color: var(--danger); min-height: 22px; margin-bottom: 8px; font-size: 14px; }
/* Six menu items on phones */
@media (max-width: 760px) { .bottom-nav a { font-size: 10px; } }
.increase-banner { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; border-color: var(--warn); }
