/* ================================================================
   SkyConnect CRM — Mobile Responsive CSS
   Targets phones (<768px) and small phones (<576px)
   Works with the existing Bootstrap 5 + sky-* CSS variable system
   ================================================================ */

/* ── Base typography: slightly larger for mobile readability ── */
@media (max-width: 768px) {
    body {
        font-size: 1rem;
        -webkit-text-size-adjust: 100%;
    }
    .sky-content p, .sky-content td, .sky-content li {
        font-size: 0.95rem;
        line-height: 1.55;
    }
}

/* ================================================================
   SIDEBAR: Collapsible overlay on mobile
   ================================================================ */

/* Hamburger toggle: only visible on mobile */
.mobile-hamburger {
    display: none;
    background: none;
    border: none;
    color: var(--sky-text);
    font-size: 1.4rem;
    padding: 6px 10px;
    cursor: pointer;
    z-index: 1080;
    line-height: 1;
    min-width: 44px;
    min-height: 44px;
    align-items: center;
    justify-content: center;
}

@media (max-width: 768px) {
    .mobile-hamburger {
        display: flex;
        position: fixed;
        top: 6px;
        left: 6px;
        z-index: 1080;
        background: var(--sky-surface);
        border-radius: 8px;
        border: 1px solid var(--sky-border);
        box-shadow: var(--ss-shadow-sm, 0 2px 8px rgba(0,0,0,0.15));
    }

    /* Sidebar: slide-in overlay on mobile */
    .app-layout.sidebar-mode .side-nav,
    .app-layout .side-nav {
        position: fixed;
        top: 0;
        left: -280px;
        width: 270px;
        min-width: 270px;
        height: 100vh;
        z-index: 1075;
        transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: none;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        background: var(--sky-surface);
        padding-top: 60px;
    }

    .side-nav.mobile-open {
        left: 0;
        box-shadow: var(--ss-shadow-sm, 4px 0 24px rgba(0,0,0,0.35));
    }

    /* Overlay backdrop */
    .mobile-sidebar-backdrop {
        display: none;
        position: fixed;
        inset: 0;
        background: var(--ss-scrim, rgba(0, 0, 0, 0.5));
        z-index: 1074;
        opacity: 0;
        transition: opacity 0.3s ease;
    }
    .mobile-sidebar-backdrop.active {
        display: block;
        opacity: 1;
    }

    /* Ensure sidebar items have proper touch targets */
    .side-nav-item {
        min-height: 44px;
        padding: 0.65rem 1.25rem;
        font-size: 0.9rem;
    }
}

/* ================================================================
   KANBAN BOARD: Horizontal scroll + single column option
   ================================================================ */
@media (max-width: 768px) {
    .kanban-board {
        gap: 0.75rem;
        padding: 0 0.25rem 1rem 0.25rem;
        scroll-snap-type: x mandatory;
        -webkit-overflow-scrolling: touch;
    }
    .kanban-column {
        min-width: 260px;
        max-width: 85vw;
        scroll-snap-align: start;
    }

    /* Single column view toggle: when active, stack vertically */
    .kanban-board.mobile-stack {
        flex-direction: column;
        overflow-x: visible;
    }
    .kanban-board.mobile-stack .kanban-column {
        min-width: 100%;
        max-width: 100%;
        scroll-snap-align: none;
    }
    .kanban-board.mobile-stack .kanban-cards {
        max-height: 300px;
        overflow-y: auto;
    }

    /* Kanban cards: more finger-friendly */
    .kanban-card {
        padding: 0.85rem;
        margin: 0.5rem;
    }

    /* View toggle buttons */
    .view-toggle .btn {
        min-width: 44px;
        min-height: 44px;
        padding: 0.4rem 0.6rem;
    }
}

/* ================================================================
   CONTACT/DEAL LISTS: Card layout on mobile instead of tables
   ================================================================ */
