:root {
    --ink: #17212b;
    --muted: #687684;
    --teal: #087f8c;
    --teal-dark: #05606a;
    --paper: #f4f7f6;
    --line: #dce7e4;
    --sidebar: #123b40;
}

* { box-sizing: border-box; }
html, body { max-width: 100%; overflow-x: hidden; }
body { margin: 0; background: var(--paper); color: var(--ink); font-family: Georgia, 'Times New Roman', serif; }
.app-shell { display: flex; min-height: 100vh; }
.sidebar { width: 245px !important; flex: 0 0 245px !important; background: var(--sidebar) !important; min-height: 100vh !important; color: white; display: flex; flex-direction: column; padding: 24px 14px !important; }
.sidebar-brand { display: flex; align-items: center; gap: 12px; min-width: 0; color: white; text-decoration: none; padding: 6px 10px 24px; border-bottom: 1px solid rgba(255,255,255,.12); }
.sidebar-brand > span:last-child { min-width: 0; }
.sidebar-brand strong, .sidebar-brand small { display: block; }
.sidebar-brand small { color: #a9cfca; font: 12px Arial, sans-serif; margin-top: 3px; }
.brand-mark { width: 46px; height: 46px; flex: 0 0 46px; min-width: 46px; min-height: 46px; border: 0; border-radius: 10px; display: grid; place-items: center; padding: 5px; background: #f7fffd; color: var(--teal-dark); box-shadow: 0 4px 12px rgba(0,0,0,.16); overflow: hidden; }
.brand-mark img { display: block; width: 100%; height: 100%; max-width: 100%; max-height: 100%; object-fit: contain; object-position: center; border-radius: 6px; image-rendering: auto; }
.sidebar-brand strong { display: block; max-width: 150px; color: #ffffff; font-size: 14px; line-height: 1.15; overflow-wrap: anywhere; }
.sidebar-nav { padding-top: 24px; }
.sidebar .nav-link { color: rgba(255,255,255,.72) !important; display: flex; align-items: center; gap: 12px; padding: 12px 14px !important; margin: 4px 0 !important; border-radius: 7px !important; font: 14px Arial, sans-serif; text-decoration: none; }
.sidebar .nav-link i { width: 18px; text-align: center; }
.sidebar .nav-link:hover, .sidebar .nav-link.active { color: white; background: rgba(255,255,255,.14); }
.sidebar-bottom { margin-top: auto; border-top: 1px solid rgba(255,255,255,.12); padding-top: 14px; }
.app-main { min-width: 0; flex: 1; display: flex; flex-direction: column; }
.topbar { min-height: 76px; display: flex; justify-content: space-between; align-items: center; padding: 16px 5%; background: white; border-bottom: 1px solid var(--line); }
.topbar-label { display: block; color: var(--muted); font: 11px Arial, sans-serif; letter-spacing: .12em; text-transform: uppercase; margin-bottom: 4px; }
.user-chip { display: flex; align-items: center; gap: 9px; font: 14px Arial, sans-serif; }
.user-chip small { color: var(--muted); border-left: 1px solid var(--line); padding-left: 9px; }
.user-avatar { width: 32px; height: 32px; display: grid; place-items: center; border-radius: 50%; background: #d9efeb; color: var(--teal-dark); font-weight: bold; object-fit: cover; }
.user-menu-toggle { border: 0; background: transparent; padding: 5px 8px; color: var(--ink); cursor: pointer; }
.user-menu-toggle:hover, .user-menu-toggle:focus-visible, .user-menu.show .user-menu-toggle { background: #eef5f3; border-radius: 8px; outline: 0; }
.user-menu-identity { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; line-height: 1.1; }
.user-menu-identity strong { font-size: 13px; }
.user-menu-identity small { border: 0; padding: 0; font-size: 11px; }
.user-menu-chevron { color: var(--muted); font-size: 11px; margin-left: 2px; transition: transform .2s ease; }
.user-menu.show .user-menu-chevron { transform: rotate(180deg); }
.user-menu-panel { min-width: 238px; margin-top: 8px !important; padding: 8px; border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 14px 30px rgba(23,61,58,.14); }
.user-menu-heading { display: flex; flex-direction: column; gap: 3px; padding: 8px 10px 10px; overflow: hidden; }
.user-menu-heading strong { overflow: hidden; color: var(--ink); font: 12px Arial, sans-serif; text-overflow: ellipsis; white-space: nowrap; }
.user-menu-label { color: var(--muted); font: 10px Arial, sans-serif; letter-spacing: .08em; text-transform: uppercase; }
.user-menu-panel .dropdown-item { display: flex; align-items: center; gap: 10px; border-radius: 6px; padding: 9px 10px; color: var(--ink); font: 13px Arial, sans-serif; }
.user-menu-panel .dropdown-item i { width: 16px; color: var(--teal); text-align: center; }
.user-menu-panel .dropdown-item:hover, .user-menu-panel .dropdown-item:focus { background: #eef5f3; color: var(--teal-dark); }
.user-menu-logout { width: 100%; border: 0; background: transparent; text-align: left; }
.page-content { width: 100%; max-width: 1440px; flex: 1; padding: 34px 5% 44px; }
.container-fluid > .row > .main-content { width: auto !important; flex: 1 1 auto !important; padding: 34px 5% 44px !important; }
.container-fluid > .row { flex-wrap: nowrap !important; margin-left: 0 !important; margin-right: 0 !important; }
.container-fluid > .row > .main-content { min-width: 0 !important; }
.container-fluid { padding: 0 !important; }
.app-footer { color: var(--muted); padding: 18px 5%; border-top: 1px solid var(--line); background: white; font: 12px Arial, sans-serif; }
.app-footer span { padding: 0 6px; color: #a4b7b4; }
.identity-control { max-width: 100%; }
.patients-table { min-width: 900px; }
.patients-table .actions-column { position: sticky; right: 0; z-index: 2; min-width: 132px; white-space: nowrap; background: white; box-shadow: -5px 0 10px rgba(23,33,43,.06); }
.patients-table thead .actions-column { background: #f8faf9; }
.patient-actions { display: flex; align-items: center; gap: 4px; }
.prescription-status-select { min-width: 118px; }
.status-badge { display: inline-flex; align-items: center; justify-content: center; min-width: 76px; padding: 6px 10px; border-radius: 999px; font: 700 12px/1 Arial, sans-serif; letter-spacing: .02em; }
.badge-paid { background: #d9f4e2; color: #176b37; border: 1px solid #9bd7ad; }
.badge-pending { background: #fff0c7; color: #795400; border: 1px solid #e5c267; }
.badge-partial { background: #dcecff; color: #14518a; border: 1px solid #9fc5ee; }
.report-stat-number { font-size: clamp(1.25rem, 2.4vw, 2rem); }
.report-bars { display: flex; flex-direction: column; gap: 13px; }
.report-bar-row { display: grid; grid-template-columns: 48px minmax(80px, 1fr) 88px; align-items: center; gap: 10px; font: 12px Arial, sans-serif; }
.report-bar-label, .report-bar-value { color: var(--muted); }
.report-bar-value { text-align: right; white-space: nowrap; }
.report-bar-track { height: 13px; overflow: hidden; border-radius: 999px; background: #e8efed; }
.report-bar-fill { height: 100%; border-radius: inherit; background: linear-gradient(90deg, #087f8c, #42b883); }
.report-list { display: flex; flex-direction: column; gap: 4px; }
.report-list-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 11px 0; border-bottom: 1px solid var(--line); font-family: Arial, sans-serif; }
.report-list-row:last-child { border-bottom: 0; }
.report-list-row strong { color: var(--teal-dark); white-space: nowrap; }
.password-toggle-wrap { position: relative; }
.password-toggle-wrap input { padding-right: 42px; }
.password-toggle { position: absolute; top: 50%; right: 8px; width: 30px; height: 30px; transform: translateY(-50%); border: 0; border-radius: 5px; background: transparent; color: var(--muted); }
.password-toggle:hover, .password-toggle:focus-visible { background: #e8f2ef; color: var(--teal-dark); }
.prescription-pos-details { padding: 10px 12px; border: 1px solid #b9dfc9; border-radius: 8px; background: #eefaf1; color: #216b3b; font: 13px Arial, sans-serif; }
.pos-workspace .page-kicker { color: #bd6b00; }
.pos-workspace .form-shell { border-top: 4px solid #f0a202; }
.pos-checkout .form-intro { background: #fff7e5; }
.mpesa-prompt { display: flex; gap: 10px; align-items: flex-start; padding: 12px; border: 1px solid #b9dfc9; border-radius: 8px; background: #eefaf1; color: #216b3b; font: 13px Arial, sans-serif; }
.mpesa-prompt i { color: #1d9b50; margin-top: 2px; }
.mobile-menu-toggle, .mobile-menu-backdrop { display: none; }
@media (max-width: 767.98px) {
    .app-shell { display: block; }
    .sidebar { position: fixed; z-index: 1045; inset: 0 auto 0 0; width: min(280px, 84vw) !important; min-height: 100vh !important; padding: 18px 14px !important; display: flex !important; transform: translateX(-105%); transition: transform .25s ease; box-shadow: 12px 0 30px rgba(0,0,0,.18); }
    .sidebar.mobile-open { transform: translateX(0); }
    .container-fluid > .row { display: block !important; }
    .sidebar-brand { padding: 0 0 14px; }

    .sidebar-brand .brand-mark { width: 52px; height: 52px; flex-basis: 52px; min-width: 52px; min-height: 52px; padding: 4px; }
    .sidebar-brand > span:last-child { flex: 1 1 auto; min-width: 0; }
    body.mobile-nav-open .mobile-menu-toggle { left: calc(min(280px, 84vw) - 56px); }
    .sidebar-nav { display: flex; flex-direction: column; gap: 4px; overflow-y: auto; padding-top: 18px; }
    .sidebar > nav { display: flex !important; flex-direction: column !important; flex-wrap: nowrap !important; gap: 4px; overflow-y: auto; padding-top: 18px !important; }
    .sidebar > nav hr { display: none; }
    .sidebar > nav .nav-link { flex: 0 0 auto; width: 100%; margin: 0 !important; padding: 12px 14px !important; }
    .sidebar .nav-link { flex: 0 0 auto; margin: 0; padding: 12px 14px; }
    .sidebar .nav-link span { display: inline; }
    .sidebar-bottom { border: 0; padding: 0; position: absolute; top: 19px; right: 16px; }
    .sidebar-bottom span { display: inline; }
    .topbar { min-height: 62px; padding: 12px 16px; }
    .topbar-brand { width: 34px; height: 34px; flex-basis: 34px; }
    .page-content { padding: 24px 16px 32px; }
    .app-footer { padding: 16px; }
    .mobile-menu-toggle { display: grid; place-items: center; position: fixed; z-index: 1050; top: 14px; left: 14px; width: 42px; height: 42px; border: 0; border-radius: 8px; background: var(--teal); color: white; box-shadow: 0 5px 15px rgba(0,0,0,.16); }
    .mobile-menu-backdrop { display: block; position: fixed; z-index: 1040; inset: 0; width: 100%; border: 0; background: rgba(7, 30, 33, .48); opacity: 0; pointer-events: none; transition: opacity .25s ease; }
    .mobile-menu-backdrop.mobile-visible { opacity: 1; pointer-events: auto; }
    .topbar { padding-left: 70px; }
}

/* Shared public/auth shell */
.public-page { min-height: 100vh; background: var(--paper); display: grid; place-items: center; padding: 24px 16px; }
.public-shell { width: min(100%, 520px); }
.public-shell-wide { width: min(100%, 760px); }
.public-brand { display: flex; align-items: center; justify-content: center; gap: 12px; margin-bottom: 14px; color: var(--ink); }
.public-brand-mark { width: 52px; height: 52px; flex: 0 0 52px; display: grid; place-items: center; padding: 4px; border-radius: 11px; background: #fff; border: 1px solid var(--line); box-shadow: 0 7px 18px rgba(23,61,58,.10); overflow: hidden; color: var(--teal-dark); font-size: 24px; }
.public-brand-mark img { display: block; width: 100%; height: 100%; object-fit: contain; object-position: center; border-radius: 7px; }
.public-brand strong, .public-brand small { display: block; }
.public-brand strong { font: 700 16px/1.15 Georgia, 'Times New Roman', serif; }
.public-brand small { margin-top: 3px; color: var(--muted); font: 11px Arial, sans-serif; }
.public-card { background: #fff; border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 16px 40px rgba(23,61,58,.10); overflow: hidden; }
.public-card-body { padding: 28px; }
.public-footer { text-align: center; color: var(--muted); padding: 14px 8px 0; font: 11px Arial, sans-serif; }
.auth-heading { margin-bottom: 24px; }
.auth-heading h1 { margin-bottom: 6px; font-size: 1.55rem; }
.auth-heading p { margin: 0; color: var(--muted); font: 13px/1.5 Arial, sans-serif; }
.public-card .form-control:focus, .public-card .form-select:focus { border-color: var(--teal); box-shadow: 0 0 0 .2rem rgba(8,127,140,.12); }
.public-card .btn-primary { background: var(--teal); border-color: var(--teal); }
.public-card .btn-primary:hover { background: var(--teal-dark); border-color: var(--teal-dark); }
.print-page { background: #eef3f1; color: var(--ink); }
@media (max-width: 575.98px) {
    .public-page { padding: 14px 10px; }
    .public-card-body { padding: 22px 18px; }
    .public-brand-mark { width: 48px; height: 48px; flex-basis: 48px; }
}

/* Shared receipt/print styles */
.receipt { width: min(760px, calc(100% - 32px)); margin: 32px auto; background: white; border: 1px solid var(--line); box-shadow: 0 14px 36px rgba(23,61,58,.12); }
.receipt-head { padding: 28px 32px 24px; background: #123b40; color: white; border-bottom: 5px solid #7dd3c7; display: flex; justify-content: space-between; gap: 24px; }
.receipt-head img { max-width: 150px; max-height: 48px; width: auto; height: auto; object-fit: contain; object-position: left center; vertical-align: middle; }
.receipt .brand { font-size: 1.35rem; font-weight: bold; }
.receipt .brand small { display: block; margin-top: 4px; color: #b8eee5; font: 12px Arial, sans-serif; letter-spacing: .08em; text-transform: uppercase; }
.receipt-number { text-align: right; }
.receipt-number strong { display: block; font-size: 1.2rem; }
.receipt-number small { color: rgba(255,255,255,.72); font: 12px Arial, sans-serif; }
.receipt-body { padding: 28px 32px; }
.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 26px; }
.info-box { border: 1px solid var(--line); border-radius: 8px; padding: 16px; background: #fbfdfc; }
.info-box h2 { margin: 0 0 12px; color: var(--teal); font: bold 12px Arial, sans-serif; letter-spacing: .08em; text-transform: uppercase; }
.info-box p { margin: 5px 0; font-size: .92rem; }
.receipt table { width: 100%; border-collapse: collapse; }
.receipt th { padding: 11px 8px; background: #eef5f3; color: #38545a; text-align: left; font: bold 11px Arial, sans-serif; letter-spacing: .05em; text-transform: uppercase; }
.receipt td { padding: 12px 8px; border-bottom: 1px solid #edf2f0; font-size: .92rem; }
.receipt .number { text-align: right; white-space: nowrap; }
.receipt .total-row td { border-top: 2px solid var(--teal); border-bottom: 0; font-weight: bold; font-size: 1.05rem; }
.receipt .total-row td:last-child { color: var(--teal); font-size: 1.25rem; }
.receipt-foot { padding: 20px 32px; border-top: 1px solid var(--line); color: var(--muted); text-align: center; font: 12px Arial, sans-serif; }
.actions { width: min(760px, calc(100% - 32px)); margin: 0 auto 28px; display: flex; justify-content: center; gap: 8px; }
.actions a, .actions button { border: 1px solid var(--teal); border-radius: 6px; padding: 10px 16px; background: white; color: var(--teal); text-decoration: none; cursor: pointer; font: 14px Arial, sans-serif; }
.actions button { background: var(--teal); color: white; }
@media (max-width: 560px) { .receipt { width: 100%; margin: 0; border: 0; } .receipt-head, .receipt-body { padding: 22px 18px; } .info-grid { grid-template-columns: 1fr; } .receipt-foot { padding: 18px; } .receipt .table-wrap { overflow-x: auto; } .receipt table { min-width: 560px; } }
@media print { body.print-page { background: white; } .receipt { width: 100%; margin: 0; border: 0; box-shadow: none; } .actions { display: none; } .no-print { display: none !important; } .receipt-head { -webkit-print-color-adjust: exact; print-color-adjust: exact; } }

/* Ordered, grouped sidebar navigation */
.sidebar-nav { overflow-y: auto; min-height: 0; padding-bottom: 10px; }
.sidebar-group { margin-top: 14px; padding-top: 10px; border-top: 1px solid var(--line); }
.sidebar-group-title { padding: 0 12px 7px; color: var(--muted); font: 700 10px/1.2 Arial, sans-serif; letter-spacing: .10em; text-transform: uppercase; }
.sidebar-group .nav-link { margin-bottom: 2px; }
.sidebar-group:first-of-type { margin-top: 10px; }
@media (max-width: 767.98px) {
    .sidebar { overflow: hidden; }
    .sidebar-nav { flex: 1 1 auto; overflow-y: auto; overscroll-behavior: contain; padding: 8px 0 70px !important; }
    .sidebar-group { margin-top: 10px; padding-top: 8px; }
    .sidebar-group-title { padding-left: 10px; }
    .sidebar-bottom { z-index: 2; }
}


/* Final sidebar organization: the PHP layout owns the order on every device. */
.sidebar-nav { display:flex; flex-direction:column; min-height:0; overflow-y:auto; }
.sidebar-nav > .nav-link { flex:0 0 auto; }
.sidebar-group { flex:0 0 auto; margin:14px 0 0; padding:12px 0 0; border-top:1px solid rgba(255,255,255,.10); }
.sidebar-group:first-of-type { margin-top:12px; }
.sidebar-group-title { padding:0 14px 7px; color:rgba(255,255,255,.50); font-size:10px; font-weight:700; letter-spacing:.11em; text-transform:uppercase; }
.sidebar-group .nav-link { margin:2px 0 !important; }
.sidebar-bottom { flex:0 0 auto; }
@media (max-width: 767.98px) {
    .sidebar-nav { padding:10px 0 78px !important; overflow-y:auto !important; }
    .sidebar-group { margin-top:10px; padding-top:9px; }
    .sidebar-group-title { padding-left:14px; }
    .sidebar .nav-link { width:100%; min-height:44px; }
    .sidebar-bottom { position:absolute; left:14px; right:14px; bottom:10px; background:var(--sidebar); padding-top:8px; border-top:1px solid rgba(255,255,255,.12); }
}

/* FINAL SIDEBAR FIX — keep each section and its links together.
   Do not use flex ordering on individual navigation links. */
.sidebar-nav {
    display: block !important;
    overflow-y: auto !important;
    min-height: 0;
    padding: 10px 0 16px !important;
}
.sidebar-nav > .nav-link {
    display: flex !important;
    width: 100%;
    order: initial !important;
}
.sidebar-group {
    display: block !important;
    width: 100%;
    margin: 14px 0 0 !important;
    padding: 12px 0 0 !important;
    border-top: 1px solid rgba(255,255,255,.10);
}
.sidebar-group:first-of-type { margin-top: 0 !important; }
.sidebar-group-title {
    display: block !important;
    width: 100%;
    padding: 0 14px 7px !important;
    margin: 0 !important;
}
.sidebar-group .nav-link {
    display: flex !important;
    width: 100%;
    order: initial !important;
    margin: 2px 0 !important;
}
.sidebar-bottom {
    flex: 0 0 auto;
}
@media (max-width: 767.98px) {
    .sidebar-nav { padding: 10px 0 84px !important; }
    .sidebar-group { margin-top: 10px !important; padding-top: 9px !important; }
    .sidebar-group-title { padding-left: 14px !important; }
    .sidebar-bottom { top: auto; bottom: 10px; }
}

/* Keep the compact dashboard prescription summary easy to scan. */
.card:has(.fa-file-prescription) .table th,
.card:has(.fa-file-prescription) .table td {
    padding: 12px 14px;
    vertical-align: middle;
}
.card:has(.fa-file-prescription) .table th {
    color: var(--muted);
    background: #f7faf9;
    font: 700 11px/1.2 Arial, sans-serif;
    letter-spacing: .06em;
    text-transform: uppercase;
    white-space: nowrap;
}
.card:has(.fa-file-prescription) .table td:last-child {
    white-space: nowrap;
}
.card:has(.fa-file-prescription) .table td:last-child .badge,
.card:has(.fa-file-prescription) .status-badge {
    min-width: 88px;
    padding: 7px 10px;
    white-space: nowrap;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.card:has(.fa-file-prescription) .badge-completed {
    background: #d9f4e2;
    color: #176b37;
    border: 1px solid #9bd7ad;
}
.card:has(.fa-file-prescription) .badge-active {
    background: #dcecff;
    color: #14518a;
    border: 1px solid #9fc5ee;
}
.card:has(.fa-file-prescription) .badge-cancelled {
    background: #fbe0e0;
    color: #9b2727;
    border: 1px solid #e5aaaa;
}

/* Keep the shell stable when navigation labels or viewport heights vary. */
.sidebar {
    position: sticky;
    top: 0;
    width: clamp(248px, 22vw, 292px) !important;
    flex: 0 0 clamp(248px, 22vw, 292px) !important;
    height: 100vh;
    max-height: 100vh;
    min-width: 0;
    overflow: hidden;
}
.sidebar-brand,
.sidebar .nav-link {
    min-width: 0;
}
.sidebar .nav-link span,
.sidebar-brand > span:last-child {
    min-width: 0;
    overflow-wrap: anywhere;
}
.sidebar .nav-link i {
    flex: 0 0 18px;
}
.sidebar-nav {
    flex: 1 1 auto;
    overscroll-behavior: contain;
}
.sidebar-bottom {
    position: relative;
}
@media (max-width: 767.98px) {
    .sidebar {
        position: fixed;
        width: min(310px, 88vw) !important;
        flex-basis: auto !important;
    }
    .sidebar-bottom {
        position: absolute;
        left: 14px;
        right: 14px;
        top: auto;
        bottom: 10px;
        padding: 8px 0 0;
        background: var(--sidebar);
    }
    body.mobile-nav-open {
        overflow: hidden;
    }
}

/* Keep the desktop navigation pinned while the application content scrolls. */
@media (min-width: 768px) {
    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        z-index: 1045;
    }
    .app-main {
        width: calc(100% - clamp(248px, 22vw, 292px));
        margin-left: clamp(248px, 22vw, 292px);
    }
}

@media (max-width: 767.98px) {
    .app-main {
        width: 100%;
        margin-left: 0;
    }
}
