:root {
  color-scheme: light dark;
  --bg: #f7f8fa;
  --panel: #ffffff;
  --border: #d8dce1;
  --text: #1a1d21;
  --muted: #6b7280;
  --accent: #1d6fb8;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --danger-text: #ffffff;

  --tone-blue-bg: #eaf3fc; --tone-blue-fg: #1d6fb8;
  --tone-violet-bg: #f1edfb; --tone-violet-fg: #6d4fd1;
  --tone-teal-bg: #e8f7f3; --tone-teal-fg: #13977a;
  --tone-purple-bg: #f5ecfb; --tone-purple-fg: #8b3fd1;
  --tone-rose-bg: #fdecee; --tone-rose-fg: #c23a56;
  --tone-orange-bg: #fef1e6; --tone-orange-fg: #c9711f;
  --tone-cyan-bg: #e7f6fa; --tone-cyan-fg: #1189a8;
  --tone-amber-bg: #fdf3e1; --tone-amber-fg: #a8790a;
  --tone-slate-bg: #eef1f4; --tone-slate-fg: #4b5563;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181c;
    --panel: #1f2226;
    --border: #33373d;
    --text: #edeef0;
    --muted: #9aa1ab;
    --accent: #4f9ce0;
    --accent-text: #0c0d0f;
    --danger: #e0564a;
    --danger-text: #1a0705;

    --tone-blue-bg: #1b2b3a; --tone-blue-fg: #6fb3ea;
    --tone-violet-bg: #27233d; --tone-violet-fg: #a995f0;
    --tone-teal-bg: #15302a; --tone-teal-fg: #4fcdae;
    --tone-purple-bg: #2c2138; --tone-purple-fg: #c190ec;
    --tone-rose-bg: #341f24; --tone-rose-fg: #ec8599;
    --tone-orange-bg: #352512; --tone-orange-fg: #eba35c;
    --tone-cyan-bg: #122c32; --tone-cyan-fg: #5cd2ec;
    --tone-amber-bg: #332a11; --tone-amber-fg: #e3bf5c;
    --tone-slate-bg: #272b30; --tone-slate-fg: #c2c8d1;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", "Noto Sans Thai", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app-shell {
  display: flex;
  align-items: flex-start;
  min-height: 100vh;
}

.sidebar {
  width: 232px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  z-index: 10;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 6px 16px;
  margin-bottom: 10px;
  border-bottom: 1px solid var(--border);
}
.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--accent), #6fb3ea);
  color: #fff;
  flex-shrink: 0;
}
.brand-mark svg { width: 19px; height: 19px; }
.brand-text { font-weight: 700; font-size: 1rem; }

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  font-size: 0.88rem;
  font-weight: 500;
}
.nav-item:hover { background: var(--bg); text-decoration: none; }
.nav-item.active {
  background: var(--tone-blue-bg);
  color: var(--tone-blue-fg);
  font-weight: 700;
}
.nav-icon { width: 18px; height: 18px; flex-shrink: 0; }

.sidebar-admin {
  margin-top: auto;
  padding-bottom: 12px;
}

