/* ── Tablet ── */
@media (max-width: 1100px) {
    .stats-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .content-grid,
    .detail-grid {
        grid-template-columns: 1fr;
    }
}

/* ── Mobile ── */
@media (max-width: 900px) {

    #add-line-item-form {
        grid-template-columns: 1fr !important;
    }
    /* NEW: shipment cost form is grid-based, so flex-wrap does not apply */
    #add-cost-form {
        grid-template-columns: 1fr !important;
        width: 100%;
    }

    #add-cost-form .form-group {
        min-width: 0;
        width: 100%;
    }

    #add-cost-form input,
    #add-cost-form select,
    #add-cost-form button {
        width: 100%;
        min-width: 0;
    }

    #add-cost-form button {
        height: 44px !important;
        margin-top: 4px;
    }

    /* keep these existing forms flexible */
    #generate-doc-form,
    #upload-doc-form,
    #add-milestone-form {
        flex-wrap: wrap;
    }

    #generate-doc-form select {
        min-width: 0;
        flex: 1 1 110px;
    }
    /* ── Bottom Navigation Bar ── */
    .bottom-nav {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 60px;
        background: var(--sidebar-bg);
        border-top: 1px solid rgba(255,255,255,0.08);
        z-index: 30;
        justify-content: space-around;
        align-items: center;
        padding: 0 4px;
        padding-bottom: env(safe-area-inset-bottom, 0);
    }

    .bottom-nav__item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        flex: 1;
        padding: 6px 0;
        color: var(--sidebar-text);
        text-decoration: none;
        font-size: 0.68rem;
        font-weight: 600;
        opacity: 0.65;
        transition: opacity 0.15s;
        -webkit-tap-highlight-color: transparent;
    }

    .bottom-nav__item--active,
    .bottom-nav__item:active {
        opacity: 1;
        color: var(--sidebar-active);
    }

    .bottom-nav__icon {
        font-size: 1.25rem;
        line-height: 1;
    }

    .bottom-nav__label {
        line-height: 1;
    }

    .bottom-nav__badge {
        position: absolute;
        top: 2px;
        right: calc(50% - 18px);
        min-width: 16px;
        height: 16px;
        background: var(--danger);
        color: #fff;
        font-size: 0.6rem;
        font-weight: 700;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0 4px;
        line-height: 1;
    }

    /* Push page content above the bottom nav */
    .page-content {
        padding-bottom: 72px;
    }

    /* Toast above bottom nav */
    .toast-root {
        bottom: 72px;
    }

    /* Auth layout */
    .auth-shell {
        grid-template-columns: 1fr;
    }

    .auth-brand {
        display: none;
    }

    .auth-panel {
        padding: 24px 16px;
        min-height: 100vh;
        align-items: flex-start;
        padding-top: 60px;
        background:
            radial-gradient(circle at top center, rgba(29, 78, 216, 0.08), transparent 50%),
            linear-gradient(180deg, #f8fbff 0%, #f5f7fb 100%);
    }

    .auth-card {
        padding: 28px 20px;
        max-width: 100%;
    }

    .auth-logo {
        font-size: 1.6rem;
    }

    .auth-card h2 {
        font-size: 1.4rem;
    }

    /* Sidebar: hidden off-screen, slides in on toggle */
    .sidebar {
        left: -100%;
        width: min(86vw, 320px);
        transition: left 0.25s ease;
    }

    .sidebar.is-open {
        left: 0;
    }

    /* Backdrop when sidebar is open */
    .sidebar.is-open ~ .app-main::before {
        content: "";
        position: fixed;
        inset: 0;
        background: rgba(15, 23, 42, 0.4);
        z-index: 15;
    }

    /* Main content takes full width on mobile */
    .app-main {
        margin-left: 0;
    }

    /* Show hamburger */
    .mobile-menu-btn {
        display: inline-flex;
    }

    /* Show logout in sidebar on mobile */
    .sidebar-logout {
        display: flex;
    }

    /* Topbar stacks on mobile */
    .topbar {
        flex-wrap: wrap;
        padding: 12px 16px;
    }

    .topbar__actions {
        width: 100%;
    }

    .topbar__actions input {
        min-width: 0;
        flex: 1;
    }

    .page-title {
        font-size: 1.3rem;
    }

    /* Tables scroll horizontally */
    .table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

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

    /* Dispatch inline forms stack */
    .dispatch-inline {
        flex-wrap: wrap;
    }

    .dispatch-inline select,
    .dispatch-inline input {
        min-width: 100px;
        flex: 1;
    }

    /* Tabs scroll horizontally */
    .dispatch-tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        width: 100%;
    }

    .dispatch-tab {
        white-space: nowrap;
        flex-shrink: 0;
    }

    /* Pagination wraps */
    .pagination {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
    }

    .pagination__controls {
        justify-content: center;
        flex-wrap: wrap;
    }

    .pagination__info {
        justify-content: center;
    }

    /* Notification panel full width */
    .notification-panel {
        right: 8px;
        left: 8px;
        width: auto;
    }

    /* Profile panel on mobile */
    .profile-panel {
        right: 8px;
        width: 180px;
    }

    /* Donut chart stacks */
    .donut-chart {
        flex-direction: column;
        align-items: center;
    }

    /* Currency input stays inline */
    .currency-input {
        flex-wrap: nowrap;
    }

    /* Chip bar wraps tighter */
    .chip-bar {
        gap: 6px;
    }

    .chip {
        font-size: 0.8rem;
        padding: 4px 10px;
    }

    /* STT overlay card smaller */
    .stt-card {
        padding: 24px 20px;
        min-width: auto;
        margin: 0 16px;
    }
}

