:root {
  --bg: #f3f6f6;
  --card: #ffffff;
  --ink: #17262a;
  --ink-2: #4b5d61;
  --muted: #8a9a9d;
  --line: #dfe7e8;
  --brand: #0f766e;
  --brand-ink: #0b5a54;
  --brand-soft: #e3f3f1;
  --accent: #d97706;
  --danger: #c2410c;
  --danger-soft: #fdece4;
  --warn-soft: #fff4dc;
  --ok: #15803d;
  --ed: #0f766e;
  --ned: #b45309;
  --ndm: #6d28d9;
  --cm: #be185d;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 40, 44, .06), 0 4px 16px rgba(16, 40, 44, .05);
  --font: 'IBM Plex Sans Thai', 'Sarabun', 'Leelawadee UI', Tahoma, system-ui, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font-family: var(--font); font-size: 15px; line-height: 1.5; color: var(--ink); background: var(--bg); }
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
h1, h2 { margin: 0; line-height: 1.3; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 24px; flex-wrap: wrap;
  padding: 10px 24px; background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center;
  background: var(--brand); color: #fff; font-weight: 700; font-size: 15px; letter-spacing: .5px;
}
.brand-mark.big { width: 52px; height: 52px; font-size: 20px; margin: 0 auto 8px; border-radius: 14px; }
.brand-title { font-weight: 700; font-size: 16px; }
.brand-sub { font-size: 12.5px; color: var(--ink-2); }
.tabs { display: flex; gap: 4px; margin-left: auto; }
.tab {
  padding: 8px 16px; border-radius: 999px; color: var(--ink-2); text-decoration: none; font-weight: 500;
  transition: background .15s, color .15s;
}
.tab:hover { background: var(--brand-soft); color: var(--brand-ink); }
.tab.active { background: var(--brand); color: #fff; }
.user-box { display: flex; align-items: center; gap: 12px; font-size: 13px; color: var(--ink-2); }

.demo-banner {
  background: var(--warn-soft); color: #7a4b00; text-align: center; font-size: 13.5px; padding: 8px 16px;
  border-bottom: 1px solid #f3dfae;
}

main { max-width: 1280px; margin: 0 auto; padding: 20px 20px 60px; }

/* ---------- Cards & layout ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px 22px; margin-bottom: 18px; }
.card-title { font-size: 20px; margin-bottom: 16px; }
.section-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin: 18px 0 10px; }
.card > .section-head:first-child { margin-top: 0; }
.section-head h2 { font-size: 16px; }
.grid-4 { display: grid; grid-template-columns: 170px 1.6fr 1fr 1fr; gap: 14px; }
.grid-filter { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }

/* ---------- Form fields ---------- */
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field.grow { flex: 1; }
.label { font-size: 13.5px; font-weight: 600; color: var(--ink-2); }
.label small, th small { font-weight: 400; color: var(--muted); }
.hint { font-size: 12.5px; color: var(--muted); }
.input {
  width: 100%; height: 42px; padding: 8px 12px; border: 1px solid #cad6d8; border-radius: 9px; background: #fff;
  outline: none; transition: border-color .15s, box-shadow .15s;
}
select.input { padding-right: 28px; }
.input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(15, 118, 110, .15); }
.input.input-error { border-color: var(--danger); box-shadow: 0 0 0 3px rgba(194, 65, 12, .12); }
.input-sm { height: 36px; width: auto; min-width: 130px; padding: 6px 10px; font-size: 14px; }
input[type=number]::-webkit-inner-spin-button { opacity: .4; }

/* ---------- Combobox ---------- */
.combo { position: relative; }
.combo-list {
  position: absolute; z-index: 30; top: calc(100% + 4px); left: 0; right: 0; min-width: 280px; max-height: 300px; overflow: auto;
  margin: 0; padding: 4px; list-style: none; background: #fff; border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 10px 30px rgba(16, 40, 44, .16);
}
.combo-list li { padding: 7px 10px; border-radius: 7px; cursor: pointer; display: flex; flex-direction: column; }
.combo-list li:hover, .combo-list li.active { background: var(--brand-soft); }
.combo-main { font-size: 14px; }
.combo-sub { font-size: 12px; color: var(--muted); }
.combo-list li.combo-empty { color: var(--muted); cursor: default; font-size: 13.5px; }
.items-table .combo-list { min-width: 420px; }

