/* ================================================================
   CRM MOBILE RESPONSIVE — Complete Override (mobile.css)
   Loaded AFTER style.css — overrides all conflicts
   Tested: 320px → 992px
   ================================================================ */

/* ============================================================
   SECTION 1: BASE TABLE — Universal fix (ALL screen sizes)
   ============================================================ */

/* Table wrapper: always scrollable */
.table-wrapper {
    width: 100%;
    overflow-x: auto;
    overflow-y: visible;
    -webkit-overflow-scrolling: touch;
    position: relative;
    background: var(--card-bg);
}

/* Tables: never collapse, always use min-width */
.data-table {
    width: 100%;
    min-width: 640px;          /* Ensures scroll on small screens */
    border-collapse: collapse;
    white-space: nowrap;       /* Prevent text wrapping in cells */
}

.data-table thead th {
    white-space: nowrap;
    padding: 12px 14px;
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    background: var(--body-bg);
    border-bottom: 2px solid var(--border-color);
    vertical-align: middle;
}

.data-table tbody td {
    padding: 13px 14px;
    border-bottom: 1px solid var(--border-color);
    font-size: 14px;
    color: var(--text-dark);
    vertical-align: middle;
    white-space: nowrap;
    max-width: 240px;
    overflow: hidden;
    text-overflow: ellipsis;
}

.data-table tbody td .td-multi {
    white-space: normal;       /* Allow multi-line inside cells that need it */
}

/* Right-scroll fade indicator */
.table-wrapper::after {
    content: '';
    position: absolute;
    top: 0; right: 0;
    width: 36px; height: 100%;
    background: linear-gradient(to right, transparent, rgba(0,0,0,0.07));
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s;
    border-radius: 0 var(--radius) var(--radius) 0;
}
.table-wrapper.scrollable::after { opacity: 1; }

/* ============================================================
   SECTION 2: GRIDS — Responsive at correct breakpoints
   ============================================================ */

/* Dashboard stat grid: 2-col on tablet */
@media (max-width: 1100px) {
    .grid-4 {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 14px !important;
    }
}

/* All grids: single column on mobile */
@media (max-width: 768px) {
    .grid-2,
    .grid-3,
    .grid-4 {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }
}

/* ============================================================
   SECTION 3: 992px — Tablet / Hamburger menu mode
   ============================================================ */

@media (max-width: 992px) {
    /* Page content: correct padding */
    .page-content {
        padding: 16px 14px;
    }

    /* Page header */
    .page-header {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
        margin-bottom: 18px;
    }
    .page-header > .btn,
    .page-header > div > .btn,
    .page-header > a {
        width: 100%;
        justify-content: center;
        text-align: center;
    }
    .page-header > div[style*="display:flex"],
    .page-header > div[style*="gap"] {
        flex-direction: column;
        gap: 8px !important;
    }

    /* Card header */
    .card-header {
        padding: 14px 16px;
        gap: 10px;
    }
    .card-body {
        padding: 16px;
    }

    /* Table full-bleed on tablet */
    .table-wrapper {
        border-radius: 0;
        margin: 0;
    }

    /* Stat cards */
    .stat-card {
        padding: 16px 18px;
        gap: 12px;
    }
    .stat-icon {
        width: 46px;
        height: 46px;
        font-size: 20px;
    }
    .stat-info .stat-value {
        font-size: 24px;
    }
    .stat-info .stat-label {
        font-size: 12px;
    }

    /* Filter form: wrap */
    .filter-form {
        flex-wrap: wrap;
        gap: 10px;
    }
    .filter-search {
        flex: 1;
        min-width: 180px;
        max-width: 100%;
    }
    .filter-select {
        width: auto;
        min-width: 140px;
    }
    .filter-actions {
        display: flex;
        gap: 8px;
    }

    /* Form grid: 2→1 col */
    .form-grid-2 {
        grid-template-columns: 1fr !important;
    }
}

/* ============================================================
   SECTION 4: 768px — Mobile
   ============================================================ */

