/* ============================================
   GoClinic - Main Stylesheet
   ============================================ */

:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --primary-light: #dbeafe;
    --secondary: #64748b;
    --success: #16a34a;
    --success-light: #dcfce7;
    --danger: #dc2626;
    --danger-light: #fee2e2;
    --warning: #d97706;
    --warning-light: #fef3c7;
    --info: #0891b2;
    --info-light: #cffafe;
    --white: #ffffff;
    --bg: #f1f5f9;
    --bg-dark: #e2e8f0;
    --sidebar-bg: #1e293b;
    --sidebar-text: #94a3b8;
    --sidebar-active: #2563eb;
    --text: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --radius: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.07);
    --sidebar-width: 240px;
    --topbar-height: 60px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: 'Sarabun', sans-serif;
    font-size: 15px;
    color: var(--text);
    background: var(--bg);
    overflow-x: hidden;
}

/* ============================================
   Login Page
   ============================================ */
.login-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #1e40af 0%, #3b82f6 100%);
}

.login-box {
    background: var(--white);
    border-radius: 16px;
    padding: 40px;
    width: 100%;
    max-width: 380px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}

.login-logo {
    text-align: center;
    margin-bottom: 32px;
}

.login-logo i {
    font-size: 48px;
    color: var(--primary);
    margin-bottom: 12px;
    display: block;
}

.login-logo h1 {
    font-size: 28px;
    font-weight: 700;
    color: var(--text);
}

.login-logo p {
    color: var(--text-muted);
    font-size: 14px;
}

.login-field {
    margin-bottom: 18px;
}

.login-label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 6px;
}

.login-input-wrap {
    position: relative;
    display: flex;
    align-items: center;
}

.login-input-icon {
    position: absolute;
    left: 14px;
    color: var(--text-muted);
    font-size: 15px;
    pointer-events: none;
}

.login-input {
    width: 100%;
    padding: 11px 44px 11px 40px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius);
    font-size: 15px;
    font-family: 'Sarabun', sans-serif;
    color: var(--text);
    background: #f8fafc;
    transition: border-color 0.2s, box-shadow 0.2s;
    outline: none;
}

.login-input:focus {
    border-color: var(--primary);
    background: var(--white);
    box-shadow: 0 0 0 3px rgba(37,99,235,0.12);
}

.login-eye-btn {
    position: absolute;
    right: 10px;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 15px;
    padding: 4px 6px;
    border-radius: 4px;
    line-height: 1;
}

.login-eye-btn:hover { color: var(--primary); }

.login-btn {
    width: 100%;
    padding: 12px;
    background: var(--primary);
    color: var(--white);
    border: none;
    border-radius: var(--radius);
    font-size: 16px;
    font-weight: 600;
    font-family: 'Sarabun', sans-serif;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
    margin-top: 4px;
}

.login-btn:hover { background: var(--primary-dark); }
.login-btn:active { transform: scale(0.98); }
.login-btn:disabled { opacity: 0.7; cursor: not-allowed; }

.login-error {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    background: var(--danger-light);
    border: 1px solid #fca5a5;
    border-radius: var(--radius);
    color: var(--danger);
    font-size: 14px;
    margin-bottom: 12px;
}

/* ============================================
   Layout
   ============================================ */
#app {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    transition: width 0.25s ease;
    overflow: hidden;
    z-index: 100;
}

.sidebar.collapsed {
    width: 60px;
}

.sidebar-header {
    display: flex;
    align-items: center;
    padding: 16px;
    border-bottom: 1px solid rgba(255,255,255,0.08);
    min-height: var(--topbar-height);
}

.sidebar-logo-icon {
    font-size: 22px;
    color: var(--primary);
    flex-shrink: 0;
    width: 28px;
    text-align: center;
}

.sidebar-title {
    font-size: 18px;
    font-weight: 700;
    color: var(--white);
    margin-left: 10px;
    white-space: nowrap;
    flex: 1;
}

.sidebar-toggle-btn {
    background: none;
    border: none;
    color: var(--sidebar-text);
    cursor: pointer;
    padding: 4px;
    font-size: 16px;
    margin-left: auto;
    flex-shrink: 0;
}

.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
    scrollbar-width: thin;
    scrollbar-color: rgba(255,255,255,0.15) transparent;
}
.sidebar-nav::-webkit-scrollbar { width: 4px; }
.sidebar-nav::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 2px; }

.nav-group-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: rgba(148,163,184,0.5);
    padding: 12px 16px 4px;
    font-weight: 600;
}
.sidebar.collapsed .nav-group-label {
    display: none;
}

.nav-item {
    display: flex;
    align-items: center;
    padding: 10px 16px;
    color: var(--sidebar-text);
    text-decoration: none;
    border-radius: 6px;
    margin: 2px 8px;
    transition: all 0.15s;
    white-space: nowrap;
    overflow: hidden;
}

.nav-item i {
    width: 24px;
    text-align: center;
    font-size: 16px;
    flex-shrink: 0;
}

.nav-item span {
    margin-left: 10px;
}

.nav-item:hover {
    background: rgba(255,255,255,0.08);
    color: var(--white);
}

.nav-item.active {
    background: var(--primary);
    color: var(--white);
}

.sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid rgba(255,255,255,0.08);
    display: flex;
    align-items: center;
    gap: 8px;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    overflow: hidden;
    font-size: 13px;
    color: var(--sidebar-text);
}

.user-info span {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.btn-logout {
    background: none;
    border: none;
    color: var(--sidebar-text);
    cursor: pointer;
    padding: 6px;
    border-radius: 4px;
    transition: color 0.15s;
}

.btn-logout:hover {
    color: var(--danger);
}

/* Main content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.topbar {
    height: var(--topbar-height);
    background: var(--white);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 20px;
    gap: 12px;
    box-shadow: var(--shadow);
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
}

.topbar-menu-btn {
    background: none;
    border: none;
    font-size: 18px;
    cursor: pointer;
    color: var(--text-muted);
    padding: 6px;
}

.topbar h2 {
    font-size: 18px;
    font-weight: 600;
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.topbar-user {
    font-size: 13px;
    color: var(--text-muted);
}

.topbar-datetime {
    font-size: 13px;
    color: var(--text-muted);
}

.page-content {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
}

/* ============================================
   Components
   ============================================ */

/* Cards */
.card {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.card-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.card-title {
    font-size: 16px;
    font-weight: 600;
}

.card-body {
    padding: 20px;
}

/* KPI Cards */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 20px;
}

.kpi-card {
    background: var(--white);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
    display: flex;
    align-items: center;
    gap: 16px;
}

.kpi-icon {
    width: 52px;
    height: 52px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    flex-shrink: 0;
}

.kpi-icon.blue { background: var(--primary-light); color: var(--primary); }
.kpi-icon.green { background: var(--success-light); color: var(--success); }
.kpi-icon.orange { background: var(--warning-light); color: var(--warning); }
.kpi-icon.red { background: var(--danger-light); color: var(--danger); }

.kpi-info h3 {
    font-size: 26px;
    font-weight: 700;
    line-height: 1.2;
}

.kpi-info p {
    font-size: 13px;
    color: var(--text-muted);
}

/* Tables */
.table-container {
    overflow-x: auto;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

table {
    width: 100%;
    border-collapse: collapse;
    background: var(--white);
    font-size: 14px;
}

thead th {
    background: #f8fafc;
    padding: 12px 16px;
    text-align: left;
    font-weight: 600;
    font-size: 13px;
    color: var(--text-muted);
    border-bottom: 2px solid var(--border);
    white-space: nowrap;
}

tbody td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}

tbody tr:hover {
    background: #f8fafc;
}

tbody tr:last-child td {
    border-bottom: none;
}

/* Badges */
.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 500;
}

.badge-primary { background: var(--primary-light); color: var(--primary); }
.badge-success { background: var(--success-light); color: var(--success); }
.badge-danger  { background: var(--danger-light);  color: var(--danger);  }
.badge-warning { background: var(--warning-light); color: var(--warning); }
.badge-info    { background: var(--info-light);    color: var(--info);    }
.badge-secondary { background: var(--bg-dark); color: var(--secondary); }

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: 6px;
    border: none;
    cursor: pointer;
    font-size: 14px;
    font-family: inherit;
    font-weight: 500;
    text-decoration: none;
    transition: all 0.15s;
    white-space: nowrap;
}

