/* ═══════════ App-shell global rules ═══════════ */

/* Lock zoom + horizontal pan; behave like a native shell */
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    touch-action: manipulation;
    -webkit-overflow-scrolling: touch;
}
html, body {
    overflow-x: hidden !important;
    overscroll-behavior-x: none;
    overscroll-behavior-y: contain;
    -webkit-user-select: none;
    user-select: none;
    max-width: 100vw;
}
body {
    position: relative;
    width: 100%;
    /* Native-app first-paint fade — covered by loader anyway */
    opacity: 0;
    transition: opacity .35s ease;
}
body.app-loaded { opacity: 1; }

/* Re-allow text selection where it matters (input fields, quiz questions, etc.) */
input, textarea, [contenteditable], .selectable, p, span, h1, h2, h3, h4, h5, h6, li, td {
    -webkit-user-select: text;
    user-select: text;
}

/* iOS auto-zoom guard: inputs ≥ 16px */
input:not([type="checkbox"]):not([type="radio"]),
textarea, select {
    font-size: 16px !important;
}

/* ═══════════ Page transition loader ═══════════ */
.app-loader {
    position: fixed;
    inset: 0;
    background: linear-gradient(180deg, #faf8f5 0%, #fef5f0 100%);
    z-index: 99999;
    display: grid;
    place-items: center;
    transition: opacity .42s ease, visibility .42s ease;
    padding: 0 28px;
    -webkit-tap-highlight-color: transparent;
}
.app-loader.hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}
.app-loader-inner {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 26px;
    text-align: center;
}
.app-loader-logo {
    width: clamp(140px, 42vw, 200px);
    height: auto;
    display: block;
    filter: drop-shadow(0 8px 22px rgba(220, 112, 52, 0.22));
    animation: appLoaderPulse 1.6s ease-in-out infinite;
}
@keyframes appLoaderPulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50%      { opacity: 0.55; transform: scale(0.96); }
}
.app-loader-spinner {
    display: flex;
    gap: 9px;
}
.app-loader-spinner span {
    width: 9px; height: 9px;
    border-radius: 50%;
    background: #DC7034;
    opacity: 0.4;
    animation: appLoaderBounce 1.2s ease-in-out infinite;
}
.app-loader-spinner span:nth-child(2) { animation-delay: 0.15s; }
.app-loader-spinner span:nth-child(3) { animation-delay: 0.3s; }
@keyframes appLoaderBounce {
    0%, 80%, 100% { transform: translateY(0);   opacity: 0.4; }
    40%           { transform: translateY(-12px); opacity: 1;   }
}

/* Reduce motion */
@media (prefers-reduced-motion: reduce) {
    body, .app-loader, .app-loader-logo, .app-loader-spinner span {
        animation: none !important;
        transition: opacity .15s ease !important;
    }
}

/* ═══════════ SVG icon size utilities (used by sprite refs) ═══════════ */
.icon-12 { width: 12px; height: 12px; flex-shrink: 0; }
.icon-14 { width: 14px; height: 14px; flex-shrink: 0; }
.icon-16 { width: 16px; height: 16px; flex-shrink: 0; }
.icon-20 { width: 20px; height: 20px; flex-shrink: 0; }
.icon-22 { width: 22px; height: 22px; flex-shrink: 0; }
.icon-24 { width: 24px; height: 24px; flex-shrink: 0; }
.icon-32 { width: 32px; height: 32px; flex-shrink: 0; }
.icon-48 { width: 48px; height: 48px; flex-shrink: 0; }

/* ═══════════ Cross-document slide-in transitions ═══════════ */
/* Browsers with the View Transitions API (Chrome/Edge 111+, Safari 18.2+,
   Firefox 134+) animate every same-origin navigation: the old view slides
   out to the left while the new view slides in from the right.
   Older browsers fall back silently to the existing page-loader splash. */

@view-transition { navigation: auto; }

/* On VT-capable browsers, drop the body opacity-0 fade — VT snapshots the
   new document when ready, and the snapshot must already be visible. */
@supports (view-transition-name: none) {
    body { opacity: 1 !important; transition: none !important; }
}

@keyframes navSlideOutLeft {
    from { transform: translateX(0);    opacity: 1; }
    to   { transform: translateX(-28%); opacity: 0; }
}
@keyframes navSlideInRight {
    from { transform: translateX(100%); opacity: 0; }
    to   { transform: translateX(0);    opacity: 1; }
}

::view-transition-old(root) {
    animation: navSlideOutLeft 320ms cubic-bezier(0.4, 0, 0.2, 1) both;
    transform-origin: top right;
}
::view-transition-new(root) {
    animation: navSlideInRight 380ms cubic-bezier(0.22, 0.61, 0.36, 1) both;
    transform-origin: top right;
}
::view-transition-group(root) { z-index: auto; }

@media (prefers-reduced-motion: reduce) {
    ::view-transition-old(root),
    ::view-transition-new(root) { animation: none !important; }
}

