/* ============================================================
 * MatLuk FotoChallenge — Modals V.6
 *
 * Moderne Bottom-Sheet-Modals die auf jedem Geraet sauber
 * scrollen und sich richtig anfuehlen.
 *
 * Setzt sich an die Spitze der CSS-Reihenfolge weil base.html
 * diese Datei als LETZTES laed - damit alte konfliktierende
 * Regeln in style.css zuverlaessig ueberschrieben werden.
 *
 * Design-Prinzipien:
 * - Body-Scroll-Lock per position:fixed + Scroll-Snapshot (iOS-Standard)
 * - Modal-Card: feste max-Hoehe abhaengig von Screen, KEIN vh (dvh)
 * - Modal-Body: overflow-y:auto + min-height:0 fuer Flex-Scroll
 * - Bottom-Sheet auf Handy: am unteren Rand andocken, abgerundet oben
 * - Kein touch-action:none am Body (sonst iOS-Scroll-Bug)
 * ============================================================ */

/* ============================================================
   1. Layer: Modal-Wrapper + Backdrop
   ============================================================ */

/* Iter v6.49d: Bottom-Nav-Hoehe als CSS-Variable. Echte Nav-Hoehe = ~68px
   (per JS in base.html zur Laufzeit exakt gemessen, --bn-h hier nur Fallback).
   --bn-gap = zusaetzlicher kleiner Atemraum unter dem Modal damit es nicht
   direkt auf die Nav klatscht (sieht sauberer aus). */
:root {
    --bn-h: 68px;
    --bn-gap: 12px;
    --bn-h-full: calc(var(--bn-h) + var(--bn-gap) + env(safe-area-inset-bottom, 0px));
}

.ig-modal {
    /* Iter v6.48b: Wrapper spannt komplett ueber Viewport. Mit align-items:
       flex-end + Card-max-height = 100dvh - var(--bn-h-full) dockt die Card
       buendig an die Nav-Oberkante an, ohne Spalt.
       Auf Pages ohne Bottom-Nav greift body:not(.has-bottom-nav)-Override
       weiter unten. */
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    padding-bottom: calc(var(--bn-h-full) + 0px);
    box-sizing: border-box;
}
.ig-modal[hidden] {
    display: none;
}

/* Backdrop muss auch nur bis ueber die Nav gehen (sonst dunkelt er sie ab) */
.ig-modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.65);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    /* Touch-Handler hier durchlassen damit Tap-zum-Schliessen geht */
    cursor: pointer;
}

/* ============================================================
   2. Layer: Modal-Card (das eigentliche Fenster)
   ============================================================ */

.ig-modal-card {
    position: relative;
    z-index: 1;
    background: var(--bg-card, #1a1d24);
    border: 1px solid var(--border, rgba(255, 255, 255, 0.08));
    border-radius: 16px;
    box-shadow: 0 20px 48px rgba(0, 0, 0, 0.5);

    width: 100%;
    max-width: 480px;

    /* Hoehe: nutze dvh wenn verfuegbar (rechnet Adressleiste raus) */
    max-height: calc(100vh - 32px);
    max-height: calc(100dvh - 32px);

    /* Inneres Layout: Header oben fix, Body scrollt */
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden;
}

/* Photo-Modal darf etwas groesser sein */
.ig-modal-photo,
.ig-modal-card.ig-modal-photo {
    max-width: 600px;
}

/* ============================================================
   3. Layer: Header (bleibt oben fix beim Scrollen)
   ============================================================ */

.ig-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border, rgba(255, 255, 255, 0.08));
    flex: 0 0 auto;
    background: var(--bg-card, #1a1d24);
    /* Sticky innerhalb der Card als Fallback - mit overflow:hidden auf
       Card eigentlich nicht noetig, aber doppelt haelt besser */
    position: sticky;
    top: 0;
    z-index: 2;
    border-radius: 16px 16px 0 0;
}

.ig-modal-header h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 700;
    line-height: 1.3;
    flex: 1;
    min-width: 0;
}