.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.btn-primary { background: var(--primary); color: var(--white); }
.btn-primary:hover { background: var(--primary-dark); }

.btn-success { background: var(--success); color: var(--white); }
.btn-success:hover { background: #15803d; }

.btn-danger { background: var(--danger); color: var(--white); }
.btn-danger:hover { background: #b91c1c; }

.btn-warning { background: var(--warning); color: var(--white); }
.btn-warning:hover { background: #b45309; }

.btn-secondary { background: var(--secondary); color: var(--white); }
.btn-secondary:hover { background: #475569; }

.btn-outline {
    background: transparent;
    color: var(--primary);
    border: 1px solid var(--primary);
}

.btn-outline:hover { background: var(--primary-light); }

.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-lg { padding: 11px 22px; font-size: 16px; }
.btn-block { width: 100%; justify-content: center; }
.btn-icon { padding: 7px; }

/* Forms */
.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 6px;
    color: var(--text);
}

.form-control {
    width: 100%;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    font-family: inherit;
    color: var(--text);
    background: var(--white);
    transition: border-color 0.15s;
    outline: none;
}

.form-control:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,0.12);
}

.form-control::placeholder {
    color: #adb5bd;
}

select.form-control {
    cursor: pointer;
}

textarea.form-control {
    resize: vertical;
    min-height: 80px;
}

.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 16px;
}

.form-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

/* Alerts */
.alert {
    padding: 12px 16px;
    border-radius: 6px;
    margin-bottom: 16px;
    font-size: 14px;
}