.sidebar-footer {
  padding-top: 14px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.logout-form {
  display: inline-block;
}
.logout-btn {
  width: 100%;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 12px;
  color: var(--text);
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
}
.logout-btn:hover {
  border-color: var(--danger);
  color: var(--danger);
}

.content {
  flex: 1;
  min-width: 0;
}

main {
  max-width: 980px;
  margin: 0 auto;
  padding: 24px 24px 80px;
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.page-header-title {
  display: flex;
  align-items: center;
  gap: 12px;
}
.page-header-title .page-icon {
  width: 42px;
  height: 42px;
  border-radius: 11px;
  background: var(--tone-blue-bg);
  color: var(--tone-blue-fg);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.page-icon svg { width: 21px; height: 21px; }
.page-header-title h1 { margin: 0; font-size: 1.25rem; }
.page-header-title p { margin: 2px 0 0; color: var(--muted); font-size: 0.85rem; }
.page-header-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.mobile-topbar, .sidebar-backdrop { display: none; }

/* Phones: sidebar becomes an off-canvas drawer opened from the top bar's ☰ button. */
@media (max-width: 780px) {
  .app-shell { flex-direction: column; align-items: stretch; }
  .mobile-topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    position: sticky;
    top: 0;
    z-index: 20;
    padding: 10px 14px;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
  }
  .menu-btn {
    width: 38px;
    height: 38px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    box-shadow: none;
  }
  .menu-btn svg { width: 20px; height: 20px; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 260px;
    max-width: 82vw;
    height: 100vh;
    height: 100dvh;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: 4px 0 18px rgba(16, 24, 40, 0.18);
  }
  body.nav-open .sidebar { transform: translateX(0); }
  body.nav-open { overflow: hidden; }
  .sidebar-backdrop:not([hidden]) {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 30;
    background: rgba(16, 24, 40, 0.4);
  }
  main { padding: 18px 16px 70px; }
}

.record-meta { color: var(--muted); font-size: 0.9rem; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge-finalized { background: var(--danger); color: var(--danger-text); }
.badge-draft { background: var(--accent); color: var(--accent-text); }
.badge-admin { background: var(--muted); color: var(--panel); }

.export-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 600;
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 3px 10px;
  white-space: nowrap;
  flex-shrink: 0;
}
.export-link:hover { background: var(--accent); color: var(--accent-text); }

.export-menu { display: inline-block; }
.export-menu summary { list-style: none; cursor: pointer; display: inline-block; }
.export-menu summary::-webkit-details-marker { display: none; }
.export-menu[open] summary { background: var(--accent); color: var(--accent-text); }
.export-menu-list {
  position: fixed;
  z-index: 50;
  min-width: 170px;
  display: flex;
  flex-direction: column;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(16, 24, 40, 0.15);
}
.export-menu-list a {
  padding: 8px 10px;
  border-radius: 6px;
  color: var(--text);
  text-decoration: none;
  font-size: 0.88rem;
  white-space: nowrap;
}
.export-menu-list a:hover { background: var(--tone-blue-bg); color: var(--tone-blue-fg); }

.link-button {
  background: none;
  border: none;
  padding: 0;
  color: var(--accent);
  font-size: 0.9rem;
  cursor: pointer;
  text-decoration: none;
}
.link-button:hover { text-decoration: underline; }

.admin-unlock {
  display: flex;
  gap: 8px;
  align-items: center;
  margin: 8px 0 16px;
}
.admin-unlock input[type="password"] {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
}
.admin-unlock button {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}

.finalize-bar { text-align: right; margin-top: 12px; }
.finalize-button {
  background: var(--danger);
  color: var(--danger-text);
  border: none;
  border-radius: 8px;
  padding: 10px 24px;
  font-size: 1rem;
  cursor: pointer;
}
.finalize-button:hover { filter: brightness(1.05); }
.delete-button {
  background: none;
  color: var(--danger);
  border: 1.5px solid var(--danger);
  border-radius: 8px;
  padding: 9px 22px;
  font-size: 0.95rem;
  box-shadow: none;
}
.delete-button:hover { background: var(--danger); color: var(--danger-text); }
.delete-button-sm { padding: 3px 10px; font-size: 0.85rem; border-width: 1px; border-radius: 6px; font-weight: 600; }
.delete-button.is-armed { background: var(--danger); color: var(--danger-text); }
.row-actions { display: inline-flex; align-items: center; gap: 6px; }

section {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 16px;
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.04);
}

section h3 {
  font-size: 0.9rem;
  margin: 16px 0 10px;
  color: var(--muted);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 13px 20px;
  font-size: 1rem;
  font-weight: 700;
  border-bottom: 1px solid var(--border);
  border-radius: 12px 12px 0 0;
}
.card-header .card-icon {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.card-icon svg { width: 18px; height: 18px; }
.card-body { padding: 18px 20px; }

.tone-blue   { background: var(--tone-blue-bg); }   .tone-blue .card-icon   { color: var(--tone-blue-fg); }
.tone-violet { background: var(--tone-violet-bg); } .tone-violet .card-icon { color: var(--tone-violet-fg); }
.tone-teal   { background: var(--tone-teal-bg); }   .tone-teal .card-icon   { color: var(--tone-teal-fg); }
.tone-purple { background: var(--tone-purple-bg); } .tone-purple .card-icon { color: var(--tone-purple-fg); }
.tone-rose   { background: var(--tone-rose-bg); }   .tone-rose .card-icon   { color: var(--tone-rose-fg); }
.tone-orange { background: var(--tone-orange-bg); } .tone-orange .card-icon { color: var(--tone-orange-fg); }
.tone-cyan   { background: var(--tone-cyan-bg); }   .tone-cyan .card-icon   { color: var(--tone-cyan-fg); }
.tone-amber  { background: var(--tone-amber-bg); }  .tone-amber .card-icon  { color: var(--tone-amber-fg); }
.tone-slate  { background: var(--tone-slate-bg); }  .tone-slate .card-icon  { color: var(--tone-slate-fg); }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px 16px;
  align-items: start;
  margin-bottom: 16px;
}
.grid:last-child { margin-bottom: 0; }

.grid-2col {
  display: grid;
  grid-template-columns: repeat(2, minmax(140px, 1fr));
  gap: 12px 16px;
  max-width: 420px;
}

.field { display: flex; flex-direction: column; gap: 4px; font-size: 0.85rem; }
.field-wide { grid-column: 1 / -1; margin-bottom: 16px; }
.field-wide:last-child { margin-bottom: 0; }
.field span { color: var(--muted); }

.row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px 16px;
  margin-bottom: 12px;
}
.row:last-child { margin-bottom: 0; }
.row .field { flex: 0 0 260px; }
.row-short .field { flex: 0 0 80px; min-width: 0; }
.row-short .field input { width: 100%; }