.ig-modal-close {
    background: transparent;
    border: none;
    color: var(--text-dim, #9ca3af);
    cursor: pointer;
    font-size: 28px;
    line-height: 1;
    padding: 4px 8px;
    margin: -4px -4px;
    border-radius: 8px;
    transition: background 0.15s ease, color 0.15s ease;
    /* Touch-Target gross genug */
    min-width: 40px;
    min-height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.ig-modal-close:hover,
.ig-modal-close:focus-visible {
    background: rgba(255, 255, 255, 0.08);
    color: var(--text, #fff);
}

/* Iter V.6: Zurueck-Button im Modal-Header */
.ig-modal-back {
    background: transparent;
    border: none;
    color: var(--text-dim, #9ca3af);
    cursor: pointer;
    padding: 4px 8px;
    margin: -4px -4px;
    border-radius: 8px;
    transition: background 0.15s ease, color 0.15s ease, transform 0.15s ease;
    min-width: 40px;
    min-height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.ig-modal-back:hover,
.ig-modal-back:focus-visible {
    background: rgba(255, 255, 255, 0.08);
    color: var(--text, #fff);
    transform: translateX(-2px);
}
[data-theme="light"] .ig-modal-back:hover,
[data-theme="light"] .ig-modal-back:focus-visible {
    background: rgba(0, 0, 0, 0.05);
    color: #1f2937;
}

/* ============================================================
   4. Layer: Body (der scrollbare Bereich)
   ============================================================ */

.ig-modal-body {
    /* WICHTIG: Diese drei Eigenschaften zusammen sind das, was den
       Scroll im Flex-Container ueberhaupt erst ermoeglicht. */
    flex: 1 1 auto !important;
    min-height: 0 !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;

    /* iOS-Smooth-Scroll */
    -webkit-overflow-scrolling: touch;
    /* Verhindert dass der Scroll auf den Body-Lock dahinter durchschlaegt */
    overscroll-behavior: contain;

    /* Scroll-Indikator dezent */
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.2) transparent;

    padding: 16px 18px 20px;
    /* Nicht display:block oder display:flex - default ist OK damit
       overflow korrekt funktioniert. */
    display: block;
}

.ig-modal-body::-webkit-scrollbar {
    width: 6px;
}
.ig-modal-body::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 3px;
}

/* ============================================================
   5. Layer: Photo-Modal-spezifische Regeln
   Foto-Bereich oben fix, Rest scrollt drunter.

   Wichtig: hier explizit gegen alte 'display: block !important;
   overflow-y: auto' Strategie auf der Card aus style.css:9181 anschreiben.
   Strategie V.6: Card = Flex-Container, Body scrollt.
   ============================================================ */

.ig-modal-card.ig-modal-photo {
    /* Alte Regel sagt: Card scrollt selbst (display:block, overflow:auto).
       Wir wollen: Card ist Flex-Container, Body scrollt. */
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
}

.ig-modal-card.ig-modal-photo .ig-modal-body {
    /* Bei Foto-Modal: KEIN Padding oben weil Foto-Wrap full-bleed sein soll */
    padding: 0 0 20px !important;
    /* display:block damit der Foto-Wrap sauber oben sitzt und der Rest
       drunter normal flowed (caption, vis-form, actions) - aber MIT overflow */
    display: block !important;
    /* Diese drei Regeln gewinnen gegen style.css:9201 'overflow: visible !important' */
    overflow-y: auto !important;
    overflow-x: hidden !important;
    flex: 1 1 auto !important;
    min-height: 0 !important;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
}

.ig-modal-photo-wrap {
    width: 100%;
    background: #000;
    overflow: hidden;
    flex-shrink: 0;
    /* Foto-Hoehe begrenzt damit der Rest unten Platz hat */
    max-height: 50vh;
    max-height: 50dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 0 14px;
}

.ig-modal-photo-wrap img {
    width: 100%;
    height: auto;
    max-height: 50vh;
    max-height: 50dvh;
    object-fit: contain;
    display: block;
}

/* Caption + Form + Actions: jetzt mit Padding (weil Body padding:0 hat) */
.ig-modal-card.ig-modal-photo .ig-modal-meta,
.ig-modal-card.ig-modal-photo .ig-modal-caption,
.ig-modal-card.ig-modal-photo .modal-duel-banner,
.ig-modal-card.ig-modal-photo .photo-modal-section,
.ig-modal-card.ig-modal-photo .photo-modal-actions {
    padding-left: 16px;
    padding-right: 16px;
}

/* ============================================================
   6. Layer: Mobile Bottom-Sheet-Style
   Auf Handy: Modal docked am unteren Rand, abgerundet oben.
   ============================================================ */

@media (max-width: 540px) {
    .ig-modal {
        /* Iter v6.48b: Modal-Wrapper geht voll zum Boden, padding-bottom =
           Nav-Hoehe + safe-area. align-items: flex-end zieht Card buendig an
           die Nav-Oberkante. KEIN Spalt mehr. */
        padding: 16px 12px 0;
        padding-bottom: var(--bn-h-full);
        align-items: flex-end;
    }

    .ig-modal-card {
        width: 100%;
        max-width: 100%;
        margin: 0;
        /* Hoehe: voller Screen minus Nav minus oberer Atemraum (16px).
           dvh rechnet iOS-Adressleiste raus. */
        max-height: calc(100vh - 16px - var(--bn-h-full));
        max-height: calc(100dvh - 16px - var(--bn-h-full));
        /* Iter v6.45: Card komplett abgerundet (vorher nur oben - Bottom-Sheet-Style entfernt) */
        border-radius: 18px 18px 0 0;
    }

    .ig-modal-header {
        border-radius: 18px 18px 0 0;
        padding: 12px 14px;
    }

    .ig-modal-body {
        padding: 14px 16px 18px;
    }

    /* Photo-Modal: auch mittig statt Bottom-Sheet, nur so hoch wie noetig */
    /* Iter v6.48b: Foto-Modal sitzt direkt auf der Nav-Oberkante, ohne Spalt. */
    .ig-modal:has(.ig-modal-photo) {
        padding: 16px 0 0 !important;
        padding-bottom: var(--bn-h-full) !important;
        align-items: flex-end !important;
    }
    .ig-modal-card.ig-modal-photo {
        max-height: calc(100vh - var(--bn-h-full)) !important;
        max-height: calc(100dvh - var(--bn-h-full)) !important;
        height: auto !important;
        border-radius: 18px 18px 0 0 !important;
        margin-bottom: 0 !important;
    }

    .ig-modal-photo-wrap,
    .ig-modal-photo-wrap img {
        /* Foto kleiner: 40dvh statt 45dvh, sonst dominiert es das Modal */
        max-height: 40vh;
        max-height: 40dvh;
    }

    /* Iter v6.45: Drag-Indicator entfernt - keine Bottom-Sheet-Optik mehr. */
    .ig-modal-card::before {
        display: none;
    }
    .ig-modal-header {
        padding-top: 12px;
    }
}

/* Ohne Bottom-Nav (Login etc.): Modal mittig, voller Platz */
body:not(.has-bottom-nav) .ig-modal {
    padding: 16px;
    padding-bottom: 16px;  /* Iter v6.48b: --bn-h-full neutralisieren */
    align-items: center;
}
body:not(.has-bottom-nav) .ig-modal-card {
    max-height: calc(100vh - 32px);
    max-height: calc(100dvh - 32px);
    border-radius: 16px;
    margin: 0;
}
@media (max-width: 540px) {
    body:not(.has-bottom-nav) .ig-modal {
        padding: 0;
        padding-bottom: 0;  /* Iter v6.48b: --bn-h-full neutralisieren */
        align-items: flex-end;
    }
    body:not(.has-bottom-nav) .ig-modal-card {
        max-height: calc(100vh - 16px);
        max-height: calc(100dvh - 16px);
        border-radius: 18px 18px 0 0;
    }
}

/* ============================================================
   7. Layer: Body-Scroll-Lock (wenn Modal offen)
   Wichtig: KEIN touch-action:none am Body, sonst kann der Modal-
   Body auf iOS nicht scrollen.
   ============================================================ */

html.has-modal-open {
    /* Iter v6.45c: KEINE Layout-Aenderungen mehr am html-Element!
       In iOS-PWAs (display-mode: standalone) aendert overflow:hidden + height:100%
       auf <html> den Containing Block der position:fixed Bottom-Nav, sodass sie
       am Modal-Ende klebt statt am Bildschirmrand.
       Scroll-Lock laeuft jetzt AUSSCHLIESSLICH ueber touchmove-Blocker im JS
       (app.js: _bgTouchHandler). Body und html bleiben unangetastet. */
    touch-action: auto !important;
}
body.has-modal-open {
    /* Iter v6.45c: Body wird komplett in Ruhe gelassen.
       Bottom-Nav klebt zuverlaessig am Viewport-Boden - auch in iOS-PWA. */
    touch-action: auto !important;
}

/* Backdrop blockiert die Touch-Interaktion am Background */
.ig-modal-backdrop {
    touch-action: none;
}

/* Modal-Body: explizit Vertical-Scroll erlauben, Horizontal blocken */
.ig-modal-body {
    touch-action: pan-y;
}

/* ============================================================
   8. Layer: PWA-Modal (anderes Modal-Pattern)
   ============================================================ */

.pwa-modal-card {
    max-height: calc(100vh - 32px);
    max-height: calc(100dvh - 32px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
}
@media (max-width: 540px) {
    .pwa-modal-card {
        max-height: calc(100vh - 96px - env(safe-area-inset-bottom, 0));
        max-height: calc(100dvh - 96px - env(safe-area-inset-bottom, 0));
    }
}

/* ============================================================
   9. Light-Mode Anpassung
   ============================================================ */

[data-theme="light"] .ig-modal-card {
    background: #fff;
    border-color: rgba(0, 0, 0, 0.08);
    box-shadow: 0 20px 48px rgba(0, 0, 0, 0.18);
}
[data-theme="light"] .ig-modal-header {
    background: #fff;
    border-bottom-color: rgba(0, 0, 0, 0.08);
}
[data-theme="light"] .ig-modal-close:hover,
[data-theme="light"] .ig-modal-close:focus-visible {
    background: rgba(0, 0, 0, 0.05);
}
[data-theme="light"] .ig-modal-body {
    scrollbar-color: rgba(0, 0, 0, 0.2) transparent;
}
[data-theme="light"] .ig-modal-body::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
}
[data-theme="light"] .ig-modal-card::before {
    background: rgba(0, 0, 0, 0.2);
}
