/* =====================================================================
   Responsive layout & UI polish (loaded after app.css)
   Breakpoints: phone <= 640px, tablet <= 1024px (sidebar becomes a drawer)
   ===================================================================== */

:root {
    --color-border: #2b3a52;
    --focus-ring: 0 0 0 3px rgba(59, 130, 246, 0.35);
    --page-gutter: 1.5rem;
}

html { -webkit-text-size-adjust: 100%; }
body { overflow-x: hidden; }
code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.8125rem;
    background: rgba(148, 163, 184, 0.12);
    padding: 0.1rem 0.4rem;
    border-radius: var(--radius-sm);
}

/* Scrollbars */
* { scrollbar-width: thin; scrollbar-color: var(--color-bg-tertiary) transparent; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--color-bg-tertiary); border-radius: 8px; }
::-webkit-scrollbar-track { background: transparent; }

/* Keyboard focus */
.btn:focus-visible, .nav-item:focus-visible, .page-btn:focus-visible { outline: none; box-shadow: var(--focus-ring); }

/* ---- Sidebar ---- */
.sidebar { box-shadow: 1px 0 0 rgba(0, 0, 0, 0.2); }
.nav-item {
    margin: 0.125rem 0.75rem;
    padding: 0.65rem 0.9rem;
    border-left: none;
    border-radius: var(--radius-md);
    font-weight: 500;
}
.nav-item:hover { background: rgba(148, 163, 184, 0.08); }
.nav-item.active {
    background: var(--color-accent-light);
    color: #93c5fd;
    box-shadow: inset 3px 0 0 var(--color-accent);
}
.nav-item svg { flex-shrink: 0; }
.nav-section { margin-bottom: 1rem; }
.nav-section-title { padding: 0.5rem 1.6rem; }
.sidebar-close { display: none; margin-left: auto; }

.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(2, 6, 23, 0.6);
    backdrop-filter: blur(2px);
    z-index: 90;
    border: none;
    animation: fadeIn var(--transition-fast);
}

/* ---- Topbar ---- */
.topbar {
    gap: 0.75rem;
    background: rgba(30, 41, 59, 0.92);
    backdrop-filter: blur(8px);
}
.topbar-left { display: flex; align-items: center; gap: 0.5rem; min-width: 0; }
.topbar-title {
    font-size: 1.125rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.menu-toggle { display: none; }
.user-info .user-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 220px; }

.main-content { min-width: 0; }
.page-content {
    padding: var(--page-gutter);
    width: 100%;
    max-width: 1600px;
    margin: 0 auto;
    min-width: 0;
}

/* Page headings inside pages (they carry inline font sizes) */
.page-content h1 { font-size: clamp(1.15rem, 2.5vw, 1.5rem) !important; line-height: 1.3; }

/* Headers & toolbars wrap instead of overflowing */
.flex.justify-between { flex-wrap: wrap; gap: 0.75rem; }
.card-header { flex-wrap: wrap; gap: 0.75rem; }
.card { box-shadow: var(--shadow-md); }

/* ---- Buttons ---- */
.btn { white-space: nowrap; line-height: 1.25; min-height: 38px; }
.btn-sm { min-height: 32px; }
.btn-icon { min-height: 0; flex-shrink: 0; }
.btn:disabled, .btn[disabled] { opacity: 0.5; cursor: not-allowed; }
.btn-outline {
    background: transparent;
    color: var(--color-text-primary);
    border: 1px solid var(--color-bg-tertiary);
}
.btn-outline:hover { background: var(--color-bg-tertiary); }

