/* =======================================================================
   Clinic Management System — Modern Poppins Stylesheet
   Design direction: Clean, clinical, modern & ultra-intuitive.
   Built with Poppins typography, rich card elevations, smooth micro-interactions,
   and responsive clinical data layouts.
   ======================================================================= */

:root {
    --ink: #1E293B;
    --ink-soft: #64748B;
    --ink-light: #94A3B8;
    --paper: #F8FAFC;
    --paper-raised: #FFFFFF;
    --line: #E2E8F0;
    --line-soft: #F1F5F9;
    --primary: #0F766E;
    --primary-dark: #0D5C56;
    --primary-tint: #CCFBF1;
    --primary-glow: rgba(15, 118, 110, 0.15);
    --accent: #E11D48;
    --accent-tint: #FFE4E6;
    --amber: #D97706;
    --amber-tint: #FEF3C7;
    --green: #16A34A;
    --green-tint: #DCFCE7;
    --radius-sm: 6px;
    --radius: 10px;
    --radius-lg: 16px;
    --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.05);
    --shadow: 0 4px 14px rgba(15, 23, 42, 0.06), 0 1px 3px rgba(15, 23, 42, 0.04);
    --shadow-lg: 0 12px 28px rgba(15, 23, 42, 0.09), 0 4px 10px rgba(15, 23, 42, 0.03);
    --font-display: 'Poppins', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-body: 'Poppins', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-mono: 'IBM Plex Mono', ui-monospace, 'SFMono-Regular', Menlo, monospace;
}

* { box-sizing: border-box; }

html {
    -webkit-tap-highlight-color: transparent; /* no grey flash on tap — controls give their own :active feedback */
    -webkit-text-size-adjust: 100%;           /* no font inflation when a phone rotates to landscape */
}

button, a, [role="button"], .tab {
    touch-action: manipulation;   /* no 300ms double-tap-zoom wait before click fires */
    -webkit-user-select: none;    /* long-pressing a control shouldn't select its label */
    user-select: none;
    -webkit-touch-callout: none;
}

