:root {
  --bg: #f6f7f9; --panel: #ffffff; --panel-2: #f1f3f6; --text: #16181d; --muted: #5d6573; --line: #e2e5ea;
  --accent: #2563eb; --accent-text: #ffffff; --danger: #dc2626; --warn: #b45309; --ok: #15803d;
  --vacant: #16a34a; --booked: #7c3aed; --occupied: #2563eb; --expiring: #ea580c; --hold: #6b7280;
  --vacant-bg: #dcfce7; --booked-bg: #ede9fe; --occupied-bg: #dbeafe; --expiring-bg: #ffedd5; --hold-bg: #e5e7eb;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 1px 6px rgba(0,0,0,.04); --radius: 10px;
  --font: Sarabun, "Noto Sans Thai", "IBM Plex Sans Thai", Thonburi, Tahoma, system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115; --panel: #171a20; --panel-2: #1f232b; --text: #e8eaee; --muted: #9aa3b2; --line: #2b313b;
    --accent: #3b82f6; --danger: #f87171; --warn: #fbbf24; --ok: #4ade80;
    --vacant-bg: #12301f; --booked-bg: #2a1f45; --occupied-bg: #172a4a; --expiring-bg: #3a2312; --hold-bg: #2a2e35;
    --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --panel: #171a20; --panel-2: #1f232b; --text: #e8eaee; --muted: #9aa3b2; --line: #2b313b;
  --accent: #3b82f6; --danger: #f87171; --warn: #fbbf24; --ok: #4ade80;
  --vacant-bg: #12301f; --booked-bg: #2a1f45; --occupied-bg: #172a4a; --expiring-bg: #3a2312; --hold-bg: #2a2e35;
  --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 15px; line-height: 1.5; }