/* ═══════════════════════════════════════════════════════════════ */
/*  RESPONSIVE SCAFFOLDING (mobile-first; tablet ≥768; desktop ≥1200) */
/* ═══════════════════════════════════════════════════════════════ */

/* Spacing tokens — overridable by pages, but useful as a baseline */
:root {
    --screen-padding: 16px;
    --card-gap: 12px;
    --section-gap: 16px;
}

@media (min-width: 768px) {
    html { font-size: 15px; }
    :root {
        --screen-padding: 24px;
        --card-gap: 16px;
        --section-gap: 24px;
    }
}
@media (min-width: 1200px) {
    html { font-size: 16px; }
    :root {
        --screen-padding: 32px;
        --card-gap: 20px;
        --section-gap: 32px;
    }
}

/* ─── Tablet+ ─── Centered "phone-frame" card with side-nav grid ─── */
@media (min-width: 768px) {
    body {
        background: #e9e5dc;
        height: 100dvh;
        display: flex;
        align-items: stretch;
        justify-content: center;
    }

    /* App shell becomes the centered card with a 2-column grid:
       side-nav (RTL start = visual right) | content. The existing
       <header class="app-bar">, <main class="app-content">, and
       <nav class="bottom-tab-bar"> children are placed via grid-area
       so no per-page HTML changes are needed. */
    .app-shell {
        width: min(960px, 95vw);
        height: calc(100dvh - 32px);
        margin: 16px auto;
        background: #fff;
        border-radius: 24px;
        overflow: hidden;
        box-shadow: 0 18px 48px rgba(0, 0, 0, .12),
                    0 4px 12px rgba(0, 0, 0, .05);
        display: grid;
        grid-template-columns: 76px 1fr;
        grid-template-rows: auto 1fr;
        grid-template-areas:
            "nav header"
            "nav content";
    }
    .app-shell > .app-bar,
    .app-shell > header.app-bar,
    .app-shell > .app-header,
    .app-shell > header.app-header { grid-area: header; }
    .app-shell > .app-content,
    .app-shell > main.app-content { grid-area: content; }
    .app-shell > .bottom-tab-bar { grid-area: nav; }

    /* Bottom-tab-bar → vertical side-nav */
    .bottom-tab-bar {
        position: static !important;
        bottom: auto !important; left: auto !important; right: auto !important;
        flex-direction: column;
        height: 100%;
        width: auto;
        padding: 24px 0 calc(24px + env(safe-area-inset-bottom, 0));
        gap: 6px;
        border-top: none !important;
        border-inline-end: 0.5px solid #f0f0f0;
        box-shadow: none !important;
        background: #fff;
    }
    .bottom-tab-bar .tab-item {
        flex: 0 0 auto;
        padding: 14px 0;
        gap: 4px;
    }
    /* Active indicator: rotate the 2px-top-bar to a 3px-side-bar.
       In RTL, the side-nav is at the visual right; the indicator should
       sit on the inner edge (visual left of each tab item). */
    .bottom-tab-bar .tab-item.active::before {
        top: 50% !important;
        bottom: auto !important;
        left: 0 !important;
        right: auto !important;
        width: 3px !important;
        height: 26px !important;
        transform: translateY(-50%) !important;
        border-radius: 0 2px 2px 0 !important;
    }

    /* Content area scrolls inside the card */
    .app-shell > .app-content,
    .app-shell > main.app-content {
        height: 100%;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 0;
    }

    /* Stats grid: 2 cols on mobile → 4 cols at tablet+ */
    .stats-grid {
        grid-template-columns: repeat(4, 1fr) !important;
        gap: var(--card-gap);
    }
}

/* ═══════════════════════════════════════════════════════════════ */
/*  DESKTOP (≥1024px) — TRUE web-app shell, no phone-frame          */
/* ═══════════════════════════════════════════════════════════════ */
/* At desktop width the phone-frame card pattern (centered, rounded
   shadow, gray surround) reads as "stretched mobile". Drop it here:
   body fills the viewport with a clean cream background, .app-shell
   becomes the entire viewport split into a 240px sidebar + a flexible
   content column. The bottom-tab-bar markup is repurposed as the side
   nav and gets a logo above the four tabs (CSS-only via ::before, no
   per-page HTML changes). Carousels (.cs-row, .rec-scroll) flatten
   into responsive grids — no horizontal scrolling on desktop. */

