/**
 * SmartQuant Mobile-First Responsive Styles
 * Covers: layout, tables, cards, forms, nav, typography
 */

/* ============================================
   GLOBAL MOBILE BASE
   ============================================ */

/* Prevent horizontal overflow on all pages */
html, body {
    overflow-x: hidden;
    max-width: 100vw;
}

/* Shared centered shell for full-width pages that should match primary app spacing */
.page-shell {
    max-width: 1520px;
    margin: 0 auto;
    padding-left: 0.75rem;
    padding-right: 0.75rem;
}

/* Breadcrumb shell uses same width and side gutters as page content wrappers */
.breadcrumb-shell {
    max-width: 1520px;
    margin: 0 auto;
    padding-left: 0.75rem;
    padding-right: 0.75rem;
}

/* All images scale within container */
img {
    max-width: 100%;
    height: auto;
}

/* Responsive iframes/embeds */
iframe, embed, object {
    max-width: 100%;
}

/* ============================================
   BODY PADDING — ACCOUNTS FOR FIXED CHROME
   ============================================ */

/* Mobile: keep safe-area spacing only */
@media (max-width: 767px) {
    body {
        padding-bottom: env(safe-area-inset-bottom, 0px) !important;
    }

    .page-shell {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .breadcrumb-shell {
        padding-left: 1rem;
        padding-right: 1rem;
    }
}

/* ============================================
   TABLES — SCROLL WRAPPER ON MOBILE
   ============================================ */

/* All Bootstrap tables get scroll wrapper on mobile */
@media (max-width: 767px) {
    .table-responsive-auto {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        border-radius: 0.5rem;
    }

    /* Give non-wrapped tables scroll fallback */
    .table:not(.no-mobile-scroll) {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
    }

    /* Shrink table text on small screens */
    .table td,
    .table th {
        font-size: 0.78rem;
        padding: 0.4rem 0.5rem;
    }

    /* Chatbot table */
    .chatbot-data-table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
    }
}

/* ============================================
   BOOTSTRAP GRID — MOBILE STACKING FIXES
   ============================================ */

/* col-xl-* without col-12 fallback should stack */
@media (max-width: 1199px) {
    [class*="col-xl-"]:not([class*="col-lg-"]):not([class*="col-md-"]):not([class*="col-sm-"]):not([class*="col-12"]) {
        flex: 0 0 100%;
        max-width: 100%;
    }
}

@media (max-width: 991px) {
    [class*="col-lg-"]:not([class*="col-md-"]):not([class*="col-sm-"]):not([class*="col-12"]) {
        flex: 0 0 100%;
        max-width: 100%;
    }
}

@media (max-width: 767px) {
    [class*="col-md-"]:not([class*="col-sm-"]):not([class*="col-12"]) {
        flex: 0 0 100%;
        max-width: 100%;
    }

    /* Stack all 2-4 col Bootstrap grids on small screens */
    .row > [class*="col-"]:not(.col-12):not(.col-sm-6):not(.col-6) {
        flex: 0 0 100%;
        max-width: 100%;
    }
}

/* ============================================
   CARDS — MOBILE PADDING & SPACING
   ============================================ */

@media (max-width: 767px) {
    .card,
    .card-body {
        padding: 1rem !important;
    }

    .card-header {
        padding: 0.75rem 1rem !important;
        font-size: 0.9rem;
    }

    /* Reduce gap between stacked cards */
    .row.g-3,
    .row.g-4,
    .row.gy-4 {
        --bs-gutter-y: 0.75rem;
    }
}

/* ============================================
   TYPOGRAPHY — SCALE DOWN ON MOBILE
   ============================================ */

@media (max-width: 767px) {
    h1, .h1 { font-size: 1.5rem !important; }
    h2, .h2 { font-size: 1.25rem !important; }
    h3, .h3 { font-size: 1.1rem !important; }
    h4, .h4 { font-size: 1rem !important; }

    /* Large stat numbers */
    .stat-value,
    .metric-value,
    .display-4,
    .display-5 {
        font-size: 1.5rem !important;
    }
}

/* ============================================
   BUTTONS — MOBILE SIZING
   ============================================ */

