/*
 * app-mode.css — Mode "App" styling for Coverband PWA / Capacitor wrapper
 * -----------------------------------------------------------------------
 * Activated when:
 *   - @media (display-mode: standalone)   → PWA installed on home screen
 *   - html.capacitor                      → native APK via Capacitor wrapper
 *
 * Design goals (issue #359):
 *   - Dedicated bottom tab bar navigation (5 icons)
 *   - Hide web-only sections (admin, staff, promo, landing, analytics)
 *   - Larger typography and touch targets (WCAG AAA 44x44 px)
 *   - Safe-area insets for iOS notch and Android gesture nav
 *   - Respect prefers-reduced-motion
 */

/* ============================================================
 * 1. App-mode activation
 * ============================================================
 * Default (web): tab bar hidden, app-mode styles inert.
 * Standalone PWA OR Capacitor: activate app mode.
 */

.app-tabbar {
    display: none;
}

/* --- Standalone PWA --- */
@media (display-mode: standalone) {
    .app-tabbar {
        display: flex;
    }

    /* Hide web-only nav items in both desktop and mobile-web nav */
    .app-hide-on-app {
        display: none !important;
    }

    /* Reserve space for bottom tab bar + safe areas (iOS notch, Android gesture bar) */
    body {
        padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px));
        padding-top: env(safe-area-inset-top, 0px);
        padding-left: env(safe-area-inset-left, 0px);
        padding-right: env(safe-area-inset-right, 0px);
    }

    /* Base font: 17px but capped so system font scaling doesn't
       blow up the layout. clamp() keeps it between 16–20px even
       if the user cranks their phone font to max. */
    html {
        font-size: clamp(16px, 17px, 20px);
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }
}

/* --- Capacitor wrapper (same rules as standalone) --- */
html.capacitor .app-tabbar {
    display: flex;
}

html.capacitor .app-hide-on-app {
    display: none !important;
}

html.capacitor body {
    padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px));
    padding-top: env(safe-area-inset-top, 0px);
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
}

html.capacitor {
    font-size: clamp(16px, 17px, 20px);
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

/* ============================================================
 * 2. Bottom tab bar — "Amp Panel" aesthetic
 * ============================================================
 * Dark brushed panel with warm amber LED glow on the active tab.
 * Feels like the front panel of a vintage guitar amplifier.
 */

.app-tabbar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 60;
    /* Layered dark with subtle warmth — not pure black */
    background:
        linear-gradient(180deg, #2A2425 0%, #1C1819 100%);
    border-top: 1px solid rgba(255, 150, 50, 0.12);
    box-shadow:
        0 -1px 0 rgba(255, 255, 255, 0.04),
        0 -8px 24px rgba(0, 0, 0, 0.45);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    justify-content: space-around;
    align-items: stretch;
    height: calc(64px + env(safe-area-inset-bottom, 0px));
}

.app-tabbar__item {
    flex: 1 1 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 6px 4px 10px;
    color: #7A7374;
    text-decoration: none;
    min-height: 44px;
    transition: color 160ms ease, transform 100ms ease;
    position: relative;
    -webkit-tap-highlight-color: transparent;
}

/* Press feedback — feels like pushing a footswitch */
.app-tabbar__item:active {
    transform: scale(0.92);
}

.app-tabbar__item:hover,
.app-tabbar__item:focus-visible {
    color: #FFBC6E;
}

.app-tabbar__item:focus-visible {
    outline: none;
    box-shadow: inset 0 0 0 2px rgba(255, 188, 110, 0.5);
    border-radius: 8px;
}

/* Active tab — amber LED glow */
.app-tabbar__item--active {
    color: #FF8C20;
}

/* Glowing indicator bar (like an amp's power LED) */
.app-tabbar__item--active::before {
    content: "";
    position: absolute;
    top: 0;
    left: 25%;
    right: 25%;
    height: 3px;
    background: #FF8C20;
    border-radius: 0 0 4px 4px;
    box-shadow:
        0 0 6px rgba(255, 140, 32, 0.7),
        0 0 16px rgba(255, 140, 32, 0.3),
        0 2px 8px rgba(255, 140, 32, 0.4);
}

/* Subtle warm backlight behind active icon */
.app-tabbar__item--active::after {
    content: "";
    position: absolute;
    top: 6px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(255, 140, 32, 0.12) 0%, transparent 70%);
    pointer-events: none;
}

.app-tabbar__icon {
    font-size: 21px;
    line-height: 1;
    margin-bottom: 4px;
    position: relative;
    z-index: 1;
}