/* ---- Forms ---- */
.form-input, .form-select, .form-textarea {
    border: 1px solid var(--color-border);
    background-color: #273449;
    min-height: 42px;
}
.form-input:hover, .form-select:hover, .form-textarea:hover { border-color: #3b4d6b; }
.form-select {
    appearance: none;
    -webkit-appearance: none;
    padding-right: 2.25rem;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.75rem center;
}
.form-select option { background: var(--color-bg-secondary); color: var(--color-text-primary); }
.form-input:disabled, .form-select:disabled { opacity: 0.6; cursor: not-allowed; }
input[type="checkbox"], input[type="radio"] { accent-color: var(--color-accent); }
.form-group small { display: block; margin-top: 0.35rem; font-size: 0.78rem; }

/* ---- Tables ---- */
.table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    /* edge shadows hint that the table scrolls sideways */
    background:
        linear-gradient(to right, var(--color-surface) 30%, rgba(30, 41, 59, 0)) left center / 40px 100% no-repeat local,
        linear-gradient(to left, var(--color-surface) 30%, rgba(30, 41, 59, 0)) right center / 40px 100% no-repeat local,
        radial-gradient(farthest-side at 0 50%, rgba(0, 0, 0, 0.45), transparent) left center / 14px 100% no-repeat scroll,
        radial-gradient(farthest-side at 100% 50%, rgba(0, 0, 0, 0.45), transparent) right center / 14px 100% no-repeat scroll;
}
.data-table th, .data-table td { padding: 0.8rem 1rem; vertical-align: middle; }
.data-table th { white-space: nowrap; background: #2a3a52; }
.data-table tbody tr:last-child td { border-bottom: none; }

/* ---- Badges ---- */
.badge { white-space: nowrap; line-height: 1.4; }
.badge.primary { background: var(--color-accent-light); color: #93c5fd; }
.badge.secondary { background: rgba(148, 163, 184, 0.14); color: var(--color-text-secondary); }
.badge-success { background: rgba(16, 185, 129, 0.12); color: var(--color-success); }
.badge-warning { background: rgba(245, 158, 11, 0.12); color: var(--color-warning); }
.badge-danger { background: rgba(239, 68, 68, 0.12); color: var(--color-danger); }

/* ---- Alerts ---- */
.alert { padding: 0.75rem 1rem; border-radius: var(--radius-md); border: 1px solid transparent; margin-bottom: 1rem; }
.alert-warning { background: rgba(245, 158, 11, 0.1); border-color: rgba(245, 158, 11, 0.3); color: #fcd34d; }

/* ---- Stats ---- */
.stats-grid { grid-template-columns: repeat(auto-fit, minmax(min(100%, 210px), 1fr)); gap: 1rem; }
.stat-card { transition: transform var(--transition-fast), border-color var(--transition-fast); }
.stat-card:hover { transform: translateY(-2px); border-color: #475569; }

/* ---- Home layout ---- */
.home-grid { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }
.quick-actions { display: flex; flex-direction: column; gap: 0.75rem; }
.quick-actions .btn { justify-content: flex-start; }

/* ---- Utility classes the pages use that were never defined ---- */
.d-block, .block { display: block; }
.justify-center { justify-content: center; }
.justify-end { justify-content: flex-end; }
.flex-wrap { flex-wrap: wrap; }
.gap-1 { gap: 0.25rem; }
.gap-4 { gap: 1.5rem; }
.text-xs { font-size: 0.75rem; }
.text-sm { font-size: 0.875rem; }
.text-lg { font-size: 1.125rem; }
.font-medium { font-weight: 500; }
.font-semibold { font-weight: 600; }
.text-gray-500 { color: var(--color-text-secondary); }
.text-gray-900 { color: var(--color-text-primary); }
.grid > * { min-width: 0; }
.grid-cols-1 { grid-template-columns: repeat(1, minmax(0, 1fr)); }
.grid-cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (min-width: 640px) {
    .sm\:grid-cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .sm\:col-span-1 { grid-column: span 1 / span 1; }
    .sm\:col-span-2 { grid-column: span 2 / span 2; }
}
@media (min-width: 768px) {
    .md\:grid-cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ---- Wide forms (package / subscription with the screens tree) ---- */
.card.form-wide { max-width: 1100px !important; }

/* ---- Tablet & below: the sidebar becomes an off-canvas drawer ---- */
@media (max-width: 1024px) {
    .sidebar {
        transform: translateX(-100%);
        transition: transform var(--transition-normal);
        width: min(85vw, var(--sidebar-width));
        box-shadow: var(--shadow-xl);
    }
    .layout.nav-open .sidebar { transform: translateX(0); }
    .layout.nav-open .sidebar-backdrop { display: block; }
    .sidebar-close { display: inline-flex; }
    .menu-toggle { display: inline-flex; }
    .main-content { margin-left: 0; }
    .home-grid { grid-template-columns: minmax(0, 1fr); }
    .grid-cols-3, .grid-cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ---- Phones ---- */
@media (max-width: 640px) {
    :root { --page-gutter: 0.875rem; --header-height: 56px; }
    .topbar { padding: 0 0.75rem; }
    .user-info .user-name { display: none; }

    .grid-cols-2, .grid-cols-3, .grid-cols-4 { grid-template-columns: minmax(0, 1fr); }
    .grid.gap-3 { column-gap: 0; }
    .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.75rem; }
    .stat-card { padding: 1rem; }
    .stat-icon { width: 38px; height: 38px; margin-bottom: 0.6rem; }
    .stat-icon svg { width: 20px; height: 20px; }
    .stat-value { font-size: 1.5rem; }
    .stat-label { font-size: 0.8rem; }

    .card-header { padding: 0.9rem 1rem; }
    .card-body { padding: 1rem; }
    .card[style*="max-width"] { max-width: none !important; }

    /* toolbar controls with fixed inline widths become full width */
    .card-header > .search-input,
    .card-header > .form-select,
    .card-header > .form-input,
    .card-header > [style*="width"],
    .search-input[style*="width"] { width: 100% !important; max-width: none !important; flex: 1 1 100%; }

    /* page header: title, then a full-width primary action */
    .page-content > .flex.justify-between > .btn,
    .page-content > .flex.justify-between > a.btn { flex: 1 1 100%; }

    /* form action rows: stacked full-width buttons, primary on top */
    form .flex.gap-3.mt-4 { flex-direction: column; }
    form .flex.gap-3.mt-4 > .btn { width: 100%; }

    .data-table th, .data-table td { padding: 0.65rem 0.75rem; font-size: 0.82rem; }
    .data-table td { white-space: nowrap; }

    .form-input, .form-select, .form-textarea { font-size: 16px; } /* stops iOS zoom on focus */
    .empty-state { padding: 2rem 1rem; }

    /* Payments page */
    .filter-input { width: 100% !important; }
    .pay-filters { flex-wrap: wrap; }
}

/* ---- Payments page (has its own page styles; these selectors are more specific) ---- */
.page-content .pay-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.page-content .pay-header { flex-wrap: wrap; gap: 1rem; }
.page-content .pay-pagination { flex-wrap: wrap; gap: 0.75rem; }
@media (max-width: 1024px) {
    .page-content .pay-root { padding: 0; min-height: 0; background: transparent; }
}
@media (max-width: 640px) {
    .page-content .pay-title { font-size: 1.5rem; }
    .page-content .pay-header { margin-bottom: 1.25rem; padding-bottom: 1rem; }
    .page-content .pay-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.75rem; }
    .page-content .pay-filters > * { flex: 1 1 100%; }
    .page-content .date-filter-wrap { flex-wrap: wrap; }
    .page-content input[type="date"].date-input { width: auto; flex: 1; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
