:root {
  --bg: #f3f6f9;
  --surface: #ffffff;
  --surface-2: #f7f9fb;
  --line: #dfe5ec;
  --text: #17222e;
  --muted: #5d6b7a;
  --primary: #0b6e8a;
  --primary-ink: #ffffff;
  --primary-soft: #e3f1f5;
  --ok: #18794e;
  --ok-soft: #e4f4ea;
  --warn: #9a5b00;
  --warn-soft: #fff3dc;
  --err: #b42318;
  --err-soft: #fde8e6;
  --info: #3451b2;
  --info-soft: #e8edfc;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f151b; --surface: #17202a; --surface-2: #1c2733; --line: #2a3644;
    --text: #e6edf3; --muted: #94a3b3; --primary: #3fb0cf; --primary-ink: #04212a; --primary-soft: #143340;
    --ok: #4cc38a; --ok-soft: #12301f; --warn: #f0b44c; --warn-soft: #33270f;
    --err: #ff8a80; --err-soft: #3a1a17; --info: #8da4ef; --info-soft: #1d2442;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.55 "Sarabun", "Leelawadee UI", "Tahoma", system-ui, sans-serif;
}
a { color: var(--primary); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .6em; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.2rem; } h3 { font-size: 1.05rem; }
code { background: var(--surface-2); padding: 1px 6px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- layout ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text); }
.brand small { display: block; color: var(--muted); font-size: .8rem; font-weight: 400; }
.logo {
  width: 34px; height: 34px; border-radius: 8px; background: var(--primary); color: var(--primary-ink);
  display: grid; place-items: center; font-weight: 700; font-size: 1.4rem; flex: none;
}
.nav { display: flex; align-items: center; gap: 4px 14px; flex-wrap: wrap; }
.nav a:not(.btn) { text-decoration: none; color: var(--text); font-weight: 500; padding: 4px 2px; }
.nav a:not(.btn):hover { color: var(--primary); }
.who { color: var(--muted); font-size: .875rem; }
.container { max-width: 1200px; margin: 0 auto; padding: 20px 16px 48px; }
.container.narrow { max-width: 520px; }
.container.mid { max-width: 820px; }
.grid { display: grid; gap: 16px; }
.grid.c2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid.c3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.grid.c4 { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack > * + * { margin-top: 16px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.card h2 { margin-bottom: 12px; }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 16px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 600;
  text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.danger { color: var(--err); border-color: color-mix(in srgb, var(--err) 40%, var(--line)); }
.btn.sm { min-height: 32px; padding: 4px 10px; font-size: .875rem; }
.btn.lg { min-height: 56px; padding: 12px 24px; font-size: 1.15rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--primary) 55%, transparent); outline-offset: 2px; }

.form { display: grid; gap: 14px; }
.form label { display: grid; gap: 6px; font-weight: 500; }
.form label.check { display: flex; align-items: center; gap: 8px; font-weight: 400; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 12px; min-height: 42px; width: 100%;
}
input[type=checkbox] { width: auto; min-height: 0; }
.inline-form { display: flex; gap: 8px; align-items: end; flex-wrap: wrap; }
.inline-form label { display: grid; gap: 4px; font-size: .875rem; color: var(--muted); }
.inline-form input, .inline-form select { width: auto; }

/* ---------- table ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: .8rem; color: var(--muted); font-weight: 600; background: var(--surface-2); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
td.actions { white-space: nowrap; text-align: right; }
td.actions .btn + .btn { margin-left: 4px; }

/* ---------- badges / alerts ---------- */
.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: .8rem; font-weight: 600; white-space: nowrap; background: var(--surface-2); color: var(--muted); }
.s-registered { background: var(--info-soft); color: var(--info); }
.s-waiting { background: var(--warn-soft); color: var(--warn); }
.s-serving { background: var(--primary-soft); color: var(--primary); }
.s-pharmacy { background: var(--info-soft); color: var(--info); }
.s-done { background: var(--ok-soft); color: var(--ok); }
.s-skipped, .s-cancelled { background: var(--err-soft); color: var(--err); }
.alert { padding: 12px 16px; border-radius: 8px; border: 1px solid transparent; margin: 0 0 16px; }
.alert.ok { background: var(--ok-soft); color: var(--ok); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.err { background: var(--err-soft); color: var(--err); }
.alert.info { background: var(--info-soft); color: var(--info); }
.toast {
  position: fixed; right: 16px; bottom: 16px; z-index: 50; max-width: min(420px, calc(100vw - 32px));
  padding: 12px 16px; border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.18);
  background: var(--surface); border-left: 4px solid var(--ok); animation: toast-in .2s ease-out;
}
.toast.err { border-left-color: var(--err); }
.toast.warn { border-left-color: var(--warn); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; }
.tabs button { background: none; border: 0; border-bottom: 3px solid transparent; padding: 10px 14px; font: inherit; font-weight: 600; color: var(--muted); cursor: pointer; white-space: nowrap; }
.tabs button[aria-selected=true] { color: var(--primary); border-bottom-color: var(--primary); }
.tabs .count { background: var(--surface-2); border-radius: 999px; padding: 0 8px; margin-left: 4px; font-size: .8rem; }