a { color: var(--accent); text-decoration: none; } a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; } h2 { font-size: 17px; margin: 0 0 10px; } h3 { font-size: 15px; margin: 14px 0 6px; }
.muted { color: var(--muted); } .small { font-size: 13px; } .right { text-align: right; } .nowrap { white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.danger { color: var(--danger); } .ok { color: var(--ok); } .warn { color: var(--warn); }

/* layout */
.shell { display: grid; grid-template-columns: 230px 1fr; min-height: 100vh; }
.nav { background: var(--panel); border-right: 1px solid var(--line); padding: 14px 10px; position: sticky; top: 0; height: 100vh; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.nav .brand { font-weight: 700; font-size: 16px; padding: 4px 10px 8px; }
.bpick { padding: 0 6px 10px; } .bpick:empty { display: none; } .bpick select { width: 100%; font-weight: 600; }
.btag { display: inline-block; font-size: 11px; font-weight: 600; padding: 0 7px; border-radius: 99px; background: var(--panel-2); color: var(--muted); border: 1px solid var(--line); vertical-align: middle; white-space: nowrap; }
h2.bhead { margin: 18px 0 4px; padding-bottom: 6px; border-bottom: 2px solid var(--accent); } h2.bhead:first-child { margin-top: 4px; }
tr.bhead-row td { background: var(--panel-2); font-weight: 700; }
.nav a { color: var(--text); padding: 8px 10px; border-radius: 8px; display: flex; gap: 8px; align-items: center; }
.nav a:hover { background: var(--panel-2); text-decoration: none; }
.nav a.on { background: var(--accent); color: var(--accent-text); }
.nbadge:not(:empty) { margin-left: auto; background: var(--danger); color: #fff; border-radius: 99px; font-size: 12px; min-width: 20px; padding: 0 6px; text-align: center; line-height: 20px; }
.thumb { width: 160px; max-height: 240px; object-fit: contain; border-radius: 8px; border: 1px solid var(--line); background: var(--panel-2); cursor: zoom-in; }
.slip { display: grid; grid-template-columns: 170px 1fr; gap: 14px; }
@media (max-width: 600px) { .slip { grid-template-columns: 1fr; } .thumb { width: 100%; } }
.timeline { list-style: none; padding: 0 0 0 14px; margin: 0; border-left: 2px solid var(--line); }
.timeline li { margin-bottom: 10px; }
.nav .me { margin-top: auto; padding: 10px; border-top: 1px solid var(--line); font-size: 13px; }
.nav .me button { margin-top: 6px; }
main { padding: 20px 24px 60px; min-width: 0; }
.topbar { display: none; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .nav { position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 40; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .nav.open { transform: none; }
  .topbar { display: flex; align-items: center; gap: 10px; position: sticky; top: 0; z-index: 30; background: var(--panel); border-bottom: 1px solid var(--line); padding: 8px 16px; }
  .topbar .t { font-weight: 700; }
  main { padding: 14px 16px 60px; }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 35; }
}

/* components */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px; }
.card.tight { padding: 10px 12px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 14px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 14px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.kpi .v { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; } .kpi .l { color: var(--muted); font-size: 13px; }
button, .btn { font: inherit; border: 1px solid var(--line); background: var(--panel); color: var(--text); border-radius: 8px; padding: 7px 14px; cursor: pointer; display: inline-flex; gap: 6px; align-items: center; white-space: nowrap; }
button:hover, .btn:hover { background: var(--panel-2); text-decoration: none; }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover { filter: brightness(1.08); }
button.danger-btn { color: var(--danger); border-color: var(--danger); }
button.link { border: 0; background: none; color: var(--accent); padding: 2px 4px; }
button:disabled { opacity: .5; cursor: not-allowed; }
button.small, .btn.small { padding: 4px 10px; font-size: 13px; }
input, select, textarea { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type=checkbox], input[type=radio] { width: auto; }
textarea { min-height: 70px; }
label.f { display: block; margin-bottom: 10px; } label.f > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 3px; }
.row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 0 12px; }
.check { display: flex; gap: 8px; align-items: center; margin: 4px 0; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); }
table.t { width: 100%; border-collapse: collapse; } table.t th, table.t td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
table.t th { font-size: 13px; color: var(--muted); font-weight: 600; background: var(--panel-2); position: sticky; top: 0; }
table.t tr:last-child td { border-bottom: 0; } table.t tr.click { cursor: pointer; } table.t tr.click:hover td { background: var(--panel-2); }
.pill { display: inline-block; padding: 1px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--panel-2); color: var(--muted); }
.pill.vacant, .pill.paid { background: var(--vacant-bg); color: var(--vacant); }
.pill.booked { background: var(--booked-bg); color: var(--booked); }
.pill.occupied, .pill.unpaid, .pill.draft { background: var(--occupied-bg); color: var(--occupied); }
.pill.expiring, .pill.partial, .pill.draft_issue { background: var(--expiring-bg); color: var(--expiring); }
.pill.hold, .pill.void { background: var(--hold-bg); color: var(--hold); }
.pill.overdue { background: var(--danger); color: #fff; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.chip { border-radius: 99px; padding: 4px 12px; font-size: 13px; }
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.empty { text-align: center; padding: 30px 10px; color: var(--muted); }
.notice { background: var(--expiring-bg); color: var(--text); border-left: 4px solid var(--expiring); padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; }
.notice.info { background: var(--occupied-bg); border-color: var(--occupied); }
.dl { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; } .dl dt { color: var(--muted); } .dl dd { margin: 0; }

/* room map */
.floor { margin-bottom: 14px; } .floor h3 { margin: 0 0 6px; font-size: 14px; color: var(--muted); }
.rooms { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.tile { border-radius: 10px; padding: 8px 9px; min-height: 70px; border: 1px solid transparent; cursor: pointer; position: relative; color: var(--text); display: block; }
.tile:hover { text-decoration: none; filter: brightness(.97); }
.tile .n { font-weight: 700; font-size: 17px; line-height: 1.1; } .tile .who { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile .st { font-size: 11px; color: var(--muted); }
.tile.vacant { background: var(--vacant-bg); border-color: var(--vacant); } .tile.booked { background: var(--booked-bg); border-color: var(--booked); }
.tile.occupied { background: var(--occupied-bg); border-color: transparent; } .tile.expiring { background: var(--expiring-bg); border-color: var(--expiring); }
.tile.hold { background: var(--hold-bg); border-color: var(--hold); }
.tile .badges { position: absolute; top: 5px; right: 6px; display: flex; gap: 3px; font-size: 12px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--danger); display: inline-block; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.legend i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; vertical-align: -1px; margin-right: 4px; }

/* meters */
.meter-in { width: 110px; text-align: right; font-variant-numeric: tabular-nums; }
.meter-cell { display: flex; flex-direction: column; gap: 2px; }
.meter-cell .used { font-size: 12px; color: var(--muted); }
.meter-cell.warn input { border-color: var(--warn); } .meter-cell.err input { border-color: var(--danger); } .meter-cell.saved input { border-color: var(--ok); }
.progress { height: 8px; background: var(--panel-2); border-radius: 99px; overflow: hidden; min-width: 140px; }
.progress > div { height: 100%; background: var(--ok); }

/* modal & toast */
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50; display: flex; align-items: flex-start; justify-content: center; padding: 40px 12px; overflow-y: auto; }
.modal { background: var(--panel); border-radius: 12px; width: 100%; max-width: 520px; padding: 18px 20px; box-shadow: 0 10px 40px rgba(0,0,0,.3); }
.modal.wide { max-width: 760px; }
.modal .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.modal .err { color: var(--danger); margin-top: 8px; white-space: pre-wrap; }
#toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; align-items: center; width: max-content; max-width: calc(100vw - 32px); }
.toast { background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); display: flex; gap: 10px; align-items: center; }
.toast.error { background: var(--danger); color: #fff; }
.toast a, .toast button { color: inherit; border-color: currentColor; background: transparent; }

.login { max-width: 380px; margin: 10vh auto; padding: 0 16px; position: relative; }
.theme-corner { position: absolute; right: 16px; top: -40px; }
.login .card { padding: 24px; }
@media print { .nav, .topbar, .toolbar, button { display: none !important; } main { padding: 0; } }
.login .brand { text-align: center; margin-top: 12px; letter-spacing: .02em; }
nav .brand { margin-top: 8px; opacity: .7; }