@media (max-width: 768px) {
    .page-content {
        padding: 14px 12px;
    }

    /* Topbar */
    .topbar {
        padding: 0 12px;
        gap: 8px;
        height: 56px;
    }
    .topbar-title {
        font-size: 15px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        flex: 1;
    }
    .topbar-user span {
        display: none;
    }

    /* Card */
    .card {
        border-radius: 10px;
    }
    .card-header {
        padding: 12px 14px;
        flex-wrap: wrap;
    }
    .card-header .card-title {
        font-size: 14px;
    }
    .card-header .btn-sm {
        font-size: 12px;
        padding: 5px 10px;
    }
    .card-body {
        padding: 14px;
    }

    /* Tables on mobile — sticky action column */
    .data-table {
        min-width: 560px;
    }
    .data-table thead th,
    .data-table tbody td {
        padding: 10px 12px;
        font-size: 13px;
    }

    /* Sticky last column (Actions) — always visible while scrolling */
    .data-table thead th:last-child,
    .data-table tbody td:last-child {
        position: sticky;
        right: 0;
        background: var(--card-bg);
        box-shadow: -4px 0 8px rgba(0,0,0,0.06);
        z-index: 3;
    }
    .data-table thead th:last-child {
        background: var(--body-bg);
    }

    /* btn-icon: bigger touch targets */
    .btn-icon {
        width: 36px;
        height: 36px;
        font-size: 15px;
    }

    /* Filter: full stack */
    .filter-card-body {
        padding: 12px 14px;
    }
    .filter-form {
        flex-direction: column;
        align-items: stretch;
    }
    .filter-search,
    .filter-select {
        max-width: 100%;
        width: 100%;
    }
    .filter-actions {
        display: flex;
        gap: 8px;
        width: 100%;
    }
    .filter-actions .btn {
        flex: 1;
        justify-content: center;
    }

    /* Badges: slightly smaller */
    .badge {
        font-size: 11px;
        padding: 3px 8px;
    }

    /* Tabs: scrollable on mobile */
    .tab-nav {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
        flex-wrap: nowrap;
        padding-bottom: 0;
    }
    .tab-btn {
        white-space: nowrap;
        padding: 10px 16px;
        font-size: 13px;
    }

    /* Info rows */
    .info-row {
        flex-direction: column !important;
        gap: 4px !important;
    }

    /* View action buttons */
    .view-actions {
        flex-wrap: wrap !important;
        gap: 8px !important;
    }
    .view-actions .btn {
        flex: 1 1 auto;
        min-width: 110px;
        justify-content: center;
    }
}

/* ============================================================
   SECTION 5: 600px — Small phone
   ============================================================ */

@media (max-width: 600px) {
    .page-content {
        padding: 12px 10px;
    }

    .page-header {
        margin-bottom: 14px;
    }
    .page-header h1 {
        font-size: 18px !important;
    }
    .page-header p {
        font-size: 12px !important;
    }
    .page-header > .btn,
    .page-header > a.btn {
        width: 100% !important;
        justify-content: center;
        min-height: 46px;
    }

    /* Touch targets */
    .btn {
        min-height: 46px;
        padding: 10px 16px;
    }
    .form-control,
    select.form-control,
    textarea.form-control {
        font-size: 16px !important;  /* Prevents iOS auto-zoom */
        min-height: 46px;
        padding: 10px 12px;
    }
    textarea.form-control {
        min-height: 90px;
    }

    /* Forms: full width */
    .form-group {
        margin-bottom: 14px;
    }
    .form-grid-2 {
        grid-template-columns: 1fr !important;
        gap: 0 !important;
    }

    /* Card */
    .card-body { padding: 12px; }
    .card-header { padding: 12px; }

    /* Stat cards on mobile: horizontal compact layout */
    .stat-card {
        padding: 14px 16px;
        gap: 12px;
    }
    .stat-icon {
        width: 42px;
        height: 42px;
        font-size: 18px;
        border-radius: 10px;
    }
    .stat-info .stat-value {
        font-size: 22px;
    }
    .stat-info .stat-label {
        font-size: 12px;
    }

    /* Table: minimal padding */
    .data-table {
        min-width: 480px;
    }
    .data-table thead th,
    .data-table tbody td {
        padding: 9px 10px;
        font-size: 12px;
    }

    /* Modal: bottom sheet on phone */
    .modal-overlay {
        align-items: flex-end !important;
        padding: 0 !important;
    }
    .modal-box {
        width: 100% !important;
        max-width: 100% !important;
        border-radius: 20px 20px 0 0 !important;
        margin: 0 !important;
        max-height: 92vh;
        overflow-y: auto;
    }
    .modal-body {
        padding: 16px !important;
    }
    .modal-footer {
        padding: 12px 16px !important;
        gap: 8px;
    }
    .modal-footer .btn {
        flex: 1;
        justify-content: center;
    }

    /* Pagination: compact */
    .pagination {
        gap: 4px;
        flex-wrap: wrap;
        justify-content: center;
    }
    .page-link {
        min-width: 34px;
        height: 34px;
        font-size: 13px;
        padding: 0;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Item builder (Quotation/Invoice) */
    .item-row {
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
    }
    .item-row > *:nth-child(1) { grid-column: 1 / -1; }
    .item-row > *:nth-child(5) {
        grid-column: 2;
        justify-self: end;
    }
    .item-labels-row { display: none !important; }

    /* Totals box: full width */
    .totals-section,
    .totals-box,
    div[style*="max-width:320px"],
    div[style*="max-width: 320px"] {
        max-width: 100% !important;
        width: 100% !important;
    }

    /* Channel radios: stack */
    .channel-radios {
        flex-direction: column !important;
        gap: 6px !important;
    }

    /* Activity feed */
    .activity-item,
    .appt-dash-row {
        padding: 10px 12px;
        gap: 10px;
    }
    .appt-dash-icon {
        width: 28px;
        height: 28px;
        font-size: 11px;
    }

    /* Doc grid: 2 cols */
    .doc-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px;
    }
    .doc-card {
        padding: 12px;
    }

    /* Summary rows in view pages */
    .summary-row {
        flex-direction: column !important;
        gap: 4px;
    }
    .summary-label {
        font-weight: 600;
        color: var(--text-muted);
        font-size: 12px;
        text-transform: uppercase;
    }
}