@media (max-width: 768px) {
    /* Hide the table on mobile, show card list */
    .contacts-table-wrap .table-responsive,
    .deals-table-wrap .table-responsive,
    .tasks-table-wrap .table-responsive {
        display: none;
    }

    .mobile-card-list {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
    }

    .mobile-card-item {
        background: var(--sky-surface);
        border: 1px solid var(--sky-border);
        border-radius: 10px;
        padding: 0.85rem 1rem;
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
        transition: border-color 0.15s ease;
        cursor: pointer;
    }
    .mobile-card-item:hover,
    .mobile-card-item:active {
        border-color: var(--sky-primary);
    }

    .mobile-card-header {
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }

    .mobile-card-name {
        font-weight: 600;
        font-size: 0.95rem;
        flex: 1;
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .mobile-card-meta {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
        font-size: 0.82rem;
        color: var(--sky-text-muted);
    }
    .mobile-card-meta span {
        display: flex;
        align-items: center;
        gap: 0.3rem;
    }

    /* On desktop, hide the mobile card list */
    .mobile-card-list { display: none; }
}
@media (min-width: 769px) {
    .mobile-card-list { display: none !important; }
}
@media (max-width: 768px) {
    .mobile-card-list { display: flex; }
}

/* ================================================================
   DASHBOARD: Stack widgets vertically
   ================================================================ */
@media (max-width: 768px) {
    .dashboard-grid,
    .row.g-3,
    .row.g-4 {
        flex-direction: column;
    }
    .dashboard-grid > .col-md-6,
    .dashboard-grid > .col-md-4,
    .dashboard-grid > .col-md-3,
    .dashboard-grid > .col-lg-6,
    .dashboard-grid > .col-lg-4,
    .dashboard-grid > .col-lg-3 {
        width: 100%;
        flex: 0 0 100%;
        max-width: 100%;
    }

    .stat-card {
        padding: 1rem;
    }
    .stat-value {
        font-size: 1.4rem;
    }

    /* Section cards full width */
    .section-card {
        margin-bottom: 0.75rem;
    }
}

/* ================================================================
   TIMELINE / ACTIVITIES: Full-width on mobile
   ================================================================ */
@media (max-width: 768px) {
    .timeline-wrap,
    .activity-feed {
        padding: 0;
    }
    .timeline-item,
    .activity-item {
        padding: 0.75rem;
        margin-bottom: 0.5rem;
    }
    .activity-icon {
        width: 36px;
        height: 36px;
        font-size: 0.9rem;
    }
}

/* ================================================================
   FORMS & MODALS: Full-screen on mobile
   ================================================================ */
@media (max-width: 768px) {
    .modal-dialog {
        margin: 0;
        max-width: 100%;
        min-height: 100vh;
    }
    .modal-content {
        border-radius: 0;
        min-height: 100vh;
        border: none;
    }
    .modal-body {
        padding: 1rem;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    .modal-header {
        position: sticky;
        top: 0;
        z-index: 10;
        background: var(--sky-surface);
    }
    .modal-footer {
        position: sticky;
        bottom: 0;
        z-index: 10;
        background: var(--sky-surface);
    }

    /* Form inputs larger for touch */
    .form-control, .form-select,
    .sky-input, .sky-select, .sky-textarea {
        font-size: 1rem;
        padding: 0.6rem 0.75rem;
        min-height: 44px;
    }

    /* Multi-value field rows: stack on very small */
    .mv-row {
        flex-wrap: wrap;
    }
    .mv-row .mv-type-select {
        width: 100%;
        margin-bottom: 0.35rem;
    }
    .mv-address-fields {
        grid-template-columns: 1fr;
    }
}

/* ================================================================
   TOUCH TARGETS: Minimum 44x44px for all interactive elements
   ================================================================ */
@media (max-width: 768px) {
    a, button, .btn,
    .nav-link, .side-nav-item,
    .form-check-input,
    .clickable-row td,
    .dropdown-item,
    .pagination .page-link {
        min-height: 44px;
    }

    .btn {
        min-height: 44px;
        padding: 0.5rem 1rem;
        font-size: 0.9rem;
    }

    .btn-sm {
        min-height: 38px;
        padding: 0.35rem 0.75rem;
        font-size: 0.85rem;
    }

    .dropdown-item {
        padding: 0.65rem 1rem;
        font-size: 0.9rem;
    }

    /* SSC.000609: the 44px min-height above also hit checkboxes, so every
       checkbox on a phone drew as a 22x44 empty box (Prospects' "Show
       archived" and each card's select box). A checkbox stays square; its
       label (or the card around it) is the larger tap target. */
    .form-check-input {
        width: 22px;
        height: 22px;
        min-height: 0;
    }

    .pagination .page-link {
        min-width: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Quick action buttons */
    .quick-action-btn {
        width: 44px;
        height: 44px;
    }
}

/* ================================================================
   BOTTOM NAV BAR: Quick actions on mobile only
   ================================================================ */
.mobile-bottom-nav {
    display: none;
}

@media (max-width: 768px) {
    .mobile-bottom-nav {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 1060;
        background: var(--sky-surface);
        border-top: 1px solid var(--sky-border);
        padding: 0.35rem 0;
        justify-content: space-around;
        align-items: center;
        box-shadow: var(--ss-shadow-sm, 0 -2px 12px rgba(0,0,0,0.15));
        -webkit-backdrop-filter: blur(10px);
        backdrop-filter: blur(10px);
    }

    .mobile-bottom-nav a {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 0.15rem;
        text-decoration: none;
        color: var(--sky-text-muted);
        font-size: 0.68rem;
        font-weight: 500;
        padding: 0.3rem 0.5rem;
        min-width: 56px;
        min-height: 48px;
        justify-content: center;
        border-radius: 8px;
        transition: all 0.15s;
    }
    .mobile-bottom-nav a:hover,
    .mobile-bottom-nav a:active,
    .mobile-bottom-nav a.active {
        color: var(--sky-primary);
        background: rgba(var(--sky-primary-rgb), 0.08);
    }
    .mobile-bottom-nav a i {
        font-size: 1.2rem;
    }

    /* Pad the body so content is not hidden behind bottom nav */
    body {
        padding-bottom: 64px;
    }

    /* Also ensure modals and command palette are above bottom nav */
    .cmd-palette-overlay {
        z-index: 10000;
    }
}

/* ================================================================
   SPLIT PANEL: Full-width on mobile
   ================================================================ */
@media (max-width: 768px) {
    #splitPanelWrapper {
        flex-direction: column;
    }
    #splitPanelWrapper.split-active #listPane {
        display: none;
    }
    #splitPanelWrapper.split-active #detailPane {
        width: 100%;
        position: relative;
    }

    /* Back button for split panel on mobile */
    .split-back-btn {
        display: inline-flex;
    }
}
@media (min-width: 769px) {
    .split-back-btn {
        display: none;
    }
}

