/* assets/css/style.css - UPVC System Design System */
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700;800&family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap');

:root {
    --primary: #0040A0;          /* PRIMETECH logo blue */
    --primary-hover: #00327e;
    --primary-light: #e8f0fb;
    --primary-glow: rgba(0, 64, 160, 0.22);

    --accent: #1f6fe0;
    --accent-dark: #0040A0;

    --bg-main: #f8fafc;
    --bg-card: #ffffff;
    --bg-sidebar: #001f4e;
    --bg-sidebar-hover: rgba(255, 255, 255, 0.12);

    --text-main: #0f172a;
    --text-muted: #64748b;
    --text-light: #94a3b8;
    --text-white: #ffffff;

    --border-color: #e2e8f0;
    --border-subtle: #f1f5f9;

    --success: #10b981;
    --success-bg: #ecfdf5;
    --success-border: #a7f3d0;

    --warning: #f59e0b;
    --warning-bg: #fffbeb;
    --warning-border: #fde68a;

    --danger: #ef4444;
    --danger-bg: #fef2f2;
    --danger-border: #fecaca;

    --info: #0284c7;
    --info-bg: #f0f9ff;
    --info-border: #bae6fd;

    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.07), 0 2px 4px -2px rgb(0 0 0 / 0.05);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.08), 0 4px 6px -4px rgb(0 0 0 / 0.04);
    --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.06);

    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;
    --radius-full: 9999px;

    --font-heading: 'Outfit', sans-serif;
    --font-body: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, sans-serif;
    --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

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

html, body {
    max-width: 100%;
    /* 'clip' contains horizontal overflow without turning html/body into a
       scroll container (which 'hidden' does, silently breaking position:sticky
       for the topbar and sticky table headers). */
    overflow-x: clip;
}

body {
    font-family: var(--font-body);
    background-color: var(--bg-main);
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    line-height: 1.5;
}

h1, h2, h3, h4, h5, h6 {
    font-family: var(--font-heading);
    font-weight: 700;
    color: var(--text-main);
    line-height: 1.25;
}

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

a:hover {
    color: var(--primary-hover);
}

/* Application Layout */
.app-wrapper {
    display: flex;
    min-height: 100vh;
    width: 100%;
    max-width: 100%;
    position: relative;
}

/* Sidebar */
.sidebar {
    width: 280px;
    background: linear-gradient(180deg, #001e4a 0%, #002c6b 45%, #001639 100%);
    color: var(--text-white);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    box-shadow: 4px 0 26px rgba(0, 24, 70, 0.3);
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    height: 100vh;
    z-index: 40;
    overflow-y: auto;
}

@media (min-width: 993px) {
    .main-content {
        margin-left: 280px !important;
        width: calc(100% - 280px) !important;
        min-width: 0 !important;
    }
}

.sidebar-header {
    padding: 18px 20px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.12);
    display: flex;
    align-items: center;
    gap: 12px;
}

.brand-logo-box {
    width: 100%;
    background: #ffffff;
    border-radius: 14px;
    padding: 11px 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.3);
}

.brand-logo {
    width: 100%;
    max-width: 190px;
    height: auto;
    display: block;
}

