:root {
  --bg: #f3f5f9;
  --card: #fff;
  --ink: #0f172a;
  --ink-2: #334155;
  --muted: #64748b;
  --line: #e3e8ef;
  --line-2: #eef1f6;

  --brand: #2563eb;
  --brand-2: #1d4ed8;
  --brand-soft: #eef4ff;

  --side: #0b1324;
  --side-2: #152038;
  --side-ink: #9aa7bd;

  --red: #dc2626;
  --red-soft: #fef1f1;
  --red-line: #fbd0d0;
  --orange: #c2410c;
  --orange-soft: #fff6ec;
  --orange-line: #fed7aa;
  --green: #15803d;
  --green-soft: #effaf2;
  --gray-soft: #f4f6f9;

  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06);
  --font: 'IBM Plex Sans Thai', 'Noto Sans Thai', 'Leelawadee UI', Tahoma, system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body { font: 14px/1.55 var(--font); color: var(--ink); background: var(--bg); -webkit-font-smoothing: antialiased; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--brand); }
code { font-family: ui-monospace, Consolas, monospace; background: var(--gray-soft); padding: 1px 6px; border-radius: 6px; font-size: .92em; }
.muted { color: var(--muted); }
.boot { display: grid; place-items: center; min-height: 100vh; color: var(--muted); }

/* ── ปุ่ม / ช่องกรอก ───────────────────────── */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 38px; padding: 0 14px; border: 1px solid var(--line); background: #fff; border-radius: 9px; cursor: pointer; font-weight: 500; white-space: nowrap; transition: background .15s, border-color .15s; }
.btn:hover { background: var(--gray-soft); }
.btn:disabled { opacity: .6; cursor: default; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-2); }
.btn.block { width: 100%; height: 44px; }
.btn.sm { height: 30px; padding: 0 10px; font-size: 13px; }
.btn.danger { color: var(--red); border-color: var(--red-line); }
.icon-btn { display: inline-grid; place-items: center; width: 36px; height: 36px; border: 0; background: transparent; border-radius: 9px; cursor: pointer; color: inherit; flex: none; }
.icon-btn:hover { background: rgba(148, 163, 184, .18); }

