:root {
    --brand: #e8730c;
    --brand-dark: #c45f08;
    --brand-light: #fff1e3;
    --sidebar-bg: #1f1a17;
    --sidebar-width: 260px;
    --body-bg: #f3f6f8;
    --bs-primary: #e8730c;
    --bs-primary-rgb: 232, 115, 12;
    --bs-link-color: #e8730c;
    --bs-link-hover-color: #c45f08;
    --bs-link-color-rgb: 232, 115, 12;
    /* KMF logo palette: orange #F5821F (primary), green #009245 (accent), black */
    --brand-logo: #f5821f;
    --brand-green: #009245;
    --brand-green-dark: #007a3a;
    --brand-green-light: #e3f5ea;
    --bs-success: #009245;
    --bs-success-rgb: 0, 146, 69;
    --bs-body-font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    --bs-body-font-size: .9rem;
}

body { background: var(--body-bg); }

.btn-primary { --bs-btn-bg: var(--brand); --bs-btn-border-color: var(--brand); --bs-btn-hover-bg: var(--brand-dark); --bs-btn-hover-border-color: var(--brand-dark); --bs-btn-active-bg: var(--brand-dark); --bs-btn-disabled-bg: var(--brand); --bs-btn-disabled-border-color: var(--brand); }
.btn-outline-primary { --bs-btn-color: var(--brand); --bs-btn-border-color: var(--brand); --bs-btn-hover-bg: var(--brand); --bs-btn-hover-border-color: var(--brand); --bs-btn-active-bg: var(--brand-dark); }
.btn-success { --bs-btn-bg: var(--brand-green); --bs-btn-border-color: var(--brand-green); --bs-btn-hover-bg: var(--brand-green-dark); --bs-btn-hover-border-color: var(--brand-green-dark); --bs-btn-active-bg: var(--brand-green-dark); }
.btn-outline-success { --bs-btn-color: var(--brand-green); --bs-btn-border-color: var(--brand-green); --bs-btn-hover-bg: var(--brand-green); --bs-btn-hover-border-color: var(--brand-green); }
.text-success { color: var(--brand-green) !important; }
.bg-success { background-color: var(--brand-green) !important; }
.text-primary { color: var(--brand) !important; }
.bg-primary { background-color: var(--brand) !important; }
.form-control:focus, .form-select:focus { border-color: #f9b26a; box-shadow: 0 0 0 .2rem rgba(232, 115, 12, .15); }
.form-check-input:checked { background-color: var(--brand); border-color: var(--brand); }
.page-link { color: var(--brand); }
.active > .page-link, .page-link.active { background-color: var(--brand); border-color: var(--brand); }

/* ---------- Shell ---------- */
.app-shell { display: flex; min-height: 100vh; }
.app-sidebar {
    width: var(--sidebar-width); background: var(--sidebar-bg); color: #cbd5e1;
    position: fixed; inset: 0 auto 0 0; overflow-y: auto; z-index: 1040; transition: transform .2s ease;
}
.app-main { flex: 1; margin-left: var(--sidebar-width); display: flex; flex-direction: column; min-width: 0; }
.sidebar-brand { display: flex; align-items: center; gap: .75rem; padding: 1.1rem 1.25rem; color: #fff; text-decoration: none; border-bottom: 1px solid rgba(255,255,255,.08); }
.sidebar-brand:hover { color: #fff; }
.brand-logo { width: 46px; height: 46px; border-radius: 50%; background: #fff; padding: 2px; flex-shrink: 0; object-fit: contain; box-shadow: 0 0 0 2px rgba(245, 130, 31, .45); }
.brand-name { font-weight: 800; letter-spacing: .02em; color: var(--brand-logo); }
.sidebar-nav { padding: .75rem .75rem 2rem; }
.sidebar-nav .nav-heading { font-size: .68rem; text-transform: uppercase; letter-spacing: .08em; color: #f9b26a; opacity: .7; padding: 1rem .75rem .35rem; font-weight: 600; }
.sidebar-nav .nav-link { display: flex; align-items: center; gap: .7rem; color: #cbd5e1; padding: .5rem .75rem; border-radius: 8px; font-size: .86rem; }
.sidebar-nav .nav-link i { font-size: 1.05rem; width: 1.2rem; text-align: center; }
.sidebar-nav .nav-link:hover { background: rgba(255,255,255,.06); color: #fff; }
.sidebar-nav .nav-link.active { background: var(--brand); color: #fff; box-shadow: 0 4px 12px rgba(232, 115, 12, .35); border-left: 3px solid var(--brand-green); }
/* Orange -> green brand strip under the top bar (logo colours) */
.app-topbar::after { content: ''; position: absolute; left: 0; right: 0; bottom: -3px; height: 3px; background: linear-gradient(90deg, var(--brand-logo) 0%, var(--brand-logo) 70%, var(--brand-green) 100%); }
.app-topbar { height: 60px; background: #fff; border-bottom: 1px solid #e5e7eb; display: flex; align-items: center; padding: 0 1.5rem; position: sticky; top: 0; z-index: 1030; }
.app-content { padding: 1.5rem; flex: 1; }
.app-footer { padding: 1rem 1.5rem; font-size: .75rem; color: #94a3b8; }
.avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--brand-light); color: var(--brand-dark); display: inline-grid; place-items: center; font-weight: 700; }
.sidebar-backdrop { display: none; }

@media (max-width: 991.98px) {
    .app-sidebar { transform: translateX(-100%); }
    .app-main { margin-left: 0; }
    body.sidebar-open .app-sidebar { transform: none; }
    body.sidebar-open .sidebar-backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 1035; }
    .app-content { padding: 1rem; }
}

/* ---------- Components ---------- */
.page-header { display: flex; flex-wrap: wrap; gap: .75rem; align-items: center; justify-content: space-between; margin-bottom: 1.25rem; }
.page-header h1 { font-size: 1.35rem; font-weight: 700; margin: 0; }
.page-header .subtitle { color: #64748b; font-size: .85rem; }
.card { border: 1px solid #e5e7eb; border-radius: 12px; box-shadow: 0 1px 2px rgba(16,24,40,.04); }
.card-header { background: #fff; border-bottom: 1px solid #eef0f3; font-weight: 600; padding: .85rem 1.1rem; border-radius: 12px 12px 0 0 !important; }
.table { --bs-table-hover-bg: #f8fafc; margin-bottom: 0; }
.table thead th { background: #f8fafc; color: #475569; font-size: .74rem; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; border-bottom: 1px solid #e5e7eb; white-space: nowrap; }
.table td { vertical-align: middle; }
.table-card .card-body { padding: 0; }
.stat-card { border-radius: 12px; padding: 1.1rem 1.2rem; background: #fff; border: 1px solid #e5e7eb; height: 100%; display: flex; gap: 1rem; align-items: center; }
.stat-card .stat-icon { width: 48px; height: 48px; border-radius: 12px; display: grid; place-items: center; font-size: 1.4rem; flex-shrink: 0; }
.stat-card .stat-value { font-size: 1.35rem; font-weight: 700; line-height: 1.1; }
.stat-card .stat-label { color: #64748b; font-size: .78rem; }
.bg-soft-primary { background: #fff1e3; color: #e8730c; }
.bg-soft-success { background: var(--brand-green-light); color: var(--brand-green-dark); }
.bg-soft-warning { background: #fef3c7; color: #b45309; }
.bg-soft-danger { background: #fee2e2; color: #b91c1c; }
.bg-soft-info { background: #e0f2fe; color: #0369a1; }
.bg-soft-purple { background: #ede9fe; color: #6d28d9; }
.filter-card .card-body { padding: .9rem 1.1rem; }
/* Filter buttons (Search/Show/Filter + Clear): one line, same height as the inputs, aligned to their bottom edge */
.filter-card .filter-actions { display: flex; align-items: flex-end; gap: .5rem; }
.filter-card .filter-actions .btn {
    display: inline-flex; align-items: center; justify-content: center; white-space: nowrap;
    height: calc(1.5em + .75rem + 2px); padding: 0 1rem; line-height: 1;
}
.filter-card .filter-actions .btn i { font-size: 1rem; }
.filter-card .filter-actions .btn:has(i:only-child) { padding: 0 .8rem; }
@media (max-width: 767.98px) { .filter-card .filter-actions .btn { flex: 1; } }
/* Single-row filters: fields flex proportionally, buttons keep their natural width at the end of the row */
@media (min-width: 768px) {
    .filter-card .filter-one-row { flex-wrap: nowrap; }
    .filter-card .filter-one-row > .filter-field { flex: var(--span) 1 0%; width: auto; max-width: none; min-width: 0; }
    .filter-card .filter-one-row > .filter-actions { flex: 0 0 auto; }
    .filter-card .filter-one-row > .ms-auto { margin-left: 0 !important; }
}
.form-label { font-weight: 500; font-size: .82rem; color: #334155; margin-bottom: .3rem; }
.required::after { content: ' *'; color: #dc2626; }
.empty-state { text-align: center; color: #94a3b8; padding: 2.5rem 1rem; }
.empty-state i { font-size: 2.4rem; display: block; margin-bottom: .5rem; }
.line-items td { padding: .4rem; vertical-align: top; }

/* ---------- Select2 ---------- */
/* Dropdown is attached to <body>, so it is never clipped by tables/cards; keep it above sticky topbar & modals */
.select2-container--open .select2-dropdown { z-index: 2000; }
.select2-container--bootstrap-5 .select2-dropdown { border-color: #f9b26a; box-shadow: 0 10px 25px rgba(15, 23, 42, .15); }
.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option { font-size: .875rem; padding: .45rem .75rem; }
.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option.select2-results__option--highlighted { background: var(--brand-light); color: #0f172a; }
.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option.select2-results__option--selected { background: var(--brand); color: #fff; }
.select2-container--bootstrap-5 .select2-dropdown .select2-results__options .select2-results__option--selected small { color: #fff1e3 !important; }
.select2-container--bootstrap-5 .select2-dropdown .select2-results__options { max-height: 320px; }
.select2-container--bootstrap-5.select2-container--focus .select2-selection,
.select2-container--bootstrap-5.select2-container--open .select2-selection { border-color: #f9b26a; box-shadow: 0 0 0 .2rem rgba(232, 115, 12, .15); }
.select2-container--bootstrap-5 .select2-selection { font-size: .9rem; }
/* Same height / alignment as .form-control so selects line up with inputs in every form row */
.select2-container { vertical-align: top; }
.select2-container--bootstrap-5 .select2-selection--single { min-height: calc(1.5em + .75rem + 2px); display: flex; align-items: center; }
.select2-container--bootstrap-5 .select2-selection--multiple .select2-selection__rendered .select2-selection__choice { background: var(--brand-light); border-color: #fcd3a8; color: #7c3d06; }
.select2-result-sub { font-size: .75rem; color: #64748b; display: block; }
.badge { font-weight: 600; }
/* Patient photo (avatar) */
.patient-photo { width: 140px; height: 140px; border-radius: 50%; background: var(--brand-light); color: var(--brand); display: grid; place-items: center; font-size: 3.5rem; font-weight: 700; overflow: hidden; border: 3px solid #fff; box-shadow: 0 0 0 2px #fcd3a8; }
.patient-photo img { width: 100%; height: 100%; object-fit: cover; }
.patient-photo-sm { width: 40px; height: 40px; font-size: 1rem; border-width: 2px; box-shadow: 0 0 0 1px #fcd3a8; }
.patient-photo-xs { width: 32px; height: 32px; font-size: .85rem; border-width: 1px; box-shadow: none; }
.camera-frame { width: 100%; max-width: 420px; aspect-ratio: 1; margin: 0 auto; border-radius: 12px; overflow: hidden; background: #111; }
.camera-frame video, .camera-frame img { width: 100%; height: 100%; object-fit: cover; }
.camera-frame video { transition: transform .2s ease; } /* rotated via JS; not mirrored, so the preview matches the saved photo */
.token-number { font-size: 2.2rem; font-weight: 800; color: var(--brand); line-height: 1; }
.dl-grid { display: grid; grid-template-columns: 140px 1fr; gap: .35rem 1rem; font-size: .87rem; }
.dl-grid dt { color: #64748b; font-weight: 500; }
.dl-grid dd { margin: 0; }
.timeline { border-left: 2px solid #e2e8f0; margin-left: .5rem; padding-left: 1.25rem; }
.timeline-item { position: relative; padding-bottom: 1.25rem; }
.timeline-item::before { content: ''; position: absolute; left: -1.62rem; top: .3rem; width: 12px; height: 12px; border-radius: 50%; background: var(--brand); border: 2px solid #fff; box-shadow: 0 0 0 2px var(--brand-light); }
.auth-bg { min-height: 100vh; background: radial-gradient(circle at 20% 15%, rgba(245, 130, 31, .35), transparent 45%), radial-gradient(circle at 85% 90%, rgba(0, 146, 69, .30), transparent 45%), #1f1a17; display: flex; align-items: center; justify-content: center; padding: 1rem; }
.auth-card { width: 100%; max-width: 420px; border-radius: 16px; border: 0; border-top: 5px solid var(--brand-logo); box-shadow: 0 20px 50px rgba(0,0,0,.35); }
.auth-logo { width: 110px; height: 110px; object-fit: contain; }

@media print {
    .app-sidebar, .app-topbar, .app-footer, .no-print, .btn { display: none !important; }
    .app-main { margin: 0; }
    .app-content { padding: 0; }
    .card { border: 0; box-shadow: none; }
}

/* ---------- Mobile / tablet ---------- */
/* Wide tables scroll inside their card instead of pushing the whole page sideways. */
.table-card .card-body { overflow-x: auto; -webkit-overflow-scrolling: touch; }
body { overflow-x: clip; } /* clip (not hidden) keeps the sticky top bar working */

@media (max-width: 767.98px) {
    :root { --bs-body-font-size: .82rem; }
    .app-topbar { height: 54px; padding: 0 .75rem; font-size: .85rem; }
    .app-topbar .ms-auto { gap: .5rem !important; }
    .avatar { width: 30px; height: 30px; font-size: .8rem; }
    .app-content { padding: .75rem; }
    .app-footer { padding: .75rem; font-size: .68rem; text-align: center; }

    .page-header { margin-bottom: .85rem; gap: .5rem; }
    .page-header h1 { font-size: 1.1rem; }
    .page-header .subtitle { font-size: .75rem; }
    .page-header > .d-flex { width: 100%; }
    .page-header > .d-flex > .btn, .page-header > .d-flex > form, .page-header > .d-flex > .dropdown { flex: 1 1 auto; }
    .page-header .btn { font-size: .8rem; padding: .4rem .65rem; }

    .row.g-3 { --bs-gutter-x: .6rem; --bs-gutter-y: .6rem; }
    .card-header { padding: .6rem .8rem; font-size: .85rem; }
    .card-body { padding: .8rem; }

    .stat-card { padding: .65rem .7rem; gap: .55rem; border-radius: 10px; }
    .stat-card .stat-icon { width: 34px; height: 34px; font-size: 1rem; border-radius: 9px; }
    .stat-card .stat-value { font-size: .98rem; white-space: normal; overflow-wrap: anywhere; }
    .stat-card .stat-label { font-size: .68rem; line-height: 1.25; }

    .table { font-size: .78rem; }
    .table thead th { font-size: .64rem; }
    .table > :not(caption) > * > * { padding: .45rem .5rem; }
    .table .btn-sm { font-size: .72rem; padding: .2rem .45rem; }
    .table .badge.fs-6 { font-size: .8rem !important; }

    .form-control, .form-select { font-size: .85rem; }
    .form-label { font-size: .76rem; }
    .modal-footer .btn { font-size: .82rem; }
    .token-number { font-size: 1.8rem; }
    .dl-grid { grid-template-columns: 110px 1fr; font-size: .8rem; }
}

/* Very small phones: stat icon hidden so the numbers get the full width. */
@media (max-width: 379.98px) {
    .stat-card .stat-icon { display: none; }
    .app-topbar .fw-semibold { font-size: .8rem; }
}
