/* i-Screening mobile layout layer. Loaded last.
   1. iPhone safe areas: the app runs edge-to-edge (viewport-fit=cover, translucent
      status bar), so header and drawer must clear the notch and home indicator.
   2. Sidebar: logo only, compact rows, logout pinned to the bottom.
   3. Phones: one consistent 16px gutter and no stacked legacy paddings. */

/* ==========================================================================
   SIDEBAR (all sizes)
   ========================================================================== */
.v11-sidebar-brand { display: none; }
.v11-sidebar-header {
    justify-content: space-between;
    gap: 12px;
}
.v11-sidebar-logo {
    height: 34px;
    width: auto;
    max-width: 150px;
    margin: 0;
}

.v11-sidebar-nav { display: flex; flex-direction: column; }
/* Legacy phase1.css turns every .nav-link into a stacked bottom-tab item on
   small screens; sidebar links are always single rows. */
.v11-sidebar-nav .nav-link {
    flex: 0 0 auto;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    gap: 14px;
    min-height: 46px;
    padding: 10px 14px;
    font-size: 0.95rem;
    text-align: left;
}
.v11-sidebar-nav .nav-link .nav-icon { margin: 0; }
.v11-sidebar-nav .nav-link.hidden { display: none !important; }
/* Logout sits at the bottom, apart from navigation. */
.v11-sidebar-nav #nav-logout-btn {
    margin-top: auto;
    border-top: 1px solid var(--ui-line);
    border-radius: 0;
    padding-top: 16px;
}
.v11-sidebar-nav #nav-logout-btn:hover { color: var(--ui-bad-text); background: transparent; transform: none; }

/* ==========================================================================
   DRAWER + MOBILE HEADER (sidebar is off-canvas up to 1024px)
   ========================================================================== */
@media (max-width: 1024px) {
    .v11-app-shell { height: 100vh; height: 100dvh; }

    .v11-mobile-header {
        height: auto;
        min-height: calc(56px + env(safe-area-inset-top, 0px));
        padding: env(safe-area-inset-top, 0px) max(12px, env(safe-area-inset-right, 0px)) 0 max(12px, env(safe-area-inset-left, 0px));
        gap: 10px;
        background: var(--ui-bg);
        -webkit-backdrop-filter: saturate(180%) blur(14px);
        backdrop-filter: saturate(180%) blur(14px);
        border-bottom: 1px solid var(--ui-line);
    }
    .v11-mobile-header #v11-sidebar-toggle {
        width: 44px;
        height: 44px;
        flex: 0 0 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border: 1px solid var(--ui-line);
        border-radius: 12px;
        background: var(--ui-card);
        color: var(--ui-text);
        padding: 0;
    }
    .v11-mobile-header #v11-sidebar-toggle:active { background: var(--ui-hover); }
    .v11-mobile-logo { width: 28px; height: 28px; border-radius: 7px; }
    .v11-mobile-brand { margin-left: 0; font-size: 1.05rem; font-weight: 700; letter-spacing: -0.02em; }

    .v11-sidebar {
        width: min(300px, 84vw);
        padding-top: env(safe-area-inset-top, 0px);
        padding-bottom: env(safe-area-inset-bottom, 0px);
        padding-left: env(safe-area-inset-left, 0px);
    }
    .v11-sidebar-header {
        min-height: 72px;
        padding: 14px 16px 14px 20px;
    }
    .v11-mobile-close-btn {
        width: 40px;
        height: 40px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        background: var(--ui-chip);
        font-size: 1.5rem;
        line-height: 1;
        margin-left: 0;
    }
    .v11-sidebar-nav { padding: 14px 12px 12px; gap: 4px; }

    .v11-screens-container { padding-bottom: env(safe-area-inset-bottom, 0px); }
}

/* ==========================================================================
   PHONES
   ========================================================================== */
