/* PWA UI: install invitation, install guide, launch screen and installed-mode app chrome.
   Loaded on every storefront page (only when the store has a logo to build icons from).
   Uses the shared design tokens (--primary-color, --on-primary) so each store keeps its own look.
   Everything installed-mode specific is scoped to html.pwa-standalone, which is set in <head>
   before first paint from the display-mode media query. */

[data-pwa-card][hidden],
[data-pwa-menu-item][hidden],
.pwa-guide[hidden],
.pwa-guide [hidden],
.pwa-update[hidden],
.pwa-back[hidden],
.pwa-tab-badge[hidden] { display: none !important; }

/* ------------------------------------------------------------------ install invitation (hero) */
/* Extra safety net (the script already refuses to reveal these on desktop): mouse-driven devices never see install UI. */
@media (hover: hover) and (pointer: fine) {
    .pw-install, [data-pwa-menu-item] { display: none !important; }
}
.pw-install {
    position: absolute;
    z-index: 4;
    top: calc(100% + 10px);
    inset-inline: 12px;
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 10px 12px;
    padding: 10px 14px 10px 12px;
    border-radius: 18px;
    background: rgba(255, 255, 255, .86);
    -webkit-backdrop-filter: blur(14px) saturate(1.2);
    backdrop-filter: blur(14px) saturate(1.2);
    border: 1px solid rgba(0, 0, 0, .06);
    box-shadow: 0 10px 28px rgba(15, 23, 42, .14), 0 2px 6px rgba(15, 23, 42, .06);
    color: #1f2937;
    animation: pw-install-in .35s ease-out both;
}
[dir="rtl"] .pw-install { padding: 10px 12px 10px 14px; }
.pw-install-icon { width: 44px; height: 44px; border-radius: 11px; object-fit: cover; box-shadow: 0 1px 4px rgba(0, 0, 0, .15); }
.pw-install-text { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.pw-install-text strong { font-size: .95rem; line-height: 1.3; color: #111827; }
.pw-install-text span {
    font-size: .78rem; line-height: 1.45; color: #4b5563;
    display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden;
}
.pw-install-btn {
    min-height: 44px;
    padding: 0 18px;
    border: 0;
    border-radius: 999px;
    background: var(--primary-color);
    color: var(--on-primary, #fff);
    font-weight: 700;
    font-size: .88rem;
    white-space: nowrap;
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    transition: transform .15s ease, box-shadow .2s ease, filter .2s ease;
    box-shadow: 0 4px 12px color-mix(in srgb, var(--primary-color) 35%, transparent);
}
.pw-install-btn:active { transform: scale(.96); }
.pw-install-btn:focus-visible, .pw-install-close:focus-visible { outline: 3px solid var(--primary-color); outline-offset: 2px; }
.pw-install-close {
    position: absolute;
    top: -10px;
    inset-inline-end: -4px;
    width: 32px; height: 32px;
    display: grid; place-items: center;
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, .08);
    background: #fff;
    color: #374151;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .15);
    cursor: pointer;
    touch-action: manipulation;
}
/* 44px hit area without changing the 32px look. */
.pw-install-close::before { content: ''; position: absolute; inset: -6px; }
/* Reserve room under the slider only while the invitation is showing (no overlap with the next section). */
.pw-hero-container:has(> .pw-install:not([hidden])) { margin-bottom: 88px; }

@media (min-width: 768px) {
    .pw-install {
        inset-inline: 0;
        margin-inline: auto;
        width: min(520px, calc(100% - 40px));
        padding: 8px 14px 8px 10px;
        gap: 10px;
    }
    .pw-install-icon { width: 38px; height: 38px; border-radius: 9px; }
    .pw-install-text strong { font-size: .9rem; }
    .pw-install-btn { min-height: 40px; font-size: .84rem; }
    .pw-hero-container:has(> .pw-install:not([hidden])) { margin-bottom: 76px; }
}
@media (max-width: 359px) {
    .pw-install { grid-template-columns: auto 1fr; }
    .pw-install-btn { grid-column: 1 / -1; width: 100%; }
    .pw-hero-container:has(> .pw-install:not([hidden])) { margin-bottom: 150px; }
}
@keyframes pw-install-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ------------------------------------------------------------------ install guide (bottom sheet) */
html.pwa-guide-open { overflow: hidden; }
.pwa-guide { position: fixed; inset: 0; z-index: 10050; display: flex; align-items: flex-end; justify-content: center; }
.pwa-guide-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, .5); animation: pwa-fade .2s ease-out both; }
.pwa-guide-sheet {
    position: relative;
    width: min(480px, 100%);
    max-height: 90vh; max-height: 90dvh;
    overflow-y: auto;
    padding: 28px 22px calc(20px + env(safe-area-inset-bottom, 0px));
    border-radius: 24px 24px 0 0;
    background: #fff;
    color: #1f2937;
    box-shadow: 0 -12px 40px rgba(15, 23, 42, .25);
    text-align: center;
    animation: pwa-sheet-in .28s cubic-bezier(.2, .8, .2, 1) both;
    outline: none;
}
@media (min-width: 640px) { .pwa-guide { align-items: center; } .pwa-guide-sheet { border-radius: 24px; } }
.pwa-guide-x {
    position: absolute; top: 10px; inset-inline-end: 10px; width: 44px; height: 44px;
    display: grid; place-items: center; border: 0; border-radius: 50%; background: transparent; color: #4b5563; cursor: pointer;
}
.pwa-guide-icon { width: 56px; height: 56px; border-radius: 14px; object-fit: cover; box-shadow: 0 2px 8px rgba(0, 0, 0, .18); margin-bottom: 12px; }
.pwa-guide-title { font-size: 1.2rem; line-height: 1.4; margin: 0 0 14px; font-weight: 800; color: #111827; }
.pwa-guide-text { color: #4b5563; margin: 0 0 12px; line-height: 1.7; font-size: .95rem; }
.pwa-steps { list-style: none; counter-reset: pwa-step; margin: 0 0 18px; padding: 0; text-align: start; display: grid; gap: 10px; }
.pwa-steps li {
    counter-increment: pwa-step; display: flex; gap: 12px; align-items: flex-start;
    padding: 12px 14px; border-radius: 14px; background: #f3f4f6; line-height: 1.6; font-size: .95rem;
}
.pwa-steps li::before {
    content: counter(pwa-step); flex: none; width: 26px; height: 26px; border-radius: 50%;
    background: var(--primary-color); color: var(--on-primary, #fff); font-size: .8rem; font-weight: 800;
    display: grid; place-items: center; margin-top: 1px;
}
.pwa-share-glyph { display: inline-block; vertical-align: -5px; color: #0a84ff; }
.pwa-guide-btn {
    display: inline-flex; align-items: center; justify-content: center; min-height: 48px; padding: 0 28px;
    border: 0; border-radius: 999px; background: var(--primary-color); color: var(--on-primary, #fff);
    font-weight: 700; font-size: .95rem; cursor: pointer; margin-bottom: 8px; touch-action: manipulation;
}
.pwa-guide-btn--ghost { background: transparent; color: var(--primary-color); border: 1px solid color-mix(in srgb, var(--primary-color) 40%, transparent); width: 100%; }
.pwa-guide :is(button, a):focus-visible { outline: 3px solid var(--primary-color); outline-offset: 2px; }
@keyframes pwa-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes pwa-sheet-in { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }

/* ------------------------------------------------------------------ update notice */
.pwa-update {
    position: fixed; z-index: 10040; inset-inline: 12px;
    bottom: calc(12px + env(safe-area-inset-bottom, 0px) + var(--pwa-bottom-offset, 0px));
    margin-inline: auto; max-width: 420px;
    display: flex; align-items: center; gap: 10px; padding: 10px 12px 10px 16px;
    border-radius: 14px; background: #111827; color: #fff; box-shadow: 0 10px 30px rgba(0, 0, 0, .3); font-size: .9rem;
}
.pwa-update span { flex: 1; line-height: 1.4; }
.pwa-update button { min-height: 40px; min-width: 40px; border: 0; border-radius: 10px; background: rgba(255, 255, 255, .14); color: #fff; padding: 0 14px; font-weight: 700; cursor: pointer; }
.pwa-update button[data-pwa-update-apply] { background: #fff; color: #111827; }

/* ------------------------------------------------------------------ launch screen (installed sessions only) */
.pwa-splash { display: none; }
html.pwa-launching .pwa-splash {
    display: flex; position: fixed; inset: 0; z-index: 100000;
    align-items: center; justify-content: center;
    background: var(--pwa-splash-bg, #fff);
    opacity: 1; transition: opacity .2s ease;
    /* Safety net: even if scripts fail, the screen never blocks the page for more than 3 seconds. */
    animation: pwa-splash-failsafe 0s linear 3s forwards;
}
html.pwa-launching.pwa-launch-done .pwa-splash { opacity: 0; pointer-events: none; }
.pwa-splash-inner { display: grid; justify-items: center; gap: 26px; }
.pwa-splash-logo { width: 120px; height: 120px; object-fit: contain; }
.pwa-splash-spinner {
    width: 26px; height: 26px; border-radius: 50%;
    border: 3px solid color-mix(in srgb, var(--primary-color) 20%, transparent);
    border-top-color: var(--primary-color);
    animation: pwa-spin .8s linear infinite;
}
@keyframes pwa-spin { to { transform: rotate(360deg); } }
@keyframes pwa-splash-failsafe { to { opacity: 0; visibility: hidden; pointer-events: none; } }

/* ------------------------------------------------------------------ installed-mode app chrome */
.pwa-back { display: none; }
.pwa-bottom-nav { display: none; }

html.pwa-standalone {
    overscroll-behavior-y: none;
    -webkit-tap-highlight-color: transparent;
    --pwa-nav-h: 62px;
}
html.pwa-standalone :is(a, button, img) { -webkit-touch-callout: none; }
html.pwa-standalone :is(a, button, .pc-btn, .pw-install-btn):active { transition-duration: .08s; }

html.pwa-standalone .pwa-back {
    display: inline-flex; align-items: center; justify-content: center;
    width: 44px; height: 44px; margin-inline-end: 4px; padding: 0;
    border: 0; border-radius: 50%; background: transparent; color: var(--primary-color);
    cursor: pointer; touch-action: manipulation;
}
html.pwa-standalone .pwa-back:active { background: color-mix(in srgb, var(--primary-color) 12%, transparent); }
html.pwa-standalone .pwa-back:focus-visible { outline: 3px solid var(--primary-color); outline-offset: 0; }
html.pwa-standalone .pwa-back svg { width: 24px; height: 24px; }
html.pwa-standalone[dir="rtl"] .pwa-back svg { transform: scaleX(-1); }

@media (max-width: 991.98px) {
    html.pwa-standalone .pwa-bottom-nav {
        position: fixed; z-index: 1030; inset-inline: 0; bottom: 0;
        display: flex; align-items: stretch; justify-content: space-around;
        padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
        padding-inline: max(4px, env(safe-area-inset-left, 0px)) max(4px, env(safe-area-inset-right, 0px));
        background: rgba(255, 255, 255, .94);
        -webkit-backdrop-filter: blur(16px) saturate(1.3);
        backdrop-filter: blur(16px) saturate(1.3);
        border-top: 1px solid rgba(0, 0, 0, .07);
        box-shadow: 0 -4px 18px rgba(15, 23, 42, .06);
        transition: transform .2s ease;
    }
    html.pwa-standalone .pwa-tab {
        position: relative; flex: 1 1 0; min-width: 48px; min-height: 48px;
        display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
        padding: 4px 2px; border: 0; background: transparent; border-radius: 14px;
        color: #6b7280; font: inherit; font-size: .75rem; font-weight: 600; line-height: 1.2; text-decoration: none;
        cursor: pointer; touch-action: manipulation; transition: color .15s ease, background .15s ease, transform .1s ease;
    }
    html.pwa-standalone .pwa-tab:active { transform: scale(.94); background: color-mix(in srgb, var(--primary-color) 10%, transparent); }
    html.pwa-standalone .pwa-tab.is-active { color: var(--primary-color); }
    html.pwa-standalone .pwa-tab.is-active::after {
        content: ''; position: absolute; top: 0; width: 22px; height: 3px; border-radius: 0 0 3px 3px; background: var(--primary-color);
    }
    html.pwa-standalone .pwa-tab:focus-visible { outline: 3px solid var(--primary-color); outline-offset: -3px; }
    html.pwa-standalone .pwa-tab svg { width: 23px; height: 23px; }
    .pwa-tab-icon { position: relative; display: inline-flex; }
    .pwa-tab-badge {
        position: absolute; top: -5px; inset-inline-end: -9px; min-width: 17px; height: 17px; padding: 0 4px;
        border-radius: 9px; background: #dc2626; color: #fff; font-size: .75rem; font-weight: 800; line-height: 17px; text-align: center;
    }

    /* Space for the bar, so it never covers content or the footer. */
    html.pwa-standalone body:has(.pwa-bottom-nav) {
        --pwa-bottom-offset: calc(var(--pwa-nav-h) + env(safe-area-inset-bottom, 0px));
        padding-bottom: var(--pwa-bottom-offset);
    }
    /* Keep the existing fixed bars (cart checkout bar, product buy bar, back-to-top) above it. */
    html.pwa-standalone body:has(.pwa-bottom-nav) :is(.cart-sticky-bar, .pd-sticky-bar) {
        bottom: var(--pwa-bottom-offset);
        padding-bottom: 12px;
    }
    html.pwa-standalone body:has(.pwa-bottom-nav) .btt {
        bottom: calc(1rem + var(--pwa-bottom-offset) + var(--btt-lift, 0px));
    }
    html.pwa-standalone.pwa-kb-open .pwa-bottom-nav { transform: translateY(110%); }
    html.pwa-standalone.pwa-kb-open body { padding-bottom: 0; }
}

@media (max-width: 991.98px) and (orientation: landscape) and (max-height: 500px) {
    html.pwa-standalone { --pwa-nav-h: 52px; }
    html.pwa-standalone .pwa-tab { flex-direction: row; gap: 6px; font-size: .75rem; }
    html.pwa-standalone .pwa-tab svg { width: 20px; height: 20px; }
}

@media (prefers-reduced-motion: reduce) {
    .pw-install, .pwa-guide-backdrop, .pwa-guide-sheet { animation: none; }
    .pwa-splash-spinner { animation: none; border-top-color: color-mix(in srgb, var(--primary-color) 20%, transparent); border-right-color: var(--primary-color); }
    html.pwa-launching .pwa-splash, .pwa-bottom-nav, .pwa-tab { transition: none; }
}