/* ---------- stats ---------- */
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.stat .label { color: var(--muted); font-size: .875rem; }
.stat .value { font-size: 1.9rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.2; }
.stat .sub { color: var(--muted); font-size: .8rem; }

/* ---------- landing ---------- */
.hero { text-align: center; padding: 32px 0 12px; }
.hero h1 { font-size: 1.9rem; }
.tile { display: block; text-decoration: none; color: var(--text); }
.tile:hover { border-color: var(--primary); }
.tile .ic { width: 44px; height: 44px; border-radius: 10px; background: var(--primary-soft); color: var(--primary); display: grid; place-items: center; margin-bottom: 10px; }
.tile .ic svg { width: 24px; height: 24px; }

/* ================= Patient status page ================= */
.qs-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.qs-sys { margin: 0; font-size: 1.35rem; }
.clock { text-align: right; }
.clock .t { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; }
.clock .d { color: var(--muted); font-size: .85rem; }

.phase { display: flex; gap: 8px; margin-bottom: 16px; }
.phase span { flex: 1 1 0; min-width: 0; text-align: center; padding: 10px 6px; border-radius: 8px; background: var(--surface-2); color: var(--muted); font-weight: 600; border: 1px solid var(--line); }
.phase span.on { color: #fff; border-color: transparent; }
.phase span.on.wait { background: var(--warn); }
.phase span.on.serve { background: var(--primary); }
.phase span.on.done { background: var(--ok); }
@media (prefers-color-scheme: dark) { .phase span.on { color: #0f151b; } }

.status-box { border: 2px solid var(--primary); border-radius: 14px; padding: 20px; background: var(--surface); }
.status-box.wait { border-color: var(--warn); }
.status-box.done { border-color: var(--ok); }
.status-box.skip, .status-box.cancel { border-color: var(--err); }
.status-box.alerting { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 8px color-mix(in srgb, var(--primary) 25%, transparent); } }

.q-main { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 18px; }
.q-cell { background: var(--surface-2); border-radius: 10px; padding: 14px; text-align: center; }
.q-cell .label { color: var(--muted); font-size: .9rem; }
.q-cell .big { font-size: 2.6rem; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; color: var(--primary); }
.q-cell .big.ahead { color: var(--warn); }
.q-cell .sub { font-size: .85rem; color: var(--muted); }

.steps { list-style: none; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); padding: 0; margin: 8px 0 4px; position: relative; }
.steps li { text-align: center; position: relative; overflow-wrap: anywhere; font-size: .85rem; color: var(--muted); }
.steps li::before {
  content: ""; position: absolute; top: 17px; left: -50%; width: 100%; height: 4px; background: var(--line); z-index: 0;
}
.steps li:first-child::before { display: none; }
.steps li.done::before, .steps li.current::before { background: var(--ok); }
.steps .dot {
  position: relative; z-index: 1; width: 38px; height: 38px; margin: 0 auto 6px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700; background: var(--surface); border: 3px solid var(--line); color: var(--muted);
}
.steps li.done .dot { background: var(--ok); border-color: var(--ok); color: #fff; }
.steps li.current .dot { border-color: var(--primary); color: var(--primary); box-shadow: 0 0 0 5px var(--primary-soft); }
.steps li.current { color: var(--text); font-weight: 600; }
.steps .tm { display: block; font-size: .75rem; color: var(--muted); font-weight: 400; min-height: 1.2em; }
.step-of { text-align: center; color: var(--muted); font-size: .9rem; margin-bottom: 14px; }

.countdown { text-align: center; padding: 14px; border-radius: 10px; background: var(--surface-2); margin-bottom: 14px; }
.countdown .label { color: var(--muted); font-size: .9rem; }
.countdown .v { font-size: 2.4rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.15; }
.countdown .bar { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin-top: 8px; }
.countdown .bar i { display: block; height: 100%; background: var(--primary); width: 0; transition: width 1s linear; }
.countdown.over .v { color: var(--warn); }

.current-status { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; justify-content: center; font-size: 1.1rem; }
.current-status b { color: var(--primary); }
.status-msg { text-align: center; margin: 8px 0 0; }
.notice-banner { display: none; }
.notice-banner.show { display: block; }

/* ================= Kiosk ================= */
body.kiosk { background: var(--primary-soft); }
.kiosk-wrap { max-width: 860px; margin: 0 auto; padding: 24px 16px; }
.kiosk h1 { text-align: center; font-size: 2rem; }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; max-width: 360px; margin: 16px auto; }
.keypad button { min-height: 64px; font-size: 1.6rem; font-weight: 700; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); color: var(--text); cursor: pointer; }
.keypad button:active { background: var(--primary-soft); }
.hn-display { font-size: 2.2rem; text-align: center; letter-spacing: 3px; font-weight: 700; min-height: 64px; }
.dept-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.dept-btn { min-height: 96px; font-size: 1.3rem; border-radius: 14px; border: 2px solid var(--line); background: var(--surface); color: var(--text); font-weight: 700; cursor: pointer; padding: 12px; }
.dept-btn small { display: block; font-weight: 400; color: var(--muted); font-size: .9rem; }
.dept-btn:hover { border-color: var(--primary); }