.brand-badge {
    width: 44px;
    height: 44px;
    background: linear-gradient(135deg, #0040A0, #00A3E0);
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-weight: 800;
    font-size: 1.2rem;
    box-shadow: 0 4px 12px rgba(0, 163, 224, 0.35);
}

.brand-info h2 {
    color: #fff;
    font-size: 1.15rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.brand-info span {
    font-size: 0.75rem;
    color: #7dd3fc;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 600;
}

.sidebar-user {
    padding: 18px 22px;
    background: rgba(255, 255, 255, 0.04);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.user-role-tag {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 10px;
    background: rgba(0, 163, 224, 0.2);
    color: #38bdf8;
    border: 1px solid rgba(56, 189, 248, 0.35);
    border-radius: var(--radius-full);
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 6px;
}

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

.nav-section-title {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: #7dd3fc;
    padding: 8px 12px;
    font-weight: 700;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 11px 16px;
    color: #cfe2fe;
    border-radius: var(--radius-md);
    margin-bottom: 4px;
    font-weight: 500;
    font-size: 0.92rem;
    transition: var(--transition);
}

.nav-item .icon {
    font-size: 1.15rem;
    width: 22px;
    text-align: center;
    color: #38bdf8;
    transition: color 0.15s ease, transform 0.15s ease;
}

.nav-item:hover {
    background-color: rgba(255, 255, 255, 0.12);
    color: #ffffff;
    transform: translateX(4px);
}

.nav-item:hover .icon {
    color: #ffffff;
    transform: scale(1.08);
}

.nav-item.active {
    background: linear-gradient(90deg, #0040A0 0%, #0098e6 100%);
    color: #ffffff;
    font-weight: 600;
    box-shadow: 0 4px 14px rgba(0, 152, 230, 0.35);
    border-left: 4px solid #7dd3fc;
}

.nav-item.active .icon {
    color: #ffffff;
}

.nav-item .badge-pill {
    margin-left: auto;
    background: rgba(56, 189, 248, 0.25);
    color: #ffffff;
    border: 1px solid rgba(56, 189, 248, 0.4);
    font-size: 0.7rem;
    padding: 2px 7px;
    border-radius: var(--radius-full);
}

.sidebar-footer {
    padding: 16px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

/* Main Content Area */
.main-content {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    max-width: 100%;
    /* 'clip' (not 'hidden') prevents horizontal overflow WITHOUT creating a
       scroll container — 'hidden' would force overflow-y:auto and silently break
       position:sticky (the topbar and sticky table headers) against the window. */
    overflow-x: clip;
    overflow-y: visible;
    background-color: var(--bg-main);
}

/* Top Navigation Bar */
.topbar {
    height: 72px;
    background-color: #ffffff;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 32px;
    position: sticky;
    top: 0;
    z-index: 30;
    box-shadow: var(--shadow-sm);
}

.topbar-title h1 {
    font-size: 1.35rem;
    color: var(--text-main);
    display: flex;
    align-items: center;
    gap: 10px;
}

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

/* Page Container */
.page-container {
    padding: 32px;
    max-width: 1440px;
    width: 100%;
    margin: 0 auto;
    box-sizing: border-box;
    /* 'clip' keeps horizontal overflow contained without breaking sticky headers
       (see .main-content). */
    overflow-x: clip;
    overflow-y: visible;
}

/* Invoices Desk header & tabs */
.invoices-header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 24px;
    flex-wrap: wrap;
    gap: 16px;
}

.desk-tabs-bar {
    display: flex;
    flex-wrap: wrap;              /* wrap onto multiple rows instead of scrolling */
    gap: 8px;
    margin-bottom: 20px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 10px;
    width: 100%;
    max-width: 100%;
}
.desk-tabs-bar .btn {
    flex-shrink: 0;
    border-radius: var(--radius-full);
    padding: 6px 14px;
    font-size: 0.82rem;
    white-space: nowrap;         /* keep each button label on one line */
}

/* Stat Cards */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 20px;
    margin-bottom: 28px;
}

/* MD portal: keep the headline "Collected Today" card at a natural width. */
.md-collected-top { grid-template-columns: minmax(240px, 340px); }

/* MD portal: compact KPI card rows — six-across (top) or five-across
   (secondary). Falls back to fewer columns as the viewport narrows. */
@media (min-width: 1100px) {
    .md-kpi-grid { grid-template-columns: repeat(6, 1fr); gap: 12px; }
    .md-kpi-5   { grid-template-columns: repeat(5, 1fr); gap: 12px; }
    /* Hide the icon in the compact layouts to give the amount full width. */
    .md-kpi-grid .stat-card, .md-kpi-5 .stat-card { padding: 13px 12px; }
    .md-kpi-grid .stat-icon, .md-kpi-5 .stat-icon { display: none; }
    .md-kpi-grid .stat-info, .md-kpi-5 .stat-info { width: 100%; }
    .md-kpi-grid .stat-info .stat-value, .md-kpi-5 .stat-info .stat-value { font-size: 0.9rem; letter-spacing: -0.02em; white-space: nowrap; }
    .md-kpi-grid .stat-info .stat-label, .md-kpi-5 .stat-info .stat-label { font-size: 0.63rem; letter-spacing: 0.02em; }
    .md-kpi-grid .stat-info .stat-desc, .md-kpi-5 .stat-info .stat-desc { font-size: 0.63rem; }
}
@media (min-width: 769px) and (max-width: 1099px) {
    .md-kpi-grid, .md-kpi-5 { grid-template-columns: repeat(3, 1fr); gap: 14px; }
}

.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 22px;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    box-shadow: var(--shadow-sm);
    transition: var(--transition);
    position: relative;
    overflow: hidden;
}

.stat-card::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: transparent;
    transition: var(--transition);
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.stat-card.primary::after { background: var(--primary); }
.stat-card.success::after { background: var(--success); }
.stat-card.warning::after { background: var(--warning); }
.stat-card.info::after { background: var(--info); }
.stat-card.danger::after { background: var(--danger); }

.stat-info .stat-label {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 6px;
}

.stat-info .stat-value {
    font-size: 1.85rem;
    font-weight: 800;
    color: var(--text-main);
    letter-spacing: -0.02em;
    line-height: 1.1;
}

.stat-info .stat-desc {
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-top: 6px;
}

.stat-icon {
    width: 48px;
    height: 48px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.4rem;
}

.stat-card.primary .stat-icon { background: var(--primary-light); color: var(--primary); }
.stat-card.success .stat-icon { background: var(--success-bg); color: var(--success); }
.stat-card.warning .stat-icon { background: var(--warning-bg); color: var(--warning); }
.stat-card.info .stat-icon { background: var(--info-bg); color: var(--info); }
.stat-card.danger .stat-icon { background: var(--danger-bg); color: var(--danger); }

/* Content Cards */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    margin-bottom: 28px;
    overflow: hidden;
}

.card-header {
    padding: 20px 24px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #ffffff;
}

.card-header h3 {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-main);
    display: flex;
    align-items: center;
    gap: 8px;
}