html, body {
    margin: 0;
    padding: 0;
    background: var(--paper);
    color: var(--ink);
    font-family: var(--font-body);
    font-size: 14px;
    font-weight: 400;
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

a { color: var(--primary); text-decoration: none; transition: color 0.15s ease; }

button, input, select, textarea { font-family: inherit; font-size: inherit; }

:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

/* ---------- Layout shell ---------- */

.app-shell {
    display: flex;
    min-height: 100vh;
    min-height: 100dvh;
}

.sidebar {
    width: 240px;
    flex-shrink: 0;
    background: linear-gradient(180deg, #0F172A 0%, #0F2926 100%);
    color: #F1F5F9;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    height: 100dvh;
    box-shadow: 2px 0 12px rgba(15, 23, 42, 0.08);
    z-index: 20;
}

.sidebar-brand {
    padding: 22px 20px 18px;
    border-bottom: 1px solid rgba(255,255,255,0.08);
}

.sidebar-brand .clinic-name {
    font-family: var(--font-display);
    font-size: 18px;
    font-weight: 600;
    color: #FFFFFF;
    line-height: 1.25;
    display: block;
    letter-spacing: -0.01em;
}

.sidebar-brand .clinic-type {
    font-size: 11px;
    color: #2DD4BF;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin-top: 4px;
    display: block;
}

.sidebar-nav {
    flex: 1;
    padding: 16px 12px;
    overflow-y: auto;
}

.sidebar-nav a {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 14px;
    border-radius: var(--radius);
    color: #94A3B8;
    font-size: 13.5px;
    font-weight: 500;
    margin-bottom: 3px;
    transition: all 0.2s ease;
}

.sidebar-nav a.active {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    color: #FFFFFF;
    font-weight: 600;
    box-shadow: 0 4px 12px rgba(15, 118, 110, 0.3);
}

.sidebar-nav .nav-icon { width: 18px; text-align: center; font-size: 15px; opacity: 0.9; }

.sidebar-foot {
    padding: 16px 20px;
    border-top: 1px solid rgba(255,255,255,0.08);
    font-size: 12px;
    color: #94A3B8;
    background: rgba(0,0,0,0.15);
}

.sidebar-foot .user-name { color: #FFFFFF; font-weight: 600; font-size: 13px; display: block; }
.sidebar-foot .logout-link { color: #FDA4AF; font-weight: 500; }

.main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.topbar {
    background: var(--paper-raised);
    border-bottom: 1px solid var(--line);
    padding: 16px 32px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 8px 16px;
    box-shadow: var(--shadow-sm);
}

.topbar h1 {
    font-family: var(--font-display);
    font-size: 21px;
    font-weight: 600;
    margin: 0;
    color: var(--ink);
    letter-spacing: -0.02em;
}

.topbar .topbar-sub {
    font-size: 12.5px;
    color: var(--ink-soft);
    font-family: var(--font-mono);
    background: var(--line-soft);
    padding: 4px 10px;
    border-radius: var(--radius-sm);
    font-weight: 500;
}

.content {
    padding: 28px 32px 48px;
    flex: 1;
}

/* ---------- Cards & Tables ---------- */

.card {
    background: var(--paper-raised);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 22px 24px;
    margin-bottom: 22px;
    transition: box-shadow 0.2s ease;
}

.card { min-width: 0; }
.card h2, .card h3, .card p, .card .stat-value, .patient-row td { overflow-wrap: anywhere; }

.card-title {
    font-size: 13px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--ink-soft);
    margin: 0 0 16px;
}

.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--paper-raised);
    border: 1px solid var(--line);
    border-left: 4px solid var(--primary);
    border-radius: var(--radius);
    padding: 18px 20px;
    box-shadow: var(--shadow);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.stat-card .stat-value {
    font-family: var(--font-display);
    font-size: 28px;
    font-weight: 700;
    color: var(--ink);
    letter-spacing: -0.02em;
    line-height: 1.15;
    margin-bottom: 4px;
}

.stat-card .stat-label {
    font-size: 11.5px;
    color: var(--ink-soft);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

table { width: 100%; border-collapse: collapse; }

.table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: var(--radius-sm);
}

.table-wrap table { min-width: 560px; }

@media (max-width: 640px) {
    .table-wrap table { min-width: 0; }
    .col-hide-sm { display: none !important; }
}
@media (max-width: 420px) {
    .col-hide-xs { display: none !important; }
}

th {
    text-align: left;
    font-size: 11.5px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--ink-soft);
    font-weight: 600;
    padding: 12px 14px;
    border-bottom: 2px solid var(--line);
    background: var(--line-soft);
    white-space: nowrap;
}

td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--line-soft);
    vertical-align: middle;
    font-size: 13.5px;
}

tr:last-child td { border-bottom: none; }
tbody tr { transition: background 0.15s ease; }

.record-no {
    font-family: var(--font-mono);
    font-size: 12px;
    font-weight: 500;
    background: var(--line-soft);
    padding: 3px 8px;
    border-radius: 6px;
    color: var(--ink-soft);
    border: 1px solid var(--line);
}

/* ---------- Badges ---------- */

.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 11.5px;
    font-weight: 600;
    padding: 4px 10px;
    border-radius: 999px;
    text-transform: capitalize;
    letter-spacing: 0.01em;
}

.badge-green { background: var(--green-tint); color: var(--green); }
.badge-amber { background: var(--amber-tint); color: var(--amber); }
.badge-red { background: var(--accent-tint); color: var(--accent); }
.badge-grey { background: var(--line-soft); color: var(--ink-soft); }

/* ---------- Buttons & Forms ---------- */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    border: 1px solid transparent;
    border-radius: var(--radius);
    padding: 9px 18px;
    font-size: 13.5px;
    font-weight: 600;
    cursor: pointer;
    background: var(--primary);
    color: #FFFFFF;
    min-height: 40px;
    box-shadow: 0 2px 6px rgba(15, 118, 110, 0.2);
    transition: all 0.2s ease;
}

