:root {
  --bg: #f4f5f2; --surface: #ffffff; --ink: #1b1f1d; --ink-2: #5b625e; --line: #e2e5e1;
  --brand: #1f5f4a; --brand-ink: #ffffff; --brand-soft: #e3efe9;
  --ok: #17734a; --ok-soft: #e1f3e8; --bad: #b3261e; --bad-soft: #fbe5e3; --warn: #8a5a00; --warn-soft: #fbf0d9;
  --radius: 12px; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121514; --surface: #1b1f1d; --ink: #eef1ee; --ink-2: #a3aba6; --line: #2c322f;
    --brand: #5fb894; --brand-ink: #0d1f18; --brand-soft: #1c2e27;
    --ok: #5fc98f; --ok-soft: #173123; --bad: #f2867c; --bad-soft: #3a1c1a; --warn: #e9b54d; --warn-soft: #362a12;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--ink); }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand); }
.hidden { display: none !important; }
.muted { color: var(--ink-2); }
.small { font-size: 13px; }
.mono { font-family: var(--mono); }

/* Top bar */
.topbar { position: sticky; top: 0; z-index: 900; display: flex; align-items: center; gap: 12px; padding: 10px 18px;
  background: var(--surface); border-bottom: 1px solid var(--line); }
.brandmark { display: flex; align-items: center; gap: 10px; font-weight: 650; }
.brandmark .logo { width: 30px; height: 30px; border-radius: 8px; background: var(--brand); color: var(--brand-ink);
  display: grid; place-items: center; font-family: var(--mono); font-size: 13px; font-weight: 700; }
.spacer { flex: 1; }
.who { text-align: right; line-height: 1.2; }
.who b { display: block; font-size: 14px; }

main { max-width: 1100px; margin: 0 auto; padding: 20px 16px 60px; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 17px; margin: 0 0 12px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.stack > * + * { margin-top: 14px; }
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }

/* Forms */
label { display: block; font-size: 13px; color: var(--ink-2); margin-bottom: 4px; }
.field { margin-bottom: 12px; }
input, select, textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface); }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 0; border-color: transparent; }
.fields2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
@media (max-width: 520px) { .fields2 { grid-template-columns: 1fr; } }
.seg { display: flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg button { flex: 1; border: 0; background: transparent; padding: 9px; cursor: pointer; }
.seg button.on { background: var(--brand); color: var(--brand-ink); font-weight: 600; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 9px 16px; border-radius: 9px;
  border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-weight: 550; text-decoration: none; color: var(--ink); }
.btn:hover { border-color: var(--ink-2); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.danger { color: var(--bad); }
.btn.big { padding: 14px 22px; font-size: 17px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.linkish { background: none; border: 0; color: var(--brand); cursor: pointer; padding: 0; font-weight: 550; }

.msg { padding: 10px 12px; border-radius: 9px; font-size: 14px; }
.msg.err { background: var(--bad-soft); color: var(--bad); }
.msg.ok { background: var(--ok-soft); color: var(--ok); }
.msg.warn { background: var(--warn-soft); color: var(--warn); }

/* Tabs */
.tabs { display: flex; gap: 4px; overflow-x: auto; border-bottom: 1px solid var(--line); margin-bottom: 18px; }
.tabs button { border: 0; background: none; padding: 10px 14px; cursor: pointer; color: var(--ink-2); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.on { color: var(--ink); border-bottom-color: var(--brand); font-weight: 600; }

/* Pills */
.pill { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; background: var(--line); color: var(--ink-2); }
.pill.active { background: var(--ok-soft); color: var(--ok); }
.pill.scheduled { background: var(--brand-soft); color: var(--brand); }
.pill.used, .pill.expired { background: var(--line); color: var(--ink-2); }
.pill.revoked, .pill.denied { background: var(--bad-soft); color: var(--bad); }

/* Tables */
.tablewrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-weight: 600; color: var(--ink-2); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
th, td { padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
tr:last-child td { border-bottom: 0; }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.stat .n { font-size: clamp(20px, 2.1vw, 28px); font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .l { font-size: 13px; color: var(--ink-2); }

/* Bar chart (single series, entries per day) */
.chart { position: relative; }
.bars { display: flex; align-items: flex-end; gap: 2px; height: 140px; border-bottom: 1px solid var(--line); }
.bars .col { flex: 1; height: 100%; display: flex; align-items: flex-end; cursor: default; }
.bars .bar { width: 100%; max-width: 28px; margin: 0 auto; background: var(--brand); border-radius: 4px 4px 0 0; min-height: 1px; }
.bars .col:hover .bar { opacity: .8; }
.xlabels { display: flex; gap: 2px; font-size: 11px; color: var(--ink-2); margin-top: 4px; }
.xlabels span { flex: 1; text-align: center; }
.tip { position: absolute; pointer-events: none; background: var(--ink); color: var(--surface); font-size: 12px; padding: 6px 8px; border-radius: 6px; white-space: nowrap; transform: translate(-50%, -110%); }

/* Code cards (resident) */
.codes { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.codecard { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.codecard.dim { opacity: .6; }
.digits { font-family: var(--mono); font-size: 32px; font-weight: 700; letter-spacing: .12em; margin: 4px 0; }
.codecard .meta { font-size: 13px; color: var(--ink-2); }
.codecard .actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.codecard .actions .btn { padding: 6px 12px; font-size: 13px; }

/* Gate console */
.gate { display: grid; grid-template-columns: minmax(280px, 380px) 1fr; gap: 18px; align-items: start; }
@media (max-width: 800px) { .gate { grid-template-columns: 1fr; } }
.codeinput { font-family: var(--mono); font-size: 40px; font-weight: 700; text-align: center; letter-spacing: .3em; padding: 12px; }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }
.keypad button { padding: 18px 0; font-size: 24px; font-weight: 600; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; }
.keypad button:active { background: var(--brand-soft); }
.verdict { border-radius: var(--radius); padding: 20px; border: 2px solid var(--line); }
.verdict.ok { border-color: var(--ok); background: var(--ok-soft); }
.verdict.bad { border-color: var(--bad); background: var(--bad-soft); }
.verdict .head { font-size: 26px; font-weight: 800; margin-bottom: 6px; }
.verdict.ok .head { color: var(--ok); }
.verdict.bad .head { color: var(--bad); }
.verdict dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 12px 0; }
.verdict dt { color: var(--ink-2); font-size: 13px; }
.verdict dd { margin: 0; font-weight: 600; }

/* Auth */
.authwrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.authcard { width: 100%; max-width: 380px; }
.authcard .brandmark { margin-bottom: 18px; font-size: 18px; }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; padding: 16px; z-index: 1000; }
.modal .card { width: 100%; max-width: 480px; max-height: 92vh; overflow: auto; }
.secret { font-family: var(--mono); font-size: 20px; padding: 10px; background: var(--bg); border-radius: 8px; text-align: center; user-select: all; }

.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--surface); padding: 10px 16px; border-radius: 9px; z-index: 1100; }
.empty { text-align: center; color: var(--ink-2); padding: 28px 10px; }

/* Small-screen polish */
.keypad + .stack { margin-top: 12px; }
@media (max-width: 560px) {
  .topbar { padding: 8px 12px; }
  .who { display: none; }
  .brandmark span { font-size: 15px; }
  main { padding: 14px 10px 50px; }
  .card { padding: 14px; }
}

/* ---------- Estate register ---------- */
h3 { font-size: 15px; margin: 6px 0 0; }
.map { height: 460px; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); }
.leaflet-container { font: inherit; }
.blk-label { background: transparent; border: 0; box-shadow: none; color: #fff; font-weight: 800; font-size: 12px; text-shadow: 0 0 3px #000, 0 0 2px #000; padding: 0; }
.blk-label::before { display: none; }
.wings { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 760px) { .wings { grid-template-columns: 1fr; } }
.wing h2 { margin-bottom: 8px; }
.blks { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.blk { border: 1px solid var(--line); border-radius: 10px; padding: 8px; background: var(--surface); }
.blk-head { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; margin-bottom: 6px; }
.floor { display: flex; gap: 3px; align-items: center; margin-top: 3px; }
.floor .fl { width: 14px; font-size: 10px; color: var(--ink-2); text-align: center; flex: none; }
.flat { position: relative; flex: 1; min-width: 0; height: 26px; border-radius: 4px; border: 1px solid transparent; font-size: 11px; font-weight: 700;
  cursor: pointer; padding: 0; font-family: var(--mono); }
.blk.big .flat { height: 40px; font-size: 14px; }
.flat:focus-visible { outline: 2px solid var(--ink); outline-offset: 1px; }
.flat .dot { position: absolute; top: 2px; right: 2px; width: 6px; height: 6px; border-radius: 50%; background: #2f6fde; box-shadow: 0 0 0 1px #fff; }
/* occupancy */
.flat.o-occupied { background: #1f5f4a; color: #fff; }
.flat.o-vacant { background: var(--surface); color: var(--ink-2); border: 1.5px dashed var(--ink-2); }
.flat.o-unknown { background: var(--line); color: var(--ink-2); }
/* dues (status: icon-free cells carry the flat number; the legend + tooltip name the state) */
.flat.d-paid { background: var(--ok); color: #fff; }
.flat.d-owing { background: #e9b54d; color: #3a2a00; }
.flat.d-arrears { background: var(--bad); color: #fff; }
.flat.d-no_charges { background: var(--line); color: var(--ink-2); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center; color: var(--ink-2); }
.legend .lg { display: inline-flex; align-items: center; gap: 6px; }
.legend .sw { flex: none; width: 18px; height: 14px; cursor: default; }
.ramp { display: inline-flex; gap: 2px; }
.ramp i { width: 16px; height: 12px; border-radius: 2px; display: inline-block; }
@media (prefers-color-scheme: dark) { .flat.o-occupied { background: #5fb894; color: #0d1f18; } }

/* ---------- Platform console ---------- */
.wingrow { display: grid; grid-template-columns: 70px 90px 90px 1fr 90px auto; gap: 8px; align-items: end; }
.wingrow .field { margin-bottom: 0; }
.wingrow .btn { height: 42px; }
@media (max-width: 560px) { .wingrow { grid-template-columns: 1fr 1fr; } }
.modal .card.wide { max-width: 760px; }
