:root {
  --navy: #0B1F3A;
  --navy-2: #122a4d;
  --blue: #1B6CA8;
  --blue-soft: #e6f0f8;
  --gold: #C9973A;
  --gold-soft: #faf1e1;
  --bg: #F7F8FA;
  --surface: #ffffff;
  --text: #1a1f2e;
  --muted: #6b7280;
  --border: #e3e6ec;
  --ok: #1f8a4c;
  --ok-soft: #e3f4ea;
  --warn: #b26b00;
  --warn-soft: #fdf0dc;
  --danger: #b42323;
  --danger-soft: #fbe5e5;
  --oos: #8a94a6;
  --oos-soft: #eef0f4;
  --nonres: #6b5ca5;
  --nonres-soft: #efecf8;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(11, 31, 58, .06), 0 4px 14px rgba(11, 31, 58, .06);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 14px/1.45 "DM Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
[hidden] { display: none !important; }
h1, h2, h3, h4 { margin: 0; line-height: 1.25; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.02rem; }
a { color: var(--blue); }
.muted { color: var(--muted); }
.small { font-size: .86em; }
.error { color: var(--danger); }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Buttons & inputs ---------- */
.btn {
  font: inherit; font-weight: 600; cursor: pointer; border: 1px solid transparent; border-radius: 8px;
  padding: .5em 1em; min-height: 36px; background: var(--border); color: var(--text);
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; white-space: nowrap;
}
.btn:disabled { opacity: .55; cursor: progress; }
.btn.primary { background: var(--navy); color: #fff; }
.btn.primary:hover { background: var(--navy-2); }
.btn.gold { background: var(--gold); color: #fff; }
.btn.ghost { background: var(--surface); border-color: var(--border); }
.btn.ghost:hover { background: var(--blue-soft); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.small { padding: .25em .7em; min-height: 30px; font-size: .86em; }
.btn.big { min-height: 46px; width: 100%; font-size: 1rem; }
.btn.link { background: none; padding: 0; min-height: 0; color: var(--blue); font-weight: 500; }

input, select, textarea {
  font: inherit; color: inherit; border: 1px solid var(--border); border-radius: 8px; padding: .5em .65em;
  background: #fff; min-height: 36px; width: 100%;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--blue); outline-offset: -1px; border-color: transparent; }
label { display: grid; gap: 4px; font-weight: 600; font-size: .9em; }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 500; }
label.check input { width: auto; min-height: 0; }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(160deg, var(--navy), #1b3a63); }
.login-card { background: var(--surface); padding: 32px 28px; border-radius: 16px; width: min(380px, 100%); display: grid; gap: 14px; box-shadow: 0 20px 50px rgba(0,0,0,.25); text-align: center; }
.login-card img { margin: 0 auto; }
.login-card label { text-align: left; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px; padding: 8px 18px;
  background: var(--navy); color: #fff; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand span { display: grid; line-height: 1.1; }
.brand small { color: var(--gold); font-weight: 600; font-size: .72rem; letter-spacing: .08em; text-transform: uppercase; }
.tabs { display: flex; gap: 2px; flex: 1; flex-wrap: wrap; }
.tabs a { color: #c9d3e3; text-decoration: none; padding: 8px 12px; border-radius: 8px; font-weight: 500; }
.tabs a:hover { background: rgba(255,255,255,.08); color: #fff; }
.tabs a.active { background: rgba(255,255,255,.14); color: #fff; box-shadow: inset 0 -2px 0 var(--gold); }
.pill { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--gold); color: #fff; font-size: .75rem; text-align: center; }
.viewdate { display: flex; align-items: center; gap: 6px; font-weight: 500; color: #c9d3e3; }
.viewdate input { width: 150px; min-height: 32px; padding: .3em .5em; }
.viewdate .btn { min-height: 32px; }
.who { display: flex; align-items: center; gap: 8px; background: none; border: 0; color: #fff; cursor: pointer; font: inherit; }
.avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--gold); display: grid; place-items: center; font-weight: 700; }
.who-text { display: grid; text-align: left; line-height: 1.15; }
.who-role { font-size: .75rem; color: #c9d3e3; text-transform: capitalize; }
.menu { position: absolute; right: 16px; top: 54px; background: #fff; color: var(--text); border-radius: 10px; box-shadow: var(--shadow); border: 1px solid var(--border); display: grid; min-width: 180px; overflow: hidden; }
.menu button { background: none; border: 0; text-align: left; padding: 10px 14px; font: inherit; cursor: pointer; }
.menu button:hover { background: var(--blue-soft); }

/* ---------- Layout ---------- */
main { padding: 20px 18px 40px; max-width: 1500px; margin: 0 auto; }
.foot { text-align: center; color: var(--muted); font-size: .8rem; padding: 16px; }
.view { display: grid; gap: 16px; }
.view-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.view-head h2 { margin-right: auto; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.panel-head { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.panel-head h3 { margin-right: auto; }
.panel-body { padding: 14px 16px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: end; }
.filters > * { width: auto; }
.filters input[type=search] { min-width: 240px; }
.filters select { min-width: 150px; }

/* ---------- KPI cards ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); border-top: 3px solid var(--border); }
.stat b { display: block; font-size: 1.7rem; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: .85rem; }
.stat.occ { border-top-color: var(--blue); }
.stat.res { border-top-color: var(--gold); }
.stat.avail { border-top-color: var(--ok); }
.stat.oos { border-top-color: var(--oos); }
.stat.pct { border-top-color: var(--navy); }
.stat.click { cursor: pointer; }
.stat.click:hover { background: var(--blue-soft); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: #fafbfc; position: sticky; top: 0; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: var(--blue-soft); }
tr.group td { background: #f3f5f8; font-weight: 700; }
td .sub { display: block; color: var(--muted); font-size: .85em; }
.bar { height: 8px; background: var(--border); border-radius: 4px; overflow: hidden; display: flex; min-width: 90px; }
.bar i { display: block; height: 100%; }
.bar .o { background: var(--blue); }
.bar .r { background: var(--gold); }
.empty { padding: 24px; text-align: center; color: var(--muted); }
.more { padding: 10px 16px; color: var(--muted); }

/* ---------- Status chips ---------- */
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px; font-size: .78rem; font-weight: 600; white-space: nowrap; background: var(--border); color: var(--text); }
.chip.Occupied, .chip.Active { background: var(--blue-soft); color: var(--blue); }
.chip.Reserved { background: var(--gold-soft); color: #8c6418; }
.chip.Available { background: var(--ok-soft); color: var(--ok); }
.chip.Out-of-Service, .chip.Cancelled { background: var(--oos-soft); color: #5b6474; }
.chip.Non-Residential { background: var(--nonres-soft); color: var(--nonres); }
.chip.Ended { background: #eef0f4; color: var(--muted); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.danger { background: var(--danger-soft); color: var(--danger); }
.chip.Inactive { background: var(--oos-soft); color: #5b6474; }

/* ---------- Bed map ---------- */
.building { padding: 0; }
.building > .panel-head { cursor: pointer; }
.area { padding: 10px 16px 14px; border-top: 1px solid var(--border); }
.area:first-of-type { border-top: 0; }
.area-head { display: flex; gap: 10px; align-items: baseline; margin-bottom: 8px; flex-wrap: wrap; }
.area-head h4 { font-size: .95rem; }
.beds { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 8px; }
.bed {
  text-align: left; font: inherit; cursor: pointer; background: #fff; border: 1px solid var(--border); border-left: 4px solid var(--ok);
  border-radius: 8px; padding: 6px 9px; display: grid; gap: 1px; min-height: 58px;
}
.bed:hover { box-shadow: var(--shadow); border-color: var(--blue); }
.bed .lbl { font-size: .78rem; color: var(--muted); display: flex; justify-content: space-between; gap: 6px; }
.bed .nm { font-weight: 600; font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; color: inherit; }
.bed .co { font-size: .78rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bed.Occupied { border-left-color: var(--blue); }
.bed.Reserved { border-left-color: var(--gold); background: #fffcf6; }
.bed.Available .nm { color: var(--ok); }
.bed.Out-of-Service { border-left-color: var(--oos); background: #f7f8fa; }
.bed.Out-of-Service .nm { color: var(--muted); }
.bed.Non-Residential { border-left-color: var(--nonres); background: #faf9fd; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: .82rem; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }

/* ---------- Drawer & modal ---------- */
.drawer { position: fixed; inset: 0; z-index: 40; background: rgba(11,31,58,.35); display: flex; justify-content: flex-end; }
.drawer-body { width: min(560px, 100%); height: 100%; overflow-y: auto; background: var(--bg); box-shadow: -8px 0 30px rgba(0,0,0,.15); padding: 18px; display: grid; gap: 14px; align-content: start; }
.drawer-head { display: flex; gap: 10px; align-items: start; }
.drawer-head h2 { margin-right: auto; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 6px 12px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; display: grid; gap: 8px; }
.card h3 { display: flex; gap: 8px; align-items: center; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.timeline { display: grid; gap: 8px; }
.timeline .item { border-left: 3px solid var(--border); padding: 2px 0 2px 10px; }
.timeline .item.Active { border-left-color: var(--blue); }
.timeline .item.Reserved { border-left-color: var(--gold); }

.modal { position: fixed; inset: 0; z-index: 50; background: rgba(11,31,58,.45); display: grid; place-items: center; padding: 16px; }
.modal-card { background: var(--surface); border-radius: 14px; width: min(720px, 100%); max-height: calc(100vh - 32px); overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-card header { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 10px; position: sticky; top: 0; background: var(--surface); z-index: 1; }
.modal-card header h2 { margin-right: auto; font-size: 1.15rem; }
.modal-card .body { padding: 16px 20px; display: grid; gap: 14px; }
.modal-card footer { padding: 12px 20px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; position: sticky; bottom: 0; background: var(--surface); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.form-grid .wide { grid-column: 1 / -1; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { font: inherit; border: 0; background: #fff; padding: 7px 12px; cursor: pointer; font-weight: 500; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--navy); color: #fff; }
.picker { border: 1px solid var(--border); border-radius: 8px; max-height: 300px; overflow-y: auto; }
.picker .opt { display: flex; gap: 10px; justify-content: space-between; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--border); cursor: pointer; }
.picker .opt:last-child { border-bottom: 0; }
.picker .opt:hover { background: var(--blue-soft); }
.picker .opt.on { background: var(--navy); color: #fff; }
.picker .opt.on .muted { color: #c9d3e3; }
.picker .group { padding: 6px 10px; background: #f3f5f8; font-weight: 700; font-size: .85rem; position: sticky; top: 0; }
.selected-box { border: 1px dashed var(--blue); background: var(--blue-soft); border-radius: 8px; padding: 8px 12px; display: flex; justify-content: space-between; gap: 10px; align-items: center; }
.alert { border-radius: 8px; padding: 10px 12px; background: var(--danger-soft); color: var(--danger); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.info { background: var(--blue-soft); color: var(--blue); }
.alert ul { margin: 6px 0 0; padding-left: 18px; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 60; background: var(--navy); color: #fff; padding: 10px 18px; border-radius: 10px; box-shadow: var(--shadow); max-width: min(560px, calc(100% - 32px)); }
.toast.err { background: var(--danger); }
.busy { position: fixed; inset: 0; z-index: 70; display: grid; place-items: center; background: rgba(255,255,255,.4); }
.spinner { width: 38px; height: 38px; border: 4px solid var(--border); border-top-color: var(--navy); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 1200px) {
  .brand { margin-right: auto; }
  .tabs { order: 3; flex: 1 1 100%; overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; }
  .tabs::-webkit-scrollbar { display: none; }
  .tabs a { white-space: nowrap; }
}
@media (max-width: 760px) {
  .topbar { gap: 8px; padding: 8px 12px; }
  .viewdate > span { display: none; }
  .viewdate input { width: 140px; }
  .who-text { display: none; }
  main { padding: 14px 12px 30px; }
  .grid2 { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 110px 1fr; }
  .filters input[type=search] { min-width: 0; width: 100%; }
  .beds { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
}
@media print {
  .topbar, .foot, .filters, .btn { display: none !important; }
  main { padding: 0; }
}