.btn:active { transform: scale(0.97); filter: brightness(0.94); transition-duration: 0.08s; }
.btn-link:active, .tab:active, .sidebar-nav a:active { background: var(--primary-tint); }

.btn-secondary {
    background: var(--paper-raised);
    color: var(--ink);
    border-color: var(--line);
    box-shadow: var(--shadow-sm);
}

.btn-danger {
    background: var(--accent);
    box-shadow: 0 2px 6px rgba(225, 29, 72, 0.2);
}

.btn-sm { padding: 6px 12px; font-size: 12.5px; min-height: 32px; border-radius: var(--radius-sm); }
.btn-link {
    background: none;
    color: var(--primary);
    padding: 6px 10px;
    min-height: 32px;
    display: inline-flex;
    align-items: center;
    box-shadow: none;
    border-radius: var(--radius-sm);
}

.row-actions { display: inline-flex; flex-wrap: wrap; gap: 4px; align-items: center; }

@media (pointer: coarse) {
    .btn-link { padding: 8px 12px; min-height: 40px; }
    .btn-sm { min-height: 40px; padding: 8px 14px; }
}

.form-row { margin-bottom: 16px; }
.form-row label {
    display: block;
    font-size: 12.5px;
    font-weight: 600;
    color: var(--ink-soft);
    margin-bottom: 6px;
}

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

input[type=text], input[type=email], input[type=password], input[type=date],
input[type=time], input[type=tel], input[type=number], input[type=color], select, textarea {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--paper-raised);
    color: var(--ink);
    font-size: 14.5px;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

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

input:focus, select:focus, textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-glow);
    outline: none;
}

.search-bar {
    display: flex;
    gap: 12px;
    margin-bottom: 18px;
    flex-wrap: wrap;
}

.search-bar input, .search-bar select { max-width: 280px; }

/* ---------- Login Page ---------- */

.login-shell {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #0F172A 0%, #0F2926 100%);
    padding: 24px;
}

.login-card {
    width: 100%;
    max-width: 400px;
    background: rgba(255, 255, 255, 0.98);
    backdrop-filter: blur(16px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: 36px 32px;
}

.login-card .clinic-name {
    font-family: var(--font-display);
    font-size: 26px;
    font-weight: 700;
    text-align: center;
    display: block;
    color: var(--ink);
    letter-spacing: -0.02em;
}

.login-card .clinic-tagline {
    text-align: center;
    color: var(--ink-soft);
    font-size: 12px;
    margin-bottom: 28px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-weight: 600;
}

.login-error {
    background: var(--accent-tint);
    color: var(--accent);
    border-radius: var(--radius);
    padding: 10px 14px;
    font-size: 13px;
    margin-bottom: 18px;
    font-weight: 500;
    border-left: 3px solid var(--accent);
}

.login-hint {
    margin-top: 20px;
    font-size: 12px;
    color: var(--ink-soft);
    text-align: center;
    background: var(--line-soft);
    padding: 8px 12px;
    border-radius: var(--radius-sm);
}

/* ---------- Modal ---------- */

.modal-backdrop {
    position: fixed; inset: 0;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
    display: none;
    align-items: flex-start;
    justify-content: center;
    padding: 40px 16px;
    overflow-y: auto;
    z-index: 50;
}

.modal-backdrop.open { display: flex; }

.modal {
    background: var(--paper-raised);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 580px;
    max-height: calc(100vh - 48px);
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--line);
}

/* Every modal's body and footer sit inside a <form>; without this the form is a plain block,
   the body can't shrink, and on a short screen the Save button ends up below the viewport. */
.modal > form { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; }

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

.modal-header h2 {
    font-family: var(--font-display);
    font-size: 19px;
    font-weight: 600;
    margin: 0;
    letter-spacing: -0.01em;
}