label { display: flex; flex-direction: column; gap: 6px; font-weight: 500; color: var(--ink-2); }
input, select { height: 40px; border: 1px solid var(--line); border-radius: 9px; padding: 0 12px; background: #fff; outline: none; transition: border-color .15s, box-shadow .15s; min-width: 0; }
input:focus, select:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(37, 99, 235, .15); }
.form-err { color: var(--red); min-height: 1.2em; margin: 0; font-size: 13px; }
.note { border-radius: 10px; padding: 10px 12px; font-size: 13px; line-height: 1.65; }
.note.demo { background: #fff8e6; border: 1px solid #fde4a8; color: #6b4200; }

/* ── ล็อกอิน ───────────────────────────────── */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: radial-gradient(900px 420px at 15% -10%, #dbe6ff 0, transparent 60%), var(--bg); }
.auth-card { width: 100%; max-width: 400px; background: #fff; border: 1px solid var(--line); border-radius: 16px; padding: 28px; box-shadow: 0 12px 32px rgba(15, 23, 42, .08); display: flex; flex-direction: column; gap: 14px; }
.auth-card h1 { font-size: 22px; margin: 6px 0 0; }
.auth-card p { margin: 0; }
.setup-link { text-align: center; }

/* ── แบรนด์ ────────────────────────────────── */
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { display: grid; place-items: center; width: 38px; height: 38px; border-radius: 10px; background: linear-gradient(135deg, #3b82f6, #1d4ed8); color: #fff; font-weight: 700; letter-spacing: .5px; flex: none; }
.brand-text { display: flex; flex-direction: column; line-height: 1.25; }
.brand-text b { font-size: 15px; }
.brand-text small { color: var(--muted); font-size: 12px; }

/* ── โครงหน้า ──────────────────────────────── */
.shell { display: grid; grid-template-columns: 248px minmax(0, 1fr); min-height: 100vh; }
.side { position: sticky; top: 0; height: 100vh; background: var(--side); color: var(--side-ink); display: flex; flex-direction: column; padding: 18px 12px; gap: 20px; }
.side .brand { padding: 0 6px; }
.side .brand-text b { color: #fff; }
.side .brand-text small { color: var(--side-ink); }
.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 9px; color: var(--side-ink); text-decoration: none; font-weight: 500; }
.nav-item:hover { background: var(--side-2); color: #fff; }
.nav-item.active { background: var(--brand); color: #fff; }
.nav-item em { margin-left: auto; font-style: normal; font-size: 11px; padding: 1px 7px; border-radius: 99px; background: rgba(148, 163, 184, .2); color: #cbd5e1; white-space: nowrap; }
.nav-ico { width: 20px; text-align: center; }
.me { display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 10px; background: var(--side-2); }
.avatar { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 50%; background: #334155; color: #fff; font-weight: 600; flex: none; }
.me-text { display: flex; flex-direction: column; min-width: 0; flex: 1; line-height: 1.3; }
.me-text b { color: #fff; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-text small { font-size: 12px; }
.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 10px; min-height: 60px; padding: 0 24px; background: rgba(243, 245, 249, .88); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.page-title { font-size: 18px; margin: 0; font-weight: 600; }
.menu-btn { display: none; }
.pill { font-size: 12px; padding: 2px 10px; border-radius: 99px; font-weight: 500; white-space: nowrap; }
.pill.demo { background: #fff1c7; color: #6b4200; margin-left: auto; }
.content { padding: 20px 24px 48px; display: flex; flex-direction: column; gap: 16px; width: 100%; max-width: 1440px; }
.scrim { display: none; }

/* ── หัวหน้างานวันนี้ ─────────────────────── */
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.meta { margin: 0; color: var(--muted); }
.meta b { color: var(--ink); }
.head-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.speed { font-size: 12px; padding: 4px 10px; border-radius: 99px; font-weight: 500; cursor: help; white-space: nowrap; }
.speed.fast { background: var(--green-soft); color: var(--green); }
.speed.mid { background: var(--orange-soft); color: var(--orange); }
.speed.slow { background: var(--red-soft); color: var(--red); }

.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.kpi { all: unset; box-sizing: border-box; cursor: pointer; position: relative; overflow: hidden; display: flex; flex-direction: column; gap: 2px; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px 14px 18px; box-shadow: var(--shadow); transition: transform .12s, box-shadow .12s; }
.kpi::before { content: ''; position: absolute; inset: 0 auto 0 0; width: 4px; background: var(--tone); }
.kpi:hover { transform: translateY(-1px); box-shadow: 0 6px 16px rgba(15, 23, 42, .08); }
.kpi:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.kpi.red { --tone: var(--red); }
.kpi.orange { --tone: #f59e0b; }
.kpi.blue { --tone: var(--brand); }
.kpi-label { font-weight: 600; color: var(--ink-2); }
.kpi-num { font-size: 30px; font-weight: 700; line-height: 1.15; color: var(--tone); font-variant-numeric: tabular-nums; }
.kpi.zero .kpi-num { color: #a3b0c2; }
.kpi-hint { font-size: 12px; color: var(--muted); }

.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.search { position: relative; flex: 1 1 260px; max-width: 420px; }
.search span { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); display: flex; }
.search input { width: 100%; padding-left: 36px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { height: 32px; padding: 0 12px; border-radius: 99px; border: 1px solid var(--line); background: #fff; cursor: pointer; font-weight: 500; font-size: 13px; }
.chip.on { background: var(--ink); border-color: var(--ink); color: #fff; }
.toggle { flex-direction: row; align-items: center; gap: 8px; cursor: pointer; margin-left: auto; }
.toggle input { width: 16px; height: 16px; accent-color: var(--brand); }

/* ── การ์ด / รายการ ───────────────────────── */
.lists { display: flex; flex-direction: column; gap: 16px; }
.card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.list { overflow: hidden; scroll-margin-top: 76px; }
.list-head { display: flex; align-items: center; gap: 10px; padding: 13px 16px; border-bottom: 1px solid var(--line-2); }
.list-head h2 { font-size: 15px; margin: 0; }
.count { display: inline-grid; place-items: center; min-width: 24px; height: 22px; padding: 0 8px; border-radius: 99px; background: var(--gray-soft); font-size: 12px; font-weight: 600; color: var(--ink-2); }
.list.red .list-head { background: var(--red-soft); }
.list.red .count { background: var(--red); color: #fff; }
.ok-banner { scroll-margin-top: 76px; padding: 12px 16px; border-radius: var(--radius); background: var(--green-soft); border: 1px solid #c9ecd4; color: var(--green); font-weight: 500; }
.ok-banner.unsure { background: var(--gray-soft); border-color: var(--line); color: var(--ink-2); }
.warn-banner { border-radius: var(--radius); padding: 14px 16px; border: 1px solid; }
.warn-banner.bad { background: var(--red-soft); border-color: var(--red-line); color: #7f1d1d; }
.warn-banner.info { background: #eff6ff; border-color: #bfdbfe; color: #1e3a8a; }
.warn-banner b { display: block; margin-bottom: 6px; font-size: 15px; }
.warn-banner ul { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 3px; }
.warn-banner p { margin: 8px 0 0; font-weight: 500; }
.kpis.unsure .kpi-num::after { content: ' ?'; font-size: 18px; color: var(--red); }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }

.table-wrap { overflow-x: auto; }
.grid { width: 100%; border-collapse: collapse; }
.grid th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); background: #fafbfd; padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.grid td { padding: 11px 12px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
.grid tr:last-child td { border-bottom: 0; }
.grid tbody tr:hover td { background: #fafcff; }
.grid th.wide { min-width: 280px; }
.grid .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.pre { white-space: pre-line; }
.strong { font-weight: 600; }
tr.is-done td { opacity: .5; }
tr.is-done td:last-child { opacity: 1; }

.badge { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge.sev0 { background: var(--red); color: #fff; }
.badge.sev1 { background: var(--red-soft); color: var(--red); border: 1px solid var(--red-line); }
.badge.sev2 { background: var(--orange-soft); color: var(--orange); border: 1px solid var(--orange-line); }
.badge.sev3 { background: var(--gray-soft); color: var(--muted); }
.ch { display: inline-block; font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 6px; background: var(--brand-soft); color: var(--brand-2); }
.order { all: unset; cursor: pointer; font-family: ui-monospace, Consolas, monospace; font-weight: 600; font-size: 13px; border-bottom: 1px dashed #94a3b8; white-space: nowrap; }
.order:hover { color: var(--brand); border-color: var(--brand); }
.status { height: 34px; min-width: 180px; max-width: 250px; font-size: 13px; background: #fffcf0; border-color: #efdc9c; }
.status.set { background: #fff; border-color: var(--line); }
.status.done { background: var(--green-soft); border-color: #bfe5cb; color: var(--green); font-weight: 600; }
.status:disabled { opacity: .55; }
.why { font-weight: 600; }
.why.warn { color: var(--orange); }
.why.bad { color: var(--red); }
.why.wait { color: var(--muted); }
.days-hot { color: var(--red); font-weight: 700; white-space: nowrap; }

.summary summary { cursor: pointer; padding: 14px 16px; font-weight: 600; }
.summary dl { margin: 0; padding: 0 16px 14px; display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 0 24px; }
.summary dl div { display: flex; justify-content: space-between; gap: 12px; padding: 7px 0; border-bottom: 1px dashed var(--line-2); }
.summary dt { color: var(--ink-2); }
.summary dd { margin: 0; font-weight: 600; text-align: right; }
.summary .sub { grid-column: 1 / -1; border-bottom: 0; padding-top: 14px; }
.summary .sub dt { font-weight: 700; color: var(--ink); }

.soon { padding: 28px; display: flex; flex-direction: column; gap: 10px; max-width: 760px; }
.soon h2 { margin: 0; font-size: 18px; }
.soon ul { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 6px; }
.soon p { margin: 6px 0 0; }
.tag { align-self: flex-start; font-size: 12px; padding: 2px 10px; border-radius: 99px; background: var(--brand-soft); color: var(--brand-2); font-weight: 600; }

/* ── ผู้ใช้ระบบ ─────────────────────────────── */
.users-layout { display: grid; grid-template-columns: 340px minmax(0, 1fr); gap: 16px; align-items: start; }
.form { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.form h2 { font-size: 15px; margin: 0; }
.card > h2 { display: flex; align-items: center; gap: 8px; font-size: 15px; margin: 0; padding: 13px 16px; border-bottom: 1px solid var(--line-2); }
.card.form > h2 { padding: 0; border-bottom: 0; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.state { font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 99px; white-space: nowrap; }
.state.on { background: var(--green-soft); color: var(--green); }
.state.off { background: var(--gray-soft); color: var(--muted); }

.dlg { border: 0; border-radius: 14px; padding: 0; width: min(420px, calc(100vw - 32px)); box-shadow: 0 20px 50px rgba(15, 23, 42, .25); }
.dlg::backdrop { background: rgba(15, 23, 42, .45); }
.dlg form { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.dlg h3 { margin: 0; font-size: 17px; }
.dlg p { margin: 0; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ── กำลังโหลด / ผิดพลาด / แจ้งเตือน ─────────── */
.skel { display: flex; flex-direction: column; gap: 12px; }
.skel div { height: 64px; border-radius: 12px; background: linear-gradient(90deg, #e9edf3 0, #f5f7fa 40%, #e9edf3 80%); background-size: 200% 100%; animation: shimmer 1.2s infinite linear; }
@keyframes shimmer { to { background-position: -200% 0; } }
.error-box { padding: 28px; text-align: center; display: flex; flex-direction: column; gap: 12px; align-items: center; }
.error-box p { margin: 0; color: var(--red); }

#toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; align-items: center; z-index: 50; pointer-events: none; width: calc(100% - 32px); max-width: 480px; }
.toast { background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: 0 8px 24px rgba(15, 23, 42, .25); font-size: 13px; transition: opacity .3s, transform .3s; }
.toast.err { background: var(--red); }
.toast.out { opacity: 0; transform: translateY(6px); }

/* ── จอเล็ก ────────────────────────────────── */
@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .users-layout { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 900px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .side { position: fixed; inset: 0 auto 0 0; width: 272px; z-index: 40; transform: translateX(-100%); transition: transform .2s ease; }
  .nav-open .side { transform: none; }
  .nav-open .scrim { display: block; position: fixed; inset: 0; background: rgba(15, 23, 42, .45); z-index: 30; }
  .menu-btn { display: inline-grid; }
  .topbar { padding: 0 12px; }
  .content { padding: 16px 16px 40px; }
  .toggle { margin-left: 0; }
}

@media (max-width: 760px) {
  .grid thead { display: none; }
  .grid, .grid tbody, .grid tr, .grid td { display: block; width: 100%; }
  .grid tr { padding: 10px 14px; border-bottom: 1px solid var(--line); }
  .grid tr:last-child { border-bottom: 0; }
  .grid td { border: 0; padding: 4px 0; display: grid; grid-template-columns: 96px minmax(0, 1fr); gap: 10px; }
  .grid td::before { content: attr(data-label); color: var(--muted); font-size: 12px; padding-top: 2px; }
  .grid .num { text-align: left; }
  .grid tbody tr:hover td { background: transparent; }
  .status { max-width: none; width: 100%; min-width: 0; }
  .row-actions { justify-content: flex-start; }
  .kpi-num { font-size: 26px; }
  .search { max-width: none; }
  .page-title { font-size: 16px; }
}
