:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-2: #f7f9fa;
  --border: #e1e6eb;
  --text: #17222c;
  --muted: #5b6875;
  --primary: #0f766e;
  --primary-hover: #0c625b;
  --primary-ink: #ffffff;
  --primary-soft: #e1f3f0;
  --warn: #a15c07;
  --warn-soft: #fdf1d6;
  --info: #1d4ed8;
  --info-soft: #e1eaff;
  --ok: #15803d;
  --ok-soft: #dcf5e4;
  --danger: #b42318;
  --danger-soft: #fde5e3;
  --neutral-soft: #eceff2;
  --sidebar: #0f2a2f;
  --sidebar-text: #cfe0de;
  --sidebar-active: #1c4148;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1418; --surface: #161d22; --surface-2: #1b242a; --border: #2a353d;
    --text: #e6ecef; --muted: #98a6b1; --primary: #2bb3a3; --primary-hover: #37c4b3; --primary-ink: #04201d;
    --primary-soft: #133a37; --warn: #f2b84b; --warn-soft: #3a2c10; --info: #8fb0ff; --info-soft: #1a2747;
    --ok: #5fd38a; --ok-soft: #12321f; --danger: #ff8a7f; --danger-soft: #3d1714; --neutral-soft: #232d34;
    --sidebar: #0b1013; --sidebar-text: #b9c9c7; --sidebar-active: #18292d;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1418; --surface: #161d22; --surface-2: #1b242a; --border: #2a353d;
  --text: #e6ecef; --muted: #98a6b1; --primary: #2bb3a3; --primary-hover: #37c4b3; --primary-ink: #04201d;
  --primary-soft: #133a37; --warn: #f2b84b; --warn-soft: #3a2c10; --info: #8fb0ff; --info-soft: #1a2747;
  --ok: #5fd38a; --ok-soft: #12321f; --danger: #ff8a7f; --danger-soft: #3d1714; --neutral-soft: #232d34;
  --sidebar: #0b1013; --sidebar-text: #b9c9c7; --sidebar-active: #18292d;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font-family: "IBM Plex Sans Thai", system-ui, sans-serif; font-size: 15px; line-height: 1.55;
}
h1, h2, h3 { margin: 0; line-height: 1.3; }
h1 { font-size: 22px; font-weight: 700; }
h3 { font-size: 16px; font-weight: 600; }
a { color: var(--primary); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-variant-numeric: tabular-nums; font-family: ui-monospace, "SF Mono", Consolas, monospace; font-size: 13.5px; }
.spacer { flex: 1; }
[hidden] { display: none !important; }