@media (min-width: 1024px) {
    body {
        background: var(--cream, #faf8f5) !important;
        display: block !important;
        height: auto !important;
        align-items: stretch !important;
        justify-content: flex-start !important;
        overflow: hidden;
    }

    .app-shell {
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        height: 100dvh !important;
        border-radius: 0 !important;
        box-shadow: none !important;
        background: var(--cream, #faf8f5) !important;
        grid-template-columns: 240px 1fr !important;
    }

    /* ─── Side nav (re-using .bottom-tab-bar markup) ─── */
    .bottom-tab-bar {
        background: #fff !important;
        padding: 8px 0 24px !important;
        gap: 4px !important;
        border-inline-end: 1px solid #f0ebe0 !important;
        box-shadow: none !important;
    }
    /* Brand mark above the tab list — CSS-only, uses the existing logo asset.
       Hidden on mobile/tablet (the side nav doesn't exist there). */
    .bottom-tab-bar::before {
        content: '';
        display: block;
        height: 56px;
        margin: 16px 20px 22px;
        background: url('/assets/logo.svg') no-repeat center center;
        background-size: contain;
        opacity: .92;
    }
    .bottom-tab-bar .tab-item {
        flex-direction: row !important;
        justify-content: flex-start !important;
        padding: 12px 16px !important;
        gap: 14px !important;
        margin: 0 12px !important;
        border-radius: 12px !important;
    }
    .bottom-tab-bar .tab-item .tab-icon { font-size: 18px; }
    .bottom-tab-bar .tab-item .tab-label {
        font-size: .94rem !important;
        font-weight: 600 !important;
    }
    .bottom-tab-bar .tab-item.active {
        background: #fef5f0 !important;
        color: #DC7034 !important;
    }
    .bottom-tab-bar .tab-item.active::before {
        height: 28px !important;
    }

    /* ─── Content column ─── */
    .app-shell > .app-bar,
    .app-shell > header.app-bar,
    .app-shell > .app-header,
    .app-shell > header.app-header {
        background: #fff !important;
        border-bottom: 1px solid #f0ebe0 !important;
    }

    /* ─── Cuisine rows: carousel → multi-column grid ─── */
    .cs-row {
        display: grid !important;
        grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)) !important;
        gap: 16px !important;
        overflow: visible !important;
        scroll-snap-type: none !important;
        padding: 4px 0 !important;
    }
    .cs-row > :first-child,
    .cs-row > :last-child {
        margin-inline-start: 0 !important;
        margin-inline-end:   0 !important;
    }
    .cs-card { width: auto !important; }

    /* Cuisine section padding so cards don't touch viewport edges */
    .cuisine-section {
        padding-inline: 32px;
    }
    .cuisine-section .section-head {
        padding-inline: 0;
    }

    /* ─── Recommended courses: carousel → grid ─── */
    .rec-scroll {
        display: grid !important;
        grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)) !important;
        gap: 16px !important;
        overflow: visible !important;
        scroll-snap-type: none !important;
        padding: 4px 32px !important;
    }
    .rec-scroll > :first-child,
    .rec-scroll > :last-child {
        margin-inline-start: 0 !important;
        margin-inline-end:   0 !important;
    }
    .rec-card { width: auto !important; }

    /* Topic chips: wrap to fit, no horizontal scroll */
    .topics-scroll {
        flex-wrap: wrap;
        overflow: visible !important;
        padding-inline: 32px !important;
    }
    .topics-scroll > :first-child,
    .topics-scroll > :last-child {
        margin-inline-start: 0 !important;
        margin-inline-end:   0 !important;
    }

    /* Section heads + stat grid + most other sections take 32px gutters */
    .section-head,
    .stats-grid,
    .daily-card,
    .live-card,
    .continue-card,
    .view-stats-btn,
    .ch-wrap,
    .md-testimonials {
        margin-inline: 32px !important;
    }
}

/* Larger desktop: cap content reading width so cards don't get huge */
@media (min-width: 1600px) {
    .app-shell > .app-content,
    .app-shell > main.app-content {
        max-width: 1400px;
        margin: 0 auto;
    }
}

/* ─── Hover affordances (real pointer devices only) ─── */
@media (hover: hover) and (min-width: 768px) {
    .cs-card,
    .rec-card,
    .cm-post,
    .tm-card,
    .live-card,
    .continue-card,
    .stat-card {
        transition: transform .25s cubic-bezier(.4,0,.2,1),
                    box-shadow .25s ease;
    }
    .cs-card:hover,
    .rec-card:hover,
    .cm-post:hover,
    .tm-card:hover,
    .live-card:hover,
    .continue-card:hover,
    .stat-card:hover {
        transform: translateY(-3px);
        box-shadow: 0 10px 24px rgba(0, 0, 0, .10),
                    0 2px 6px rgba(0, 0, 0, .04);
    }
    .bottom-tab-bar .tab-item:not(.active):hover {
        background: #f5f5f5;
        color: #1a1a1a;
    }
}

/* ─── Keyboard focus (visible only when keyboard-driven) ─── */
:focus-visible {
    outline: 2px solid #DC7034;
    outline-offset: 2px;
    border-radius: 6px;
}

/* Skip-to-content for keyboard users (each page can render <a class="skip-to-content"> if desired) */
.skip-to-content {
    position: absolute;
    top: -100px; left: 0;
    background: #DC7034;
    color: #fff;
    font-family: 'Alyamama', 'Tajawal', sans-serif;
    font-weight: 700;
    padding: 10px 16px;
    border-radius: 0 0 8px 0;
    z-index: 9999;
    transition: top .2s ease;
}
.skip-to-content:focus { top: 0; }
