/* ==========================================================================
   pwa.css — PWA chrome + iPhone ergonomics for TikTok Campaign Launcher

   Loaded LAST so it can override without editing the existing stylesheets.

   Layout facts this file must respect (from shell.css):
     .app-header      position: fixed; height: 60px
     .app-layout      padding-top: 60px; min-height: 100vh
     .sidebar         position: fixed (hidden <= 768px)
     .mobile-bottom-nav  position: fixed; bottom: 0
     The BODY scrolls. There is no inner scroll container.

   Because the body scrolls, this file must never set overflow, height or
   overscroll-behavior on html/body — doing so breaks iOS scrolling and
   kills position: sticky.
   ========================================================================== */

:root {
    --sa-top:    env(safe-area-inset-top, 0px);
    --sa-bottom: env(safe-area-inset-bottom, 0px);
    --sa-left:   env(safe-area-inset-left, 0px);
    --sa-right:  env(safe-area-inset-right, 0px);
    --app-header-h: 60px;                    /* must match shell.css */
    --nav-h: 56px;                           /* bottom nav, excl. safe area */
    --tap: 44px;
}

/* ==========================================================================
   1. iOS input zoom
   Safari zooms the viewport whenever a focused field is under 16px and never
   zooms back out. The forms use 14px, which made every field tap a fight.
   ========================================================================== */

@media (max-width: 900px) {
    input[type="text"],
    input[type="number"],
    input[type="email"],
    input[type="password"],
    input[type="search"],
    input[type="tel"],
    input[type="url"],
    input[type="date"],
    input[type="time"],
    input[type="datetime-local"],
    select,
    textarea {
        font-size: 16px !important;
    }
}

/* ==========================================================================
   2. Safe areas

   .app-header has a FIXED height, so the notch inset has to grow that height
   — padding alone would push its contents out of the box — and .app-layout's
   top padding has to grow by exactly the same amount or the page sits under
   the header.
   ========================================================================== */

@supports (top: env(safe-area-inset-top)) {
    .app-header {
        height: calc(var(--app-header-h) + var(--sa-top));
        padding-top: var(--sa-top);
        padding-left:  max(24px, var(--sa-left));
        padding-right: max(24px, var(--sa-right));
        box-sizing: border-box;
    }

    .app-layout {
        padding-top: calc(var(--app-header-h) + var(--sa-top));
    }

    .sidebar {
        top: calc(var(--app-header-h) + var(--sa-top));
    }

    @media (max-width: 768px) {
        .app-header {
            padding-left:  max(12px, var(--sa-left));
            padding-right: max(12px, var(--sa-right));
        }

        .main-content {
            padding-left:  max(12px, var(--sa-left));
            padding-right: max(12px, var(--sa-right));
            /* clear the bottom nav and the home indicator */
            padding-bottom: calc(var(--nav-h) + var(--sa-bottom) + 20px) !important;
        }
    }
}

/* ==========================================================================
   3. Bottom navigation — rebuilt

   The old bar was cramped: 10px labels, uneven icon sizes, no safe-area
   padding (so it sat under the home indicator) and colour as the only
   active cue.
   ========================================================================== */

@media (max-width: 768px) {
    .mobile-bottom-nav {
        display: flex !important;
        align-items: stretch;
        gap: 2px;
        padding: 6px 6px calc(6px + var(--sa-bottom)) !important;
        background: rgba(255, 255, 255, 0.94) !important;
        -webkit-backdrop-filter: saturate(180%) blur(20px);
        backdrop-filter: saturate(180%) blur(20px);
        border-top: 1px solid rgba(15, 23, 42, 0.08) !important;
        box-shadow: 0 -1px 16px rgba(15, 23, 42, 0.06) !important;
    }

    .mobile-bottom-nav a {
        flex: 1 1 0;
        min-width: 0;                        /* let labels shrink, not overflow */
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        height: var(--nav-h);
        padding: 6px 2px !important;
        border-radius: 12px;
        font-size: 10.5px !important;
        font-weight: 600;
        line-height: 1.15;
        letter-spacing: -0.01em;
        color: #64748b;
        text-decoration: none;
        text-align: center;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
        transition: background 0.18s ease, color 0.18s ease;
    }

    .mobile-bottom-nav a svg {
        width: 22px !important;
        height: 22px !important;
        flex: 0 0 auto;
        stroke-width: 2;
    }

    /* Active: tinted pill — readable without relying on colour alone */
    .mobile-bottom-nav a.active {
        color: #0369a1 !important;
        background: rgba(3, 105, 161, 0.10);
    }

    .mobile-bottom-nav a.active svg { stroke-width: 2.4; }

    /* Pressed state, applied by pwa.js on touch for instant feedback */
    .mobile-bottom-nav a.tapped {
        background: rgba(15, 23, 42, 0.08);
        transform: scale(0.96);
    }
}

/* Very narrow phones: drop to icon-forward sizing rather than wrapping text */
@media (max-width: 360px) {
    .mobile-bottom-nav a { font-size: 9.5px !important; gap: 2px; }
    .mobile-bottom-nav a svg { width: 20px !important; height: 20px !important; }
}

/* Phone landscape: reclaim vertical space */
@media (max-height: 460px) and (orientation: landscape) {
    :root { --nav-h: 44px; }
    .mobile-bottom-nav a { flex-direction: row; gap: 6px; font-size: 11px !important; }
    .mobile-bottom-nav a svg { width: 17px !important; height: 17px !important; }
}