.inline-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 14px;
  font-size: 0.85rem;
}
.inline-group .label { color: var(--muted); white-space: nowrap; }
.inline-input {
  flex: 1;
  min-width: 160px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

input[type="text"], input[type="time"], input:not([type]), textarea {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.time24 { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 2px; }
.time24 select {
  padding: 6px 4px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
.time24-sep { color: var(--muted); }

.time24-now {
  margin-left: 4px;
  padding: 5px 9px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: transparent;
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  box-shadow: none;
}
.time24-now:hover { background: var(--accent); color: var(--accent-text); filter: none; }

textarea { width: 100%; resize: vertical; }

input:disabled, textarea:disabled, input[readonly], textarea[readonly] {
  opacity: 1;
  background: var(--bg);
  color: var(--text);
}

fieldset.group {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  margin: 0;
}
fieldset.group legend { font-size: 0.85rem; color: var(--muted); padding: 0 4px; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 3px 8px 3px 0;
  font-size: 0.85rem;
}

.nowrap-pair {
  display: inline-flex;
  white-space: nowrap;
}

.chip-block {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 0.85rem;
  grid-column: 1 / -1;
}
.chip-block input { margin-top: 3px; }

.dose-with-unit {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.dose-input {
  width: 56px;
  min-width: 0;
}
.unit-label {
  color: var(--muted);
  font-size: 0.9rem;
  white-space: nowrap;
}

.submit-bar { text-align: right; }

button {
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  border-radius: 10px;
  padding: 10px 24px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.08);
}
button:hover { filter: brightness(1.05); }

.table-scroll {
  overflow-x: auto;
}

.records-table {
  width: 100%;
  min-width: 720px;
  border-collapse: collapse;
  background: var(--panel);
}
.records-table th, .records-table td {
  text-align: left;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.records-table a { color: var(--accent); }

.med-table, .consult-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}
.med-table td, .consult-table td {
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.med-table tr:last-child td, .consult-table tr:last-child td { border-bottom: none; }
.med-table td:first-child { width: 32%; white-space: nowrap; }
.med-table td:nth-child(2) { width: 38%; }
.med-table input[type="text"] { width: 100%; min-width: 90px; }
.consult-table td:first-child { width: 40%; }
.consult-table td:last-child { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; }

.consult-other-cell {
  display: inline-flex;
  align-items: center;
  flex-wrap: nowrap;
  gap: 6px;
}
.consult-other-cell input[type="text"] { flex: none; width: 110px; min-width: 0; }

@media (max-width: 640px) {
  .med-table, .med-table tbody, .med-table tr, .med-table td,
  .consult-table, .consult-table tbody, .consult-table tr, .consult-table td {
    display: block;
    width: auto;
  }
  .med-table tr, .consult-table tr {
    padding: 8px 0;
  }
  .med-table td, .consult-table td {
    border-bottom: none;
    padding: 3px 0;
  }
  .med-table tr:not(:last-child), .consult-table tr:not(:last-child) {
    border-bottom: 1px solid var(--border);
  }
  .med-table td:first-child, .consult-table td:first-child {
    font-weight: 600;
    white-space: normal;
  }
  .consult-table td:last-child {
    padding-top: 4px;
  }

  .topbar nav {
    gap: 8px 16px;
  }
  .logout-form {
    margin-left: auto;
  }
}

/* Referral Flow and Contact page */
.flow-image { display: block; margin-bottom: 12px; }
.flow-image img {
  display: block;
  max-width: min(100%, 640px);
  height: auto;
  margin: 0 auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.flow-hint { color: var(--muted); font-size: 0.85rem; margin: 0; text-align: center; }

.contact-list { list-style: none; margin: 0; padding: 0; }
.contact-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.contact-row:last-child { border-bottom: none; }
.contact-name { font-weight: 600; }
.contact-meta { color: var(--muted); font-size: 0.85rem; margin-top: 2px; }
@media (max-width: 780px) {
  .contact-meta span { display: block; }
  .contact-meta .contact-sep { display: none; }
}
.contact-actions { display: flex; gap: 8px; flex-shrink: 0; }
.contact-btn {
  text-decoration: none;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  border: none;
  border-radius: 6px;
  padding: 6px 14px;
  white-space: nowrap;
  color: #fff;
  cursor: pointer;
  box-shadow: none;
}
.contact-call { background: var(--accent); }
.contact-line { background: #06c755; }
.contact-btn:hover { filter: brightness(1.05); }

.line-modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.line-modal[hidden] { display: none; }
.line-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(16, 24, 40, 0.5);
}
.line-modal-box {
  position: relative;
  background: var(--panel);
  border-radius: 12px;
  padding: 24px;
  max-width: 320px;
  width: 100%;
  text-align: center;
  box-shadow: 0 8px 24px rgba(16, 24, 40, 0.2);
}
.line-modal-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
  border: none;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
}
.line-modal-close:hover { background: var(--border); }
.line-modal-title { font-weight: 700; margin-bottom: 14px; }
.line-modal-qr {
  display: block;
  width: 100%;
  max-width: 220px;
  margin: 0 auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.line-modal-hint { color: var(--muted); font-size: 0.85rem; margin: 10px 0 0; }
.line-modal-phone-lead { color: var(--muted); font-size: 0.9rem; margin: 0 0 4px; }
.line-modal-phone { font-size: 1.25rem; font-weight: 700; margin: 0; white-space: nowrap; }