/* ============================================================
   SECTION 6: 400px — Very small (SE, Galaxy A mini)
   ============================================================ */

@media (max-width: 400px) {
    .page-content {
        padding: 10px 8px;
    }

    .data-table {
        min-width: 420px;
    }

    .stat-card {
        padding: 12px;
        gap: 10px;
    }
    .stat-info .stat-value {
        font-size: 20px;
    }

    .btn-icon {
        width: 32px;
        height: 32px;
        font-size: 13px;
    }

    .item-row {
        grid-template-columns: 1fr !important;
    }
    .item-row > *:nth-child(5) {
        grid-column: 1 !important;
        justify-self: start !important;
    }

    .doc-grid {
        grid-template-columns: 1fr !important;
    }

    .badge {
        font-size: 10px;
        padding: 2px 6px;
    }
}

/* ============================================================
   SECTION 7: SETTINGS PAGE — Mobile fixes
   ============================================================ */

@media (max-width: 768px) {
    /* Settings tabs: scrollable */
    .settings-tabs,
    .tab-nav {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        padding-bottom: 2px;
    }
    .settings-tabs::-webkit-scrollbar { display: none; }

    /* Permission table */
    .permission-table {
        min-width: 400px;
    }

    /* SMTP section */
    .smtp-row {
        flex-direction: column;
        gap: 8px;
    }
}

/* ============================================================
   SECTION 8: FORMS — All create/edit pages
   ============================================================ */

@media (max-width: 768px) {
    /* Two-col form grids → single col */
    .form-grid-2 {
        grid-template-columns: 1fr !important;
        gap: 0 !important;
    }

    /* Submit buttons: full width */
    .form-actions {
        flex-direction: column;
        gap: 10px;
    }
    .form-actions .btn {
        width: 100%;
        justify-content: center;
    }

    /* Date inputs: prevent overflow */
    input[type="date"],
    input[type="time"] {
        font-size: 16px;  /* Prevents iOS zoom */
    }
}

/* ============================================================
   SECTION 9: LOGIN PAGE — Comprehensive mobile fix
   ============================================================ */

/* Base login styles already in login.php inline CSS.
   These override and enhance for mobile. */
@media (max-width: 480px) {
    .login-card {
        padding: 28px 20px !important;
        border-radius: 16px !important;
        margin: 8px !important;
    }
    .login-logo .logo-icon {
        width: 64px !important;
        height: 64px !important;
        border-radius: 16px !important;
    }
    .login-logo h1 {
        font-size: 17px !important;
    }
    .login-logo p {
        font-size: 12px !important;
    }
    .input-wrap input {
        font-size: 16px !important;
        padding: 13px 14px 13px 42px !important;
        min-height: 50px;
    }
    .btn-login {
        padding: 14px !important;
        font-size: 15px !important;
        min-height: 50px;
    }
}