/* layout */
.layout { display: grid; grid-template-columns: 250px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--sidebar); color: var(--sidebar-text); padding: 20px 14px;
  display: flex; flex-direction: column; gap: 18px; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 6px; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 9px; background: var(--primary); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 15px; flex: none;
}
.brand-title { font-weight: 700; color: #fff; }
.brand-sub { font-size: 12.5px; opacity: .75; }
.who { background: rgba(255,255,255,.06); border-radius: 8px; padding: 10px 12px; display: grid; gap: 2px; }
.who b { color: #fff; font-weight: 600; }
.who-role { font-size: 12px; opacity: .75; }
.nav { display: grid; gap: 2px; flex: 1; align-content: start; }
.nav-item {
  color: var(--sidebar-text); text-decoration: none; padding: 9px 12px; border-radius: 7px;
  display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 14.5px;
}
.nav-item:hover { background: rgba(255,255,255,.05); }
.nav-item.active { background: var(--sidebar-active); color: #fff; font-weight: 600; }
.nav-count { background: #f2b84b; color: #2b1d00; font-size: 12px; font-weight: 700; border-radius: 99px; padding: 0 8px; min-width: 22px; text-align: center; }
.sidebar .btn-ghost { color: var(--sidebar-text); border-color: rgba(255,255,255,.15); }
.sidebar .btn-ghost:hover { background: rgba(255,255,255,.06); }

.main { padding: 26px 32px 60px; min-width: 0; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.banner {
  background: var(--info-soft); color: var(--info); border-radius: var(--radius); padding: 10px 14px;
  margin-bottom: 18px; display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}

/* cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 18px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h3 .muted { font-weight: 400; font-size: 14px; }
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; margin-bottom: 18px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); border-top: 3px solid var(--border); }
.kpi.warn { border-top-color: var(--warn); } .kpi.info { border-top-color: var(--info); } .kpi.ok { border-top-color: var(--ok); }
.kpi-label { color: var(--muted); font-size: 13.5px; }
.kpi-value { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; }
.empty { text-align: center; padding: 40px 18px; color: var(--muted); }
.empty-icon { width: 44px; height: 44px; margin: 0 auto 8px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); display: grid; place-items: center; font-size: 20px; }

/* tables */
.table-wrap { overflow-x: auto; margin: 0 -18px; padding: 0 18px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-weight: 600; color: var(--muted); font-size: 13px; padding: 9px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; background: var(--surface-2); }
td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: var(--surface-2); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.strong { font-weight: 700; }
.nowrap { white-space: nowrap; }
td.drug { min-width: 240px; }
.row-err td { background: var(--danger-soft) !important; }
.group-row td { background: var(--surface-2); font-weight: 600; font-size: 13.5px; }

/* pills */
.pill { display: inline-block; font-size: 12.5px; font-weight: 600; padding: 2px 10px; border-radius: 99px; white-space: nowrap; }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.info { background: var(--info-soft); color: var(--info); }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.danger { background: var(--danger-soft); color: var(--danger); }
.pill.muted { background: var(--neutral-soft); color: var(--muted); }

/* forms */
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 7px; padding: 8px 10px; min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--primary); }
.field { display: grid; gap: 5px; }
.field > span { font-size: 13.5px; color: var(--muted); font-weight: 500; }
.form-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; margin-bottom: 16px; }
.form-grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.room-match { padding: 8px 10px; border-radius: 7px; background: var(--surface-2); border: 1px dashed var(--border); min-height: 40px; font-weight: 600; }
.room-match.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.room-match.bad { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.edit-table input { width: 100%; padding: 6px 8px; }
.edit-table .w-code { width: 110px; } .edit-table .w-num { width: 90px; text-align: right; } .edit-table .w-unit { width: 100px; }
.qty-input { width: 90px; text-align: right; padding: 5px 8px; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.filters input[type="search"] { flex: 1; min-width: 200px; }
.row-actions { display: flex; gap: 8px; align-items: center; margin-top: 14px; flex-wrap: wrap; }
.error-text { color: var(--danger); font-size: 14px; min-height: 20px; }
.err-list { margin: 10px 0 0; padding: 10px 14px 10px 30px; background: var(--danger-soft); color: var(--danger); border-radius: 8px; font-size: 14px; }

/* buttons */
.btn {
  font: inherit; font-size: 14px; font-weight: 500; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 7px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger-ghost { color: var(--danger); }
.btn-ghost { background: transparent; }
.btn-sm { padding: 4px 10px; font-size: 13px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { border: none; background: none; font-size: 22px; line-height: 1; cursor: pointer; color: var(--muted); padding: 4px 8px; }
.link-btn { border: none; background: none; color: var(--primary); cursor: pointer; font: inherit; font-size: 13.5px; padding: 2px 4px; }
.link-btn.danger { color: var(--danger); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 18px; }
.tab { font: inherit; background: none; border: none; padding: 10px 16px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

/* login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: linear-gradient(160deg, var(--primary-soft), var(--bg) 55%); }
.login-card { width: 100%; max-width: 430px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 30px; box-shadow: 0 10px 30px rgba(16,24,40,.08); }
.login-card h1 { margin: 18px 0 4px; }
.login-card form { display: grid; gap: 12px; margin-top: 20px; }
.login-card input, .login-card select { font-size: 16px; padding: 10px 12px; }
.auth-alt { text-align: center; margin: 18px 0 0; padding-top: 16px; border-top: 1px solid var(--border); color: var(--muted); }
.side-actions { display: grid; gap: 6px; }
.login-card .brand-title { color: var(--text); }
.demo-codes { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--border); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { font: inherit; font-size: 13px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); border-radius: 99px; padding: 4px 12px; cursor: pointer; }
.chip b { font-family: ui-monospace, Consolas, monospace; color: var(--primary); }
.chip:hover { border-color: var(--primary); }

/* modal & toast */
.overlay { position: fixed; inset: 0; background: rgba(10, 20, 25, .45); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal { background: var(--surface); border-radius: 12px; width: 100%; max-width: 520px; max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.modal-wide { max-width: 860px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-body { padding: 18px; overflow: auto; }
.modal-body .field { margin-bottom: 12px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); }
#toast-root { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 60; }
.toast { background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-size: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.2); max-width: 360px; }
.toast-error { background: var(--danger); color: #fff; }

/* dispatch slip */
.slip { color: #111; background: #fff; padding: 8px; }
.slip-head { text-align: center; margin-bottom: 12px; }
.slip-head h2 { font-size: 18px; margin-top: 2px; }
.slip-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 20px; font-size: 14px; margin-bottom: 12px; }
.slip table th { background: none; color: #111; border-bottom: 1px solid #111; border-top: 1px solid #111; }
.slip table td { border-bottom: 1px solid #ddd; }
.slip tbody tr:hover td { background: none; }
.slip-sign { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-top: 48px; text-align: center; font-size: 14px; }
#print-root { display: none; }

@media print {
  body > *:not(#print-root) { display: none !important; }
  #print-root { display: block; }
  body { background: #fff; }
}

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; padding: 14px 16px; gap: 12px; }
  .nav { display: flex; overflow-x: auto; gap: 4px; }
  .nav-item { white-space: nowrap; }
  .main { padding: 18px 16px 48px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 520px) {
  .form-grid, .form-grid.cols-2 { grid-template-columns: 1fr; }
  .slip-meta { grid-template-columns: 1fr; }
}