@media (max-width: 768px) {
    /* One gutter for every screen. Same selector as the shell rule so it wins. */
    .v11-screens-container .screen:not(#setup-screen):not(#testing-screen):not(#calibration-screen) > *:not(.section-header):not(.v11-page-header) {
        padding-left: 16px;
        padding-right: 16px;
    }
    .v11-screens-container .screen { padding-bottom: 24px !important; }
    .mobile-only { padding: 0 !important; gap: 12px; }
    /* Lists placed directly in a screen keep the screen gutter. */
    .v11-screens-container .screen > .mobile-only { padding: 0 16px 16px !important; }
    /* Desktop-only tables wrapped in a card would leave an empty card behind. */
    .v11-card:has(> .desktop-only:only-child) { display: none; }

    /* Page headers: left-aligned, compact, with a side margin. */
    .v11-screens-container .section-header {
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
        min-height: 0;
        padding: 14px 16px;
        margin-bottom: 16px;
        text-align: left;
    }
    .v11-screens-container .section-header h2 { font-size: 1.25rem; margin: 0; }
    .v11-page-header,
    #event-detail-screen > .v11-page-header,
    #lead-detail-screen > .v11-page-header,
    #record-form-screen > .v11-page-header {
        padding: 16px !important;
        margin-bottom: 0;
        gap: 12px;
        flex-direction: column;
        align-items: stretch;
    }
    .v11-page-header .v11-title-page { font-size: 1.4rem; }
    .v11-page-header .v11-action-bar { gap: 8px; }
    .v11-page-header .v11-btn { min-height: 44px; }

    /* Cards: tighter padding on small screens. */
    .v11-card { padding: 16px; }
    .v11-detail-grid { gap: 12px 16px; }

    /* Dashboard */
    #dashboard-screen .dashboard-intro { padding-top: 18px; padding-bottom: 16px; margin-bottom: 16px; gap: 10px; }
    #dashboard-screen .dashboard-intro h2 { font-size: 1.75rem; }
    #dashboard-screen .dashboard-intro p { font-size: 0.9rem; margin-bottom: 0; }
    #dashboard-screen .dashboard-section { margin-bottom: 22px; }
    #dashboard-screen .kpi-grid,
    #dashboard-screen .funnel-container {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px;
        padding: 0;
        width: auto;
    }
    #dashboard-screen .kpi-grid > :last-child:nth-child(odd) { grid-column: 1 / -1; }
    #dashboard-screen .funnel-connector { display: none; }
    #dashboard-screen .funnel-stage { width: auto; min-width: 0; }
    #dashboard-screen .kpi-card { padding: 14px; min-height: 0; }
    #dashboard-screen .kpi-value { font-size: 1.9rem; }
    #dashboard-screen .conversion-insights-card { margin: 12px 0 0; }

    /* Event / lead detail: compact stat grid. */
    .v11-kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
    .v11-stat-card { padding: 14px; }
    .v11-stat-card .v11-stat-value { font-size: 1.5rem; }

    /* Filters: full-width search, then two filters per row; the last one spans. */
    .filters-bar,
    #leads-list-screen .v11-action-bar {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
        margin: 0 0 14px;
    }
    .filters-bar > *,
    #leads-list-screen .v11-action-bar > * { min-width: 0 !important; width: 100% !important; margin: 0; }
    .filters-bar > :first-child,
    .filters-bar > :last-child,
    #leads-list-screen .v11-action-bar > :first-child,
    #leads-list-screen .v11-action-bar > :last-child { grid-column: 1 / -1; }
    .filters-bar select,
    #leads-list-screen .v11-action-bar select { font-size: 0.9rem; min-height: 44px; }
    #leads-list-screen .v11-card:has(> .v11-action-bar) { padding: 12px !important; }

    /* Record form: one card layer, full-width actions, aligned prefix input. */
    #record-form-screen #verification-layout { padding: 16px 0 32px; max-width: none; }
    #record-form-screen #verification-form-container {
        padding: 0;
        border: none;
        background: transparent;
        box-shadow: none;
    }
    #record-form-screen .v11-card { padding: 16px; margin-bottom: 12px !important; }
    #record-form-screen > .v11-page-header .v11-action-bar {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 10px;
        width: 100%;
    }
    #record-form-screen > .v11-page-header .v11-action-bar .v11-btn { width: 100%; }
    /* Draw the border once on the wrapper so prefix and input always line up. */
    #record-form-screen .input-with-prefix {
        align-items: stretch !important;
        border: 1px solid var(--ui-line);
        border-radius: 10px;
        overflow: hidden;
        background: var(--ui-card);
    }
    #record-form-screen .input-with-prefix .prefix-label,
    #record-form-screen .input-with-prefix .v11-input {
        border: none !important;
        border-radius: 0 !important;
        height: 48px !important;
        min-height: 48px;
        margin: 0;
        box-shadow: none;
    }
    #record-form-screen .input-with-prefix .v11-input { flex: 1; min-width: 0; }
    #record-form-screen .input-with-prefix .prefix-label { border-right: 1px solid var(--ui-line) !important; }
    #record-form-screen .input-with-prefix:focus-within { border-color: var(--ui-accent); }
    #record-form-screen .clinical-matrix .matrix-row label { flex-basis: 44px; }

    /* Old full-width table wrappers carry their own side margins. */
    .table-responsive, .form-container, .detail-card, .conversion-insights-card { margin-left: 0; margin-right: 0; }
    .table-section { padding-left: 0; padding-right: 0; }
}
