:root {
  --bg: #f4f5f7; --panel: #fff; --text: #1f2937; --muted: #6b7280; --line: #e5e7eb;
  --primary: #2563eb; --primary-weak: #eff6ff; --danger: #dc2626; --ok: #16a34a; --warn: #d97706; --line-green: #06c755;
  --vacant: #dcfce7; --occupied: #dbeafe; --reserved: #fef3c7; --maint: #e5e7eb;
  --radius: 10px;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: 'Sarabun', 'Noto Sans Thai', system-ui, sans-serif; font-size: 15px; color: var(--text); background: var(--bg); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 18px; margin: 0 0 10px; }
h3 { font-size: 16px; margin: 0 0 8px; }

/* layout */
.shell { display: grid; grid-template-columns: 230px 1fr; min-height: 100vh; }
.side { background: #111827; color: #d1d5db; padding: 16px 10px; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.side .brand { color: #fff; font-weight: 700; font-size: 17px; padding: 4px 10px 16px; }
.side a { display: flex; align-items: center; gap: 8px; color: #d1d5db; padding: 9px 12px; border-radius: 8px; margin-bottom: 2px; text-decoration: none; }
.side a:hover { background: #1f2937; }
.side a.active { background: var(--primary); color: #fff; }
.side .sep { font-size: 12px; color: #6b7280; padding: 14px 12px 4px; }
.side .badge { margin-left: auto; background: var(--danger); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 7px; }
.main { padding: 20px 24px 60px; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.topbar .spacer { flex: 1; }
.btn.menu-btn { display: none; }

/* components */
.card { background: var(--panel); border-radius: var(--radius); padding: 16px; box-shadow: 0 1px 2px rgba(0,0,0,.05); margin-bottom: 16px; }
.grid { display: grid; gap: 14px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.kpi .label { color: var(--muted); font-size: 13px; }
.kpi .value { font-size: 24px; font-weight: 700; margin-top: 2px; }
.kpi .sub { color: var(--muted); font-size: 13px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.right { text-align: right; }
.danger { color: var(--danger); }
.ok { color: var(--ok); }
.warn { color: var(--warn); }
hr { border: 0; border-top: 1px solid var(--line); margin: 14px 0; }

.btn { font: inherit; border: 1px solid #d1d5db; background: #fff; color: var(--text); padding: 7px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { background: #f9fafb; text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: #1d4ed8; }
.btn.danger { color: var(--danger); border-color: #fecaca; }
.btn.line { background: var(--line-green); border-color: var(--line-green); color: #fff; }
.btn.sm { padding: 3px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

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: middle; }
table.t th { font-weight: 600; color: var(--muted); font-size: 13px; background: #fafafa; }
table.t td.num, table.t th.num { text-align: right; white-space: nowrap; }
table.t tr.click { cursor: pointer; }
table.t tr.click:hover td { background: #f9fafb; }
.table-wrap { overflow-x: auto; }

.pill { display: inline-block; padding: 1px 10px; border-radius: 999px; font-size: 12.5px; background: #e5e7eb; white-space: nowrap; }
.pill.draft { background: #f3f4f6; color: #6b7280; }
.pill.issued, .pill.in_progress { background: #dbeafe; color: #1e40af; }
.pill.partial { background: #fef3c7; color: #92400e; }
.pill.paid, .pill.done, .pill.active, .pill.sent { background: #dcfce7; color: #166534; }
.pill.overdue, .pill.urgent, .pill.failed { background: #fee2e2; color: #991b1b; }
.pill.cancelled, .pill.ended, .pill.renewed, .pill.skipped { background: #f3f4f6; color: #6b7280; }
.pill.open, .pill.high { background: #ffedd5; color: #9a3412; }
.pill.simulated { background: #ede9fe; color: #5b21b6; }
.pill.line { background: #d1fae5; color: #047857; }

label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 3px; }
input, select, textarea { font: inherit; width: 100%; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 8px; background: #fff; color: var(--text); }
input:focus, select:focus, textarea:focus { outline: 2px solid #bfdbfe; border-color: var(--primary); }
textarea { resize: vertical; }
input[type=checkbox] { width: auto; }
.field { margin-bottom: 12px; }
.check { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 15px; }
.inline-input { width: 110px; padding: 6px 8px; }

/* room map */
.floor { margin-bottom: 14px; }
.floor-title { font-weight: 600; margin-bottom: 6px; color: var(--muted); }
.rooms { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 8px; }
.room { border-radius: 8px; padding: 8px 10px; cursor: pointer; border: 1px solid transparent; min-height: 74px; }
.room:hover { border-color: #9ca3af; }
.room .no { font-weight: 700; font-size: 16px; }
.room .who { font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room.vacant { background: var(--vacant); }
.room.occupied { background: var(--occupied); }
.room.reserved { background: var(--reserved); }
.room.maintenance { background: var(--maint); }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; }
.legend span::before { content: ''; display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; background: var(--c); }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(17,24,39,.45); display: flex; align-items: flex-start; justify-content: center; padding: 40px 12px; z-index: 50; overflow-y: auto; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 620px; padding: 20px; box-shadow: 0 10px 30px rgba(0,0,0,.2); }
.modal.wide { max-width: 900px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.modal-head .x { border: 0; background: none; font-size: 22px; cursor: pointer; color: var(--muted); }

#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 6px; align-items: center; }
#toast div { background: #111827; color: #fff; padding: 10px 18px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,.2); max-width: 90vw; }
#toast div.err { background: var(--danger); }

.login { max-width: 380px; margin: 10vh auto; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; flex-wrap: wrap; }
.tabs a { padding: 8px 14px; border-bottom: 2px solid transparent; color: var(--muted); text-decoration: none; }
.tabs a.active { border-color: var(--primary); color: var(--primary); font-weight: 600; }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.notice { background: var(--primary-weak); border: 1px solid #bfdbfe; padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; }
.notice.warn { background: #fffbeb; border-color: #fde68a; }
.code { font-family: ui-monospace, Menlo, monospace; background: #f3f4f6; padding: 2px 6px; border-radius: 4px; word-break: break-all; }
.big-code { font-size: 30px; letter-spacing: 6px; font-weight: 700; font-family: ui-monospace, Menlo, monospace; }
.chat { background: #8cabd9; border-radius: 10px; padding: 12px; height: 340px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
.chat .me { align-self: flex-end; background: #a7f3a0; }
.chat .bot { align-self: flex-start; background: #fff; }
.chat div { padding: 8px 12px; border-radius: 14px; max-width: 80%; white-space: pre-wrap; font-size: 14px; }
.slip-img { max-width: 100%; max-height: 360px; border-radius: 8px; border: 1px solid var(--line); }
.warn-cell { background: #fffbeb; }
.err-cell { background: #fee2e2; }

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; left: -260px; width: 240px; z-index: 40; transition: left .2s; }
  .side.open { left: 0; }
  .btn.menu-btn { display: inline-flex; }
  .g3, .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .main { padding: 14px 12px 60px; }
}
@media (max-width: 560px) {
  .g2, .g3, .g4 { grid-template-columns: 1fr; }
  .kpi .value { font-size: 20px; }
  .hide-sm { display: none; }
}

/* จดมิเตอร์บนมือถือ: แสดงเป็นการ์ดต่อห้อง ไม่ต้องเลื่อนซ้าย-ขวา */
@media (max-width: 560px) {
  .meter-table thead { display: none; }
  .meter-table tr { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
  .meter-table td { border: 0; padding: 2px 0; }
  .meter-table td.room-cell { grid-column: 1 / -1; font-size: 16px; }
  .meter-table td[data-label]::before { content: attr(data-label); display: block; font-size: 12px; color: var(--muted); }
  .meter-table td.num { text-align: left; }
  .meter-table .inline-input { width: 100%; }
}