@media (max-width: 767px) {
    /* Don't make ALL buttons full width (overrides components.css) — only utility blocks */
    .btn-block-mobile {
        width: 100%;
        display: block;
    }

    /* Revert the components.css full-width rule for buttons in navs/dropdowns */
    .btn {
        width: auto;
    }

    /* Touch-friendly minimum tap size */
    .btn, a.btn {
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
}

/* ============================================
   FORMS — MOBILE INPUT SIZING
   ============================================ */

@media (max-width: 767px) {
    .form-control,
    .form-select,
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    select,
    textarea {
        font-size: 16px !important; /* Prevents iOS zoom on focus */
        min-height: 44px;
    }

    label {
        font-size: 0.85rem;
        font-weight: 600;
    }
}

/* ============================================
   NAVIGATION
   ============================================ */

@media (max-width: 767px) {
    /* Mobile hamburger button tap target */
    #mobile-menu-btn {
        min-width: 44px;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Mobile menu full width */
    .mobile-menu {
        max-height: calc(100vh - 5rem);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Mobile menu links — larger tap targets */
    .mobile-menu a,
    .mobile-menu button {
        min-height: 44px;
        display: flex;
        align-items: center;
    }
}

/* ============================================
   CONTAINER PADDING ON MOBILE
   ============================================ */

@media (max-width: 767px) {
    .container,
    .container-fluid {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    /* Section top padding reduction */
    .py-5 { padding-top: 2rem !important; padding-bottom: 2rem !important; }
    .py-4 { padding-top: 1rem !important; padding-bottom: 1rem !important; }
    .px-6 { padding-left: 1rem !important; padding-right: 1rem !important; }
    .p-6  { padding: 1rem !important; }
}

/* ============================================
   FLEX LAYOUT — STACK ON MOBILE
   ============================================ */

@media (max-width: 767px) {
    /* Items that should wrap or stack on mobile */
    .flex-mobile-col {
        flex-direction: column !important;
    }

    .gap-mobile-sm {
        gap: 0.5rem !important;
    }

    /* Header/toolbar rows with search+buttons */
    .filter-row,
    .toolbar-row,
    .page-header-row {
        flex-direction: column;
        align-items: stretch;
        gap: 0.5rem;
    }

    .filter-row .btn,
    .toolbar-row .btn,
    .page-header-row .btn {
        width: 100%;
    }
}

/* ============================================
   MODALS — FULL-SCREEN ON MOBILE
   ============================================ */

@media (max-width: 767px) {
    .modal-dialog {
        margin: 0 !important;
        max-width: 100% !important;
    }

    .modal-content {
        border-radius: 1rem 1rem 0 0 !important;
        position: fixed !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        max-height: 90vh;
        overflow-y: auto;
    }

    .modal-body {
        padding: 1rem !important;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* ============================================
   CHATBOT — MOBILE POSITIONING
   ============================================ */

@media (max-width: 767px) {
    #chatbot-widget {
        bottom: 12px !important;
        right: 12px !important;
    }

    #chatbot-window {
        position: fixed !important;
        bottom: 150px !important;
        left: 8px !important;
        right: 8px !important;
        width: auto !important;
        max-width: 100% !important;
        height: 60vh !important;
        min-height: 320px !important;
    }
}

/* ============================================
   FOOTER — MOBILE PADDING
   ============================================ */

@media (max-width: 767px) {
    footer {
        text-align: center;
    }

    footer .flex-col.md\:flex-row {
        flex-direction: column;
        gap: 0.75rem;
    }
}

/* ============================================
   STAT CARDS / METRIC GRIDS
   ============================================ */

@media (max-width: 767px) {
    /* 2-col grid for stat cards on mobile (not 1-col) */
    .stats-grid,
    .metrics-grid {
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: 0.5rem !important;
    }
}

/* ============================================
   SCROLLABLE TABS / PILL NAVS
   ============================================ */

@media (max-width: 767px) {
    .nav-tabs,
    .nav-pills {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        -ms-overflow-style: none;
        gap: 0.25rem;
        padding-bottom: 2px;
    }

    .nav-tabs::-webkit-scrollbar,
    .nav-pills::-webkit-scrollbar {
        display: none;
    }

    .nav-tabs .nav-item,
    .nav-pills .nav-item {
        flex-shrink: 0;
    }

    .nav-tabs .nav-link,
    .nav-pills .nav-link {
        white-space: nowrap;
        font-size: 0.8rem;
        padding: 0.4rem 0.75rem;
    }
}

/* ============================================
   BADGE & TAG OVERFLOW
   ============================================ */

@media (max-width: 767px) {
    .badge-row,
    .tag-row {
        flex-wrap: wrap;
        gap: 0.25rem;
    }
}

/* ============================================
   BREADCRUMB ON MOBILE
   ============================================ */

@media (max-width: 767px) {
    #breadcrumbs {
        font-size: 0.75rem;
    }

    #breadcrumb-list {
        flex-wrap: wrap;
        gap: 0.1rem;
    }
}

/* ============================================
   TOUCH IMPROVEMENTS
   ============================================ */

/* Prevent double-tap zoom on buttons/links */
a, button, [role="button"] {
    touch-action: manipulation;
}

/* Smooth scroll */
html {
    scroll-behavior: smooth;
}

/* ============================================
   SCROLLABLE HORIZONTAL DATA CONTAINERS
   ============================================ */

@media (max-width: 767px) {
    /* Any div containing a table gets scroll */
    .table-scroll,
    .results-table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Tables in card bodies that aren't already wrapped */
    .card-body > .table,
    .adv-panel .table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
    }
}

/* ============================================
   OPPORTUNITY DETAIL — SIDE-BY-SIDE COLLAPSE
   ============================================ */

@media (max-width: 767px) {
    .opportunity-side-panel {
        position: static !important;
        width: 100% !important;
        margin-top: 1rem;
    }
}

/* ============================================
   HERO SECTION ACTION BUTTONS — STACK ON MOBILE
   ============================================ */

@media (max-width: 767px) {
    /* All hero CTA button rows become column */
    .col-xl-5.d-flex,
    .col-xl-5.d-flex.justify-content-xl-end {
        justify-content: stretch !important;
        flex-direction: column !important;
        gap: 0.5rem !important;
        margin-top: 1rem;
    }

    .col-xl-5.d-flex > a,
    .col-xl-5.d-flex > button {
        width: 100% !important;
        justify-content: center !important;
    }

    /* Hero text: reduce padding on mobile */
    [class*="-hero"] {
        padding: 1rem !important;
        border-radius: 1rem !important;
    }

    /* Hero h2 font clamp becomes more conservative */
    [class*="-hero"] h2 {
        font-size: 1.35rem !important;
        letter-spacing: -0.02em !important;
    }

    [class*="-hero"] p.fs-5 {
        font-size: 0.9rem !important;
    }
}