.modal-close {
    background: none; border: none; font-size: 22px; cursor: pointer; color: var(--ink-soft);
    line-height: 1;
    min-width: 36px; min-height: 36px;
    display: inline-flex; align-items: center; justify-content: center;
    border-radius: 50%;
    transition: background 0.15s ease;
}

.modal-body { padding: 22px 24px; flex: 1 1 auto; min-height: 0; overflow-y: auto; }
.modal-footer {
    padding: 16px 24px;
    border-top: 1px solid var(--line);
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    flex-shrink: 0;
    flex-wrap: wrap;
    background: var(--line-soft);
    border-bottom-left-radius: var(--radius-lg);
    border-bottom-right-radius: var(--radius-lg);
}

/* ---------- Toast ---------- */

#toast-container {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 100;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.toast {
    background: var(--ink);
    color: #FFFFFF;
    padding: 12px 18px;
    border-radius: var(--radius);
    font-size: 13.5px;
    font-weight: 500;
    box-shadow: var(--shadow-lg);
    border-left: 4px solid var(--primary);
}

.toast.error { border-left-color: var(--accent); }

/* ---------- Utility ---------- */

.empty-state {
    text-align: center;
    padding: 48px 24px;
    color: var(--ink-soft);
}

/* ---------- CSS-Only Charts ---------- */

.bar-chart {
    display: flex;
    align-items: flex-end;
    gap: 12px;
    height: 180px;
    padding-top: 24px;
    overflow-x: auto;
}
.bar-chart .bar-col { flex: 1; min-width: 48px; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; }
.bar-chart .bar { width: 100%; max-width: 42px; background: linear-gradient(180deg, var(--primary) 0%, var(--primary-dark) 100%); border-radius: 6px 6px 0 0; min-height: 4px; }
.bar-chart .bar-value { font-size: 11px; font-weight: 600; color: var(--ink-soft); margin-bottom: 4px; white-space: nowrap; }
.bar-chart .bar-label { font-size: 11px; font-weight: 500; color: var(--ink-soft); margin-top: 8px; white-space: nowrap; }