.app-tabbar__label {
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    line-height: 1.1;
    text-align: center;
    position: relative;
    z-index: 1;
    /* Immune to system font scaling — fixed px, not rem */
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Notification badge — warm red, slightly raised */
.app-tabbar__badge {
    position: absolute;
    top: 4px;
    right: calc(50% - 18px);
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border-radius: 8px;
    background: #D93025;
    color: white;
    font-size: 10px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    box-shadow: 0 1px 4px rgba(217, 48, 37, 0.5);
    z-index: 2;
}

/* ============================================================
 * 3. Touch targets and density (app mode)
 * ============================================================
 * Enforce WCAG AAA 44×44 touch targets globally in app mode, with
 * opt-outs via [data-no-touch] or elements inside the top navbar
 * (icon-only buttons that are intentionally compact).
 */

@media (display-mode: standalone) {

    main button,
    main a.btn,
    main a.button,
    main input[type="submit"],
    main input[type="button"],
    main input[type="text"],
    main input[type="email"],
    main input[type="password"],
    main input[type="search"],
    main input[type="tel"],
    main input[type="url"],
    main input[type="number"],
    main select,
    main textarea {
        min-height: 44px;
    }

    main [data-no-touch],
    main .no-touch-enforce,
    main .no-touch-enforce * {
        min-height: initial;
    }

    /* Generous line-height and comfortable reading width */
    main {
        line-height: 1.6;
    }

    /* Cards and list items become a thumb-friendly 56 px minimum */
    main .card,
    main .list-item,
    main [role="listitem"] {
        min-height: 56px;
    }

    /* Focus rings renforced for accessibility on touch devices */
    :focus-visible {
        outline: 3px solid rgba(255, 97, 32, 0.75);
        outline-offset: 2px;
    }

    /* Slightly increase letter-spacing on buttons for legibility */
    button,
    .btn,
    .button {
        letter-spacing: 0.01em;
    }
}

html.capacitor main button,
html.capacitor main a.btn,
html.capacitor main input[type="submit"],
html.capacitor main input[type="button"],
html.capacitor main input[type="text"],
html.capacitor main input[type="email"],
html.capacitor main input[type="password"],
html.capacitor main input[type="search"],
html.capacitor main select,
html.capacitor main textarea {
    min-height: 44px;
}

html.capacitor main {
    line-height: 1.6;
}

/* Debug override: same rules when ?appmode=1 is on */
html.app-mode-debug main button,
html.app-mode-debug main a.btn,
html.app-mode-debug main input[type="submit"],
html.app-mode-debug main input[type="button"],
html.app-mode-debug main input[type="text"],
html.app-mode-debug main input[type="email"],
html.app-mode-debug main input[type="password"],
html.app-mode-debug main input[type="search"],
html.app-mode-debug main select,
html.app-mode-debug main textarea {
    min-height: 44px;
}

html.app-mode-debug main {
    line-height: 1.6;
}

/* ============================================================
 * 4. Offline banner (visible in app mode when navigator.onLine === false)
 * ============================================================ */

.app-offline-banner {
    display: none;
    position: fixed;
    top: env(safe-area-inset-top, 0px);
    left: 0;
    right: 0;
    z-index: 70;
    background: linear-gradient(90deg, #B01808 0%, #C81A11 50%, #B01808 100%);
    color: white;
    padding: 10px 16px;
    font-size: 13px;
    font-weight: 700;
    text-align: center;
    letter-spacing: 0.03em;
    text-transform: uppercase;
    box-shadow:
        0 2px 8px rgba(200, 26, 17, 0.3),
        inset 0 -1px 0 rgba(0, 0, 0, 0.15);
}

body.app-offline .app-offline-banner {
    display: block;
}

/* ============================================================
 * 4a. Floating Action Button (FAB)
 * ============================================================
 * Primary action shortcut, fixed to the bottom-right above the
 * tab bar. Visible only in app mode (standalone/Capacitor/debug).
 *
 * Usage:
 *   <a href="{% url 'songs:create' %}" class="app-fab" aria-label="Add song">
 *     <i class="fas fa-plus" aria-hidden="true"></i>
 *   </a>
 */

.app-fab {
    display: none;
    position: fixed;
    right: max(16px, env(safe-area-inset-right, 16px));
    bottom: calc(64px + 16px + env(safe-area-inset-bottom, 0px));
    width: 56px;
    height: 56px;
    border-radius: 16px;
    /* Warm gradient — like an amp's power button */
    background: linear-gradient(135deg, #FF8C20 0%, #FF5510 100%);
    color: #231F20;
    font-size: 22px;
    align-items: center;
    justify-content: center;
    box-shadow:
        0 4px 12px rgba(255, 97, 32, 0.35),
        0 8px 24px rgba(0, 0, 0, 0.2),
        inset 0 1px 0 rgba(255, 255, 255, 0.2);
    text-decoration: none;
    z-index: 55;
    transition: transform 120ms ease, box-shadow 120ms ease;
    border: none;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.app-fab:hover,
.app-fab:focus-visible {
    transform: scale(1.06);
    box-shadow:
        0 6px 20px rgba(255, 97, 32, 0.45),
        0 12px 32px rgba(0, 0, 0, 0.25),
        inset 0 1px 0 rgba(255, 255, 255, 0.25);
    outline: none;
}

.app-fab:active {
    transform: scale(0.92);
    box-shadow:
        0 2px 8px rgba(255, 97, 32, 0.3),
        0 4px 12px rgba(0, 0, 0, 0.15);
}

.app-fab--extended {
    width: auto;
    border-radius: 16px;
    padding: 0 22px;
    gap: 8px;
    font-size: 15px;
    font-weight: 700;
}

@media (display-mode: standalone) {
    .app-fab {
        display: flex;
    }
}

html.capacitor .app-fab {
    display: flex;
}

html.app-mode-debug .app-fab {
    display: flex;
}

/* ============================================================
 * 4b. Bottom sheet component
 * ============================================================
 * Usage:
 *   <div class="app-sheet" data-app-sheet id="my-sheet" hidden>
 *     <div class="app-sheet__backdrop" data-sheet-close></div>
 *     <div class="app-sheet__panel">
 *       <div class="app-sheet__handle" aria-hidden="true"></div>
 *       <div class="app-sheet__content">...</div>
 *     </div>
 *   </div>
 *
 * Open with: window.CoverbandAppMode.sheet.open('my-sheet')
 * Close with: window.CoverbandAppMode.sheet.close('my-sheet') or
 *             clicking backdrop / pressing Escape.
 *
 * Style is identical on web and app — intentionally. A bottom
 * sheet is an ergonomic modal on all form factors; we just make
 * sure it is wide-friendly with max-width on desktop.
 */

.app-sheet {
    position: fixed;
    inset: 0;
    z-index: 80;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
}

.app-sheet[hidden] {
    display: none;
}

.app-sheet__backdrop {
    position: absolute;
    inset: 0;
    background: rgba(20, 16, 14, 0.65);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    opacity: 0;
    transition: opacity 220ms ease;
}

.app-sheet--open .app-sheet__backdrop {
    opacity: 1;
}

.app-sheet__panel {
    position: relative;
    width: 100%;
    max-width: 640px;
    margin: 0 auto;
    /* Warm parchment with subtle grain feel */
    background:
        linear-gradient(180deg, #FFF9F0 0%, #FFF4E6 100%);
    color: #231F20;
    border-radius: 24px 24px 0 0;
    box-shadow:
        0 -4px 24px rgba(0, 0, 0, 0.2),
        0 -1px 0 rgba(255, 150, 50, 0.08),
        inset 0 1px 0 rgba(255, 255, 255, 0.6);
    transform: translateY(100%);
    transition: transform 280ms cubic-bezier(0.22, 0.68, 0, 1.04);
    max-height: calc(100vh - 48px);
    display: flex;
    flex-direction: column;
    padding-bottom: env(safe-area-inset-bottom, 0px);
}

.app-sheet--open .app-sheet__panel {
    transform: translateY(0);
}

.app-sheet__handle {
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: rgba(35, 31, 32, 0.15);
    margin: 12px auto 6px;
    flex-shrink: 0;
}

.app-sheet__title {
    font-size: 18px;
    font-weight: 800;
    text-align: center;
    padding: 4px 16px 0;
    flex-shrink: 0;
    letter-spacing: -0.01em;
}

.app-sheet__content {
    padding: 16px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.app-sheet__actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 8px 16px 16px;
    flex-shrink: 0;
}

.app-sheet__action {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 50px;
    padding: 14px 20px;
    border-radius: 14px;
    font-size: 16px;
    font-weight: 700;
    background: linear-gradient(135deg, #FF8C20 0%, #FF5510 100%);
    color: #FFF9F0;
    border: none;
    cursor: pointer;
    text-decoration: none;
    transition: transform 100ms ease, box-shadow 100ms ease;
    box-shadow:
        0 2px 8px rgba(255, 85, 16, 0.25),
        inset 0 1px 0 rgba(255, 255, 255, 0.15);
    -webkit-tap-highlight-color: transparent;
    letter-spacing: 0.01em;
}

.app-sheet__action:hover,
.app-sheet__action:focus-visible {
    outline: none;
    box-shadow:
        0 4px 16px rgba(255, 85, 16, 0.35),
        inset 0 1px 0 rgba(255, 255, 255, 0.2);
}

.app-sheet__action:active {
    transform: scale(0.97);
}

.app-sheet__action--secondary {
    background: transparent;
    color: #231F20;
    border: 1.5px solid rgba(35, 31, 32, 0.15);
    box-shadow: none;
}

.app-sheet__action--secondary:hover,
.app-sheet__action--secondary:focus-visible {
    background: rgba(35, 31, 32, 0.04);
    box-shadow: none;
}

.app-sheet__action--danger {
    background: linear-gradient(135deg, #E02D1B 0%, #B01808 100%);
    color: white;
    box-shadow:
        0 2px 8px rgba(200, 26, 17, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

.app-sheet__action--danger:hover,
.app-sheet__action--danger:focus-visible {
    box-shadow:
        0 4px 16px rgba(200, 26, 17, 0.4),
        inset 0 1px 0 rgba(255, 255, 255, 0.15);
}

/* ============================================================
 * 4c. Pull-to-refresh
 * ============================================================
 * Add data-pull-to-refresh to any scrollable region. On app mode
 * a spinner appears at the top when the user drags past 60px and
 * releases, reloading the page.
 */

.app-ptr {
    position: relative;
    /* necessary so that .app-ptr__indicator absolute positioning works */
}

.app-ptr__indicator {
    position: absolute;
    top: -50px;
    left: 0;
    right: 0;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #231F20;
    font-size: 18px;
    pointer-events: none;
    transform: translateY(0);
    transition: transform 180ms ease;
    z-index: 5;
}

.app-ptr--pulling .app-ptr__indicator {
    transform: translateY(var(--ptr-offset, 0px));
}

.app-ptr--refreshing .app-ptr__indicator {
    transform: translateY(50px);
}

.app-ptr__spinner {
    animation: app-ptr-spin 800ms linear infinite;
    transform-origin: 50% 50%;
}

@keyframes app-ptr-spin {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

/* ============================================================
 * 5. Reduced motion
 * ============================================================ */

@media (prefers-reduced-motion: reduce) {

    .app-tabbar__item,
    .app-offline-banner,
    .app-sheet__backdrop,
    .app-sheet__panel,
    .app-ptr__indicator,
    .app-ptr__spinner,
    .app-fab {
        transition: none !important;
        animation: none !important;
    }
}

/* ============================================================
 * 5b. Hide install prompts in app mode
 * ============================================================
 * Both install banners are already gated by !isStandalone() in
 * base.html, but that check misses Capacitor and the debug mode.
 * Belt-and-braces CSS ensures they never leak onto the app UI.
 */

@media (display-mode: standalone) {

    #pwa-install-banner,
    #ios-install-banner {
        display: none !important;
    }
}

html.capacitor #pwa-install-banner,
html.capacitor #ios-install-banner {
    display: none !important;
}

html.app-mode-debug #pwa-install-banner,
html.app-mode-debug #ios-install-banner {
    display: none !important;
}

/* ============================================================
 * 6. Compact navbar in app mode
 * ============================================================
 * The default top navbar is h-16 (64px) with heavy wood texture
 * and a prominent brand block. In app mode the bottom tab bar is
 * the primary nav, so the top bar becomes a thin header that
 * shows only brand + notifications + user avatar.
 */

.app-mode-shrink {
    /* Base state is unchanged; rules below only activate in app mode. */
}

@media (display-mode: standalone) {
    #global-navbar {
        box-shadow:
            0 1px 0 rgba(255, 150, 50, 0.06),
            0 2px 8px rgba(0, 0, 0, 0.15);
    }

    #global-navbar .flex.justify-between.h-16 {
        height: 48px;
    }

    /* Hide the wood-texture background — keep it clean */
    #global-navbar > div.absolute.inset-0 {
        display: none;
    }

    #global-navbar img[alt="Cover Band Logo"] {
        height: 30px;
        width: 30px;
        border-radius: 8px;
    }

    #global-navbar .text-xl.font-bold.text-white {
        font-size: 15px;
        letter-spacing: -0.01em;
    }

    #global-footer {
        display: none;
    }
}

html.capacitor #global-navbar .flex.justify-between.h-16 {
    height: 48px;
}