.card-body {
    padding: 24px;
}

/* Tables */
.table-responsive {
    width: 100%;
    overflow-x: auto;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.88rem;
    text-align: left;
}

.data-table th {
    background-color: #f8fafc;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.72rem;
    letter-spacing: 0.06em;
    padding: 13px 18px;
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
}

.data-table td {
    padding: 14px 18px;
    border-bottom: 1px solid var(--border-subtle);
    color: var(--text-main);
    vertical-align: middle;
}

.data-table tbody tr {
    transition: var(--transition);
}

.data-table tbody tr:hover {
    background-color: #f8fafc;
}

/* Badges */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 4px 10px;
    border-radius: var(--radius-full);
    font-size: 0.75rem;
    font-weight: 600;
    white-space: nowrap;
}

.badge-warning { background-color: var(--warning-bg); color: #b45309; border: 1px solid var(--warning-border); }
.badge-success { background-color: var(--success-bg); color: #065f46; border: 1px solid var(--success-border); }
.badge-primary { background-color: var(--primary-light); color: var(--primary-hover); border: 1px solid #bfdbfe; }
.badge-info { background-color: var(--info-bg); color: var(--info); border: 1px solid var(--info-border); }
.badge-danger { background-color: var(--danger-bg); color: #b91c1c; border: 1px solid var(--danger-border); }
.badge-secondary { background-color: #f1f5f9; color: #475569; border: 1px solid #cbd5e1; }

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 9px 18px;
    font-size: 0.88rem;
    font-weight: 600;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: var(--transition);
    border: 1px solid transparent;
    line-height: 1.4;
    text-decoration: none;
}

.btn:hover {
    transform: translateY(-1px);
}

.btn-primary {
    background: linear-gradient(135deg, #0b53c0, var(--primary));
    color: #ffffff;
    box-shadow: 0 4px 10px var(--primary-glow);
}
.btn-primary:hover {
    background: linear-gradient(135deg, var(--primary), var(--primary-hover));
    color: #ffffff;
    box-shadow: 0 6px 14px var(--primary-glow);
}

.btn-success {
    background: linear-gradient(135deg, #10b981, #059669);
    color: #ffffff;
    box-shadow: 0 4px 10px rgba(16, 185, 129, 0.25);
}
.btn-success:hover {
    background: linear-gradient(135deg, #059669, #047857);
    color: #ffffff;
}

.btn-outline {
    background: #ffffff;
    border-color: var(--border-color);
    color: var(--text-main);
}
.btn-outline:hover {
    background: #f8fafc;
    border-color: var(--text-muted);
}

.btn-warning {
    background: linear-gradient(135deg, #f59e0b, #d97706);
    color: #ffffff;
    box-shadow: 0 4px 10px rgba(245, 158, 11, 0.25);
}
.btn-warning:hover {
    background: linear-gradient(135deg, #d97706, #b45309);
    color: #ffffff;
}

.btn-danger {
    background: #ef4444;
    color: #ffffff;
}
.btn-danger:hover {
    background: #dc2626;
    color: #ffffff;
}

.btn-sm {
    padding: 6px 12px;
    font-size: 0.8rem;
    border-radius: var(--radius-sm);
}

.btn-icon {
    padding: 7px;
    border-radius: var(--radius-md);
}

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

.form-label {
    display: block;
    font-size: 0.92rem;
    font-weight: 600;
    color: #334155;
    margin-bottom: 6px;
}

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

.form-control {
    width: 100%;
    padding: 11px 14px;
    font-size: 0.95rem;
    font-family: var(--font-body);
    color: var(--text-main);
    background-color: #ffffff;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    transition: var(--transition);
}

.form-control:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-glow);
}

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

/* Alerts / Flash messages */
.alert {
    padding: 14px 18px;
    border-radius: var(--radius-md);
    margin-bottom: 22px;
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 0.9rem;
}

.alert-success { background: var(--success-bg); color: #065f46; border: 1px solid var(--success-border); }
.alert-error { background: var(--danger-bg); color: #b91c1c; border: 1px solid var(--danger-border); }
.alert-warning { background: var(--warning-bg); color: #92400e; border: 1px solid var(--warning-border); }
.alert-info { background: var(--info-bg); color: #075985; border: 1px solid var(--info-border); }

/* Quick Persona Cards on Login */
.persona-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 12px;
    margin-top: 20px;
}

.persona-btn {
    display: flex;
    flex-direction: column;
    text-align: left;
    padding: 12px 14px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    background: #ffffff;
    cursor: pointer;
    transition: var(--transition);
}

.persona-btn:hover {
    border-color: var(--primary);
    background-color: var(--primary-light);
    transform: translateY(-2px);
}

.persona-btn .role-name {
    font-size: 0.88rem;
    font-weight: 700;
    color: var(--text-main);
}

.persona-btn .role-desc {
    font-size: 0.74rem;
    color: var(--text-muted);
    margin-top: 2px;
}

/* Modal */
.modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    opacity: 0;
    visibility: hidden;
    transition: var(--transition);
}

.modal-backdrop.show {
    opacity: 1;
    visibility: visible;
}

.modal-dialog {
    background: #ffffff;
    border-radius: var(--radius-lg);
    width: 90%;
    max-width: 600px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: var(--shadow-xl);
    transform: scale(0.95);
    transition: var(--transition);
}

.modal-backdrop.show .modal-dialog {
    transform: scale(1);
}

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

.modal-title {
    font-size: 1.15rem;
    font-weight: 700;
}

.modal-close {
    background: transparent;
    border: none;
    font-size: 1.3rem;
    color: var(--text-muted);
    cursor: pointer;
}

.modal-body {
    padding: 24px;
}

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

/* Dynamic Line Items Table (Invoices) */
.items-builder-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 12px;
}

.items-builder-table th {
    background: #f1f5f9;
    padding: 10px 12px;
    font-size: 0.76rem;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--text-muted);
    border: 1px solid var(--border-color);
}

.items-builder-table td {
    padding: 8px 10px;
    border: 1px solid var(--border-color);
    background: #ffffff;
}

.items-builder-table input,
.items-builder-table select {
    width: 100%;
    padding: 7px 10px;
    font-size: 0.88rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
}

/* Hamburger toggle (hidden on desktop) */
.sidebar-toggle {
    display: none;
    background: #e8f0fb;
    border: 1px solid rgba(0, 64, 160, 0.2);
    color: #0040A0;
    font-size: 1.2rem;
    cursor: pointer;
    padding: 7px 11px;
    margin-right: 8px;
    border-radius: var(--radius-md);
    line-height: 1;
    transition: var(--transition);
}
.sidebar-toggle:hover {
    background: #0040A0;
    color: #ffffff;
}

/* Off-canvas backdrop (hidden until sidebar opens) */
.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.5);
    z-index: 39;
    opacity: 0;
    transition: opacity 0.25s ease;
}
.sidebar-backdrop.show {
    opacity: 1;
}

/* Make data tables scroll sideways instead of breaking layout */
.table-responsive {
    -webkit-overflow-scrolling: touch;
}

/* ================= Responsive ================= */
@media (max-width: 992px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        width: 280px;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
    }
    .sidebar.open {
        transform: translateX(0) !important;
    }
    .sidebar-backdrop.show {
        display: block;
    }
    .sidebar-toggle {
        display: inline-flex;
        align-items: center;
    }
    body.sidebar-open {
        overflow: hidden;
    }
    .topbar {
        padding: 0 16px;
    }
    .topbar-title h1 {
        font-size: 1.1rem;
    }
    .page-container {
        padding: 20px 16px;
    }
    /* Tables scroll horizontally within their card */
    .table-responsive {
        overflow-x: auto;
    }
}

@media (max-width: 640px) {
    .topbar {
        height: 60px;
        padding: 0 12px;
    }
    .topbar-title h1 {
        font-size: 1rem;
    }
    /* De-clutter the top bar: drop the date chip, keep the location badge */
    .topbar-actions > span:first-child {
        display: none;
    }
    .page-container {
        padding: 16px 12px;
    }
    .stats-grid {
        grid-template-columns: 1fr;
        gap: 14px;
    }
    .stat-card {
        padding: 16px;
    }
    .card-header {
        flex-wrap: wrap;
        gap: 8px;
    }
    /* Tables: comfortable min width, tap-scroll */
    .data-table {
        min-width: 640px;
    }
    /* Buttons that were forced inline wrap nicely */
    .btn {
        white-space: nowrap;
    }
    /* Page-level two-column grids collapse (inline-styled ones too) */
    [style*="grid-template-columns: 1.8fr"],
    [style*="grid-template-columns: 1.3fr"],
    [style*="grid-template-columns: 1.5fr"],
    [style*="grid-template-columns: 2fr"],
    [style*="grid-template-columns: 1fr 1fr"],
    [style*="grid-template-columns: 1fr 2fr"],
    [style*="grid-template-columns: 2fr 1fr"] {
        grid-template-columns: 1fr !important;
    }
    /* Any flex row that lays two panels side-by-side wraps on phones */
    .card-body > [style*="display: flex"][style*="gap"] {
        flex-wrap: wrap;
    }
}

/* Collapse content-heavy dashboard two-column grids a bit earlier (tablets) */
@media (max-width: 860px) {
    [style*="grid-template-columns: 1.3fr"],
    [style*="grid-template-columns: 1.8fr"] {
        grid-template-columns: 1fr !important;
    }
}

/* ============================================================
   Stack .data-table.stack-mobile into cards on phones (no scroll)
   ============================================================ */
.mobile-quick-pay-btn {
    display: none;
}

.mobile-sticky-action-bar {
    display: none;
}

@media (max-width: 768px) {
    .invoices-header-row {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 12px !important;
        margin-bottom: 16px !important;
    }
    .invoices-header-row h2 {
        font-size: 1.25rem !important;
    }
    .invoices-header-row .btn {
        width: 100% !important;
        justify-content: center !important;
    }

    .topbar {
        height: 56px !important;
        padding: 0 12px !important;
        overflow: hidden !important;
    }
    .topbar-title {
        min-width: 0 !important;
        flex: 1 1 auto !important;
    }
    .topbar-title h1 {
        font-size: 0.95rem !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }
    .topbar-actions > span:first-child {
        display: none !important;
    }
    .topbar-actions {
        min-width: 0 !important;
        flex: 0 1 auto !important;
        overflow: hidden !important;
    }
    .topbar-actions .badge {
        font-size: 0.72rem !important;
        padding: 3px 8px !important;
        max-width: 44vw !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        display: inline-block !important;
        vertical-align: middle !important;
    }

    .mobile-quick-pay-btn {
        display: inline-flex !important;
        align-items: center;
        gap: 6px;
    }

    .mobile-sticky-action-bar {
        display: block !important;
        position: fixed !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        background: rgba(255, 255, 255, 0.97) !important;
        backdrop-filter: blur(10px) !important;
        -webkit-backdrop-filter: blur(10px) !important;
        padding: 10px 16px !important;
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.14) !important;
        border-top: 1px solid #e2e8f0 !important;
        z-index: 1000 !important;
    }

    body:has(.mobile-sticky-action-bar) {
        padding-bottom: 74px !important;
    }

    .card:has(.data-table.stack-mobile) {
        border: none !important;
        background: transparent !important;
        box-shadow: none !important;
    }

    .table-responsive:has(.data-table.stack-mobile) {
        overflow-x: visible !important;
    }

    .data-table.stack-mobile {
        min-width: 0 !important;
        width: 100% !important;
        display: block !important;
    }
    .data-table.stack-mobile thead {
        display: none !important;
    }
    .data-table.stack-mobile,
    .data-table.stack-mobile tbody,
    .data-table.stack-mobile tr,
    .data-table.stack-mobile td {
        display: block !important;
        width: 100% !important;
        box-sizing: border-box !important;
    }
    .data-table.stack-mobile tr {
        border: 1px solid var(--border-color);
        border-radius: 12px;
        margin-bottom: 14px;
        padding: 10px 14px;
        background: #fff;
        box-shadow: 0 2px 6px rgba(15, 23, 42, 0.06);
        max-width: 100% !important;
        overflow: hidden !important;
    }
    .data-table.stack-mobile td {
        display: flex !important;
        align-items: flex-start !important;
        justify-content: space-between !important;
        gap: 10px !important;
        padding: 9px 0 !important;
        border-bottom: 1px solid #f1f5f9 !important;
        text-align: right !important;
        white-space: normal !important;
        width: 100% !important;
        overflow: hidden !important;
        box-sizing: border-box !important;
    }
    .data-table.stack-mobile td:last-child {
        border-bottom: 0 !important;
    }
    .data-table.stack-mobile td::before {
        content: attr(data-label);
        flex: 0 0 95px !important;
        max-width: 110px !important;
        min-width: 80px !important;
        text-align: left !important;
        font-size: 0.72rem !important;
        font-weight: 700 !important;
        text-transform: uppercase !important;
        letter-spacing: 0.03em !important;
        color: #64748b !important;
        line-height: 1.4 !important;
        flex-shrink: 0 !important;
    }
    .data-table.stack-mobile td .stack-cell-val,
    .data-table.stack-mobile td > div:not(.mobile-quick-pay-btn) {
        flex: 1 1 auto !important;
        min-width: 0 !important;
        text-align: right !important;
        word-break: break-word !important;
        overflow-wrap: break-word !important;
    }
    .data-table.stack-mobile td[data-label="Action"] {
        display: flex !important;
        flex-direction: column !important;
        gap: 8px !important;
        padding-top: 12px !important;
        border-bottom: 0 !important;
    }
    .data-table.stack-mobile td[data-label="Action"]::before {
        display: none !important;
    }
    .data-table.stack-mobile td[data-label="Action"] .btn {
        width: 100% !important;
        justify-content: center !important;
        padding: 10px 16px !important;
        font-size: 0.92rem !important;
        font-weight: 700 !important;
    }
}

/* Blinking alert for pending Tax Invoices to Issue */
@keyframes pendingInvoiceBlink {
    0%, 100% {
        border-color: #0040A0;
        box-shadow: 0 0 0 0 rgba(0, 64, 160, 0.35), 0 2px 8px rgba(0, 0, 0, 0.05);
        background-color: #ffffff;
        transform: scale(1);
    }
    50% {
        border-color: #f59e0b;
        box-shadow: 0 0 24px 5px rgba(245, 158, 11, 0.5), 0 6px 20px rgba(245, 158, 11, 0.25);
        background-color: #fffdf5;
        transform: scale(1.02);
    }
}

.stat-card.stat-card-blinking {
    animation: pendingInvoiceBlink 1.4s infinite ease-in-out !important;
    border: 2px solid #f59e0b !important;
    position: relative;
    overflow: visible;
}

.stat-card.stat-card-blinking .stat-value {
    color: #d97706 !important;
    font-weight: 800 !important;
}

.stat-card.stat-card-blinking::before {
    content: "ACTION REQUIRED";
    position: absolute;
    top: -11px;
    right: 18px;
    background: #d97706;
    color: #ffffff;
    font-size: 0.65rem;
    font-weight: 800;
    padding: 2px 9px;
    border-radius: 9999px;
    letter-spacing: 0.06em;
    box-shadow: 0 2px 8px rgba(217, 119, 6, 0.4);
    z-index: 5;
}

/* Shared Large App Grid & Tiles (Salesman & Central Office Hub) */
.app-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr); /* 2 2 2 on phone */
    gap: 16px;
    margin-bottom: 30px;
}

.app-tile {
    background: #ffffff;
    border: 1px solid var(--border-color);
    border-radius: 18px;
    padding: 22px 14px;
    display: flex; 
    flex-direction: column; 
    align-items: center; 
    text-align: center;
    text-decoration: none; 
    box-shadow: 0 4px 14px rgba(15, 23, 42, 0.05);
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: visible;
}
.app-tile:hover { 
    transform: translateY(-4px); 
    box-shadow: 0 12px 28px rgba(15, 23, 42, 0.12);
    border-color: #cbd5e1;
}

.at-icon-wrap { 
    position: relative; 
    margin-bottom: 12px;
}
.app-tile .at-icon {
    width: 86px; 
    height: 86px; 
    border-radius: 50%;
    display: flex; 
    align-items: center; 
    justify-content: center;
    color: #fff; 
    font-size: 2.3rem;
    box-shadow: 0 8px 22px rgba(0,0,0,0.14);
    transition: transform 0.2s ease;
}
.app-tile:hover .at-icon { 
    transform: scale(1.08); 
}
.app-tile .at-label { 
    font-size: 0.98rem; 
    font-weight: 800; 
    color: #0f172a !important; 
    line-height: 1.25;
    margin-bottom: 4px;
}
.app-tile .at-sub {
    font-size: 0.76rem;
    font-weight: 500;
    color: #64748b;
    line-height: 1.25;
}

.c-blue    { background: linear-gradient(135deg, #2563eb, #0040A0); }
.c-green   { background: linear-gradient(135deg, #10b981, #059669); }
.c-orange  { background: linear-gradient(135deg, #f59e0b, #d97706); }
.c-purple  { background: linear-gradient(135deg, #a855f7, #7c3aed); }
.c-teal    { background: linear-gradient(135deg, #06b6d4, #0284c7); }
.c-indigo  { background: linear-gradient(135deg, #6366f1, #4338ca); }
.c-rose    { background: linear-gradient(135deg, #f43f5e, #e11d48); }

/* Blinking / Pulse animation for Action Required Tile */
.app-tile.tile-blinking {
    animation: pendingTileBlink 1.4s infinite ease-in-out !important;
    border: 2px solid #ef4444 !important;
    background: #fffafa !important;
}

@keyframes pendingTileBlink {
    0%, 100% {
        border-color: #ef4444;
        box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4), 0 4px 14px rgba(15, 23, 42, 0.05);
        transform: scale(1);
    }
    50% {
        border-color: #dc2626;
        box-shadow: 0 0 22px 6px rgba(239, 68, 68, 0.45), 0 8px 24px rgba(220, 38, 38, 0.2);
        transform: scale(1.02);
    }
}

.at-badge-pulse {
    position: absolute;
    top: -2px;
    right: -10px;
    background: #dc2626;
    color: #ffffff;
    font-size: 0.65rem;
    font-weight: 800;
    padding: 3px 8px;
    border-radius: 9999px;
    letter-spacing: 0.05em;
    box-shadow: 0 2px 8px rgba(220, 38, 38, 0.5);
    z-index: 5;
    animation: pulseBadge 1.2s infinite;
}

@keyframes pulseBadge {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.1); }
}

@media (min-width: 680px) {
    .app-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 20px;
    }
    .app-tile {
        padding: 26px 18px;
        border-radius: 20px;
    }
    .app-tile .at-icon {
        width: 94px;
        height: 94px;
        font-size: 2.5rem;
    }
    .app-tile .at-label {
        font-size: 1.08rem;
    }
    .app-tile .at-sub {
        font-size: 0.82rem;
    }
}

@media (min-width: 1200px) {
    .app-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 20px;
    }
}

/* MD portal: lay the workspace tiles out 5-across (so ~10 fit on two rows) with
   a more compact tile. Falls back to the shared 2/3-column layout below 1100px. */
@media (min-width: 1100px) {
    .app-grid.app-grid-5 { grid-template-columns: repeat(5, 1fr); gap: 16px; }
    .app-grid.app-grid-5 .app-tile { padding: 18px 12px; border-radius: 16px; }
    .app-grid.app-grid-5 .app-tile .at-icon { width: 62px; height: 62px; font-size: 1.6rem; }
    .app-grid.app-grid-5 .app-tile .at-label { font-size: 0.92rem; }
    .app-grid.app-grid-5 .app-tile .at-sub { font-size: 0.72rem; }
}

/* ============================================================
   Global compact data tables (desktop): shrink padding & allow
   cell wrapping so wide tables on every page (salesperson, central
   office, godown, invoices) fit the viewport without a horizontal
   scroll. Mobile stacking rules above are untouched.
   ============================================================ */
@media (min-width: 901px) {
    .data-table {
        font-size: 0.86rem;
        width: 100%;
    }
    .data-table th,
    .data-table td {
        padding: 9px 11px;
    }
    .data-table th {
        white-space: nowrap;          /* headers stay on one line */
    }
    .data-table td {
        overflow-wrap: break-word;    /* wrap only genuinely long words, never split normal text */
    }
    /* Interactive/label elements must stay intact on one line - never break
       a button caption like "Tax Invoice" into single letters. */
    .data-table td .btn,
    .data-table td .badge,
    .data-table th .btn,
    .data-table th .badge {
        white-space: nowrap;
    }
    /* Long free-text columns (remarks/notes/description) may wrap hard */
    .data-table td code,
    .data-table td small {
        white-space: normal;
        overflow-wrap: break-word;
    }
}



/* ============================================================
   GLOBAL PRINT STYLES
   Strip the app chrome (sidebar, top bar, nav, buttons) so any
   page prints only its content. Invoice pages load invoice.css
   which has its own print block; this covers everything else
   (Customer Ledger statement, reports, etc.).
   ============================================================ */
@media print {
    .sidebar,
    .topbar,
    .no-print,
    .btn,
    .sidebar-toggle,
    .badge-pill {
        display: none !important;
    }
    .app-wrapper {
        display: block !important;
        min-height: 0 !important;
    }
    .main-content {
        margin-left: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        overflow: visible !important;
        background: #ffffff !important;
    }
    .page-container {
        padding: 0 !important;
        margin: 0 !important;
        max-width: 100% !important;
    }
    body {
        background: #ffffff !important;
    }
    .card,
    .stat-card {
        box-shadow: none !important;
        break-inside: avoid;
    }
}

/* Stock Quantity Styling across tables */
.stock-qty,
.text-stock-qty,
td.stock-qty,
.stock-quantity-val {
    color: #059669 !important;
    font-weight: 700;
}


/* Compact, vertically-stacked action buttons in invoice desk tables so the
   action controls stay visible without horizontal scrolling. */
.data-table td.action-cell { text-align: center; white-space: nowrap; vertical-align: top; }
.data-table th.action-col-head { text-align: center; }
.data-table .row-actions {
    display: inline-flex;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    min-width: 150px;
}
.data-table .row-actions .btn { width: 100%; justify-content: center; white-space: nowrap; }
@media (max-width: 768px) {
    .data-table .row-actions { min-width: 0; width: 100%; }
}

/* Wide invoice/data tables stay inside their card and scroll horizontally when
   they don't fit the content area, so the Action column is never clipped on
   smaller desktops/laptops. Mobile keeps the stacked-card layout. */
@media (min-width: 769px) {
    .table-responsive.sticky-head {
        overflow-x: auto;
        overflow-y: visible;
        -webkit-overflow-scrolling: touch;
    }
    /* Keep the heading row visually distinct at the top of the scroll area. */
    .table-responsive.sticky-head .data-table thead th {
        background: #f8fafc;
        box-shadow: inset 0 -1px 0 #e2e8f0;
    }
}

/* Two-tone product picker: native <select> can't colour part of an <option>, so
   we hide it (still submits + drives all JS) and render a custom list where the
   product name is black and the stock count is orange. */
.cpd-wrap { position: relative; }
.cpd-native { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.cpd-trigger {
    text-align: left;
    cursor: pointer;
    font-size: 0.82rem;
    padding: 6px 26px 6px 8px;
    background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath fill='%2364748b' d='M0 0l5 6 5-6z'/%3E%3C/svg%3E") no-repeat right 10px center;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cpd-panel {
    display: none;
    /* Fixed so the list escapes the items-table scroll/overflow clipping and
       overlays freely, the way a native <select> popup does. JS sets left/top
       from the trigger's position. */
    position: fixed; top: 0; left: 0;
    z-index: 1200; background: #fff;
    border: 1px solid #cbd5e1; border-radius: 8px;
    box-shadow: 0 12px 30px rgba(15,23,42,0.15);
    max-height: 340px; overflow-y: auto; overflow-x: hidden; padding: 4px;
    min-width: 340px; width: max-content; max-width: min(560px, 92vw);
}
.cpd-panel.open { display: block; }
.cpd-item { padding: 7px 10px; border-radius: 6px; cursor: pointer; font-size: 0.82rem; line-height: 1.3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cpd-item:hover, .cpd-item.active { background: #eff6ff; }
.cpd-name { color: #0f172a; }
.cpd-stock { color: #059669; font-weight: 600; }
.cpd-stock.oos { color: #94a3b8; font-weight: 500; }
.cpd-placeholder { color: #64748b; }

/* ============================================================
   Global responsive tables: auto-stacked data tables (class .rstack added by
   app.js) collapse into labelled cards on phones/tablets so no table needs
   horizontal scrolling. Desktop layout is unchanged.
   ============================================================ */
@media (max-width: 820px) {
    /* !important so these win over per-table rules (e.g. id selectors that set a
       min-width to force horizontal scroll on desktop). */
    table.rstack { width: 100% !important; min-width: 0 !important; table-layout: auto !important; border-collapse: collapse; }
    table.rstack thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
    table.rstack, table.rstack tbody, table.rstack tr, table.rstack td { display: block !important; width: 100% !important; min-width: 0 !important; box-sizing: border-box; }
    table.rstack col, table.rstack colgroup { width: auto !important; }
    table.rstack tr {
        border: 1px solid var(--border-color);
        border-radius: 10px;
        margin-bottom: 12px;
        padding: 8px 12px;
        background: #fff;
        box-shadow: 0 2px 6px rgba(15, 23, 42, 0.05);
    }
    table.rstack td {
        display: flex;
        justify-content: space-between;
        align-items: baseline;
        gap: 14px;
        text-align: right;
        border: none;
        border-bottom: 1px solid #f1f5f9;
        padding: 7px 0;
        white-space: normal;
        word-break: break-word;
        min-height: 0;
    }
    table.rstack td:last-child { border-bottom: 0; }
    table.rstack td::before {
        content: attr(data-label);
        flex: 0 0 42%;
        text-align: left;
        font-size: 0.7rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.02em;
        color: #64748b;
        white-space: normal;
    }
    /* A cell with no data-label (e.g. an action cell) spans full width. */
    table.rstack td:not([data-label])::before { content: ""; flex: 0; }
    table.rstack td:not([data-label]) { justify-content: flex-start; text-align: left; }
    /* The scroll wrapper no longer needs to scroll once stacked. */
    .table-responsive:has(table.rstack) { overflow: visible; }
}