/* ── Small phone ── */
@media (max-width: 640px) {
    .page-content {
        padding: 12px;
    }

    .stats-grid {
        grid-template-columns: 1fr;
    }

    .stat-card h3 {
        font-size: 1.5rem;
    }

    .stat-card__icon {
        font-size: 1.2rem;
        top: 12px;
        right: 12px;
    }

    .panel__header {
        padding: 14px 16px;
        flex-wrap: wrap;
        gap: 8px;
        display: inline-grid;
    }

    .panel__body {
        padding: 14px 16px;
        display: inline-grid;
    }

    .detail-grid {
        grid-template-columns: 1fr;
    }

    .auth-card {
        padding: 20px 16px;
    }

    .bar-chart {
        gap: 8px;
    }

    .bar-chart__bar {
        max-width: 32px;
    }

    .topbar {
        padding: 10px 12px;
        min-height: auto;
    }

    .btn {
        min-height: 40px;
        padding: 0 14px;
        font-size: 0.88rem;
    }

    .btn--sm {
        min-height: 34px;
        padding: 0 10px;
    }

    .form-group input,
    .form-group textarea,
    .form-group select {
        padding: 10px 12px;
        font-size: 0.9rem;
        width: 100%;
        box-sizing: border-box;
    }

    /* STT mic button smaller */
    .stt-mic {
        width: 32px;
        height: 32px;
        font-size: 0.85rem;
    }

    .form-group .stt-mic {
        bottom: 6px;
        right: 2px;
    }

    .form-group input,
    .form-group textarea {
        padding-right: 38px;
    }
}


/* ── Dashboard mobile fixes ── */
@media (max-width: 900px) {
    /* Action items card */
    .alert-item {
        font-size: 0.84rem;
        padding: 10px 12px;
    }

    /* Stat cards: ensure icon doesn't overlap text */
    .stat-card {
        padding: 16px 14px;
        padding-right: 48px;
    }

    .stat-card h3 {
        font-size: 1.4rem;
        word-break: break-word;
    }

    .stat-card p {
        font-size: 0.8rem;
    }

    /* Content grid: single column on mobile */
    .content-grid {
        grid-template-columns: 1fr;
    }

    /* Shipment detail summary cards — horizontal scroll on mobile */
    .sd-summary {
        gap: 10px;
        scroll-snap-type: x mandatory;
        scrollbar-width: none;
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        margin-bottom: 80px;
    }
    .sd-summary::-webkit-scrollbar { display: none; }

    .sd-card {
        flex: 0 0 140px;
        min-width: 140px;
        padding: 12px 10px;
        scroll-snap-align: start;
    }

    .sd-card__icon { font-size: 1.2rem; margin-bottom: 4px; }
    .sd-card h4 { font-size: 0.72rem; }
    .sd-card p { font-size: 0.88rem; }
    .sd-card .muted { font-size: 0.75rem; }
    .sd-card:hover { transform: none; box-shadow: none; }

    /* Progress stepper: horizontal row on mobile, smaller */
    .sp-steps {
        grid-template-columns: repeat(4, 1fr);
        gap: 4px;
    }

    .sp-dot {
        width: 36px;
        height: 36px;
    }

    .sp-dot__icon {
        font-size: 0.9rem;
    }

    .sp-label {
        font-size: 0.7rem;
    }

    /* Tracking map responsive height */
    #tracking-map {
        height: 220px !important;
    }

    .tracking-live-map {
        height: 260px;
    }

    /* Driver performance cards */
    .kpi-mini__value {
        font-size: 1.1rem;
    }

    /* Toast narrower on mobile */
    .toast-root {
        left: 12px;
        right: 12px;
        bottom: 12px;
    }

    .toast {
        min-width: auto;
        max-width: 100%;
    }
}

