/* ============================================================
   ClearDeck Responsive CSS — v1.0
   Comprehensive responsive framework for all breakpoints.
   Load AFTER cockpit.css and per-page <style> blocks.
   ============================================================ */

/* ──────────────────────────────────────────────────────────
   BASE RESPONSIVE OVERRIDES
   Ensure no horizontal overflow on any screen
   ────────────────────────────────────────────────────────── */

html, body {
    max-width: 100vw;
    overflow-x: hidden;
}

/* Ensure images/media never overflow */
img, video, canvas, svg, iframe {
    max-width: 100%;
}

/* Universal table wrapper: horizontal scroll */
.table-scroll-wrapper,
.table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
}

/* ──────────────────────────────────────────────────────────
   PHONE: 0 – 480px
   Single column, compact, thumb-reachable
   ────────────────────────────────────────────────────────── */

@media (max-width: 480px) {

    /* -- Nav -- */
    .nav {
        padding: 0 12px !important;
        height: 52px !important;
    }

    .nav-left {
        gap: 12px !important;
    }

    .nav-logo {
        font-size: 1.1rem !important;
    }

    .nav-links {
        display: none !important;
    }

    .nav-links.open {
        display: flex !important;
        top: 52px !important;
    }

    .nav-mobile-toggle {
        display: block !important;
    }

    /* -- Main container -- */
    .main,
    main,
    .main-content,
    .content-wrapper,
    .app-container {
        padding-left: 12px !important;
        padding-right: 12px !important;
        padding-top: 64px !important;
    }

    body.cd-has-bottom-bar .main,
    body.cd-has-bottom-bar main,
    body.cd-has-bottom-bar .main-content,
    body.cd-has-bottom-bar .app-container {
        padding-bottom: 100px !important;
    }

    /* -- Page titles -- */
    .page-title {
        font-size: 1.3rem !important;
    }

    .page-header {
        margin-bottom: 20px !important;
    }

    .section-header {
        margin-bottom: 12px !important;
    }

    .section-title {
        font-size: 1rem !important;
    }

    /* -- Cards -- */
    .card {
        padding: 14px !important;
        border-radius: 10px !important;
    }

    .card-value {
        font-size: 1.3rem !important;
    }

    /* -- Grids: collapse to 1-2 columns -- */
    .summary-grid {
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
    }

    .currency-grid,
    .two-col,
    .three-col,
    .planner-layout,
    .wb-layout,
    .main-grid,
    .analysis-grid,
    .checklist-layout {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }

    .stats-bar,
    .stats-grid,
    .stats-row {
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
    }

    .field-row,
    .form-row.cols-2 {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }

    .field-row-3,
    .form-row.cols-3 {
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
    }

    .form-row.cols-4 {
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
    }

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

    .totals-bar {
        grid-template-columns: 1fr 1fr !important;
        gap: 6px !important;
    }

    .weather-details {
        grid-template-columns: 1fr 1fr !important;
        gap: 6px !important;
    }

    /* -- Readback columns (ATC page) -- */
    .readback-cols {
        grid-template-columns: 1fr !important;
    }

    /* -- Frequency chips (ATC page) -- */
    .frequency-chips {
        flex-wrap: wrap !important;
        gap: 6px !important;
    }

    .freq-chip {
        font-size: 12px !important;
        padding: 6px 10px !important;
    }

    /* -- Tables: add horizontal scroll -- */
    table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        width: 100%;
    }

    table thead,
    table tbody,
    table tr {
        display: table;
        width: 100%;
        table-layout: auto;
    }

    /* Fix table display when wrapped in scrollable container */
    .table-scroll-wrapper table,
    .table-wrapper table {
        display: table;
        min-width: 600px;
    }

    /* Logbook table compact */
    .flights-table td,
    .flights-table th {
        padding: 6px 4px !important;
        font-size: 11px !important;
    }

    .logbook-table td,
    .logbook-table th {
        padding: 6px 4px !important;
        font-size: 11px !important;
        white-space: nowrap;
    }

    /* -- Bottom bar: QAB compact on phones -- */
    #cd-qab {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        -ms-overflow-style: none;
    }

    #cd-qab::-webkit-scrollbar {
        display: none;
    }

    .cd-qab-item {
        min-width: 50px !important;
        padding: 0 6px !important;
    }

    .cd-qab-label {
        font-size: 8px !important;
    }

    .cd-qab-val {
        font-size: 11px !important;
    }

    /* -- Bottom nav: smaller labels, ensure all fit -- */
    #cd-bottom-nav {
        height: 52px !important;
    }

    .cd-nav-link {
        gap: 2px !important;
        min-height: 44px !important;
    }

    .cd-nav-label {
        font-size: 8px !important;
    }

    .cd-nav-icon {
        width: 20px !important;
        height: 20px !important;
    }

    .cd-nav-icon svg {
        width: 18px !important;
        height: 18px !important;
    }

    /* -- Modals & popups -- */
    .modal,
    .modal-content,
    [class*="modal"],
    [class*="popup"],
    [class*="dialog"] {
        max-width: 95vw !important;
        margin: 8px !important;
    }

    /* -- Tabs -- */
    .tabs,
    .tab-bar,
    .mode-tabs,
    .info-tabs,
    .filter-tabs {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap !important;
        scrollbar-width: none;
    }

    .tabs::-webkit-scrollbar,
    .tab-bar::-webkit-scrollbar,
    .mode-tabs::-webkit-scrollbar,
    .info-tabs::-webkit-scrollbar,
    .filter-tabs::-webkit-scrollbar {
        display: none;
    }

    .tab,
    .tab-btn,
    .mode-tab,
    .mode-btn,
    .filter-btn {
        white-space: nowrap !important;
        flex-shrink: 0 !important;
    }

    /* -- Map page -- */
    #map,
    .map-container,
    [id*="map"] {
        min-height: 50vh;
    }

    .map-sidebar,
    .map-panel,
    .sidebar {
        position: fixed !important;
        bottom: 90px !important;
        left: 0 !important;
        right: 0 !important;
        top: auto !important;
        max-height: 50vh !important;
        border-radius: 12px 12px 0 0 !important;
        z-index: 200 !important;
        overflow-y: auto !important;
    }

    /* -- Buttons -- */
    .btn,
    .btn-small,
    .btn-primary,
    .action-btn {
        font-size: 13px !important;
    }

    /* -- Split screen: stack on phones -- */
    .cd-split-container {
        flex-direction: column !important;
    }

    .cd-split-handle {
        width: 100% !important;
        height: 6px !important;
        cursor: row-resize !important;
    }

    .cd-split-handle::before {
        width: 40px !important;
        height: 2px !important;
    }

    .cd-split-panel {
        min-width: 100% !important;
        min-height: 200px !important;
    }

    /* -- Cockpit instruments grid -- */
    .instruments-grid,
    .gauges-grid {
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
    }
}