/* ticket (print) */
.ticket { width: 72mm; margin: 0 auto; padding: 4mm; text-align: center; background: #fff; color: #000; font-size: 13px; }
.ticket .hosp { font-weight: 700; font-size: 15px; }
.ticket .code { font-size: 46px; font-weight: 700; line-height: 1.1; margin: 4px 0; }
.ticket .qr { display: flex; justify-content: center; margin: 6px 0; }
.ticket hr { border: 0; border-top: 1px dashed #000; }
#print-area { display: none; }
@media print {
  @page { size: 80mm auto; margin: 0; }
  body * { visibility: hidden; }
  #print-area, #print-area * { visibility: visible; }
  #print-area { display: block; position: absolute; left: 0; top: 0; }
}

/* ================= Display board ================= */
body.board { background: #0b1d2a; color: #fff; overflow: hidden; }
.board-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 28px; background: #0b6e8a; }
.board-head h1 { margin: 0; font-size: 2rem; }
.board-head .clock .t { font-size: 2.4rem; }
.board-head .clock .d { color: #cde8f0; }
.board-body { display: grid; grid-template-columns: 1.6fr 1fr; gap: 20px; padding: 20px 28px; height: calc(100vh - 140px); }
.board-panel { background: #12293a; border-radius: 14px; padding: 16px 20px; overflow: hidden; }
.board-panel h2 { color: #9fd3e3; font-size: 1.4rem; }
.call-row { display: grid; grid-template-columns: 1.1fr 1.4fr 1.4fr; align-items: center; gap: 12px; padding: 12px 8px; border-bottom: 1px solid #23415a; font-size: 1.6rem; }
.call-row .code { font-size: 3rem; font-weight: 700; color: #ffd166; font-variant-numeric: tabular-nums; }
.call-row.flash { animation: flash 1s ease-in-out 5; }
@keyframes flash { 50% { background: #1d6f8c; } }
.wait-list { display: flex; flex-wrap: wrap; gap: 10px; }
.wait-list span { font-size: 1.8rem; font-weight: 700; background: #1b3a52; border-radius: 10px; padding: 6px 14px; font-variant-numeric: tabular-nums; }
.board-foot { position: fixed; bottom: 0; left: 0; right: 0; background: #0b6e8a; padding: 10px 28px; font-size: 1.2rem; white-space: nowrap; overflow: hidden; }
.board-foot span { display: inline-block; padding-left: 100%; animation: marquee 28s linear infinite; }
@keyframes marquee { to { transform: translateX(-100%); } }
.sound-btn { position: fixed; right: 16px; bottom: 60px; }

/* ================= Room (doctor) ================= */
.now-serving { text-align: center; padding: 24px; }
.now-serving .code { font-size: 4rem; font-weight: 700; color: var(--primary); line-height: 1; font-variant-numeric: tabular-nums; }
.now-serving .name { font-size: 1.2rem; margin-top: 6px; }
.btn-bar { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 16px; }

/* chart */
.chart-box { position: relative; height: 300px; }

@media (max-width: 640px) {
  .topbar { padding: 10px 16px; }
  .nav { gap: 4px 10px; font-size: .9rem; }
  .phase span { font-size: .85rem; padding: 8px 4px; }
  .status-box { padding: 14px; }
  .q-cell .big { font-size: 2rem; }
  .countdown .v { font-size: 2rem; }
  .steps li { font-size: .75rem; }
  .board-body { grid-template-columns: 1fr; height: auto; }
  .call-row { font-size: 1.1rem; } .call-row .code { font-size: 2rem; }
  td.actions { white-space: normal; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