.alert-danger  { background: var(--danger-light);  color: #991b1b; }
.alert-success { background: var(--success-light); color: #14532d; }
.alert-warning { background: var(--warning-light); color: #78350f; }
.alert-info    { background: var(--info-light);    color: #155e75; }

/* Search bar */
.search-bar {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

.search-input-wrap {
    position: relative;
    flex: 1;
    min-width: 200px;
}

.search-input-wrap i {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    font-size: 14px;
}

.search-input-wrap input {
    padding-left: 34px;
}

/* Pagination */
.pagination {
    display: flex;
    align-items: center;
    gap: 4px;
    justify-content: center;
    padding: 16px;
}

.page-btn {
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--white);
    cursor: pointer;
    font-size: 14px;
    color: var(--text);
    transition: all 0.15s;
}

.page-btn:hover { background: var(--bg); }
.page-btn.active { background: var(--primary); color: var(--white); border-color: var(--primary); }
.page-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* Modal */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 20px;
}

.modal-container {
    background: var(--white);
    border-radius: 12px;
    width: 100%;
    max-width: 600px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}

.modal-header {
    padding: 20px 24px 16px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-title {
    font-size: 18px;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    color: var(--text-muted);
    padding: 4px;
    line-height: 1;
}

.modal-body {
    padding: 20px 24px;
}

.modal-footer {
    padding: 16px 24px;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

/* Toast notifications */
.toast-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.toast {
    padding: 12px 16px;
    border-radius: 8px;
    color: var(--white);
    font-size: 14px;
    min-width: 250px;
    max-width: 380px;
    box-shadow: var(--shadow-md);
    display: flex;
    align-items: center;
    gap: 8px;
    animation: slideIn 0.3s ease;
}

.toast-success { background: var(--success); }
.toast-danger  { background: var(--danger); }
/* โค้ดเรียก UI.toast(..., 'error') อยู่ 89 จุด ถ้าไม่มีคลาสนี้ ข้อความจะเป็น
   ตัวอักษรขาวบนพื้นโปร่งใส = ผู้ใช้ไม่เห็นเลยว่างานล้มเหลว */
.toast-error   { background: var(--danger); }
.toast-warning { background: var(--warning); }
.toast-info    { background: var(--info); }

@keyframes slideIn {
    from { transform: translateX(100%); opacity: 0; }
    to   { transform: translateX(0); opacity: 1; }
}

/* Loading */
.loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(255,255,255,0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9998;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Status badges */
.status-waiting    { background: #fef3c7; color: #92400e; }
.status-in-progress { background: #dbeafe; color: #1e40af; }
.status-completed  { background: #dcfce7; color: #14532d; }
.status-cancelled  { background: #fee2e2; color: #991b1b; }
.status-scheduled  { background: #ede9fe; color: #5b21b6; }
.status-pending    { background: #fef3c7; color: #92400e; }
.status-paid       { background: #dcfce7; color: #14532d; }

/* Section title */
.section-title {
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 20px;
}

/* Empty state */
.empty-state {
    text-align: center;
    padding: 48px 20px;
    color: var(--text-muted);
}

.empty-state i {
    font-size: 48px;
    margin-bottom: 12px;
    display: block;
    opacity: 0.4;
}

/* Grid */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 20px; }

/* Charts */
.charts-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-top: 20px;
}

.chart-card {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
}

.chart-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 16px;
}

/* Tabs */
.tabs {
    display: flex;
    gap: 0;
    border-bottom: 2px solid var(--border);
    margin-bottom: 20px;
}

.tab-btn {
    padding: 10px 20px;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 14px;
    font-family: inherit;
    color: var(--text-muted);
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: all 0.15s;
    font-weight: 500;
}

.tab-btn.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

/* Action buttons group */
.action-btns {
    display: flex;
    gap: 4px;
}

/* Flex utilities */
.flex { display: flex; }
.flex-1 { flex: 1; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-2 { gap: 8px; }
.gap-4 { gap: 16px; }
.mt-4 { margin-top: 16px; }
.mb-4 { margin-bottom: 16px; }

/* ============================================
   Utility Classes (Bootstrap-compatible)
   ============================================ */

/* Container */
.container-fluid { width: 100%; }

/* Grid System */
.row {
    display: flex;
    flex-wrap: wrap;
    margin-left: -8px;
    margin-right: -8px;
}
[class*="col-"] {
    padding-left: 8px;
    padding-right: 8px;
    box-sizing: border-box;
    min-width: 0;
    flex: 0 0 100%;
    max-width: 100%;
}
@media (min-width: 768px) {
    .col-md-1  { flex: 0 0 8.333%;   max-width: 8.333%; }
    .col-md-2  { flex: 0 0 16.667%;  max-width: 16.667%; }
    .col-md-3  { flex: 0 0 25%;      max-width: 25%; }
    .col-md-4  { flex: 0 0 33.333%;  max-width: 33.333%; }
    .col-md-5  { flex: 0 0 41.667%;  max-width: 41.667%; }
    .col-md-6  { flex: 0 0 50%;      max-width: 50%; }
    .col-md-7  { flex: 0 0 58.333%;  max-width: 58.333%; }
    .col-md-8  { flex: 0 0 66.667%;  max-width: 66.667%; }
    .col-md-9  { flex: 0 0 75%;      max-width: 75%; }
    .col-md-10 { flex: 0 0 83.333%;  max-width: 83.333%; }
    .col-md-11 { flex: 0 0 91.667%;  max-width: 91.667%; }
    .col-md-12 { flex: 0 0 100%;     max-width: 100%; }
}

/* Spacing */
.mb-0 { margin-bottom: 0 !important; }
.mb-1 { margin-bottom: 4px !important; }
.mb-2 { margin-bottom: 8px !important; }
.mb-3 { margin-bottom: 12px !important; }
.mb-5 { margin-bottom: 32px !important; }
.mt-0 { margin-top: 0 !important; }
.mt-1 { margin-top: 4px !important; }
.mt-2 { margin-top: 8px !important; }
.mt-3 { margin-top: 12px !important; }
.mt-5 { margin-top: 32px !important; }
.ms-2 { margin-left: 8px !important; }
.me-2 { margin-right: 8px !important; }
.py-3 { padding-top: 12px !important; padding-bottom: 12px !important; }
.py-4 { padding-top: 16px !important; padding-bottom: 16px !important; }
.px-3 { padding-left: 12px !important; padding-right: 12px !important; }
.pb-3 { padding-bottom: 12px !important; }
.pt-3 { padding-top: 12px !important; }
.p-0  { padding: 0 !important; }

/* Text utilities */
.text-center { text-align: center; }
.text-right  { text-align: right; }
.text-left   { text-align: left; }
.text-muted  { color: var(--text-muted); }
.text-primary { color: var(--primary); }
.text-success { color: var(--success); }
.text-danger  { color: var(--danger); }
.text-warning { color: var(--warning); }
.text-info    { color: var(--info); }
.text-white   { color: var(--white); }
.fw-bold      { font-weight: 700; }
.fw-semibold  { font-weight: 600; }
.small        { font-size: 13px; }

/* Display utilities */
.d-block        { display: block; }
.d-flex         { display: flex; }
.d-none         { display: none; }
.d-inline-flex  { display: inline-flex; }
.d-inline-block { display: inline-block; }
.flex-wrap      { flex-wrap: wrap; }
.align-items-start   { align-items: flex-start; }
.align-items-end     { align-items: flex-end; }
.justify-content-end    { justify-content: flex-end; }
.justify-content-center { justify-content: center; }
.gap-1 { gap: 4px; }
.gap-3 { gap: 12px; }
.ms-auto { margin-left: auto; }

/* Position */
.position-relative { position: relative; }
.position-absolute { position: absolute; }

/* Size */
.w-100 { width: 100%; }
.w-50  { width: 50%; }
.h-100 { height: 100%; }

/* Background utilities */
.bg-light   { background-color: #f8fafc; }
.bg-white   { background-color: var(--white); }
.bg-primary { background-color: var(--primary); color: var(--white); }
.bg-success { background-color: var(--success); color: var(--white); }
.bg-danger  { background-color: var(--danger);  color: var(--white); }
.bg-warning { background-color: var(--warning); color: var(--white); }

/* Border utilities */
.border         { border: 1px solid var(--border); }
.border-0       { border: none !important; }
.border-top     { border-top: 1px solid var(--border); }
.border-bottom  { border-bottom: 1px solid var(--border); }
.border-primary { border-color: var(--primary) !important; }
.border-success { border-color: var(--success) !important; }
.border-danger  { border-color: var(--danger)  !important; }
.rounded        { border-radius: var(--radius); }

/* Table utilities */
.table-responsive { overflow-x: auto; }
.table-hover tbody tr:hover { background: #f8fafc; cursor: pointer; }
thead.table-light th, .table-light { background-color: #f8fafc; }
.table-sm td, .table-sm th { padding: 6px 12px; }
.table-striped tbody tr:nth-child(odd) { background: #f8fafc; }

/* Form utilities */
.form-control-sm { padding: 5px 10px !important; font-size: 13px !important; }
.form-label { display: block; font-size: 14px; font-weight: 500; margin-bottom: 6px; }

/* Additional button variants */
.btn-info  { background: var(--info); color: var(--white); }
.btn-info:hover { background: #0e7490; }
.btn-light { background: #f1f5f9; color: var(--text); border: 1px solid var(--border); }
.btn-light:hover { background: var(--bg-dark); }
.btn-outline-primary {
    background: transparent;
    color: var(--primary);
    border: 1px solid var(--primary);
}
.btn-outline-primary:hover { background: var(--primary-light); }
.btn-outline-secondary {
    background: transparent;
    color: var(--secondary);
    border: 1px solid var(--secondary);
}
.btn-outline-secondary:hover { background: var(--bg-dark); }
.btn-outline-danger {
    background: transparent;
    color: var(--danger);
    border: 1px solid var(--danger);
}
.btn-outline-danger:hover { background: var(--danger-light); }
.btn-outline-success {
    background: transparent;
    color: var(--success);
    border: 1px solid var(--success);
}
.btn-outline-success:hover { background: var(--success-light); }

/* Standalone button support (used without .btn base class) */
button:not(.btn)[class*="btn-"],
a:not(.btn)[class*="btn-"] {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    border-radius: 6px;
    border: none;
    cursor: pointer;
    font-size: 14px;
    font-family: inherit;
    font-weight: 500;
    text-decoration: none;
    transition: all 0.15s;
}

/* Dropdown */
.dropdown-menu {
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    z-index: 1050;
    min-width: 200px;
    max-height: 300px;
    overflow-y: auto;
}
.dropdown-item {
    display: block;
    padding: 8px 16px;
    color: var(--text);
    cursor: pointer;
    text-decoration: none;
    font-size: 14px;
}
.dropdown-item:hover { background: var(--bg); }
.dropdown-item.disabled { color: var(--text-muted); cursor: default; }
.dropdown-item.disabled:hover { background: none; }

/* ============================================
   Page Component Classes
   ============================================ */

/* Page header — flex bar with title + action button */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
    gap: 12px;
    flex-wrap: wrap;
}
.page-header h1,
.page-header h2 {
    font-size: 20px;
    font-weight: 600;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 8px;
}
.page-header h1 i,
.page-header h2 i { color: var(--primary); }

/* Section header */
.section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
    gap: 12px;
}
.section-header h2,
.section-header h3 { font-size: 16px; font-weight: 600; margin: 0; }

/* Header title */
.header-title { flex: 1; }
.header-title h2 { font-size: 20px; font-weight: 600; margin: 0; }

/* Filter section */
.filter-section {
    background: var(--white);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 16px;
    box-shadow: var(--shadow);
}
.filter-row {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    align-items: flex-end;
}
.filter-row .form-group {
    flex: 1;
    min-width: 160px;
    margin-bottom: 0;
}
.filter-row .form-group label {
    font-size: 12px;
    font-weight: 500;
    color: var(--text-muted);
    margin-bottom: 4px;
    display: block;
}
.filter-row .form-group select,
.filter-row .form-group input {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    font-family: inherit;
    background: var(--white);
    outline: none;
}
.filter-row .form-group select:focus,
.filter-row .form-group input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,0.12);
}

/* Filters row (labs style) */
.filters-row {
    display: flex;
    gap: 10px;
    align-items: center;
    margin-bottom: 16px;
    flex-wrap: wrap;
}
.filter-group { flex: 1; min-width: 140px; }
.filter-input,
.filter-select {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    font-family: inherit;
    background: var(--white);
    outline: none;
}
.filter-input:focus,
.filter-select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,0.12);
}

/* Summary cards (billing) */
.summary-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 20px;
}
.card-label  { font-size: 13px; color: var(--text-muted); margin-bottom: 4px; }
.card-count  { font-size: 28px; font-weight: 700; color: var(--text); }
.card-amount { font-size: 15px; font-weight: 600; margin-top: 4px; }

/* Back button */
.btn-back {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 14px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    font-family: inherit;
    color: var(--text);
    text-decoration: none;
}
.btn-back:hover { background: var(--bg-dark); }

/* Tabs container */
.tabs-nav {
    display: flex;
    border-bottom: 2px solid var(--border);
    margin-bottom: 20px;
    gap: 0;
    background: var(--white);
    border-radius: var(--radius) var(--radius) 0 0;
    overflow: hidden;
    box-shadow: var(--shadow);
}
.tabs-nav .tab-btn {
    flex: 0 0 auto;
    padding: 12px 20px;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 14px;
    font-family: inherit;
    color: var(--text-muted);
    border-bottom: 3px solid transparent;
    transition: all 0.15s;
    font-weight: 500;
}
.tabs-nav .tab-btn:hover { color: var(--primary); background: var(--primary-light); }
.tabs-nav .tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); background: var(--white); }
.tab-pane { display: none; }
.tab-pane.active { display: block; }

/* Order detail */
.order-info-header {
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
    background: var(--white);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: var(--shadow);
}
.info-group { flex: 1; min-width: 120px; }
.info-group label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 4px;
    display: block;
}
.order-items-section {
    background: var(--white);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
    margin-bottom: 20px;
}
.order-items-section h3 {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Abnormal lab row */
.abnormal-row { background: #fff5f5; }
.abnormal-row:hover { background: #fee2e2 !important; }

/* Pagination container */
.pagination-container { display: flex; justify-content: center; padding: 16px 0; }

/* Result input */
.result-input { max-width: 120px; }

/* fa-spin animation support */
.fa-spin { animation: spin 1s linear infinite; }

/* Treatment action tiles (visit queue modal) */
.visit-treat-tile {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 20px 12px;
    background: var(--white);
    border: 1.5px solid var(--border);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.18s;
    font-family: inherit;
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
}
.visit-treat-tile:hover {
    border-color: var(--primary);
    background: var(--primary-light);
    box-shadow: 0 4px 14px rgba(37,99,235,0.12);
    transform: translateY(-2px);
}
.visit-treat-tile i { font-size: 28px; }
.visit-treat-tile span { font-size: 13px; }

/* Treatment Modal Tabs */
.treat-tab {
    display: flex; align-items: center; gap: 6px;
    padding: 10px 14px; border: none; background: transparent;
    font-size: 13px; font-weight: 500; cursor: pointer;
    color: #64748b; border-bottom: 2px solid transparent;
    margin-bottom: -2px; transition: color 0.15s, border-color 0.15s;
    font-family: inherit;
}
.treat-tab:hover { color: #2563eb; }
.treat-tab-active { color: #2563eb; border-bottom-color: #2563eb; font-weight: 600; }
.treat-badge {
    background: #2563eb; color: #fff; border-radius: 10px;
    font-size: 10px; padding: 1px 6px; font-weight: 700; line-height: 1.4;
}

/* Responsive overrides for utility grid */
@media (max-width: 768px) {
    .row { margin-left: -6px; margin-right: -6px; }
    [class*="col-"] { padding: 0 6px; margin-bottom: 12px; }
    .filter-row { flex-direction: column; }
    .filter-row .form-group { min-width: 100%; }
    .stats-row { flex-direction: column; }
    .summary-cards { grid-template-columns: 1fr 1fr; }
    .page-header { flex-direction: column; align-items: flex-start; }
    .order-info-header { flex-direction: column; }
}
@media (max-width: 480px) {
    .summary-cards { grid-template-columns: 1fr; }
}

/* Responsive */
@media (max-width: 768px) {
    :root { --sidebar-width: 0px; }
    /* sidebar off-canvas + .sidebar.open ย้ายไปคุมรวมที่บล็อก "Responsive — เฟส 1" ท้ายไฟล์ (≤840px)
       เพื่อไม่ให้ left/width/transform ขัดกันเอง */
    .main-content { width: 100%; }
    .kpi-grid { grid-template-columns: 1fr 1fr; }
    .charts-grid { grid-template-columns: 1fr; }
    .grid-2, .grid-3 { grid-template-columns: 1fr; }
}

/* ============================================
   Patient Module Styles
   ============================================ */

/* Toolbar */
.patient-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.patient-toolbar-left { display: flex; align-items: center; gap: 12px; flex: 1; }
.patient-toolbar-right { display: flex; align-items: center; gap: 10px; }

/* View Toggle */
.view-toggle {
    display: flex;
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: 6px;
    overflow: hidden;
}

.view-toggle-btn {
    background: none;
    border: none;
    padding: 8px 12px;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 14px;
    transition: all 0.15s;
}

.view-toggle-btn.active {
    background: var(--primary);
    color: var(--white);
}

.view-toggle-btn:hover:not(.active) {
    background: var(--bg);
}

/* Patient Table Row */
.patient-row:hover {
    background: var(--primary-light) !important;
}

/* Patient Avatar Small (table) */
.patient-avatar-sm {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--primary-light);
    color: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    overflow: hidden;
    flex-shrink: 0;
}

.patient-avatar-sm.female {
    background: #fce7f3;
    color: #db2777;
}

.patient-avatar-sm img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* HN Badge */
.hn-badge {
    display: inline-block;
    background: var(--primary-light);
    color: var(--primary);
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 13px;
    font-weight: 600;
    font-family: monospace, 'Sarabun', sans-serif;
}

.hn-badge-lg {
    font-size: 15px;
    padding: 4px 12px;
    border-radius: 6px;
}

/* Patient Name Cell */
.patient-name-cell {
    display: flex;
    align-items: center;
    gap: 6px;
}

.patient-name { font-weight: 500; }

/* Allergy Flag */
.allergy-flag {
    color: var(--danger);
    font-size: 13px;
    animation: pulse-allergy 2s infinite;
}

@keyframes pulse-allergy {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.allergy-label-warning {
    color: var(--danger);
    font-size: 12px;
    font-weight: 600;
    margin-left: 8px;
}

.allergy-input:focus {
    border-color: var(--danger) !important;
    box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.12) !important;
}

.allergy-banner {
    background: var(--danger-light);
    border: 1px solid #fca5a5;
    border-radius: 8px;
    padding: 10px 12px;
    margin-top: 12px;
    display: flex;
    align-items: flex-start;
    gap: 8px;
    color: #991b1b;
    font-size: 13px;
}

.allergy-banner i {
    color: var(--danger);
    margin-top: 2px;
    flex-shrink: 0;
}

.allergy-banner strong { display: block; margin-bottom: 2px; }

.allergy-banner.compact {
    padding: 6px 10px;
    margin-top: 8px;
    font-size: 12px;
    border-radius: 6px;
}

/* ---- Card View ---- */
.patient-card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 16px;
}

.patient-card {
    background: var(--white);
    border-radius: 10px;
    box-shadow: var(--shadow);
    cursor: pointer;
    transition: all 0.2s;
    overflow: hidden;
    border: 1px solid transparent;
}

.patient-card:hover {
    border-color: var(--primary);
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.15);
    transform: translateY(-2px);
}

.patient-card-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px;
    position: relative;
}

.patient-card-header .allergy-flag {
    position: absolute;
    top: 12px;
    right: 12px;
    font-size: 16px;
}

.patient-avatar {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--primary-light);
    color: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    overflow: hidden;
    flex-shrink: 0;
}

.patient-avatar.female {
    background: #fce7f3;
    color: #db2777;
}

.patient-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.patient-card-info h4 {
    font-size: 15px;
    font-weight: 600;
    margin: 4px 0 2px;
}

.patient-card-info p {
    font-size: 13px;
    color: var(--text-muted);
}

.patient-card-body {
    padding: 0 16px 12px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.patient-card-field {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: var(--text-muted);
}

.patient-card-field i {
    width: 16px;
    text-align: center;
    color: var(--secondary);
    font-size: 12px;
}

.patient-card-actions {
    display: flex;
    gap: 6px;
    padding: 10px 16px;
    border-top: 1px solid var(--border);
    background: #fafbfc;
}

/* ---- Detail Page ---- */
.detail-topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
    flex-wrap: wrap;
}

.detail-topbar-actions {
    display: flex;
    gap: 8px;
    margin-left: auto;
    flex-wrap: wrap;
}

.detail-layout {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 20px;
    align-items: start;
}

/* Detail Sidebar */
.detail-sidebar {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.profile-card {
    background: var(--white);
    border-radius: 12px;
    box-shadow: var(--shadow);
    padding: 24px;
    text-align: center;
}

.profile-photo-wrap {
    margin-bottom: 16px;
}

.profile-photo {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    background: var(--primary-light);
    color: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 56px;
    margin: 0 auto;
    overflow: hidden;
    border: 4px solid var(--white);
    box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

.profile-photo.female {
    background: #fce7f3;
    color: #db2777;
}

.profile-photo img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.profile-card h3 {
    font-size: 18px;
    font-weight: 600;
    margin: 8px 0 6px;
}

.profile-meta {
    font-size: 14px;
    color: var(--text-muted);
    margin-top: 8px;
    line-height: 1.6;
}

.profile-meta small {
    font-size: 12px;
}

/* Detail Info Card */
.detail-info-card {
    background: var(--white);
    border-radius: 10px;
    box-shadow: var(--shadow);
    padding: 16px;
}

.detail-info-card h4 {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.detail-info-card h4 i { font-size: 13px; }

.info-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.info-item {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    font-size: 14px;
}

.info-item > i {
    width: 18px;
    text-align: center;
    color: var(--primary);
    margin-top: 3px;
    font-size: 13px;
    flex-shrink: 0;
}

.info-item label {
    font-size: 12px;
    color: var(--text-muted);
    display: block;
    margin-bottom: 1px;
    font-weight: 500;
}

.info-item span {
    color: var(--text);
    word-break: break-word;
}

/* Detail Main Content */
.detail-main {
    background: var(--white);
    border-radius: 12px;
    box-shadow: var(--shadow);
    min-height: 400px;
}

.detail-main .tabs {
    padding: 0 20px;
    margin-bottom: 0;
}

.tab-count {
    display: inline-block;
    background: var(--bg-dark);
    color: var(--text-muted);
    font-size: 11px;
    padding: 1px 6px;
    border-radius: 10px;
    margin-left: 4px;
    font-weight: 600;
}

.tab-panel {
    display: none;
    padding: 20px;
}

.tab-panel.active {
    display: block;
}

/* ---- Form Page ---- */
.patient-form-page {
    max-width: 1100px;
}

.form-layout {
    display: grid;
    grid-template-columns: 240px 1fr;
    gap: 24px;
    align-items: start;
}

.form-sidebar {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.form-main {
    display: flex;
    flex-direction: column;
    gap: 0;
}

/* Photo Section */
.photo-section {
    background: var(--white);
    border-radius: 10px;
    box-shadow: var(--shadow);
    padding: 20px;
    text-align: center;
}

.photo-preview {
    width: 180px;
    height: 180px;
    border-radius: 50%;
    background: var(--bg);
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 72px;
    margin: 0 auto 16px;
    overflow: hidden;
    border: 3px dashed var(--border);
    transition: border-color 0.2s;
}

.photo-preview:hover {
    border-color: var(--primary);
}

.photo-preview img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.photo-actions {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

/* ID Card Section */
.idcard-section {
    background: var(--white);
    border-radius: 10px;
    box-shadow: var(--shadow);
    padding: 16px;
    text-align: center;
}

.text-hint {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 6px;
}

/* ID Card Scanner Overlay */
.idcard-scan-area {
    position: relative;
    display: inline-block;
}

.idcard-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.idcard-frame {
    width: 85%;
    height: 60%;
    border: 2px dashed rgba(255,255,255,0.7);
    border-radius: 12px;
    box-shadow: 0 0 0 9999px rgba(0,0,0,0.3);
}

/* Progress Bar */
.progress-bar-wrap {
    width: 100%;
    height: 8px;
    background: var(--bg-dark);
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 8px;
}

.progress-bar {
    height: 100%;
    background: var(--primary);
    border-radius: 4px;
    transition: width 0.3s ease;
}

/* Form Sections */
.form-section {
    background: var(--white);
    border-radius: 10px;
    box-shadow: var(--shadow);
    padding: 20px;
    margin-bottom: 16px;
}

.form-section-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 16px;
    padding-bottom: 8px;
    border-bottom: 2px solid var(--primary-light);
    display: flex;
    align-items: center;
    gap: 8px;
}

.form-section-title i { font-size: 14px; }

.required { color: var(--danger); }

/* Patient Info Mini (in modals) */
.patient-info-mini {
    background: var(--bg);
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 16px;
    font-size: 14px;
}

/* Webcam */
.webcam-container {
    position: relative;
    display: inline-block;
}

/* Timeline */
.timeline {
    position: relative;
    padding-left: 32px;
}

.timeline::before {
    content: '';
    position: absolute;
    left: 14px;
    top: 0;
    bottom: 0;
    width: 2px;
    background: var(--border);
}

.timeline-item {
    position: relative;
    padding-bottom: 20px;
    display: flex;
    gap: 12px;
}

.timeline-dot {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    flex-shrink: 0;
    position: absolute;
    left: -32px;
    background: var(--white);
    border: 2px solid var(--border);
    z-index: 1;
}

.timeline-dot.blue   { border-color: var(--primary); color: var(--primary); background: var(--primary-light); }
.timeline-dot.green  { border-color: var(--success); color: var(--success); background: var(--success-light); }
.timeline-dot.purple { border-color: #7c3aed; color: #7c3aed; background: #ede9fe; }
.timeline-dot.orange { border-color: var(--warning); color: var(--warning); background: var(--warning-light); }
.timeline-dot.red    { border-color: var(--danger);  color: var(--danger);  background: var(--danger-light); }

.timeline-content {
    background: var(--bg);
    border-radius: 8px;
    padding: 10px 14px;
    flex: 1;
}

.timeline-date {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 2px;
}

.timeline-title {
    font-weight: 600;
    font-size: 14px;
    margin-bottom: 2px;
}

.timeline-desc {
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 4px;
}

/* ---- Patient Module Responsive ---- */
@media (max-width: 960px) {
    .detail-layout {
        grid-template-columns: 1fr;
    }
    .form-layout {
        grid-template-columns: 1fr;
    }
    .form-sidebar {
        flex-direction: row;
        flex-wrap: wrap;
    }
    .photo-section {
        flex: 1;
        min-width: 200px;
    }
    .idcard-section {
        flex: 1;
        min-width: 200px;
    }
}

@media (max-width: 640px) {
    .patient-toolbar {
        flex-direction: column;
        align-items: stretch;
    }
    .patient-toolbar-right {
        justify-content: space-between;
    }
    .patient-card-grid {
        grid-template-columns: 1fr;
    }
    .detail-topbar-actions {
        width: 100%;
    }
    .detail-topbar-actions .btn {
        flex: 1;
        justify-content: center;
        font-size: 13px;
        padding: 6px 8px;
    }
}

/* ============================================
   Module-Shared Styles
   ============================================ */

/* Page Header */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 12px;
}
.page-header h3 {
    font-size: 20px;
    font-weight: 600;
    color: var(--text);
}

/* Tab Navigation */
.tab-nav {
    display: flex;
    border-bottom: 2px solid var(--border);
    margin-bottom: 20px;
    gap: 0;
}
.tab-btn {
    padding: 10px 20px;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-muted);
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: all 0.2s;
}
.tab-btn:hover {
    color: var(--primary);
}
.tab-btn.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

/* Filter Bar */
.filter-bar {
    display: flex;
    gap: 10px;
    margin-bottom: 16px;
    flex-wrap: wrap;
    align-items: center;
}
.filter-bar input, .filter-bar select {
    padding: 7px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    background: var(--white);
}
.filter-bar input:focus, .filter-bar select:focus {
    border-color: var(--primary);
    outline: none;
    box-shadow: 0 0 0 2px var(--primary-light);
}

/* Stats Cards Row */
.stats-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 16px;
    margin-bottom: 20px;
}
.stat-card {
    background: var(--white);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
    text-align: center;
}
.stat-card .stat-value {
    font-size: 28px;
    font-weight: 700;
    color: var(--primary);
}
.stat-card .stat-label {
    font-size: 13px;
    color: var(--text-muted);
    margin-top: 4px;
}
.stat-card.stat-warning .stat-value { color: var(--warning); }
.stat-card.stat-success .stat-value { color: var(--success); }
.stat-card.stat-danger .stat-value { color: var(--danger); }
.stat-card.stat-info .stat-value { color: var(--info); }

/* Detail Page Layout */
.detail-page {
    max-width: 1100px;
}
.detail-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 10px;
}
.detail-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

/* Info Cards */
.info-card {
    background: var(--white);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
    margin-bottom: 16px;
}
.info-card h4 {
    font-size: 15px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
}
.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 10px;
}
.info-item {
    display: flex;
    flex-direction: column;
}
.info-item .label {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 2px;
}
.info-item .value {
    font-size: 14px;
    font-weight: 500;
}

/* Form Sections */
.form-section {
    background: var(--white);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
    box-shadow: var(--shadow);
}
.form-section h4 {
    font-size: 15px;
    font-weight: 600;
    margin-bottom: 16px;
    padding-bottom: 8px;
    border-bottom: 2px solid var(--primary-light);
    color: var(--primary);
}
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 12px;
}

/* Autocomplete Dropdown */
.autocomplete-wrap {
    position: relative;
}
.autocomplete-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: 0 0 var(--radius) var(--radius);
    box-shadow: var(--shadow-md);
    max-height: 250px;
    overflow-y: auto;
    z-index: 100;
}
.autocomplete-item {
    padding: 8px 12px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
}
.autocomplete-item:hover {
    background: var(--primary-light);
}
.autocomplete-item:last-child {
    border-bottom: none;
}

/* Allergy Warning */
.allergy-warning {
    background: var(--danger-light);
    color: var(--danger);
    padding: 6px 12px;
    border-radius: var(--radius);
    font-size: 13px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

/* Items Table (for prescriptions, billing, lab orders) */
.items-table {
    width: 100%;
    border-collapse: collapse;
    margin: 12px 0;
}
.items-table th {
    background: var(--bg);
    padding: 8px 12px;
    text-align: left;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
    border-bottom: 2px solid var(--border);
}
.items-table td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    font-size: 14px;
}
.items-table tr:hover {
    background: rgba(37,99,235,0.03);
}
.items-table .text-right {
    text-align: right;
}
.items-table .text-center {
    text-align: center;
}
.items-table .total-row {
    font-weight: 700;
    background: var(--bg);
}
.items-table .total-row td {
    border-top: 2px solid var(--border);
}

/* Summary Box */
.summary-box {
    background: var(--bg);
    border-radius: var(--radius);
    padding: 16px;
    margin-top: 12px;
}
.summary-row {
    display: flex;
    justify-content: space-between;
    padding: 6px 0;
    font-size: 14px;
}
.summary-row.total {
    font-size: 18px;
    font-weight: 700;
    border-top: 2px solid var(--border);
    padding-top: 10px;
    margin-top: 4px;
}
.text-danger { color: var(--danger); }
.text-success { color: var(--success); }
.text-warning { color: var(--warning); }

/* Eye Exam Grid */
.exam-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}
.exam-col h5 {
    font-size: 14px;
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 8px;
}

/* Signature Canvas */
.signature-pad-wrap {
    margin-bottom: 16px;
}
.signature-pad-wrap label {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 6px;
    display: block;
}
.signature-canvas {
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    cursor: crosshair;
    background: var(--white);
    display: block;
    touch-action: none;
}
.signature-canvas:active {
    border-color: var(--primary);
}
.signature-img {
    max-width: 300px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 4px;
    background: var(--white);
}

/* Document Grid */
.doc-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 16px;
}
.doc-card {
    background: var(--white);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s;
}
.doc-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}
.doc-card-thumb {
    height: 140px;
    background: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}
.doc-card-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.doc-card-thumb i {
    font-size: 48px;
    color: var(--text-muted);
}
.doc-card-thumb .icon-pdf { color: #dc2626; }
.doc-card-thumb .icon-image { color: #2563eb; }
.doc-card-thumb .icon-doc { color: #16a34a; }
.doc-card-body {
    padding: 10px 12px;
}
.doc-card-title {
    font-size: 13px;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.doc-card-meta {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 4px;
}

/* Document Viewer */
.doc-viewer {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.9);
    z-index: 1000;
    display: flex;
    flex-direction: column;
}
.doc-viewer-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 16px;
    background: rgba(0,0,0,0.5);
    color: var(--white);
}
.doc-viewer-toolbar button {
    background: rgba(255,255,255,0.15);
    border: none;
    color: white;
    padding: 6px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
}
.doc-viewer-toolbar button:hover {
    background: rgba(255,255,255,0.25);
}
.doc-viewer-content {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: auto;
    padding: 20px;
}
.doc-viewer-content img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

/* Upload Area */
.upload-area {
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    padding: 30px;
    text-align: center;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s;
}
.upload-area:hover, .upload-area.drag-over {
    border-color: var(--primary);
    background: var(--primary-light);
}
.upload-area i {
    font-size: 36px;
    color: var(--text-muted);
    margin-bottom: 8px;
}
.upload-area p {
    font-size: 14px;
    color: var(--text-muted);
}

/* Camera Section */
.camera-section {
    margin-top: 12px;
}
.camera-preview {
    width: 100%;
    max-width: 480px;
    background: #000;
    border-radius: var(--radius);
}
.camera-controls {
    display: flex;
    gap: 10px;
    margin-top: 8px;
}

/* Print Styles */
@media print {
    body { background: white; font-size: 12pt; }
    .sidebar, .topbar, .toast-container, .modal-overlay, .loading-overlay { display: none !important; }
    .main-content { margin: 0; padding: 0; }
    .page-content { padding: 0; }
    .btn, .filter-bar, .tab-nav, .pagination { display: none !important; }
    .info-card, .form-section { box-shadow: none; border: 1px solid #ddd; }
}

/* Certificate Print Layout */
.cert-print {
    font-family: 'TH Sarabun New', 'Sarabun', serif;
    font-size: 16pt;
    line-height: 1.6;
    max-width: 210mm;
    margin: 0 auto;
    padding: 20mm;
}
.cert-print .cert-header {
    text-align: center;
    margin-bottom: 20px;
}
.cert-print .cert-header h2 {
    font-size: 22pt;
    font-weight: 700;
}
.cert-print .cert-body {
    text-indent: 2em;
}
.cert-print .cert-sig {
    margin-top: 40px;
    text-align: right;
    width: 50%;
    margin-left: auto;
}

/* Thermal Receipt */
.thermal-receipt {
    width: 80mm;
    font-family: 'Courier New', monospace;
    font-size: 12px;
    padding: 5mm;
}
.thermal-receipt .receipt-header {
    text-align: center;
    border-bottom: 1px dashed #000;
    padding-bottom: 5px;
    margin-bottom: 5px;
}
.thermal-receipt .receipt-items {
    width: 100%;
}
.thermal-receipt .receipt-total {
    border-top: 1px dashed #000;
    font-weight: bold;
    padding-top: 5px;
    margin-top: 5px;
}

/* Abnormal Lab Result */
.result-abnormal {
    color: var(--danger);
    font-weight: 700;
}

/* Payment Method Icons */
.payment-method {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
}

/* Badge Variants */
.badge-secondary {
    background: #e2e8f0;
    color: #475569;
}
.badge-primary {
    background: var(--primary-light);
    color: var(--primary-dark);
}

/* Quick Action Buttons in Detail */
.action-group {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

/* Responsive for new modules */
@media (max-width: 768px) {
    .exam-grid { grid-template-columns: 1fr; }
    .doc-grid { grid-template-columns: repeat(2, 1fr); }
    .stats-row { grid-template-columns: repeat(2, 1fr); }
    .filter-bar { flex-direction: column; }
    .page-header { flex-direction: column; align-items: stretch; }
}
@media (max-width: 480px) {
    .doc-grid { grid-template-columns: 1fr; }
    .stats-row { grid-template-columns: 1fr; }
}

/* ============================================================================
   Responsive — เฟส 1
   ----------------------------------------------------------------------------
   บล็อกนี้เพิ่มใหม่ทั้งหมด และเป็น "ตัวคุมหลัก" ของพฤติกรรม responsive
   ต้องอยู่ท้ายไฟล์เสมอ เพราะอาศัยลำดับ cascade ทับกฎ responsive เดิมด้านบน

   โครงสร้าง:
     A. Base            — กฎที่จำเป็นต้องอยู่นอก media query
     B. ≤840px          — แท็บเล็ต: sidebar off-canvas, modal sheet, grid 1 คอลัมน์
     C. ≤700px          — มือถือ: ตาราง→การ์ด, topbar กระชับ, ปุ่มเต็มความกว้าง
     D. ≤480px          — มือถือจอเล็ก
     E. landscape เตี้ย — มือถือแนวนอน
     F. pointer: coarse — touch target ≥44px / input ≥16px (วางท้ายสุดให้ชนะ)
     G. prefers-reduced-motion

   หมายเหตุ: ทุกบล็อกที่อิงความกว้างใช้ `@media screen and (...)` เพื่อไม่ให้
   มีผลตอนสั่งพิมพ์ (กระดาษ A4 = ~794px จะเข้าเงื่อนไข ≤840px โดยไม่ตั้งใจ)
   ทำให้ @media print และ .cert-print / .thermal-receipt ทำงานเหมือนเดิม
   ============================================================================ */

/* ---------- A. Base ---------- */

/* กันข้อความพองเองบน iOS ตอนหมุนจอ */
html { -webkit-text-size-adjust: 100%; }

/* backdrop ของเมนูมือถือ (JS แทรก <div class="sidebar-backdrop"> เป็นลูกของ body)
   ค่าเริ่มต้นซ่อนเสมอ และจะถูกเปิดใช้เฉพาะในบล็อก ≤840px เท่านั้น
   เพื่อไม่ให้หลุดมาบังจอเดสก์ท็อป */
.sidebar-backdrop { display: none; }

@keyframes gcFadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ---------- B. ≤840px — แท็บเล็ต ---------- */
@media screen and (max-width: 840px) {

    /* ใช้ dynamic viewport height เลี่ยงปัญหาแถบ URL ของ mobile browser */
    @supports (height: 100dvh) {
        #app { height: 100dvh; }
        .login-page { min-height: 100dvh; }
    }

    /* ขนาดช่องกรอก/ปุ่มบนจอแคบ — ไม่ผูกกับ pointer:coarse เพราะอุปกรณ์สัมผัสหลายรุ่น
       (แท็บเล็ต Windows, เครื่องที่มีปากกา) รายงานตัวเองเป็น pointer:fine
       16px คือค่าต่ำสุดที่ iOS ไม่ซูมหน้าจอเองตอนโฟกัส */
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
    select,
    textarea,
    input.form-control,
    select.form-control,
    textarea.form-control,
    .login-input {
        font-size: 16px;
        min-height: 44px;
    }
    textarea, textarea.form-control { min-height: 88px; }
    .btn, button.btn, .btn-block { min-height: 44px; }
    .topbar-menu-btn, .sidebar-toggle-btn, .btn-logout {
        min-width: 44px;
        min-height: 44px;
    }

    /* --- Sidebar off-canvas --- */
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        height: 100%;
        width: 260px;
        max-width: 82vw;
        z-index: 950;              /* เหนือ content แต่ต่ำกว่า modal-overlay (1000) */
        transform: translateX(-100%);
        visibility: hidden;
        transition: transform 0.25s ease, visibility 0s linear 0.25s;
        box-shadow: 4px 0 24px rgba(0,0,0,0.28);
    }
    .sidebar.open {
        transform: translateX(0);
        visibility: visible;
        transition: transform 0.25s ease, visibility 0s;
    }
    /* .collapsed เป็นสถานะย่อเมนูของเดสก์ท็อป — ถ้าติดมาจากการ resize ต้องไม่ทำให้ drawer แคบผิดรูป */
    .sidebar.collapsed {
        width: 260px;
        max-width: 82vw;
    }
    .sidebar.collapsed .nav-group-label { display: block; }
    .sidebar-nav { overscroll-behavior: contain; }

    /* --- Backdrop --- */
    .sidebar-backdrop {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(15,23,42,0.5);
        z-index: 940;              /* ใต้ sidebar (950) เหนือ topbar/page-content */
        -webkit-tap-highlight-color: transparent;
    }
    .sidebar-backdrop.show {
        display: block;
        animation: gcFadeIn 0.18s ease;
    }
    /* ล็อกการเลื่อนพื้นหลังขณะเปิดเมนู (progressive enhancement) */
    body:has(.sidebar-backdrop.show) { overflow: hidden; }

    .main-content { width: 100%; min-width: 0; }

    /* --- Modal → full-screen sheet ---
       JS ตั้ง inline max-width (460/480/600/640/800/820/860px) จึงต้องใช้ !important */
    .modal-overlay {
        padding: 0;
        align-items: flex-end;
    }
    .modal-container {
        width: 100%;
        max-width: 100% !important;
        max-height: 92vh;
        border-radius: 16px 16px 0 0;
        overscroll-behavior: contain;
    }
    .modal-header { padding: 16px 18px 14px; }
    /* ไม่ตั้ง overflow-x ที่นี่ — จะทำให้ overflow-y กลายเป็น auto ตามสเปก
       แล้ว dropdown/autocomplete ที่ล้นออกนอก modal-body ถูกตัดหาย
       ตารางกว้างมี .table-container ครอบ scroll ให้อยู่แล้ว */
    .modal-body   { padding: 16px 18px; }
    .modal-footer {
        position: sticky;
        bottom: 0;
        background: var(--white);
        padding: 12px 18px calc(12px + env(safe-area-inset-bottom, 0px));
        box-shadow: 0 -4px 12px rgba(0,0,0,0.06);
        z-index: 1;
    }

    /* --- Grid หลายคอลัมน์ → คอลัมน์เดียว --- */
    .form-row,
    .form-grid,
    .info-grid,
    .exam-grid,
    .charts-grid,
    .grid-2,
    .grid-3,
    .detail-layout,
    .form-layout {
        grid-template-columns: 1fr;
    }
    .kpi-grid,
    .stats-row,
    .summary-cards,
    .patient-card-grid,
    .doc-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    /* --- Filter bar: ห่อบรรทัดแทนการอัดในแถวเดียว --- */
    .filter-bar,
    .filters-row,
    .search-bar {
        flex-direction: row;
        flex-wrap: wrap;
        align-items: stretch;
    }
    .filter-bar > input,
    .filter-bar > select,
    .filter-group {
        flex: 1 1 180px;
        min-width: 0;
    }
    .filter-bar > .btn,
    .filter-bar > button,
    .filters-row > .btn { justify-content: center; }
    .filter-row .form-group { flex: 1 1 220px; min-width: 0; }

    /* --- Tabs เลื่อนแนวนอนได้ ไม่ตกบรรทัดจนพัง --- */
    .tabs,
    .tabs-nav,
    .tab-nav {
        flex-wrap: nowrap;
        overflow-x: auto;
        /* ไม่ตั้ง overflow-y:hidden — จะตัดเส้นใต้ของแท็บที่เลือกอยู่
           (.tab-btn ใช้ margin-bottom ติดลบให้เส้นทับขอบ container พอดี) */
        padding-bottom: 2px;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }
    .tabs::-webkit-scrollbar,
    .tabs-nav::-webkit-scrollbar,
    .tab-nav::-webkit-scrollbar { display: none; }
    .tabs .tab-btn,
    .tabs-nav .tab-btn,
    .tab-nav .tab-btn,
    .treat-tab {
        flex: 0 0 auto;
        white-space: nowrap;
    }

    /* --- เบ็ดเตล็ด --- */
    .page-content { padding: 16px; }
    .detail-page, .patient-form-page { max-width: 100%; }
    .form-sidebar { flex-direction: row; flex-wrap: wrap; }
    .toast-container { left: 16px; right: 16px; }
    .toast { min-width: 0; max-width: 100%; }
}

/* ---------- C. ≤700px — มือถือ ---------- */
@media screen and (max-width: 700px) {

    /* --- ตาราง → การ์ด ---
       สัญญากับฝั่ง JS: <table class="cards-on-mobile"> และทุก <td data-th="ชื่อคอลัมน์"> */
    table.cards-on-mobile {
        display: block;
        background: transparent;
        border-collapse: separate;
    }
    table.cards-on-mobile thead { display: none; }
    table.cards-on-mobile tbody,
    table.cards-on-mobile tr,
    table.cards-on-mobile td {
        display: block;
        width: 100%;
    }
    table.cards-on-mobile tr {
        background: var(--white);
        border: 1px solid var(--border);
        border-radius: var(--radius);
        box-shadow: var(--shadow);
        margin-bottom: 12px;
        padding: 2px 0;
        /* ห้าม overflow:hidden — จะตัดปุ่ม action ที่อยู่แถวล่างของการ์ดหายไปกดไม่ได้ */
    }
    table.cards-on-mobile tr:last-child { margin-bottom: 0; }
    /* hover เดิมของ tbody tr ทำให้พื้นการ์ดเทาค้างบนจอสัมผัส
       ต้องมี !important เพราะ .patient-row:hover ประกาศ !important ไว้ */
    table.cards-on-mobile tbody tr:hover { background: var(--white) !important; }
    /* แถวที่มีความหมายเชิงคลินิก (ผลแล็บผิดปกติ) ต้องคงสีเตือนไว้ในโหมดการ์ด */
    table.cards-on-mobile tr.abnormal-row,
    table.cards-on-mobile tbody tr.abnormal-row:hover { background: #fff5f5 !important; }

    table.cards-on-mobile td {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: 12px;
        padding: 9px 14px;
        border-bottom: 1px dashed var(--border);
        text-align: right;
        word-break: break-word;
        min-height: 0;
    }
    table.cards-on-mobile tr td:last-child { border-bottom: none; }

    table.cards-on-mobile td[data-th]::before {
        content: attr(data-th);
        flex: 0 0 38%;
        text-align: left;
        font-size: 12px;
        font-weight: 600;
        color: var(--text-muted);
        line-height: 1.5;
    }

    /* แถวสถานะว่าง/สรุป (colspan) ให้กินเต็มการ์ด ไม่ต้องมีป้ายชื่อคอลัมน์ */
    table.cards-on-mobile td[colspan] {
        display: block;
        text-align: center;
    }
    table.cards-on-mobile td[colspan]::before { content: none; }

    table.cards-on-mobile td .action-btns,
    table.cards-on-mobile td .action-group {
        justify-content: flex-end;
        flex-wrap: wrap;
        gap: 8px;
    }

    /* container ของตารางการ์ดไม่ต้องมีเงา/สโครลแนวนอน */
    .table-container:has(table.cards-on-mobile),
    .table-responsive:has(table.cards-on-mobile) {
        overflow: visible;
        box-shadow: none;
        border-radius: 0;
        background: transparent;
    }

    /* --- Topbar กระชับ --- */
    .topbar { padding: 0 12px; gap: 8px; }
    .topbar h2 {
        font-size: 16px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .topbar-left { min-width: 0; }
    .topbar-right { gap: 8px; }
    .topbar-datetime { display: none; }      /* วันที่ยาวเกินไปบนจอแคบ */
    .topbar-user {
        max-width: 38vw;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* --- ระยะห่าง/ขนาดหน้า --- */
    .page-content   { padding: 12px; }
    .card-header    { padding: 14px; flex-wrap: wrap; }
    .card-body      { padding: 14px; }
    .form-section   { padding: 14px; }
    .filter-section { padding: 12px; }
    .kpi-card       { padding: 14px; gap: 12px; }
    .kpi-icon       { width: 44px; height: 44px; font-size: 18px; border-radius: 10px; }
    .kpi-info h3    { font-size: 22px; }
    .section-title,
    .page-header h1,
    .page-header h2,
    .page-header h3 { font-size: 18px; }
    .empty-state    { padding: 32px 16px; }
    .profile-photo  { width: 96px; height: 96px; font-size: 42px; }
    .photo-preview  { width: 140px; height: 140px; font-size: 56px; }
    .signature-pad-wrap { overflow-x: auto; }
    .signature-img  { max-width: 100%; }
    .pagination     { flex-wrap: wrap; padding: 12px 4px; }

    /* --- ปุ่มกลุ่ม action เต็มความกว้าง --- */
    .form-actions,
    .modal-footer,
    .action-group,
    .camera-controls,
    .detail-topbar-actions {
        flex-wrap: wrap;
    }
    .form-actions > .btn,
    .form-actions > button,
    .modal-footer > .btn,
    .modal-footer > button,
    .action-group > .btn,
    .action-group > button,
    .camera-controls > .btn,
    .camera-controls > button {
        flex: 1 1 100%;
        justify-content: center;
    }
    .patient-card-actions > .btn { flex: 1 1 auto; justify-content: center; }

    /* --- Filter/Search เรียงเป็นคอลัมน์เดียว --- */
    .filter-bar,
    .search-bar {
        flex-direction: column;
        align-items: stretch;
    }
    .filter-bar > *,
    .search-bar > * {
        flex: 0 0 auto;
        width: 100%;
    }
    .search-input-wrap { min-width: 0; }
    .filters-row > *,
    .filter-group { flex: 1 1 100%; }

    /* --- Toolbar/Header --- */
    .patient-toolbar,
    .section-header,
    .detail-header {
        flex-direction: column;
        align-items: stretch;
    }
    .detail-topbar-actions { margin-left: 0; }
}

/* ---------- D. ≤480px — มือถือจอเล็ก ---------- */
@media screen and (max-width: 480px) {
    .kpi-grid,
    .stats-row,
    .summary-cards,
    .patient-card-grid,
    .doc-grid {
        grid-template-columns: 1fr;
    }
    .login-box { padding: 24px 20px; border-radius: 12px; }
    .login-page { padding: 16px; }
    .topbar-user { display: none; }
    .page-content { padding: 10px; }
    .modal-header { padding: 14px 14px 12px; }
    .modal-body   { padding: 14px; }
    .modal-footer { padding: 12px 14px calc(12px + env(safe-area-inset-bottom, 0px)); }
    table.cards-on-mobile td { padding: 8px 12px; }
    table.cards-on-mobile td[data-th]::before { flex: 0 0 42%; }
    .toast-container { left: 10px; right: 10px; top: 10px; }
}

/* ---------- E. มือถือแนวนอน (จอเตี้ย) ---------- */
@media screen and (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
    .modal-overlay { align-items: stretch; }
    .modal-container {
        max-height: 100vh;
        border-radius: 0;
    }
    .modal-footer { position: static; box-shadow: none; }
}

/* ---------- F. อุปกรณ์สัมผัส (pointer: coarse) ----------
   วางไว้ท้ายบล็อกที่อิงความกว้าง เพื่อให้ค่า touch target / font-size 16px ชนะเสมอ */
/* any-pointer ครอบโน้ตบุ๊กจอสัมผัสที่ต่อเมาส์อยู่ด้วย — เครื่องพวกนี้รายงาน
   pointer: fine ทั้งที่ผู้ใช้แตะจอจริง ถ้าเช็คแค่ pointer จะได้ปุ่มเล็กเกินกด */
@media screen and (pointer: coarse), screen and (any-pointer: coarse) {

    /* touch target ≥ 44px */
    .btn,
    button:not(.btn)[class*="btn-"],
    a:not(.btn)[class*="btn-"],
    .login-btn,
    .btn-back,
    .page-btn,
    .view-toggle-btn,
    .tab-btn,
    .treat-tab,
    .modal-close,
    .topbar-menu-btn,
    .sidebar-toggle-btn,
    .btn-logout,
    .dropdown-item,
    .nav-item {
        min-height: 44px;
    }
    .btn,
    button:not(.btn)[class*="btn-"],
    a:not(.btn)[class*="btn-"],
    .login-btn,
    .btn-back,
    .page-btn {
        align-items: center;
    }
    /* ปุ่มไอคอนล้วนต้องกว้างพอด้วย ไม่ใช่แค่สูง */
    .btn-icon,
    .modal-close,
    .topbar-menu-btn,
    .sidebar-toggle-btn,
    .btn-logout,
    .login-eye-btn,
    .view-toggle-btn {
        min-width: 44px;
        justify-content: center;
    }
    .btn-sm { padding: 8px 12px; }
    .dropdown-item { display: flex; align-items: center; }

    /* font-size ≥16px กัน iOS zoom เองตอนโฟกัส input
       select/textarea ต้องระบุ .form-control ด้วย ไม่งั้นแพ้ .form-control{font-size:14px} */
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
    select,
    textarea,
    select.form-control,
    textarea.form-control,
    input.form-control {
        font-size: 16px;
        min-height: 44px;
    }
    textarea { min-height: 88px; }
    /* .form-control-sm ประกาศ font-size 13px !important จึงต้องทับด้วย !important
       จำกัดที่จอแคบเท่านั้น — แท็บเล็ตแนวนอน/จอสัมผัสจอใหญ่ยังใช้ layout เดสก์ท็อป
       ที่มีกริดคอลัมน์ตายตัว การขยายช่องกรอกจะทำให้ตกกริด */
    @media (max-width: 840px) {
        .form-control-sm {
            font-size: 16px !important;
            padding: 9px 12px !important;
        }
    }
    input[type="checkbox"],
    input[type="radio"] {
        width: 20px;
        height: 20px;
    }

    /* ระยะห่างปุ่ม action ในตาราง — กันกดพลาด */
    .action-btns { gap: 10px; }
    td .action-btns .btn,
    td .action-group .btn { min-width: 44px; justify-content: center; }
    .patient-card-actions { gap: 10px; }

    /* checkbox/radio ในตารางไม่ควรถูกยืดตามกฎ 44px ด้านบน */
    td input[type="checkbox"],
    td input[type="radio"] { min-height: 0; }
}

/* ---------- G. ลดการเคลื่อนไหว ---------- */
@media (prefers-reduced-motion: reduce) {
    *:not(.spinner):not(.fa-spin),
    *:not(.spinner):not(.fa-spin)::before,
    *:not(.spinner):not(.fa-spin)::after {
        animation-duration: 0.01ms !important;
        animation-delay: 0s !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        transition-delay: 0s !important;
    }
    /* คง spinner ไว้เพราะเป็นตัวบอกสถานะกำลังโหลด แต่ช้าลง */
    .spinner, .fa-spin { animation-duration: 1.4s !important; }
    html { scroll-behavior: auto !important; }
}

/* ============================================
   Shared components (เฟส 1) — ช่องค้นหาผู้ป่วยที่ใช้ร่วมกันทุกโมดูล
   ============================================ */
.patient-picker-list {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    z-index: 50;
    max-height: 260px;
    overflow-y: auto;
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg, 0 8px 24px rgba(0,0,0,.12));
}
.patient-picker-item {
    display: block;
    width: 100%;
    text-align: left;
    padding: 10px 14px;
    border: none;
    background: none;
    cursor: pointer;
    font-family: inherit;
    font-size: .95rem;
    border-bottom: 1px solid var(--border);
}
.patient-picker-item:last-child { border-bottom: none; }
.patient-picker-item:hover,
.patient-picker-item:focus { background: var(--primary-light, #eff6ff); }
.patient-picker-meta { color: var(--text-muted, #64748b); margin-left: 8px; font-size: .88rem; }
.patient-picker-empty { padding: 12px 14px; color: var(--text-muted, #64748b); }