/* ──────────────────────────────────────────────────────────
   SMALL PHONE: 0 – 360px (iPhone SE, older devices)
   Extra compact
   ────────────────────────────────────────────────────────── */

@media (max-width: 360px) {

    .main,
    main,
    .main-content,
    .app-container {
        padding-left: 8px !important;
        padding-right: 8px !important;
    }

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

    .stats-bar,
    .stats-grid,
    .stats-row,
    .totals-bar {
        grid-template-columns: 1fr !important;
    }

    .card {
        padding: 12px !important;
    }

    .page-title {
        font-size: 1.15rem !important;
    }

    .cd-qab-time {
        display: none !important;
    }

    /* Hide some QAB items to save space */
    .cd-qab-item.cd-qab-hdg {
        display: none !important;
    }

    #cd-bottom-nav {
        height: 48px !important;
    }

    .cd-nav-label {
        font-size: 7px !important;
    }
}

/* ──────────────────────────────────────────────────────────
   PHABLET / SMALL TABLET: 481px – 768px
   2-column layouts, nav hamburger visible
   ────────────────────────────────────────────────────────── */

@media (min-width: 481px) and (max-width: 768px) {

    .nav-links {
        display: none !important;
    }

    .nav-links.open {
        display: flex !important;
    }

    .nav-mobile-toggle {
        display: block !important;
    }

    .main,
    main,
    .app-container {
        padding-left: 16px !important;
        padding-right: 16px !important;
    }

    .summary-grid {
        grid-template-columns: 1fr 1fr !important;
        gap: 10px !important;
    }

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

    .two-col,
    .planner-layout,
    .wb-layout,
    .main-grid,
    .analysis-grid {
        grid-template-columns: 1fr !important;
    }

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

    .stats-bar {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .field-row-3 {
        grid-template-columns: 1fr 1fr !important;
    }

    .form-row.cols-4 {
        grid-template-columns: 1fr 1fr !important;
    }

    .totals-bar {
        grid-template-columns: repeat(3, 1fr) !important;
    }
}

/* ──────────────────────────────────────────────────────────
   TABLET PORTRAIT: 769px – 1024px
   Nav via bottom bar, 2-3 column grids
   ────────────────────────────────────────────────────────── */

@media (min-width: 769px) and (max-width: 1024px) {

    .nav {
        padding: 0 20px !important;
        height: 54px !important;
    }

    /* Hide top nav links — bottom bar handles navigation */
    .nav-links {
        display: none !important;
    }

    .nav-mobile-toggle {
        display: none !important;
    }

    .main,
    main,
    .app-container {
        padding-left: 20px !important;
        padding-right: 20px !important;
    }

    .summary-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .currency-grid {
        grid-template-columns: repeat(3, 1fr) !important;
    }

    .two-col,
    .planner-layout {
        grid-template-columns: 1fr 1fr !important;
    }

    .three-col {
        grid-template-columns: repeat(3, 1fr) !important;
    }

    .stats-bar {
        grid-template-columns: repeat(4, 1fr) !important;
    }

    .totals-bar {
        grid-template-columns: repeat(3, 1fr) !important;
    }

    /* Tables: horizontal scroll */
    .table-scroll-wrapper,
    .table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* ──────────────────────────────────────────────────────────
   DESKTOP: 1025px+
   Full layout, sidebar + content
   ────────────────────────────────────────────────────────── */

@media (min-width: 1025px) {

    .nav-mobile-toggle {
        display: none !important;
    }

    .nav-links {
        display: flex !important;
    }

    .summary-grid {
        grid-template-columns: repeat(4, 1fr) !important;
    }
}

/* ──────────────────────────────────────────────────────────
   LANDSCAPE PHONE / iPHONE LANDSCAPE
   Compact bottom bar, optimized for thumb reach
   ────────────────────────────────────────────────────────── */

@media (orientation: landscape) and (max-height: 500px) {

    .main,
    main,
    .app-container {
        padding-top: 56px !important;
    }

    body.cd-has-bottom-bar .main,
    body.cd-has-bottom-bar main {
        padding-bottom: 52px !important;
    }

    /* QAB: single row, compact */
    #cd-bottom-bar {
        flex-direction: row !important;
        height: 44px !important;
    }

    #cd-qab {
        flex: 1 !important;
        border-bottom: none !important;
        border-right: 1px solid rgba(255,255,255,0.05) !important;
        height: 44px !important;
        overflow-x: auto !important;
    }

    #cd-bottom-nav {
        height: 44px !important;
        width: auto !important;
        min-width: 280px !important;
        flex-shrink: 0 !important;
    }

    .cd-nav-label {
        display: none !important;
    }

    .cd-nav-icon {
        width: 20px !important;
        height: 20px !important;
    }
}