html.capacitor #global-navbar > div.absolute.inset-0 {
    display: none;
}

html.capacitor #global-navbar img[alt="Cover Band Logo"] {
    height: 30px;
    width: 30px;
    border-radius: 8px;
}

html.capacitor #global-navbar .text-xl.font-bold.text-white {
    font-size: 15px;
    letter-spacing: -0.01em;
}

html.capacitor #global-footer {
    display: none;
}

/* ============================================================
 * 7. Debug helper (toggle via app-mode.js)
 * ============================================================
 * When html.app-mode-debug is set, force-show the tab bar
 * regardless of standalone / capacitor state. Useful for dev.
 */

html.app-mode-debug .app-tabbar {
    display: flex;
}

html.app-mode-debug .app-hide-on-app {
    display: none !important;
}

html.app-mode-debug body {
    padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px));
}

html.app-mode-debug #global-navbar .flex.justify-between.h-16 {
    height: 48px;
}

html.app-mode-debug #global-navbar > div.absolute.inset-0 {
    display: none;
}

html.app-mode-debug #global-navbar img[alt="Cover Band Logo"] {
    height: 30px;
    width: 30px;
    border-radius: 8px;
}

html.app-mode-debug #global-navbar .text-xl.font-bold.text-white {
    font-size: 15px;
    letter-spacing: -0.01em;
}

