  :root {
    --bg: #f4f6f5;
    --card: #ffffff;
    --ink: #1d2a26;
    --muted: #63716c;
    --line: #dde3e0;
    --brand: #0f766e;
    --brand-ink: #ffffff;
    --brand-soft: #e3f1ee;
    --warn: #b45309;
    --warn-soft: #fef3c7;
    --danger: #b91c1c;
    --radius: 12px;
    --shadow: 0 1px 2px rgba(16, 40, 32, .06), 0 2px 8px rgba(16, 40, 32, .04);
  }
  * { box-sizing: border-box; }
  html, body { margin: 0; }
  body {
    background: var(--bg); color: var(--ink);
    font: 15px/1.5 'Sarabun', system-ui, -apple-system, 'Segoe UI', sans-serif;
    -webkit-font-smoothing: antialiased;
  }
  h1, h2 { margin: 0; font-weight: 600; }
  button, input, select, textarea { font: inherit; color: inherit; }

  /* ---------- header & tabs ---------- */
  .topbar {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 12px 16px; background: var(--brand); color: var(--brand-ink);
  }
  .brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
  .logo { width: 36px; height: 36px; border-radius: 10px; background: rgba(255,255,255,.16); display: grid; place-items: center; flex: none; }
  .topbar h1 { font-size: 17px; line-height: 1.25; }
  .topbar p { margin: 0; font-size: 12px; opacity: .8; }
  .top-actions { display: flex; gap: 6px; flex: none; }
  .topbar .btn.ghost { color: #fff; border-color: rgba(255,255,255,.35); background: transparent; }
  .topbar .btn.ghost:hover { background: rgba(255,255,255,.12); }

  .tabs {
    position: sticky; top: 0; z-index: 5; display: flex; gap: 4px;
    padding: 8px 16px 0; background: var(--card); border-bottom: 1px solid var(--line);
  }
  .tab {
    border: 0; background: none; padding: 10px 14px; cursor: pointer; color: var(--muted);
    border-bottom: 3px solid transparent; font-weight: 500;
  }
  .tab.active { color: var(--brand); border-bottom-color: var(--brand); }

  main { max-width: 1100px; margin: 0 auto; padding: 16px; }
  .panel { display: none; }
  .panel.active { display: block; }

  /* ---------- cards & fields ---------- */
  .card {
    background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 16px; margin-bottom: 12px;
  }
  .card h2 { font-size: 15px; margin-bottom: 12px; display: flex; align-items: baseline; gap: 6px; flex-wrap: wrap; }
  .card h2 small { font-weight: 400; color: var(--muted); font-size: 13px; }
  .req { color: var(--danger); font-style: normal; }

  .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
  .span2 { grid-column: span 2; }
  .field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
  .field > span { font-size: 13px; color: var(--muted); font-weight: 500; }
  input[type=text], input[type=search], input[type=date], input[type=datetime-local], select, textarea {
    width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: #fff;
    min-height: 40px;
  }
  textarea { resize: vertical; }
  input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: transparent; }
  .invalid { animation: shake .3s; }
  .card.invalid { border-color: var(--danger); }
  @keyframes shake { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }

  /* ---------- chips (ward / specimens) ---------- */
  .chips { display: flex; flex-wrap: wrap; gap: 8px; }
  .chip { position: relative; }
  .chip input { position: absolute; opacity: 0; pointer-events: none; }
  .chip > span {
    display: inline-flex; align-items: center; gap: 8px; padding: 8px 14px; min-height: 40px;
    border: 1px solid var(--line); border-radius: 999px; background: #fff; cursor: pointer; user-select: none;
  }
  .chip input:checked + span { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); font-weight: 600; }
  .chip input:focus-visible + span { outline: 2px solid var(--brand); outline-offset: 2px; }
  .dot { width: 12px; height: 12px; border-radius: 50%; flex: none; box-shadow: inset 0 0 0 1px rgba(0,0,0,.15); }
  .other-input { margin-top: 10px; }

  /* ---------- list choices (reasons / actions) ---------- */
  .list-choices { display: grid; gap: 6px; }
  .opt {
    display: flex; align-items: flex-start; gap: 10px; padding: 9px 12px; border: 1px solid var(--line);
    border-radius: 8px; cursor: pointer; background: #fff;
  }
  .opt input { margin-top: 4px; accent-color: var(--brand); width: 17px; height: 17px; flex: none; }
  .opt:has(input:checked) { background: var(--brand-soft); border-color: var(--brand); }
  .opt.other { flex-wrap: wrap; }
  .opt.other input[type=text] { flex: 1 1 200px; min-height: 34px; padding: 5px 9px; }

  .hemo { margin-top: 12px; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 10px 12px; background: var(--warn-soft); border-radius: 8px; }
  .hemo > span { font-weight: 600; color: var(--warn); }
  .seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: #fff; }
  .seg label { position: relative; }
  .seg input { position: absolute; opacity: 0; }
  .seg span { display: block; padding: 6px 12px; cursor: pointer; border-left: 1px solid var(--line); }
  .seg label:first-child span { border-left: 0; }
  .seg input:checked + span { background: var(--warn); color: #fff; }

  .history { margin-top: 12px; padding: 10px 12px; border-radius: 8px; background: var(--warn-soft); color: #7c3d06; font-size: 14px; }
  .history ul { margin: 6px 0 0; padding-left: 18px; }

  .edit-banner {
    display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
    padding: 10px 14px; margin-bottom: 12px; border-radius: var(--radius); background: var(--warn-soft); color: #7c3d06;
  }
  .form-actions {
    position: sticky; bottom: 0; display: flex; justify-content: flex-end; gap: 10px;
    padding: 12px 0; background: linear-gradient(transparent, var(--bg) 30%);
  }

  /* ---------- buttons ---------- */
  .btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 10px 20px; min-height: 42px; border-radius: 8px; border: 1px solid transparent;
    cursor: pointer; font-weight: 600; text-decoration: none; white-space: nowrap;
  }
  .btn.sm { padding: 5px 12px; min-height: 32px; font-size: 13px; font-weight: 500; }
  .btn.primary { background: var(--brand); color: var(--brand-ink); min-width: 140px; }
  .btn.primary:hover { filter: brightness(1.08); }
  .btn.ghost { background: #fff; border-color: var(--line); color: var(--ink); }
  .btn.ghost:hover { background: var(--bg); }
  .btn:disabled { opacity: .6; cursor: progress; }

  /* ---------- list ---------- */
  .filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
  .filters .field { flex: 1 1 140px; }
  .filters .field.grow { flex: 3 1 220px; }
  .list-head { display: flex; justify-content: space-between; align-items: center; margin: 4px 0 8px; color: var(--muted); }
  .table-wrap { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
  .card .table-wrap { border: 0; border-radius: 0; }
  .tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
  .tbl th, .tbl td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
  .tbl th { background: #f8faf9; font-weight: 600; color: var(--muted); font-size: 13px; white-space: nowrap; position: sticky; top: 0; }
  .tbl tbody tr:hover { background: #f8faf9; }
  .tbl td.nowrap { white-space: nowrap; }
  .tbl .tag { display: inline-block; padding: 1px 8px; border-radius: 999px; background: var(--brand-soft); color: var(--brand); font-size: 12px; font-weight: 600; white-space: nowrap; }
  .tbl .tubes { display: flex; flex-wrap: wrap; gap: 4px; min-width: 120px; }
  .tbl .tube { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; white-space: nowrap; }
  .tbl .small { font-size: 13px; color: var(--muted); }
  .tbl .reason { min-width: 180px; }
  .empty { padding: 32px; text-align: center; color: var(--muted); }

  /* ---------- dashboard ---------- */
  .kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 12px; }
  .kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); min-width: 0; }
  .kpi span { display: block; font-size: 13px; color: var(--muted); }
  .kpi b { display: block; font-size: 28px; line-height: 1.2; margin-top: 4px; font-variant-numeric: tabular-nums; }
  .kpi b.sm { font-size: 16px; line-height: 1.35; margin-top: 6px; overflow-wrap: anywhere; }
  .charts { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
  .charts .card { margin-bottom: 0; }
  .chart.wide { grid-column: span 2; }
  .cbox { position: relative; height: 260px; }
  .cbox.tall { height: 340px; }
  .charts + .card { margin-top: 12px; }
  .xtab td, .xtab th { text-align: center; }
  .xtab th:first-child, .xtab td:first-child { text-align: left; position: sticky; left: 0; background: #fff; }
  .xtab thead th { white-space: normal; min-width: 80px; vertical-align: bottom; }
  .xtab td.h { font-weight: 600; }
  .xtab tfoot td { font-weight: 700; background: #f8faf9; }

  /* ---------- overlays ---------- */
  .loading { position: fixed; inset: 0; display: flex; flex-direction: column; gap: 12px; align-items: center; justify-content: center; background: rgba(244,246,245,.85); z-index: 20; color: var(--muted); }
  .loading.hide { display: none; }
  .spinner { width: 34px; height: 34px; border: 3px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; }
  @keyframes spin { to { transform: rotate(360deg); } }
  .toast {
    position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
    background: #1d2a26; color: #fff; padding: 10px 18px; border-radius: 8px; z-index: 30; transition: .2s; max-width: calc(100% - 32px);
  }
  .toast.show { opacity: 1; transform: translate(-50%, 0); }
  .toast.error { background: var(--danger); }

  /* ---------- mobile ---------- */
  @media (max-width: 720px) {
    .grid { grid-template-columns: 1fr; }
    .span2 { grid-column: auto; }
    .kpis { grid-template-columns: 1fr 1fr; }
    .charts { grid-template-columns: 1fr; }
    .chart.wide { grid-column: auto; }
    .topbar p { display: none; }
    .tab { flex: 1; padding: 10px 6px; }
    .form-actions .btn { flex: 1; }
  }
  @media print {
    .topbar, .tabs, .filters, .form-actions, .list-head .btn { display: none !important; }
    body { background: #fff; }
  }

  /* hidden ต้องชนะ display ของ .btn / .tab เสมอ */
  [hidden] { display: none !important; }

  /* ---------- modal (login / password / user) ---------- */
  .modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
    padding: 16px; background: rgba(16, 32, 28, .45); backdrop-filter: blur(2px); }
  .modal-box { width: 100%; max-width: 380px; background: var(--card); border-radius: 16px; padding: 24px;
    box-shadow: 0 20px 50px rgba(0,0,0,.25); display: grid; gap: 12px; animation: pop .15s ease-out; }
  @keyframes pop { from { transform: scale(.96); opacity: 0; } }
  .modal-box h2 { font-size: 19px; text-align: center; }
  .modal-logo { width: 56px; height: 56px; border-radius: 14px; margin: 0 auto -4px; display: block; }
  .modal-sub { margin: -8px 0 4px; text-align: center; color: var(--muted); font-size: 14px; }
  .modal-msg { color: var(--danger); font-size: 14px; }
  .modal-msg:empty { display: none; }
  .modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 4px; }
  .modal-actions .btn { flex: 1; min-width: 0; }
  .btn.danger { color: var(--danger); border-color: #f3c2c2; }
  .modal-box input.pin { font-size: 20px; letter-spacing: 6px; text-align: center; }
  .modal-box input.pin::placeholder { letter-spacing: 6px; }
  .check { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }
  .check input { width: 17px; height: 17px; accent-color: var(--brand); }

  .tab.locked::after { content: ' 🔒'; font-size: 12px; opacity: .7; }
  .tag-admin { background: #fef3c7 !important; color: #92400e !important; }
  .muted-row td { color: var(--muted); }
  .danger-text { color: var(--danger); font-weight: 600; }

  /* ---------- login & user ---------- */
  .login { position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; padding: 16px; background: var(--bg); }
  .login[hidden] { display: none; }
  .login-box { width: 100%; max-width: 400px; background: var(--card); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); padding: 28px 24px; text-align: center; }
  .login-box img { width: 72px; height: 72px; border-radius: 18px; display: block; margin: 0 auto 14px; }
  .login-box h2 { font-size: 20px; margin-bottom: 4px; }
  .login-box p { margin: 0 0 18px; color: var(--muted); }
  .login-box #gbtn { display: flex; justify-content: center; min-height: 44px; }
  .code-form { display: grid; gap: 10px; }
  .code-form[hidden], #gsiWrap[hidden] { display: none; }
  .code-form p { margin-bottom: 4px; }
  .code-form input { text-align: center; font-size: 18px; letter-spacing: 2px; }
  .code-form .btn { width: 100%; }
  .or { margin: 18px 0 10px !important; font-size: 13px; }
  .login-msg { margin-top: 14px; font-size: 14px; color: var(--danger); }
  .login-msg:empty { display: none; }
  .user-chip { font-size: 13px; opacity: .9; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; align-self: center; }
  @media (max-width: 720px) { .user-chip { display: none; } }