@media (max-width: 640px) {
    /* Bar chart smaller */
    .bar-chart {
        height: 120px;
    }

    /* Donut chart smaller */
    .donut-chart__ring {
        width: 140px !important;
        height: 140px !important;
    }

    /* ETA edit form stacks */
    #update-eta-form {
        flex-wrap: wrap;
    }

    /* Shipper fields stack */
    #shipper-fields .detail-grid {
        grid-template-columns: 1fr;
    }
}


/* ── Shipment detail mobile fixes ── */
@media (max-width: 900px) {
    /* Progress panel header: wrap status + share */
    .sd-progress-panel .panel__header {
        flex-wrap: wrap;
        gap: 8px;
    }

    .sd-progress-panel .panel__header > div {
        flex-wrap: wrap;
    }

    /* Delay/ETA cards: stack icon + text */
    .sd-delay-card {
        flex-direction: column;
        gap: 8px;
    }

    /* POD card same */
    .sd-pod-card {
        flex-direction: column;
        gap: 8px;
    }

    /* ETA inline edit wraps */
    .sd-progress-panel form.dispatch-inline {
        flex-wrap: wrap;
        gap: 6px;
    }

    /* Tabs: horizontal scroll, no wrap */
    #sd-tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap;
        width: 100%;
    }

    #sd-tabs .dispatch-tab {
        white-space: nowrap;
        flex-shrink: 0;
        font-size: 0.82rem;
        padding: 8px 14px;
    }

    /* Tab content panels: tighter padding */
    .sd-tab-content .panel__body {
        padding: 12px 14px;
    }

    /* Assign form in actions tab */
    #assign-form .dispatch-inline {
        flex-wrap: wrap;
    }

    /* Status form fields */
    #status-form .detail-grid {
        grid-template-columns: 1fr;
    }

    #status-form,
    #assign-form {
        width: 100%;
    }

    #status-form select,
    #status-form input,
    #assign-form select,
    #assign-form input {
        width: 100% !important;
        min-width: 0;
        box-sizing: border-box;
    }
    #ds-cards{
        margin-bottom: 100px;
    }
    .sd-tab-content .panel__body {
        overflow-x: hidden;
        width: 100%;
        margin-bottom: 70px;
    }

    /* Cost/doc inline forms */
    #add-cost-form,
    #generate-doc-form,
    #upload-doc-form,
    #add-milestone-form {
        flex-wrap: wrap;
    }

    #add-cost-form input,
    #add-cost-form select,
    #generate-doc-form select {
        min-width: 0;
        flex: 1 1 120px;
    }

    /* Upload doc area */
    #upload-doc-wrap {
        padding: 12px;
    }

    #upload-doc-wrap .detail-grid {
        grid-template-columns: 1fr;
    }

    /* Customs form */
    #create-customs-form .detail-grid {
        grid-template-columns: 1fr;
    }

    /* Rating stars bigger touch target */
    .star-btn {
        font-size: 2rem;
        padding: 4px 6px;
    }

    /* Share dropdown: position left on mobile */
    .share-dropdown {
        right: auto;
        left: 0;
    }

    /* Carrier info card — no special sizing in flex layout */
    .sd-summary .sd-card:last-child {
        grid-column: 1 / -1;
    }
}

@media (max-width: 640px) {
    /* Progress stepper dots smaller */
    .sp-dot {
        width: 32px;
        height: 32px;
    }

    .sp-dot__icon {
        font-size: 0.8rem;
    }

    .sp-label {
        font-size: 0.65rem;
    }

    /* Timeline items tighter */
    .tl-item {
        grid-template-columns: 28px 1fr;
        gap: 8px;
    }

    .tl-dot {
        width: 28px;
        height: 28px;
        font-size: 0.75rem;
    }

    /* Shipment detail tabs: smaller text */
    #sd-tabs .dispatch-tab {
        font-size: 0.78rem;
        padding: 6px 10px;
    }

    /* Line items table scroll */
    .sd-tab-content .table-wrap {
        overflow-x: auto;
    }

    .sd-tab-content .data-table {
        min-width: 500px;
    }
}


/* ── Driver quick actions: prominent on mobile ── */
@media (max-width: 900px) {
    .quick-actions {
        flex-direction: column;
        gap: 6px;
    }

    .quick-actions .btn--sm {
        width: 100%;
        min-height: 36px;
        font-size: 0.85rem;
        justify-content: center;
    }

    .quick-actions .start-btn {
        font-size: 1rem;
        min-height: 42px;
        font-weight: 700;
    }

    .quick-actions .gps-quick-btn {
        font-size: 0.95rem;
        min-height: 40px;
    }
}