html.app-mode-debug #global-footer {
    display: none;
}

/* ============================================================
 * 8. Font scaling protection
 * ============================================================
 * Users who crank system font size to max (accessibility or by
 * mistake) can break the app layout. Strategy:
 *   - Chrome (navbar, tab bar, FAB, banners): fixed px sizes,
 *     immune to scaling. These structural elements MUST fit.
 *   - Content (main): allowed to scale but capped via clamp()
 *     on the root font-size (see §1). Titles and buttons use
 *     max() to stay readable but not enormous.
 *   - overflow protection on cramped elements.
 */

@media (display-mode: standalone) {
    /* Navbar stays compact regardless of font scaling */
    #global-navbar,
    #global-navbar * {
        font-size: max(13px, 0.85rem);
    }

    #global-navbar .text-xl.font-bold.text-white {
        font-size: 15px !important;
    }

    /* Cap heading sizes in content */
    main h1 { font-size: clamp(20px, 1.75rem, 28px); }
    main h2 { font-size: clamp(18px, 1.5rem, 24px); }
    main h3 { font-size: clamp(16px, 1.25rem, 22px); }

    /* Buttons: readable but bounded */
    main button,
    main a.btn,
    main input[type="submit"] {
        font-size: clamp(14px, 1rem, 18px);
    }

    /* Prevent text from overflowing card containers */
    main .truncate,
    main h1, main h2, main h3 {
        overflow-wrap: break-word;
        word-break: break-word;
    }
}