/* ============================================================
   SECTION 10: PWA STANDALONE + SAFE AREA
   ============================================================ */

@media all and (display-mode: standalone) {
    body {
        padding-top: env(safe-area-inset-top, 0);
        padding-bottom: env(safe-area-inset-bottom, 0);
    }
    .sidebar {
        padding-top: env(safe-area-inset-top, 0);
        height: calc(100vh + env(safe-area-inset-top, 0));
    }
    .topbar {
        padding-top: max(env(safe-area-inset-top, 0px), 8px);
        min-height: calc(64px + env(safe-area-inset-top, 0px));
    }
    /* All tappable elements */
    a, button, .btn, .btn-icon, .nav-link, .stat-card {
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
        cursor: pointer;
    }
}

/* ============================================================
   SECTION 11: PWA INSTALL BUTTON
   ============================================================ */

.pwa-install-nav {
    display: none;  /* Hidden by default */
}
.pwa-install-nav.pwa-visible {
    display: block !important;
}
.pwa-install-nav.pwa-hidden {
    display: none !important;
}

/* ============================================================
   SECTION 12: DARK MODE — Table sticky column fix
   ============================================================ */

[data-theme="dark"] .data-table thead th:last-child {
    background: #1e293b;
}
[data-theme="dark"] .data-table tbody td:last-child {
    background: #111827;
    box-shadow: -4px 0 8px rgba(0,0,0,0.2);
}

/* ============================================================
   SECTION 13: UTILITY CLASSES
   ============================================================ */

/* Mobile-only show/hide */
.show-mobile { display: none !important; }
@media (max-width: 768px) {
    .show-mobile { display: block !important; }
    .hide-mobile { display: none !important; }
}

/* Overflow-safe flex children */
.flex-min-0 { min-width: 0; }

/* Touch-friendly clickable */
.touch-target {
    min-height: 44px;
    min-width: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}


/* ============================================================
   SECTION 14: PAGE HEADER ACTIONS — Mobile button stacking
   ============================================================ */

@media (max-width: 600px) {
    .page-header-actions {
        display: flex !important;
        flex-direction: column !important;
        width: 100%;
        gap: 8px !important;
    }
    .page-header-actions .btn {
        width: 100% !important;
        justify-content: center !important;
    }

    /* Inline flex divs inside page-header: also stack */
    .page-header > div[style*="display:flex"],
    .page-header > div[style*="display: flex"] {
        flex-direction: column !important;
        width: 100% !important;
        gap: 8px !important;
    }
    .page-header > div[style*="display:flex"] > .btn,
    .page-header > div[style*="display: flex"] > .btn,
    .page-header > div[style*="display:flex"] > a,
    .page-header > div[style*="display: flex"] > a {
        width: 100% !important;
        justify-content: center !important;
    }
}

/* ============================================================
   SECTION 15: REPORTS PAGE — Mobile fixes
   ============================================================ */

@media (max-width: 768px) {
    .report-card {
        padding: 14px;
    }
    .report-filters {
        flex-direction: column;
        gap: 10px;
    }
    .report-filters select,
    .report-filters input {
        width: 100%;
    }
}

/* ============================================================
   SECTION 16: APPOINTMENTS — Calendar / Timeline fixes
   ============================================================ */

@media (max-width: 768px) {
    .timeline-item {
        flex-direction: column !important;
        gap: 6px;
    }
    .timeline-time {
        font-size: 12px;
        min-width: unset;
    }
    .appt-card {
        padding: 12px;
    }
}

/* ============================================================
   SECTION 17: CLIENTS VIEW PAGE — Info grid
   ============================================================ */

@media (max-width: 600px) {
    .client-info-grid {
        grid-template-columns: 1fr !important;
    }
    .info-row {
        padding: 10px 0;
        flex-direction: row !important;
        justify-content: space-between;
        align-items: flex-start;
        gap: 8px;
        flex-wrap: wrap;
    }
    .info-label {
        font-size: 12px;
        color: var(--text-muted);
        font-weight: 600;
        text-transform: uppercase;
        min-width: 100px;
        flex-shrink: 0;
    }
    .info-value {
        font-size: 13px;
        text-align: right;
        flex: 1;
        word-break: break-word;
    }
}

