/* ======================================================
   Mobile-first UI — Bag Design Generator
   Active ≤ 768px (body.is-mobile also for force)
   ====================================================== */

/* Safe areas (notch / home indicator) */
:root {
    --m-bottom-nav: 64px;
    --m-safe-bottom: env(safe-area-inset-bottom, 0px);
    --m-safe-top: env(safe-area-inset-top, 0px);
    --m-tap: 44px;
}

/* Desktop: hide mobile chrome */
.mobile-bottom-nav,
.sidebar-backdrop {
    display: none;
}

@media (max-width: 768px) {
    html, body {
        font-size: 15px;
        overflow-x: hidden;
        -webkit-tap-highlight-color: transparent;
        overscroll-behavior-y: contain;
    }

    body.is-mobile,
    body {
        padding-bottom: calc(var(--m-bottom-nav) + var(--m-safe-bottom) + 8px);
    }

    /* ---------- Backdrop when drawer open ---------- */
    .sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.55);
        z-index: 90;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.25s ease;
        backdrop-filter: blur(2px);
        -webkit-backdrop-filter: blur(2px);
    }
    body.sidebar-open .sidebar-backdrop {
        opacity: 1;
        pointer-events: auto;
    }

    /* ---------- Sidebar as drawer ---------- */
    .sidebar {
        width: min(86vw, 300px);
        max-width: 300px;
        height: 100dvh;
        height: 100vh;
        padding-top: var(--m-safe-top);
        box-shadow: 8px 0 32px rgba(0, 0, 0, 0.35);
        z-index: 110;
        transform: translateX(-105%);
    }
    .sidebar.open {
        transform: translateX(0);
    }
    .sidebar-header {
        padding: 16px 16px 14px;
    }
    .logo span {
        font-size: 15px;
        font-weight: 700;
    }
    .nav-menu {
        padding: 8px 10px 20px;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        flex: 1;
    }
    .nav-link {
        min-height: var(--m-tap);
        padding: 12px 14px;
        border-radius: 12px;
        font-size: 14px;
        gap: 12px;
    }
    .nav-section-label {
        margin-top: 14px;
        margin-bottom: 6px;
        font-size: 11px;
        letter-spacing: 0.06em;
    }
    .sidebar-footer {
        padding: 12px 12px calc(12px + var(--m-safe-bottom));
        gap: 10px;
    }
    .sidebar-footer-actions {
        flex-direction: column;
        gap: 8px;
    }
    .sidebar-footer-actions .btn {
        min-height: var(--m-tap);
        width: 100%;
    }
    .user-info {
        padding: 10px;
        border-radius: 12px;
        background: var(--bg-surface);
    }

    /* ---------- Top bar ---------- */
    .content.with-sidebar {
        margin-left: 0 !important;
        padding: 0 0 calc(var(--m-bottom-nav) + var(--m-safe-bottom)) !important;
        min-height: 100dvh;
    }
    .topbar {
        position: sticky;
        top: 0;
        z-index: 50;
        margin: 0 !important;
        padding: calc(10px + var(--m-safe-top)) 14px 12px !important;
        background: color-mix(in srgb, var(--bg-secondary) 92%, transparent);
        backdrop-filter: blur(14px);
        -webkit-backdrop-filter: blur(14px);
        border-bottom: 1px solid var(--border-color);
        min-height: auto;
        gap: 10px;
    }
    .topbar-left {
        gap: 10px;
        min-width: 0;
        flex: 1;
    }
    .topbar-title {
        font-size: 16px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .topbar-subtitle { display: none !important; }
    .mobile-menu-btn {
        display: flex !important;
        align-items: center;
        justify-content: center;
        width: var(--m-tap);
        height: var(--m-tap);
        min-width: var(--m-tap);
        padding: 0;
        border-radius: 12px;
        background: var(--bg-surface);
    }
    .theme-toggle {
        width: var(--m-tap);
        height: var(--m-tap);
        border-radius: 12px;
    }

    /* ---------- Page content ---------- */
    .content > .alert,
    .content > .page-header,
    .content > .dashboard-container,
    .content > .image-base-grid,
    .content > .history-header,
    .content > .history-grid,
    .content > .provider-grid,
    .content > .calculator-container,
    .content > .page-hero,
    .content > div,
    .content > section {
        /* spacing handled per-block */
    }

    main.content.with-sidebar > *:not(.topbar):not(script) {
        padding-left: 14px;
        padding-right: 14px;
    }
    main.content.with-sidebar > .topbar {
        padding-left: 14px;
        padding-right: 14px;
    }
    /* Nested full-bleed fix for containers that set their own padding */
    .dashboard-container,
    .image-base-grid,
    .page-header,
    .history-header,
    .alert {
        margin-left: 0;
        margin-right: 0;
    }

    .page-header {
        margin: 14px 0 12px;
    }
    .page-header h1 {
        font-size: 20px;
        letter-spacing: -0.02em;
        line-height: 1.3;
    }
    .page-header p {
        font-size: 13px;
        margin-top: 6px;
        line-height: 1.5;
    }

    .page-hero {
        min-height: 160px !important;
        border-radius: 14px;
        margin: 12px 0 16px;
        width: auto;
    }
    .page-hero-content {
        padding: 18px 16px !important;
        width: 100% !important;
    }
    .page-hero h1 {
        font-size: 20px !important;
    }
    .page-hero p {
        font-size: 13px !important;
        margin-top: 6px;
    }

    /* ---------- Dashboard / generation ---------- */
    .dashboard-container {
        display: flex !important;
        flex-direction: column;
        gap: 14px !important;
        max-width: 100%;
        padding: 0 0 12px;
    }
    .generation-panel,
    .result-panel {
        padding: 16px !important;
        border-radius: 14px !important;
        box-shadow: var(--shadow-sm);
    }
    .generation-panel h2,
    .result-panel h2 {
        font-size: 15px !important;
        margin-bottom: 14px !important;
    }
    .view-labels {
        display: none; /* hide desktop dual labels on mobile single-column */
    }
    .result-container {
        min-height: 220px;
        border-radius: 12px;
    }
    .result-container img {
        width: 100%;
        height: auto;
        border-radius: 12px;
    }
    .result-actions {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        margin-top: 12px;
    }
    .result-actions .btn {
        flex: 1 1 calc(50% - 4px);
        min-height: var(--m-tap);
        justify-content: center;
    }

    .template-grid {
        grid-template-columns: repeat(3, 1fr) !important;
        max-height: 200px !important;
        gap: 8px !important;
    }
    .template-card {
        padding: 12px 6px !important;
        min-height: 72px;
        border-radius: 12px;
    }
    .template-label {
        font-size: 11px;
    }

    .prompt-textarea,
    textarea.prompt-textarea,
    .form-group textarea,
    .form-group input,
    .form-group select {
        font-size: 16px !important; /* prevent iOS zoom */
        min-height: 48px;
        border-radius: 12px;
    }
    .prompt-textarea {
        min-height: 120px !important;
    }
    .form-group label {
        font-size: 13px;
        margin-bottom: 6px;
    }
    .btn {
        min-height: var(--m-tap);
        border-radius: 12px;
        font-weight: 600;
    }
    .btn-lg,
    .btn-block {
        width: 100%;
        min-height: 50px;
        font-size: 15px;
        border-radius: 14px;
    }
    .btn-block {
        margin-top: 4px;
    }
    .status-indicator {
        padding: 12px;
        border-radius: 12px;
        font-size: 13px;
    }

    .recent-history {
        margin-top: 8px;
        padding-bottom: 8px;
    }
    .history-strip {
        display: flex;
        gap: 10px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 8px;
        scroll-snap-type: x mandatory;
    }
    .history-thumb {
        flex: 0 0 96px;
        width: 96px;
        height: 96px;
        border-radius: 12px;
        overflow: hidden;
        scroll-snap-align: start;
    }
    .history-thumb img {
        width: 100%;
        height: 100%;
        object-fit: cover;
    }

    /* ---------- Image base / design sheet ---------- */
    .image-base-grid {
        grid-template-columns: 1fr !important;
        gap: 14px !important;
        margin-top: 12px !important;
    }
    .image-dropzone {
        min-height: 200px !important;
        border-radius: 14px !important;
    }
    .image-edit-result-panel {
        min-height: 240px !important;
        border-radius: 14px !important;
    }
    .hint-chips {
        gap: 6px;
    }
    .hint-chip {
        min-height: 36px;
        padding: 8px 12px;
        font-size: 12px;
        border-radius: 999px;
    }

    /* ---------- History ---------- */
    .history-header {
        flex-direction: column;
        align-items: stretch !important;
        gap: 12px;
        margin-bottom: 14px;
    }
    .history-grid {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }
    .design-card {
        border-radius: 14px;
        overflow: hidden;
    }
    .design-card img {
        width: 100%;
        aspect-ratio: 16/10;
        object-fit: cover;
    }

    /* ---------- Settings / users ---------- */
    .provider-grid {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }
    .provider-card {
        border-radius: 14px;
        padding: 16px;
    }
    .user-table-wrap,
    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .user-table {
        min-width: 520px;
    }

    /* ---------- Calculator ---------- */
    .calculator-container {
        display: flex !important;
        flex-direction: column;
        gap: 12px;
    }
    .calculator-container .input-panel,
    .calculator-container .output-panel {
        border-radius: 14px !important;
        padding: 16px !important;
    }

    /* ---------- Modals ---------- */
    .modal-overlay {
        align-items: flex-end;
        padding: 0;
    }
    .modal-overlay.active {
        display: flex;
    }
    .modal {
        width: 100% !important;
        max-width: 100% !important;
        max-height: 90dvh;
        border-radius: 18px 18px 0 0 !important;
        margin: 0;
        padding-bottom: calc(12px + var(--m-safe-bottom));
        overflow-y: auto;
    }
    .modal-header,
    .modal-body,
    .modal-footer {
        padding-left: 16px;
        padding-right: 16px;
    }
    .modal-footer {
        display: flex;
        gap: 8px;
        flex-wrap: wrap;
    }
    .modal-footer .btn {
        flex: 1;
        min-height: var(--m-tap);
    }

    /* ---------- Toasts ---------- */
    .toast-container {
        left: 12px !important;
        right: 12px !important;
        bottom: calc(var(--m-bottom-nav) + var(--m-safe-bottom) + 12px) !important;
        top: auto !important;
        width: auto !important;
        max-width: none !important;
    }
    .toast {
        width: 100%;
        border-radius: 12px;
        font-size: 13px;
    }

    /* ---------- Bottom tab navigation ---------- */
    .mobile-bottom-nav {
        display: flex;
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 80;
        height: calc(var(--m-bottom-nav) + var(--m-safe-bottom));
        padding: 6px 4px calc(6px + var(--m-safe-bottom));
        background: color-mix(in srgb, var(--bg-secondary) 94%, transparent);
        border-top: 1px solid var(--border-color);
        backdrop-filter: blur(16px);
        -webkit-backdrop-filter: blur(16px);
        justify-content: space-around;
        align-items: flex-start;
        box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.12);
    }
    .mobile-bottom-nav a {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        min-height: 52px;
        padding: 4px 2px;
        color: var(--text-muted);
        font-size: 10px;
        font-weight: 600;
        text-decoration: none;
        border-radius: 12px;
        transition: color 0.2s, background 0.2s;
        -webkit-user-select: none;
        user-select: none;
    }
    .mobile-bottom-nav a svg {
        width: 22px;
        height: 22px;
        stroke: currentColor;
        flex-shrink: 0;
    }
    .mobile-bottom-nav a.active {
        color: var(--accent-primary);
        background: var(--accent-soft);
    }
    .mobile-bottom-nav a span {
        line-height: 1.15;
        text-align: center;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        padding: 0 2px;
    }

    /* ---------- Login mobile ---------- */
    .login-page {
        min-height: 100dvh;
        padding: calc(24px + var(--m-safe-top)) 16px calc(24px + var(--m-safe-bottom)) !important;
        display: flex;
        flex-direction: column;
    }
    .login-topbar {
        margin-bottom: 24px;
    }
    .login-card {
        width: 100%;
        max-width: 100%;
        padding: 24px 18px !important;
        border-radius: 18px;
    }
    .login-form .btn-block {
        min-height: 50px;
        border-radius: 14px;
    }

    /* ---------- Image viewer ---------- */
    .image-viewer {
        padding: 12px;
    }
    .image-viewer img {
        max-width: 100%;
        max-height: 90dvh;
        border-radius: 12px;
    }

    /* ---------- Alert ---------- */
    .alert {
        border-radius: 12px;
        margin: 12px 0;
        font-size: 13px;
    }
}

/* Very small phones */
@media (max-width: 380px) {
    .mobile-bottom-nav a span {
        font-size: 9px;
    }
    .template-grid {
        grid-template-columns: repeat(3, 1fr) !important;
    }
    .topbar-title {
        font-size: 15px;
    }
}

/* Landscape mobile */
@media (max-width: 900px) and (orientation: landscape) {
    .mobile-bottom-nav {
        height: calc(52px + var(--m-safe-bottom));
    }
    body {
        padding-bottom: calc(52px + var(--m-safe-bottom));
    }
    .page-hero {
        min-height: 120px !important;
    }
}