.hbar-row { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; font-size: 13px; }
.hbar-label { width: 44%; flex-shrink: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.hbar-track { flex: 1; background: var(--line-soft); border-radius: 6px; height: 20px; overflow: hidden; }
.hbar-fill { height: 100%; background: linear-gradient(90deg, var(--primary) 0%, #0D9488 100%); border-radius: 6px; }
.hbar-value { min-width: 36px; text-align: right; flex-shrink: 0; font-weight: 600; }

.alert-banner {
    background: var(--amber-tint);
    border: 1px solid var(--amber);
    border-left: 4px solid var(--amber);
    border-radius: var(--radius);
    padding: 14px 18px;
    margin-bottom: 18px;
    font-size: 13.5px;
    color: #92400E;
}
.alert-banner strong { color: #78350F; }
.alert-banner .clear-delay-link { margin-left: 12px; font-size: 12px; font-weight: 600; }

.quick-contact { display: inline-flex; gap: 6px; }
.quick-contact a {
    display: inline-flex; align-items: center; justify-content: center;
    width: 28px; height: 28px;
    border-radius: 50%;
    background: var(--primary-tint);
    color: var(--primary-dark);
    font-size: 13px;
    transition: all 0.15s ease;
}

.text-muted { color: var(--ink-soft); }
.mt-0 { margin-top: 0; }
.flex-between { display: flex; align-items: center; justify-content: space-between; gap: 14px; flex-wrap: wrap; }
.tabs { display: flex; gap: 6px; border-bottom: 2px solid var(--line); margin-bottom: 20px; }
.tab {
    padding: 10px 16px;
    font-size: 13.5px;
    font-weight: 600;
    color: var(--ink-soft);
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: all 0.15s ease;
}
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }

.svc-row { grid-template-columns: 60px 1fr 2fr auto; align-items: end; margin-bottom: 10px; }
.hrs-row { grid-template-columns: 1fr 1fr auto; align-items: end; margin-bottom: 10px; }
.invoice-item-row { grid-template-columns: 2fr 0.7fr 1fr auto; margin-bottom: 8px; align-items: center; }

@media (max-width: 640px) {
    .svc-row, .hrs-row { grid-template-columns: 1fr; gap: 8px; padding: 12px; border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 12px; }
    .svc-row > div:last-child, .hrs-row > div:last-child { display: flex; }
    .svc-row > div:last-child button, .hrs-row > div:last-child button { flex: 1; justify-content: center; }
    .invoice-item-row { grid-template-columns: 1fr 1fr; gap: 8px; padding: 12px; border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 10px; }
    .invoice-item-row .item-desc { grid-column: 1 / -1; }
    .invoice-item-row button { grid-column: 1 / -1; }
}

@media print {
    .no-print, .sidebar, .topbar, #toast-container { display: none !important; }
    body { background: #FFFFFF; }
    .app-shell { display: block; min-height: 0; }
    .main { margin: 0; }
    .content { padding: 0; }
    .card { box-shadow: none; border: 1px solid #CBD5E1; max-width: none !important; }
    a { color: inherit; }
}

/* ---------- Responsive ---------- */

/* Phone shell: the desktop sidebar becomes an off-canvas drawer opened from a compact top bar.
   (Previously it collapsed into a horizontally-scrolling strip of 10 links that hid the
   user's name and the Log out link entirely.) */

.mobile-bar { display: none; }
.sidebar-backdrop { display: none; }

@media (max-width: 860px) {
    .app-shell { flex-direction: column; }

    .mobile-bar {
        display: flex;
        align-items: center;
        gap: 12px;
        position: sticky;
        top: 0;
        z-index: 30;
        background: linear-gradient(180deg, #0F172A 0%, #10231F 100%);
        color: #FFFFFF;
        padding: 10px 12px;
        padding-top: calc(10px + env(safe-area-inset-top, 0px));
        padding-left: calc(12px + env(safe-area-inset-left, 0px));
        padding-right: calc(12px + env(safe-area-inset-right, 0px));
        box-shadow: 0 2px 10px rgba(15, 23, 42, 0.25);
    }
    .mobile-bar .menu-btn {
        flex-shrink: 0;
        width: 44px; height: 44px;
        border: 1px solid rgba(255,255,255,0.14);
        border-radius: var(--radius);
        background: rgba(255,255,255,0.06);
        color: #FFFFFF;
        font-size: 20px;
        display: inline-flex; align-items: center; justify-content: center;
        cursor: pointer;
    }
    .mobile-bar .menu-btn:active { background: rgba(255,255,255,0.16); }
    .mobile-bar .mobile-title {
        flex: 1; min-width: 0;
        font-family: var(--font-display);
        font-size: 17px; font-weight: 600;
        white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    }
    .mobile-bar .mobile-clinic {
        font-size: 11px; color: #2DD4BF; font-weight: 600;
        text-transform: uppercase; letter-spacing: 0.06em;
        white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 38%;
    }

    .sidebar {
        position: fixed;
        top: 0; left: 0; bottom: 0;
        width: min(300px, 84vw);
        height: 100dvh;
        transform: translateX(-100%);
        transition: transform 0.24s cubic-bezier(0.32, 0.72, 0, 1);
        z-index: 60;
        box-shadow: none;
        padding-top: env(safe-area-inset-top, 0px);
        padding-left: env(safe-area-inset-left, 0px);
        overscroll-behavior: contain;
    }
    .sidebar.open { transform: translateX(0); box-shadow: 12px 0 40px rgba(15, 23, 42, 0.35); }
    .sidebar-nav a { min-height: 46px; font-size: 14.5px; }
    .sidebar-foot { padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px)); }

    .sidebar-backdrop {
        display: block;
        position: fixed; inset: 0;
        background: rgba(15, 23, 42, 0.55);
        z-index: 55;
        opacity: 0; pointer-events: none;
        transition: opacity 0.24s ease;
    }
    .sidebar-backdrop.open { opacity: 1; pointer-events: auto; }

    /* The page title now lives in the mobile bar; keep the date but drop the duplicate h1. */
    .topbar { padding: 10px 16px; }
    .topbar h1 { display: none; }
    .topbar .topbar-sub { font-size: 12px; }

    .content { padding: 16px 16px calc(40px + env(safe-area-inset-bottom, 0px)); }
    .content { padding-left: calc(16px + env(safe-area-inset-left, 0px)); padding-right: calc(16px + env(safe-area-inset-right, 0px)); }

    /* Page-level action rows ("search + New Patient"): stack, and make the primary button
       a full-width thumb target instead of a small pill squeezed beside the search box. */
    .content > .flex-between { flex-direction: column; align-items: stretch; }
    .content > .flex-between > .btn, .content > .flex-between > div > .btn { width: 100%; }
    .content > .flex-between > div { display: flex; flex-direction: column; gap: 8px; }
    .content > .flex-between > .search-bar { flex-direction: row; flex-wrap: wrap; gap: 8px; width: 100%; }
    .search-bar input, .search-bar select { max-width: none; flex: 1 1 140px; min-width: 0; }
    .search-bar > .btn { flex: 0 0 auto; }

    /* Tabs never wrap onto two lines; they scroll sideways like a native segmented bar. */
    .tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; flex-wrap: nowrap; scrollbar-width: none; margin-left: -16px; margin-right: -16px; padding: 0 16px; }
    .tabs::-webkit-scrollbar { display: none; }
    .tab { min-height: 44px; display: flex; align-items: center; white-space: nowrap; flex-shrink: 0; }

    #toast-container { right: 16px; left: 16px; bottom: calc(16px + env(safe-area-inset-bottom, 0px)); }
    .toast { text-align: center; }
}

@media (max-width: 640px) {
    .stat-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
    .stat-card { padding: 14px 14px; }
    .stat-card .stat-value { font-size: 22px; }
    .card { padding: 16px 16px; margin-bottom: 14px; }
    .form-grid { grid-template-columns: 1fr; gap: 12px; }

    /* Tables become a stack of cards. app.js stamps each cell with data-label from its column
       header; every column is shown (nothing hidden), so the phone shows *more* than before. */
    .table-wrap { overflow: visible; }
    .table-wrap table { min-width: 0; display: block; }
    .table-wrap thead { display: none; }
    .table-wrap tbody { display: block; }
    .table-wrap tr {
        display: block;
        padding: 12px 14px;
        margin: 10px 12px;
        border: 1px solid var(--line);
        border-radius: var(--radius);
        background: var(--paper-raised);
        box-shadow: var(--shadow-sm);
    }
    .table-wrap tr:first-child { margin-top: 12px; }
    .table-wrap tr:active { background: var(--line-soft); }
    .table-wrap td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 12px;
        padding: 6px 0;
        border: none;
        font-size: 14px;
        text-align: right;
        min-width: 0;
        overflow-wrap: anywhere;
        flex-wrap: wrap;
        row-gap: 4px;
    }
    .table-wrap td::before { margin-right: auto; }
    .table-wrap td::before {
        content: attr(data-label);
        flex-shrink: 0;
        font-size: 11px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.06em;
        color: var(--ink-soft);
        text-align: left;
    }
    .table-wrap td[data-label=""]::before, .table-wrap td:not([data-label])::before { display: none; }
    .table-wrap td[data-label=""] { justify-content: flex-start; text-align: left; }
    .table-wrap td.row-actions {
        display: flex; flex-wrap: wrap; gap: 6px;
        justify-content: flex-start;
        margin-top: 8px; padding-top: 10px;
        border-top: 1px solid var(--line-soft);
    }
    .table-wrap td.row-actions .btn-link, .table-wrap td.row-actions .btn-sm {
        border: 1px solid var(--line); background: var(--paper-raised);
        min-height: 40px; padding: 8px 12px; border-radius: var(--radius-sm);
    }
    .table-wrap td.row-actions .btn:not(.btn-link) { background: var(--primary); color: #FFFFFF; border-color: transparent; }
    .col-hide-sm, .col-hide-xs { display: flex !important; }
    .table-wrap .empty-state { padding: 36px 16px; }

    /* Modals become bottom sheets: slide up from the bottom, full width, rounded top,
       footer buttons pinned above the home indicator. */
    .modal-backdrop { padding: 0; align-items: flex-end; inset: auto 0 0 0; height: 100dvh; }
    .modal {
        max-width: none;
        max-height: 90dvh;
        border-radius: var(--radius-lg) var(--radius-lg) 0 0;
        border-bottom: none;
        animation: sheet-up 0.28s cubic-bezier(0.32, 0.72, 0, 1);
    }
    .modal::before {
        content: "";
        display: block;
        width: 40px; height: 4px;
        border-radius: 999px;
        background: var(--line);
        margin: 10px auto 0;
    }
    .modal-header { padding: 10px 18px 14px; }
    .modal-header h2 { font-size: 17px; }
    .modal-body { padding: 16px 18px; overscroll-behavior: contain; }
    .modal-footer {
        padding: 12px 18px calc(12px + env(safe-area-inset-bottom, 0px));
        border-radius: 0;
        flex-wrap: nowrap;
    }
    .modal-footer .btn { flex: 1 1 0; min-height: 46px; }
    @keyframes sheet-up { from { transform: translateY(24px); opacity: 0.6; } to { transform: translateY(0); opacity: 1; } }

    .login-shell { padding: 16px; align-items: stretch; }
    .login-card { max-width: none; padding: 28px 22px; align-self: center; }
    .bar-chart { height: 150px; }
    .hbar-label { width: 38%; }
}

@media (max-width: 420px) {
    .stat-card .stat-value { font-size: 20px; }
    .btn { padding: 9px 14px; font-size: 13px; }
}

/* Touch input: bigger targets, no page zoom on focus (iOS zooms any input under 16px). */
@media (pointer: coarse) {
    input[type=text], input[type=email], input[type=password], input[type=date],
    input[type=time], input[type=tel], input[type=number], select, textarea { font-size: 16px; min-height: 46px; }
    .btn { min-height: 44px; }
}

@media (prefers-reduced-motion: reduce) {
    .sidebar, .sidebar-backdrop, .modal { transition: none; animation: none; }
}

/* ---------- Hover states — only for inputs that can actually hover. On touch, a tapped
   element would otherwise keep its hover look until the next tap. ---------- */

@media (hover: hover) and (pointer: fine) {
    a:hover { color: var(--primary-dark); text-decoration: none; }
    .sidebar-nav a:hover {
        background: rgba(255, 255, 255, 0.07);
        color: #F8FAFC;
        transform: translateX(2px);
    }
    .sidebar-foot .logout-link:hover { color: #FECDD3; text-decoration: underline; }
    .stat-card:hover {
        transform: translateY(-2px);
        box-shadow: var(--shadow-lg);
    }
    tbody tr:hover { background: var(--primary-tint); }
    .btn:hover {
        background: var(--primary-dark);
        box-shadow: 0 4px 12px rgba(15, 118, 110, 0.3);
        transform: translateY(-1px);
        text-decoration: none;
        color: #FFFFFF;
    }
    .btn-secondary:hover {
        background: var(--line-soft);
        color: var(--ink);
        box-shadow: var(--shadow);
    }
    .btn-danger:hover {
        background: #BE123C;
        box-shadow: 0 4px 12px rgba(225, 29, 72, 0.3);
    }
    .btn-link:hover {
        background: var(--primary-tint);
        transform: none;
        box-shadow: none;
    }
    .modal-close:hover { background: var(--line-soft); color: var(--ink); }
    .quick-contact a:hover { background: var(--primary); color: #FFFFFF; text-decoration: none; transform: scale(1.08); }
    .tab:hover { color: var(--primary); }
}