/* ---------- Items table ---------- */
.items-table-wrap { border: 1px solid var(--line); border-radius: 10px; overflow: visible; }
.items-table { width: 100%; border-collapse: collapse; }
.items-table th { text-align: left; font-size: 13px; color: var(--ink-2); font-weight: 600; background: #f7faf9; padding: 9px 10px; border-bottom: 1px solid var(--line); }
.items-table th:first-child { border-top-left-radius: 10px; }
.items-table th:last-child { border-top-right-radius: 10px; }
.items-table td { padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.items-table tr:last-child td { border-bottom: 0; }
.col-no { width: 40px; color: var(--muted); text-align: center; }
.col-invs { width: 190px; }
.col-type { width: 70px; }
.col-unit { width: 80px; color: var(--ink-2); font-size: 13.5px; }
.col-qty { width: 140px; }
.col-del { width: 44px; text-align: center; }
.cell-name { min-width: 200px; }
.drug-name { display: block; font-weight: 500; }
.drug-gr { display: block; font-size: 12px; color: var(--muted); }
.item-row.row-ok { background: #fbfefd; }
.item-row.row-error { background: var(--danger-soft); }
.item-row.row-dup { background: var(--warn-soft); }
.item-row.row-dup .cell-name::after { content: 'รหัสซ้ำในใบเบิกนี้'; display: block; color: var(--accent); font-size: 12px; }
.err-inline { color: var(--danger); font-size: 13.5px; }
.muted { color: var(--muted); }
.icon-btn { width: 32px; height: 32px; border: 0; border-radius: 8px; background: transparent; color: var(--muted); cursor: pointer; font-size: 14px; }
.icon-btn:hover { background: var(--danger-soft); color: var(--danger); }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: #eef2f2; color: var(--ink-2); }
.badge-ed { background: #dff3ef; color: var(--ed); }
.badge-ned { background: #fdf0dc; color: var(--ned); }
.badge-ndm { background: #ede6fd; color: var(--ndm); }
.badge-cm { background: #fbe4ef; color: var(--cm); }

/* ---------- Summary / % box ---------- */
.summary-row { display: flex; gap: 16px; align-items: stretch; margin-top: 18px; flex-wrap: wrap; }
.pct-box { min-width: 240px; padding: 12px 16px; border-radius: 10px; background: var(--brand-soft); border: 1px solid #c4e5e0; }
.pct-box.warn { background: var(--warn-soft); border-color: #f1d79e; }
.pct-box.bad { background: var(--danger-soft); border-color: #f4c3ad; }
.pct-label { font-size: 13px; color: var(--ink-2); font-weight: 600; }
.pct-value { font-size: 28px; font-weight: 700; color: var(--brand-ink); line-height: 1.2; font-variant-numeric: tabular-nums; }
.pct-box.warn .pct-value { color: #9a5b00; }
.pct-box.bad .pct-value { color: var(--danger); }
.pct-detail { font-size: 13px; color: var(--ink-2); }

/* ---------- Buttons ---------- */
.btn { height: 42px; padding: 0 18px; border-radius: 9px; border: 1px solid transparent; cursor: pointer; font-weight: 600; transition: background .15s, border-color .15s; white-space: nowrap; }
.btn:disabled { opacity: .6; cursor: progress; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--brand-ink); }
.btn-outline { background: #fff; border-color: #b9d3cf; color: var(--brand-ink); }
.btn-outline:hover { background: var(--brand-soft); }
.btn-ghost { background: transparent; color: var(--ink-2); }
.btn-ghost:hover { background: #eef2f2; }
.btn-sm { height: 36px; padding: 0 14px; font-size: 14px; }
.btn.block { width: 100%; margin-top: 8px; }
#btnAddItem { margin-top: 10px; }
.btn-link { background: none; border: 0; padding: 0; color: var(--brand); cursor: pointer; font-size: 13.5px; text-decoration: underline; text-underline-offset: 2px; }
.btn-link.danger { color: var(--danger); }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

/* ---------- Data tables ---------- */
.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.data-table th { text-align: left; font-weight: 600; color: var(--ink-2); font-size: 13px; padding: 8px 10px; border-bottom: 2px solid var(--line); background: #f7faf9; position: sticky; top: 0; }
.data-table td { padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.data-table tbody tr:hover { background: #f8fbfb; }
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.data-table .small, .small { font-size: 12.5px; color: var(--ink-2); }
.nowrap { white-space: nowrap; }
.total-row td { font-weight: 700; background: #f7faf9; border-top: 2px solid var(--line); }
.empty { padding: 28px; text-align: center; color: var(--muted); }

/* ---------- Results toolbar & segmented ---------- */
.result-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin: 6px 0 12px; }
.toolbar-actions { display: flex; gap: 8px; }
.seg { display: inline-flex; background: #eef2f2; border-radius: 10px; padding: 3px; gap: 2px; }
.seg-btn { border: 0; background: transparent; padding: 6px 14px; border-radius: 8px; cursor: pointer; font-size: 14px; color: var(--ink-2); font-weight: 500; }
.seg-btn.active { background: #fff; color: var(--brand-ink); box-shadow: 0 1px 3px rgba(0, 0, 0, .1); }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 18px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
#searchKpis .kpi { box-shadow: none; background: #f9fbfb; }
.kpi-label { font-size: 13px; color: var(--ink-2); font-weight: 600; }
.kpi-value { font-size: 26px; font-weight: 700; line-height: 1.25; font-variant-numeric: tabular-nums; }
.kpi-sub { font-size: 12.5px; color: var(--muted); }
.kpi-main { background: var(--brand); border-color: var(--brand); color: #fff; }
.kpi-main .kpi-label, .kpi-main .kpi-sub { color: rgba(255, 255, 255, .85); }

/* ---------- Dashboard ---------- */
.dash-filter { display: flex; flex-wrap: wrap; gap: 12px 20px; align-items: center; justify-content: space-between; }
.dash-filter .card-title { margin: 0; }
.dash-controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.dash-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.dash-grid .card { margin: 0; }
.span-2 { grid-column: span 2; }

.bars { display: flex; flex-direction: column; gap: 8px; margin: 0; padding: 0; }
.bars-scroll { max-height: 520px; overflow-y: auto; padding-right: 6px; }
.bars.rank { list-style: none; counter-reset: rank; }
.bars.rank li { counter-increment: rank; display: flex; gap: 8px; align-items: center; }
.bars.rank li::before { content: counter(rank); width: 22px; height: 22px; flex: none; border-radius: 50%; background: var(--brand-soft); color: var(--brand-ink); font-size: 12px; font-weight: 700; display: grid; place-items: center; }
.bars.rank li .bar-row { flex: 1; }
.bar-row { display: grid; grid-template-columns: minmax(150px, 38%) 1fr 72px; gap: 10px; align-items: center; min-width: 0; }
.bar-label { font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-sub { display: block; font-size: 11.5px; color: var(--muted); }
.bar-track { height: 14px; background: #eef3f3; border-radius: 7px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--brand); border-radius: 7px; min-width: 2px; }
.bar-warm .bar-fill { background: #e59e2c; }
.bar-hot .bar-fill { background: var(--danger); }
.bar-alt .bar-fill { background: #3b82a0; }
.bar-type-ed .bar-fill { background: var(--ed); }
.bar-type-ned .bar-fill { background: var(--ned); }
.bar-type-ndm .bar-fill { background: var(--ndm); }
.bar-type-cm .bar-fill { background: var(--cm); }
.bar-value { text-align: right; font-weight: 600; font-size: 13.5px; font-variant-numeric: tabular-nums; }
.stack { display: flex; height: 18px; border-radius: 9px; overflow: hidden; margin-bottom: 14px; background: #eef3f3; }
.stack-seg { display: block; height: 100%; }
.badge-bg-ed { background: var(--ed); } .badge-bg-ned { background: var(--ned); }
.badge-bg-ndm { background: var(--ndm); } .badge-bg-cm { background: var(--cm); }

.trend { width: 100%; height: auto; display: block; }
.trend .grid { stroke: #e6eded; stroke-width: 1; }
.trend .axis { font-size: 11px; fill: var(--muted); font-family: var(--font); }
.trend .val { font-size: 10.5px; fill: var(--ink-2); font-family: var(--font); font-weight: 600; }
.trend .col { fill: var(--brand); }
.trend .col:hover { fill: var(--brand-ink); }

/* ---------- Modal, toast, loading ---------- */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(15, 30, 33, .45); display: grid; place-items: center; padding: 16px; }
.modal-box { width: min(420px, 100%); background: #fff; border-radius: 16px; padding: 26px; box-shadow: 0 20px 60px rgba(0, 0, 0, .25); }
.modal-box h2 { font-size: 19px; margin-bottom: 4px; text-align: center; }
#loginForm .field { margin-top: 12px; }
#loginForm .hint { text-align: center; margin: 0 0 6px; }
#confirmModal h2 { text-align: left; }
.confirm-text { margin: 10px 0 4px; color: var(--ink-2); }
.error-text { color: var(--danger); font-size: 13.5px; min-height: 20px; margin-top: 8px; }

.toasts { position: fixed; right: 18px; bottom: 18px; z-index: 60; display: flex; flex-direction: column; gap: 8px; max-width: min(440px, calc(100vw - 36px)); }
.toast { padding: 12px 16px; border-radius: 10px; color: #fff; background: var(--ok); box-shadow: 0 8px 24px rgba(0, 0, 0, .18); font-size: 14px; transition: opacity .4s, transform .4s; }
.toast.err { background: var(--danger); }
.toast.hide { opacity: 0; transform: translateY(8px); }

.loading { position: fixed; inset: 0; z-index: 55; background: rgba(255, 255, 255, .6); display: grid; place-content: center; justify-items: center; gap: 12px; color: var(--ink-2); font-weight: 500; }
.spinner { width: 38px; height: 38px; border: 4px solid var(--brand-soft); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.flatpickr-day.selected, .flatpickr-day.selected:hover { background: var(--brand); border-color: var(--brand); }

.print-only { display: none; }

/* ---------- Responsive ---------- */
@media (max-width: 1000px) {
  .grid-4 { grid-template-columns: 1fr 1fr; }
  .dash-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
}
@media (max-width: 720px) {
  body { font-size: 15px; }
  .topbar { padding: 10px 14px; gap: 10px; }
  .tabs { order: 3; width: 100%; margin: 0; overflow-x: auto; }
  .tab { padding: 7px 12px; font-size: 14px; }
  .user-box { margin-left: auto; }
  .user-name { display: none; }
  main { padding: 14px 10px 50px; }
  .card { padding: 16px 14px; }
  .grid-4, .grid-filter { grid-template-columns: 1fr; }
  .items-table thead { display: none; }
  .items-table, .items-table tbody, .items-table tr, .items-table td { display: block; width: 100%; }
  .items-table tr { position: relative; padding: 10px 44px 10px 10px; border-bottom: 1px solid var(--line); }
  .items-table td { border: 0; padding: 3px 0; }
  .items-table td.col-no { display: none; }
  .items-table td.col-del { position: absolute; right: 6px; top: 8px; width: 32px; }
  .items-table td.col-type, .items-table td.col-unit { display: inline-block; width: auto; margin-right: 10px; }
  .items-table td.col-qty::before { content: attr(data-label); font-size: 12.5px; color: var(--muted); display: block; }
  .items-table .combo-list { min-width: 0; }
  .bar-row { grid-template-columns: minmax(110px, 40%) 1fr 62px; }
  .form-actions .btn { flex: 1; }
  .dash-controls .input-sm { flex: 1; }
}

/* ---------- Print / PDF ---------- */
@media print {
  @page { size: A4 landscape; margin: 12mm; }
  body { background: #fff; font-size: 11.5pt; }
  .topbar, .demo-banner, .no-print, .toasts, .loading, .modal, #page-record { display: none !important; }
  main { max-width: none; padding: 0; }
  .card, .kpi { box-shadow: none; border-color: #ccc; break-inside: avoid; }
  .print-only { display: block; }
  .print-head { margin-bottom: 12px; }
  .print-head h1 { font-size: 18pt; }
  body[data-print="search"] #page-dashboard, body[data-print="dashboard"] #page-search { display: none !important; }
  .bars-scroll { max-height: none; overflow: visible; }
  .dash-grid { grid-template-columns: 1fr 1fr; }
  .data-table th { position: static; }
  .kpi-main { background: #fff; color: var(--ink); border: 2px solid var(--brand); }
  .kpi-main .kpi-label, .kpi-main .kpi-sub { color: var(--ink-2); }
  .bar-fill, .stack-seg, .trend .col, .badge { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