/* ================================================================
   SEARCH BAR: Full width on mobile
   ================================================================ */
@media (max-width: 768px) {
    .search-bar {
        width: 100%;
    }
    .search-bar input {
        font-size: 1rem;
    }

    /* Filters: wrap onto next line */
    .filter-bar,
    .d-flex.gap-2.align-items-center {
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    .filter-select {
        flex: 1;
        min-width: calc(50% - 0.5rem);
    }
}

/* ================================================================
   HEADER ACTIONS: Stack on mobile
   ================================================================ */
@media (max-width: 768px) {
    .page-header {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
    }
    .page-header .d-flex {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    /* Page title area */
    h2 {
        font-size: 1.25rem;
    }
    h3 {
        font-size: 1.1rem;
    }
}

/* ================================================================
   FLASH / TOAST on mobile: center-bottom
   ================================================================ */
@media (max-width: 768px) {
    .flash-container {
        top: auto;
        bottom: 72px;
        left: 10px;
        right: 10px;
        max-width: none;
    }
    .sky-toast {
        min-width: auto;
        width: 100%;
    }
}

/* ================================================================
   RICH EDITOR: full-width on mobile
   ================================================================ */
@media (max-width: 768px) {
    .rich-editor-wrap {
        border-radius: 8px;
    }
    .rich-editor-body {
        min-height: 120px;
        font-size: 1rem;
    }
    .rich-editor-toolbar button {
        width: 36px;
        height: 36px;
    }
}

/* ================================================================
   COMMAND PALETTE: Adjust for mobile
   ================================================================ */
@media (max-width: 768px) {
    .cmd-palette-overlay {
        padding-top: 5vh;
    }
    .cmd-palette {
        max-width: calc(100vw - 1rem);
        max-height: 80vh;
        margin: 0 0.5rem;
        border-radius: 12px;
    }
}

/* ================================================================
   TABLES: Responsive improvements
   ================================================================ */
@media (max-width: 768px) {
    .table-responsive {
        -webkit-overflow-scrolling: touch;
        border-radius: 8px;
    }

    /* Make table rows breathe on mobile */
    .table td, .table th {
        white-space: nowrap;
    }

    /* Hide less important columns on mobile */
    .hide-mobile {
        display: none;
    }
}

/* ================================================================
   NOTIFICATION DROPDOWN: Full-width on mobile
   ================================================================ */
@media (max-width: 768px) {
    #notifDropdown {
        position: fixed !important;
        left: 0 !important;
        right: 0 !important;
        top: auto !important;
        bottom: 64px;
        width: 100% !important;
        max-height: 60vh;
        border-radius: 12px 12px 0 0;
    }
}

/* ================================================================
   HOVER CARD: Hidden on mobile (use tap instead)
   ================================================================ */
@media (max-width: 768px) {
    .hover-card { display: none !important; }
}

/* ================================================================
   ONBOARDING BAR: Full width on mobile
   ================================================================ */
@media (max-width: 768px) {
    .onboarding-bar {
        flex-direction: column;
        padding: 0.75rem;
        gap: 0.5rem;
    }
}

/* ================================================================
   PULL-TO-REFRESH INDICATOR (visual only, JS required)
   ================================================================ */
@media (max-width: 768px) {
    .pull-refresh-indicator {
        position: fixed;
        top: 0;
        left: 50%;
        transform: translateX(-50%) translateY(-60px);
        z-index: 9999;
        background: var(--sky-surface);
        border: 1px solid var(--sky-border);
        border-radius: 50%;
        width: 40px;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: var(--ss-shadow-sm, 0 2px 12px rgba(0,0,0,0.15));
        transition: transform 0.3s ease;
    }
    .pull-refresh-indicator.visible {
        transform: translateX(-50%) translateY(10px);
    }
}

/* ================================================================
   UTILITY: Smooth scrolling for mobile
   ================================================================ */
@media (max-width: 768px) {
    * {
        -webkit-tap-highlight-color: rgba(var(--sky-primary-rgb), 0.1);
    }

    /* Prevent horizontal scroll on the whole page */
    html, body {
        overflow-x: hidden;
    }

    /* Card shadow adjustments for mobile */
    .sky-card {
        border-radius: 10px;
    }
}

/* ================================================================
   PRINT: Hide mobile elements when printing
   ================================================================ */
@media print {
    .mobile-bottom-nav,
    .mobile-hamburger,
    .mobile-sidebar-backdrop {
        display: none !important;
    }
}
