:root {
  --bg: #f6f3ee;
  --surface: #fffdf9;
  --surface-2: #f0ebe2;
  --line: #e2dbcf;
  --text: #211e1a;
  --muted: #6d665c;
  --brand: #1d5c4d;
  --brand-ink: #f6efe2;
  --paid: #2f7a4f;
  --paid-bg: #e3f1e7;
  --pastdue: #b3261e;
  --pastdue-bg: #fbe5e2;
  --overdue: #8a5a00;
  --overdue-bg: #fbefd4;
  --dueout: #1f57a8;
  --dueout-bg: #e1ebfa;
  --soon: #6a3fa0;
  --soon-bg: #efe6f8;
  --vacant: #8b8478;
  --vacant-bg: #f3f0ea;
  --reserved: #0f7a87;
  --reserved-bg: #dcf1f3;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(33, 30, 26, .06), 0 2px 8px rgba(33, 30, 26, .04);
  --font: 'General Sans', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45; -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
h1 { font-size: 22px; font-weight: 600; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 15px; font-weight: 600; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.num, .tbl td.n, .kpi .v { font-variant-numeric: tabular-nums lining-nums; }
button { font: inherit; color: inherit; cursor: pointer; }
input, select { font: inherit; color: inherit; }

.btn { border: 1px solid var(--line); background: var(--surface); padding: 7px 12px; border-radius: 8px; font-weight: 500; }
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); width: 100%; padding: 10px; }
.btn-primary:hover { background: #174a3e; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.link { background: none; border: 0; color: var(--brand); font-weight: 500; padding: 0; }
.link:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

/* Brand */
.brand { display: flex; align-items: center; gap: 10px; color: var(--brand); min-width: 0; }
.brand svg { width: 30px; height: 30px; flex: none; }
.brand strong { display: block; color: var(--text); font-size: 15px; line-height: 1.1; }
.brand span { display: block; color: var(--muted); font-size: 12px; white-space: nowrap; }
.brand-lg svg { width: 40px; height: 40px; }
.brand-lg strong { font-size: 19px; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 10px; }
.login-card .brand { margin-bottom: 14px; }
.login-card label { font-weight: 500; }
.login-card input { padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
.error { color: var(--pastdue); min-height: 1em; margin: 0; }
.fine { color: var(--muted); font-size: 12px; margin: 0; }

/* Layout */
.app { display: grid; grid-template-rows: auto 1fr; grid-template-columns: minmax(0, 1fr); height: 100dvh; }
.topbar { display: flex; align-items: center; gap: 18px; padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.tabs { display: flex; gap: 2px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.tabs button { border: 0; background: none; padding: 8px 12px; border-radius: 8px; color: var(--muted); font-weight: 500; white-space: nowrap; }
.tabs button:hover { background: var(--surface-2); color: var(--text); }
.tabs button.active { background: var(--brand); color: var(--brand-ink); }
.tabs .count { display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 99px; background: var(--pastdue); color: #fff; font-size: 11px; line-height: 18px; }
.top-actions { display: flex; align-items: center; gap: 4px; white-space: nowrap; }
.main { overflow-y: auto; overscroll-behavior: contain; padding: 22px 20px 48px; }
.panel { max-width: 1440px; margin: 0 auto; }
.panel-head { margin-bottom: 16px; }
.panel-head.row { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; }
.panel-head p { margin: 4px 0 0; }

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); text-align: left; }
button.kpi:hover { border-color: var(--brand); }
.kpi .l { color: var(--muted); font-size: 12px; font-weight: 500; }
.kpi .v { font-size: 26px; font-weight: 600; margin-top: 2px; letter-spacing: -.02em; }
.kpi .s { color: var(--muted); font-size: 12px; }
.kpi.alert .v { color: var(--pastdue); }
.kpi.warn .v { color: var(--overdue); }
.kpi.info .v { color: var(--dueout); }

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.span-2 { grid-column: 1 / -1; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; padding: 14px 16px 10px; border-bottom: 1px solid var(--line); }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: grid; grid-template-columns: 64px 1fr auto; gap: 10px; align-items: center; padding: 9px 16px; border-bottom: 1px solid var(--line); cursor: pointer; }
.list li:last-child { border-bottom: 0; }
.list li:hover { background: var(--surface-2); }
.list .room { font-weight: 600; }
.list .who { min-width: 0; }
.list .who b { font-weight: 500; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .who span { color: var(--muted); font-size: 12px; }
.list .right { text-align: right; font-variant-numeric: tabular-nums; }
.empty { padding: 22px 16px; color: var(--muted); }

/* Badges */
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 99px; font-size: 12px; font-weight: 500; white-space: nowrap; }
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.b-pastdue { color: var(--pastdue); background: var(--pastdue-bg); }
.b-overdue { color: var(--overdue); background: var(--overdue-bg); }
.b-dueout { color: var(--dueout); background: var(--dueout-bg); }
.b-soon { color: var(--soon); background: var(--soon-bg); }
.b-paid { color: var(--paid); background: var(--paid-bg); }
.b-balance { color: var(--overdue); background: var(--overdue-bg); }
.b-vacant { color: var(--vacant); background: var(--vacant-bg); }
.b-reserved { color: var(--reserved); background: var(--reserved-bg); }
.tag { display: inline-block; padding: 1px 6px; border-radius: 5px; background: var(--surface-2); color: var(--muted); font-size: 11px; font-weight: 500; }

/* Filters / segments */
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.filters input, .filters select { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.filters input { min-width: 260px; flex: 1; max-width: 420px; }
.seg { display: inline-flex; background: var(--surface); border: 1px solid var(--line); border-radius: 9px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.seg button { border: 0; background: none; padding: 6px 10px; border-radius: 6px; color: var(--muted); font-weight: 500; }
.seg button.active { background: var(--surface-2); color: var(--text); }
.legend { display: flex; flex-wrap: wrap; gap: 6px; }

/* Tables */
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow-x: auto; box-shadow: var(--shadow); }
.tbl { width: 100%; border-collapse: collapse; min-width: 980px; }
.tbl th, .tbl td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
.tbl th { position: sticky; top: 0; background: var(--surface-2); font-size: 12px; font-weight: 600; color: var(--muted); cursor: pointer; user-select: none; z-index: 1; }
.tbl th.n, .tbl td.n { text-align: right; }
.tbl tbody tr { cursor: pointer; }
.tbl tbody tr:hover { background: #faf6ef; }
.tbl td.wrap { white-space: normal; max-width: 260px; }
.tbl .neg { color: var(--paid); }
.tbl .red { color: var(--pastdue); font-weight: 600; }
.tbl tfoot td { font-weight: 600; background: var(--surface-2); }

/* Aging */
.aging { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; margin-bottom: 14px; }

/* Rack */
.rack { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; }
.bldg { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; box-shadow: var(--shadow); }
.bldg-h { display: flex; justify-content: space-between; font-weight: 600; font-size: 13px; margin: 0 2px 8px; }
.bldg-h span { color: var(--muted); font-weight: 500; }
.rooms { display: grid; gap: 5px; }
.room-t { display: grid; grid-template-columns: 54px 1fr auto; gap: 8px; align-items: center; padding: 6px 8px; border-radius: 7px; border: 1px solid transparent; text-align: left; background: var(--vacant-bg); width: 100%; min-height: 40px; }
.room-t:hover { border-color: var(--text); }
.room-t .rn { font-weight: 600; font-size: 13px; }
.room-t .gn { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-t .gd { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; }
.room-t.s-pastdue { background: var(--pastdue-bg); border-left: 4px solid var(--pastdue); }
.room-t.s-overdue { background: var(--overdue-bg); border-left: 4px solid var(--overdue); }
.room-t.s-balance { background: var(--overdue-bg); border-left: 4px solid var(--overdue); }
.room-t.s-dueout { background: var(--dueout-bg); border-left: 4px solid var(--dueout); }
.room-t.s-soon { background: var(--soon-bg); border-left: 4px solid var(--soon); }
.room-t.s-paid { background: var(--paid-bg); border-left: 4px solid var(--paid); }
.room-t.s-vacant { border-left: 4px solid #cfc8bb; }
.room-t.s-vacant .gn { color: var(--muted); }
.room-t.s-reserved { background: var(--reserved-bg); border-left: 4px solid var(--reserved); }
.room-t.dim { opacity: .25; }

/* Tape chart */
.tape-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: auto; max-height: calc(100dvh - 200px); box-shadow: var(--shadow); }
.tape { display: grid; position: relative; min-width: max-content; }
.tape .hd { position: sticky; top: 0; background: var(--surface-2); z-index: 3; font-size: 11px; color: var(--muted); text-align: center; padding: 6px 0; border-bottom: 1px solid var(--line); border-left: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.tape .hd.today { background: var(--brand); color: var(--brand-ink); }
.tape .hd.wk { color: var(--text); font-weight: 600; }
.tape .corner { position: sticky; left: 0; top: 0; z-index: 4; background: var(--surface-2); border-bottom: 1px solid var(--line); padding: 6px 10px; font-size: 11px; font-weight: 600; color: var(--muted); }
.tape .rl { position: sticky; left: 0; z-index: 2; background: var(--surface); border-bottom: 1px solid var(--line); padding: 0 10px; display: flex; align-items: center; gap: 8px; font-size: 12px; height: 34px; cursor: pointer; white-space: nowrap; overflow: hidden; }
.tape .rl b { width: 48px; flex: none; }
.tape .rl span { overflow: hidden; text-overflow: ellipsis; max-width: 150px; }
.tape .cell { border-bottom: 1px solid var(--line); border-left: 1px solid #efe9df; height: 34px; }
.tape .cell.today { background: rgba(29, 92, 77, .06); }
.tape .bar { height: 22px; align-self: center; border-radius: 6px; font-size: 11px; display: flex; align-items: center; padding: 0 6px; overflow: hidden; white-space: nowrap; z-index: 1; cursor: pointer; font-weight: 500; }
.tape .bar.paid { background: var(--paid); color: #fff; }
.tape .bar.unpaid { background: repeating-linear-gradient(135deg, var(--pastdue) 0 6px, #c9463e 6px 12px); color: #fff; }
.tape .bar.ended { background: repeating-linear-gradient(135deg, #e9c46a 0 6px, #f1d68e 6px 12px); color: #5b4300; }
.tape .bar.res { background: var(--reserved); color: #fff; }
.tape .mark { width: 3px; background: var(--text); align-self: stretch; z-index: 2; margin: 4px 0; border-radius: 2px; justify-self: end; }

/* Drawer */
.drawer-bg { position: fixed; inset: 0; background: rgba(33, 30, 26, .35); z-index: 40; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(560px, 100vw); background: var(--surface); z-index: 41; display: flex; flex-direction: column; box-shadow: -8px 0 30px rgba(0, 0, 0, .12); }
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; padding: 18px 20px 14px; border-bottom: 1px solid var(--line); }
.drawer-head h2 { font-size: 19px; margin: 2px 0 6px; }
.drawer-head #dBadges { display: flex; gap: 6px; flex-wrap: wrap; }
.drawer-body { overflow-y: auto; overscroll-behavior: contain; padding: 16px 20px 32px; }
.facts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 16px; margin-bottom: 18px; }
.facts div { min-width: 0; }
.facts dt { color: var(--muted); font-size: 12px; }
.facts dd { margin: 1px 0 0; font-weight: 500; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.facts .big dd { font-size: 18px; font-weight: 600; }
.section-t { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 0 8px; }
.note { background: var(--surface-2); border-radius: 8px; padding: 10px 12px; white-space: pre-wrap; font-size: 13px; }
.folio { width: 100%; border-collapse: collapse; font-size: 13px; }
.folio td { padding: 6px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
.folio td.n { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.folio tr.pay td { color: var(--paid); }
.folio tr.open td:first-child::after { content: ' • open'; color: var(--pastdue); font-size: 11px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.actions a { text-decoration: none; }

/* Loading / errors */
.loading { max-width: 1440px; margin: 0 auto; display: grid; gap: 14px; }
.sk { background: linear-gradient(90deg, #ece6dc 0%, #f5f0e8 50%, #ece6dc 100%); background-size: 200% 100%; animation: sh 1.2s infinite; border-radius: var(--radius); }
.sk-row { height: 86px; } .sk-grid { height: 260px; } .sk-table { height: 320px; }
@keyframes sh { to { background-position: -200% 0; } }
.error-box { max-width: 1440px; margin: 0 auto 16px; background: var(--pastdue-bg); color: var(--pastdue); border-radius: var(--radius); padding: 14px 16px; }

@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .topbar { flex-wrap: wrap; }
  .tabs { order: 3; flex-basis: 100%; }
  .top-actions { margin-left: auto; }
}
@media (max-width: 720px) {
  .main { padding: 16px 12px 40px; }
  .topbar { padding: 10px 12px; gap: 8px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: 1fr; }
  .aging { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filters input { min-width: 0; max-width: none; flex-basis: 100%; }
  #updated { display: none; }
  .facts { grid-template-columns: 1fr 1fr; }
  .main, .page-head, .legend { min-width: 0; }
  .legend { flex-wrap: wrap; }
}
@media print {
  .topbar .tabs, .top-actions, .filters, .seg, .drawer, .drawer-bg { display: none !important; }
  .app { display: block; height: auto; }
  .main { overflow: visible; padding: 0; }
  body { background: #fff; }
  .card, .kpi, .table-wrap, .bldg { box-shadow: none; break-inside: avoid; }
  .tbl { min-width: 0; font-size: 11px; }
  .tape-wrap { max-height: none; overflow: visible; }
}
.folio tr.rev td { color: var(--pastdue); }

.room-t .gd { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 110px; }

/* Housekeeping */
.hk-name { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); font-weight: 500; }
.hk-name input { width: 160px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); font: inherit; color: var(--text); }
.hk-kpis { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.hk-grid { grid-template-columns: minmax(0, 1.7fr) minmax(0, 1fr); align-items: start; }
.hk-row { display: grid; grid-template-columns: 64px minmax(0, 1fr) auto; gap: 10px; align-items: center; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.hk-row:last-child { border-bottom: 0; }
.hk-row .rn { font-weight: 600; }
.hk-row .who { min-width: 0; }
.hk-row .who b { font-weight: 500; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hk-row .who span { color: var(--muted); font-size: 12px; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hk-btns { display: inline-flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.hk-btns button { border: 1px solid var(--line); background: var(--surface); padding: 6px 9px; border-radius: 7px; font-size: 12px; font-weight: 500; color: var(--muted); min-height: 32px; }
.hk-btns button:hover { border-color: var(--text); color: var(--text); }
.hk-btns button.on.hk-dirty { background: var(--pastdue-bg); color: var(--pastdue); border-color: var(--pastdue); }
.hk-btns button.on.hk-clean { background: var(--paid-bg); color: var(--paid); border-color: var(--paid); }
.hk-btns button.on.hk-inspected { background: #dfeaf8; color: var(--dueout); border-color: var(--dueout); }
.hk-btns button.on.hk-ooo { background: #2b2722; color: #fff; border-color: #2b2722; }
.hk-btns button:disabled { opacity: .5; }
.hk-pill { display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 99px; font-size: 12px; font-weight: 500; white-space: nowrap; }
.hk-pill::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.hk-pill.hk-dirty { color: var(--pastdue); background: var(--pastdue-bg); }
.hk-pill.hk-clean { color: var(--paid); background: var(--paid-bg); }
.hk-pill.hk-inspected { color: var(--dueout); background: #dfeaf8; }
.hk-pill.hk-ooo { color: #fff; background: #2b2722; }
.hk-pill.hk-unset { color: var(--vacant); background: var(--vacant-bg); }
.hk-meta { font-size: 11px; color: var(--muted); margin-top: 2px; }
.hk-note { margin-top: 3px; font-size: 12px; color: var(--overdue); }
.hk-log li { grid-template-columns: 64px 1fr auto !important; cursor: default !important; }
.room-t .hkd { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; vertical-align: middle; }
.hkd.hk-dirty { background: var(--pastdue); } .hkd.hk-clean { background: var(--paid); } .hkd.hk-inspected { background: var(--dueout); } .hkd.hk-ooo { background: #2b2722; }
.drawer .hk-box { background: var(--surface-2); border-radius: 10px; padding: 12px; margin-bottom: 16px; }
.drawer .hk-box .hk-btns { justify-content: flex-start; margin-top: 8px; }
.drawer .hk-box input { width: 100%; margin-top: 8px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; font: inherit; background: var(--surface); }
@media (max-width: 1100px) { .hk-grid { grid-template-columns: 1fr; } .hk-kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 720px) {
  .hk-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .hk-row { grid-template-columns: 54px minmax(0, 1fr); }
  .hk-row .hk-right { grid-column: 1 / -1; }
  .hk-btns { justify-content: flex-start; }
  .hk-btns button { flex: 1; min-height: 38px; }
  .hk-name { width: 100%; } .hk-name input { flex: 1; width: auto; }
}
@media print { .hk-btns, .hk-name { display: none !important; } }
.hk-row .who span.hk-pill { display: inline-flex; color: inherit; font-size: 12px; overflow: visible; }
.hk-row .who span.hk-pill.hk-dirty { color: var(--pastdue); } .hk-row .who span.hk-pill.hk-clean { color: var(--paid); }
.hk-row .who span.hk-pill.hk-inspected { color: var(--dueout); } .hk-row .who span.hk-pill.hk-ooo { color: #fff; } .hk-row .who span.hk-pill.hk-unset { color: var(--vacant); }
.hk-row .who span.hk-meta { display: inline; margin-left: 4px; }
.hk-row .who > span:not(.hk-pill) { margin-top: 3px; }