/* ==========================================================================
   4. Touch targets

   Deliberately narrow: only real buttons and links. Sizing bare
   input[type=checkbox] broke the div-based campaign toggle switches, so
   that is gone — tap area comes from the surrounding label instead.
   ========================================================================== */

@media (hover: none) and (pointer: coarse) {
    button,
    .btn-primary,
    .btn-secondary,
    .btn-success,
    .tab-btn,
    a.btn-connect-large {
        min-height: var(--tap);
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }

    /* Grow the hit area via the label, never by resizing the control */
    .radio-option,
    .age-group-item,
    .location-method-item {
        min-height: var(--tap);
        display: flex;
        align-items: center;
        gap: 10px;
    }

    button:active,
    .btn-primary:active,
    .btn-secondary:active,
    .btn-success:active {
        opacity: 0.85;
    }
}

/* ==========================================================================
   5. Keep "Launch Campaign" reachable on long forms
   ========================================================================== */

@media (max-width: 768px) {
    #launch-button {
        position: sticky;
        bottom: calc(var(--nav-h) + var(--sa-bottom) + 12px);
        z-index: 40;
        width: 100%;
        min-height: 52px;
        font-size: 16px;
        font-weight: 700;
        border-radius: 14px;
        box-shadow: 0 6px 22px rgba(0, 0, 0, 0.2);
    }

    .form-actions {
        display: flex;
        flex-direction: column;
        gap: 10px;
    }

    .form-actions .btn-primary,
    .form-actions .btn-success {
        width: 100%;
        min-height: 50px;
        font-size: 16px;
    }
}

/* ==========================================================================
   6. Forms — single column on phones
   ========================================================================== */

@media (max-width: 768px) {
    .form-row {
        display: flex;
        flex-direction: column;
    }

    .form-row > .form-group { width: 100% !important; }

    .form-group { margin-bottom: 15px; }
}

/* ==========================================================================
   7. Wide tables scroll inside their own wrapper
   (no html/body overflow rules — those break iOS scrolling and sticky)
   ========================================================================== */

@media (max-width: 768px) {
    .metrics-table-wrapper,
    .campaign-metrics-table-container,
    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* ==========================================================================
   8. Navigation progress bar (driven by pwa.js)
   Multi-page app: taps cost a full page load, so show movement immediately.
   ========================================================================== */

#pwa-progress {
    position: fixed;
    top: 0;
    left: 0;
    height: 3px;
    width: 0;
    z-index: 4000;
    background: linear-gradient(90deg, #25f4ee, #fe2c55);
    box-shadow: 0 0 10px rgba(254, 44, 85, 0.55);
    opacity: 0;
    transition: width 0.2s ease, opacity 0.25s ease;
    pointer-events: none;
}

#pwa-progress.active { opacity: 1; }

/* ==========================================================================
   9. Install banner + update toast
   ========================================================================== */

.pwa-install-banner {
    position: fixed;
    left: max(12px, var(--sa-left));
    right: max(12px, var(--sa-right));
    bottom: calc(var(--nav-h) + var(--sa-bottom) + 12px);
    z-index: 2000;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    background: #16162a;
    color: #e2e8f0;
    border-radius: 16px;
    box-shadow: 0 10px 34px rgba(0, 0, 0, 0.34);
    transform: translateY(160%);
    transition: transform 0.32s cubic-bezier(0.22, 1, 0.36, 1);
}

.pwa-install-banner.show { transform: translateY(0); }
.pwa-install-banner img { width: 40px; height: 40px; border-radius: 9px; flex: 0 0 auto; }
.pwa-install-banner .pwa-text { flex: 1; min-width: 0; }
.pwa-install-banner .pwa-title { font-size: 14px; font-weight: 700; }
.pwa-install-banner .pwa-sub { font-size: 12px; color: #94a3b8; margin-top: 2px; line-height: 1.4; }

.pwa-install-banner button {
    flex: 0 0 auto;
    min-height: 38px;
    padding: 9px 15px;
    font-size: 14px;
    font-weight: 600;
    color: #fff;
    background: #fe2c55;
    border: none;
    border-radius: 10px;
    cursor: pointer;
}

.pwa-install-banner .pwa-dismiss {
    background: transparent;
    color: #64748b;
    font-size: 22px;
    line-height: 1;
    padding: 4px 6px;
    min-height: 0;
}

.pwa-update-toast {
    position: fixed;
    left: 50%;
    bottom: calc(var(--nav-h) + var(--sa-bottom) + 14px);
    transform: translate(-50%, 220%);
    z-index: 2100;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 11px 16px;
    background: #0f172a;
    color: #fff;
    font-size: 13.5px;
    border-radius: 999px;
    box-shadow: 0 8px 26px rgba(0, 0, 0, 0.3);
    transition: transform 0.3s cubic-bezier(0.22, 1, 0.36, 1);
    white-space: nowrap;
}

.pwa-update-toast.show { transform: translate(-50%, 0); }

.pwa-update-toast button {
    background: #25f4ee;
    color: #06202a;
    border: none;
    border-radius: 999px;
    padding: 6px 13px;
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
}

/* ==========================================================================
   10. Reduced motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    .pwa-install-banner,
    .pwa-update-toast,
    #pwa-progress,
    .mobile-bottom-nav a { transition: none; }
    .mobile-bottom-nav a.tapped { transform: none; }
}