html.capacitor #global-navbar,
html.capacitor #global-navbar * {
    font-size: max(13px, 0.85rem);
}

html.app-mode-debug #global-navbar,
html.app-mode-debug #global-navbar * {
    font-size: max(13px, 0.85rem);
}

/* ============================================================
 * 9. Fixed bottom bars above the tab bar
 * ============================================================
 * Any fixed element pinned to the bottom of the screen (floating
 * action bars, cookie banners, etc.) must sit above the 64px tab
 * bar in app mode, or it gets hidden underneath.
 */

@media (display-mode: standalone) {
    [style*="position:fixed"][style*="bottom:"],
    [style*="position: fixed"][style*="bottom:"],
    .fixed.bottom-0,
    .fixed.bottom-4,
    #floating-absence-bar {
        bottom: calc(64px + 16px + env(safe-area-inset-bottom, 0px)) !important;
    }
}

html.capacitor [style*="position:fixed"][style*="bottom:"],
html.capacitor .fixed.bottom-0,
html.capacitor .fixed.bottom-4,
html.capacitor #floating-absence-bar {
    bottom: calc(64px + 16px + env(safe-area-inset-bottom, 0px)) !important;
}

html.app-mode-debug [style*="position:fixed"][style*="bottom:"],
html.app-mode-debug .fixed.bottom-0,
html.app-mode-debug .fixed.bottom-4,
html.app-mode-debug #floating-absence-bar {
    bottom: calc(64px + 16px + env(safe-area-inset-bottom, 0px)) !important;
}