/* ──────────────────────────────────────────────────────────
   LANDSCAPE TABLET (iPad landscape)
   ────────────────────────────────────────────────────────── */

@media (orientation: landscape) and (min-height: 501px) and (max-width: 1366px) {

    .two-col,
    .planner-layout,
    .wb-layout,
    .main-grid,
    .analysis-grid {
        grid-template-columns: 1fr 1fr !important;
    }
}

/* ──────────────────────────────────────────────────────────
   TOUCH DEVICE ENHANCEMENTS
   ────────────────────────────────────────────────────────── */

@media (pointer: coarse) {

    /* All interactive elements: 44px minimum */
    a, button, [role="button"],
    .tab, .tab-btn, .mode-tab, .mode-btn,
    .filter-btn, .action-btn, .cd-nav-link,
    .freq-chip, .cd-qab-item {
        min-height: 44px;
    }

    /* Prevent iOS Safari zoom on input focus */
    input, select, textarea {
        font-size: 16px !important;
    }

    /* Larger checkboxes and radios */
    input[type="checkbox"],
    input[type="radio"] {
        width: 20px !important;
        height: 20px !important;
    }

    /* Tap highlight removal for clean feel */
    a, button, [role="button"] {
        -webkit-tap-highlight-color: transparent;
    }
}

/* ──────────────────────────────────────────────────────────
   SAFE AREA (iPhone notch, home indicator)
   ────────────────────────────────────────────────────────── */

@supports (padding-bottom: env(safe-area-inset-bottom)) {

    body.cd-has-bottom-bar {
        padding-bottom: calc(90px + env(safe-area-inset-bottom, 0px));
    }

    /* Landscape compact override */
    @media (orientation: landscape) and (max-height: 500px) {
        body.cd-has-bottom-bar {
            padding-bottom: calc(44px + env(safe-area-inset-bottom, 0px));
        }
    }
}

/* ──────────────────────────────────────────────────────────
   PRINT STYLES
   ────────────────────────────────────────────────────────── */

@media print {
    .nav,
    #cd-bottom-bar,
    .nav-mobile-toggle {
        display: none !important;
    }

    body {
        padding: 0 !important;
        background: white !important;
        color: black !important;
    }

    .card {
        border: 1px solid #ccc !important;
        background: white !important;
    }
}
