/* ============================================================================
   Filema · iOS-native mobile & tablet layer
   ----------------------------------------------------------------------------
   ΟΛΕΣ οι σελίδες (επεκτείνουν base.html / auth_layout.html) αποκτούν αυτόματα
   iOS-like εμπειρία σε mobile & tablet:
     · off-canvas drawer sidebar + frosted bottom tab bar
     · safe-area insets (notch / home indicator)
     · momentum scrolling, tactile :active feedback, χωρίς tap highlight
     · adaptive πίνακες (momentum horizontal scroll + auto-card σε phones)
     · bottom-sheet modals
   Όλα είναι ΑΥΣΤΗΡΑ scoped σε mobile/tablet breakpoints (<1024px).
   Το desktop (≥1024px) ΔΕΝ επηρεάζεται καθόλου.
   ============================================================================ */

:root {
    --ios-safe-top: env(safe-area-inset-top, 0px);
    --ios-safe-bottom: env(safe-area-inset-bottom, 0px);
    --ios-safe-left: env(safe-area-inset-left, 0px);
    --ios-safe-right: env(safe-area-inset-right, 0px);
    --ios-tabbar-h: 56px;
    --ios-blue: #1e40af;
    --ios-radius: 16px;
    --ios-ease: cubic-bezier(0.32, 0.72, 0, 1); /* iOS-spring-like */
}

/* ───────────────────────── Global touch niceties ───────────────────────── */
@media (max-width: 1023px) {
    html {
        /* αποφυγή οριζόντιου bounce, native vertical scroll */
        -webkit-text-size-adjust: 100%;
    }
    body {
        -webkit-tap-highlight-color: transparent;
        -webkit-font-smoothing: antialiased;
        overscroll-behavior-y: none;
        text-rendering: optimizeLegibility;
    }
    /* κάθε κουμπί / σύνδεσμος αποκτά tactile feedback */
    a, button, .nav-link, [role="button"], .premium-card[onclick] {
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }
    a:active, button:active, .nav-link:active, [role="button"]:active {
        transition: transform 0.08s ease;
    }
    .ios-pressable:active { transform: scale(0.97); opacity: 0.85; }

    /* momentum scrolling παντού όπου υπάρχει scroll */
    main, .overflow-auto, .overflow-y-auto, .overflow-x-auto,
    .custom-scrollbar, nav {
        -webkit-overflow-scrolling: touch;
    }
}

/* ============================================================================
   1. OFF-CANVAS DRAWER SIDEBAR  (phones + tablets, <1024px)
   ============================================================================ */
@media (max-width: 1023px) {
    #sidebar {
        position: fixed !important;
        top: 0; left: 0; bottom: 0;
        height: 100% !important;
        max-height: 100% !important;
        width: 84vw !important;
        max-width: 320px !important;
        margin: 0 !important;
        border-radius: 0 22px 22px 0 !important;
        transform: translateX(-106%);
        transition: transform 0.38s var(--ios-ease);
        z-index: 130;
        box-shadow: 0 20px 60px -10px rgba(15, 23, 42, 0.45);
        padding-top: var(--ios-safe-top);
        padding-left: var(--ios-safe-left);
        will-change: transform;
    }
    /* Στο drawer θέλουμε ΠΑΝΤΑ πλήρη labels — ακυρώνουμε τις desktop καταστάσεις */
    #sidebar.sidebar-mini,
    #sidebar.sidebar-hidden {
        width: 84vw !important;
        max-width: 320px !important;
    }
    #sidebar.sidebar-mini .nav-label,
    #sidebar.sidebar-mini .sidebar-logo-text,
    #sidebar.sidebar-mini .nav-section-label { display: block !important; }
    #sidebar.sidebar-mini .nav-link { justify-content: flex-start !important; padding: 0.85rem 1rem !important; }
    #sidebar.sidebar-mini .nav-link svg,
    #sidebar.sidebar-mini .nav-link i { margin-right: 0.75rem !important; }
    #sidebar.sidebar-hidden { transform: translateX(-106%); }

    /* Ανοιχτό drawer */
    body.drawer-open #sidebar { transform: translateX(0) !important; }

    /* Μεγαλύτερα touch targets στα nav links */
    #sidebar .nav-link { padding: 0.9rem 1rem; font-size: 1rem; border-radius: 12px; }
    #sidebar .nav-link svg { width: 22px; height: 22px; }

    /* Backdrop */
    #drawer-backdrop {
        position: fixed; inset: 0;
        background: rgba(15, 23, 42, 0.45);
        backdrop-filter: blur(2px);
        -webkit-backdrop-filter: blur(2px);
        opacity: 0; pointer-events: none;
        transition: opacity 0.3s ease;
        z-index: 125;
    }
    body.drawer-open #drawer-backdrop { opacity: 1; pointer-events: auto; }
    body.drawer-open { overflow: hidden; }

    /* Το main δεν έχει πλέον αριστερό margin από το sidebar */
    #main-content { margin-left: 0 !important; }
}

/* Desktop: το drawer-backdrop & bottom tab bar δεν υπάρχουν */
@media (min-width: 1024px) {
    #drawer-backdrop, #ios-tabbar { display: none !important; }
}

/* ============================================================================
   2. iOS TOP BAR  (sticky, safe-area, large tap target)
   ============================================================================ */
@media (max-width: 1023px) {
    #main-content > header {
        padding-top: calc(0.5rem + var(--ios-safe-top));
        padding-left: calc(1rem + var(--ios-safe-left));
        padding-right: calc(1rem + var(--ios-safe-right));
        background: rgba(255, 255, 255, 0.82);
        -webkit-backdrop-filter: saturate(180%) blur(20px);
        backdrop-filter: saturate(180%) blur(20px);
        border-bottom: 0.5px solid rgba(0, 0, 0, 0.12);
        z-index: 90;
    }
    #main-content > header h2 {
        font-size: 1.0625rem;
        font-weight: 600;
        letter-spacing: -0.01em;
        max-width: none;
    }
    #main-content > header > button:first-child {
        width: 40px; height: 40px;
        display: flex; align-items: center; justify-content: center;
    }
}

/* ============================================================================
   3. BOTTOM TAB BAR  (frosted glass, safe-area, 5 items)
   ============================================================================ */
#ios-tabbar {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 120;
    display: flex;
    align-items: stretch;
    height: calc(var(--ios-tabbar-h) + var(--ios-safe-bottom));
    padding-bottom: var(--ios-safe-bottom);
    padding-left: var(--ios-safe-left);
    padding-right: var(--ios-safe-right);
    background: rgba(255, 255, 255, 0.80);
    -webkit-backdrop-filter: saturate(180%) blur(22px);
    backdrop-filter: saturate(180%) blur(22px);
    border-top: 0.5px solid rgba(0, 0, 0, 0.13);
}
#ios-tabbar .tab-item {
    flex: 1 1 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    color: #8e8e93;
    font-size: 10px;
    font-weight: 600;
    letter-spacing: -0.01em;
    text-decoration: none;
    padding-top: 6px;
    transition: color 0.15s ease, transform 0.08s ease;
    -webkit-tap-highlight-color: transparent;
}
#ios-tabbar .tab-item svg { width: 24px; height: 24px; stroke-width: 1.9; }
#ios-tabbar .tab-item.active { color: var(--ios-blue); }
#ios-tabbar .tab-item:active { transform: scale(0.90); }
#ios-tabbar .tab-item span { line-height: 1; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Δώσε χώρο στο περιεχόμενο ώστε να μην κρύβεται κάτω από το tab bar */
@media (max-width: 1023px) {
    #main-content > main {
        padding-bottom: calc(var(--ios-tabbar-h) + var(--ios-safe-bottom) + 1.25rem) !important;
        padding-left: calc(1rem + var(--ios-safe-left));
        padding-right: calc(1rem + var(--ios-safe-right));
    }
    /* Το footer περιττεύει σε mobile (καλύπτεται από το tab bar) */
    body > footer { display: none !important; }
}

/* ============================================================================
   4. ADAPTIVE TABLES
   ----------------------------------------------------------------------------
   · Default (όλα τα mobile): momentum horizontal scroll με κομψό scrollbar.
   · Phones (<=640px): «απλοί» πίνακες (επισημαίνονται από το JS με .ios-cardify)
     μετατρέπονται σε στοιβαγμένες κάρτες iOS-style.
   ============================================================================ */
@media (max-width: 1023px) {
    .ios-xscroll {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: thin;
    }
    .ios-xscroll::-webkit-scrollbar { height: 5px; }
    .ios-xscroll::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 10px; }
}

@media (max-width: 640px) {
    /* Card mode — εφαρμόζεται ΜΟΝΟ σε πίνακες που το JS έκρινε ασφαλείς */
    table.ios-cardify { display: block; width: 100%; }
    table.ios-cardify thead { display: none; }
    table.ios-cardify tbody { display: block; }
    table.ios-cardify tr {
        display: block;
        background: #fff;
        border: 1px solid #e5e7eb;
        border-radius: 14px;
        padding: 6px 14px;
        margin-bottom: 12px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
    }
    table.ios-cardify td {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        padding: 9px 0 !important;
        border: none !important;
        border-bottom: 0.5px solid #f1f5f9 !important;
        text-align: right;
        white-space: normal;
        min-width: 0;
    }
    table.ios-cardify tr td:last-child { border-bottom: none !important; }
    table.ios-cardify td::before {
        content: attr(data-label);
        flex: 0 0 42%;
        text-align: left;
        font-size: 0.72rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.03em;
        color: #94a3b8;
    }
    /* Κρύψε κελιά που δεν έχουν περιεχόμενο/label */
    table.ios-cardify td:empty { display: none; }
}

/* ============================================================================
   5. SEARCH MODAL → BOTTOM SHEET (phones)
   ============================================================================ */
@media (max-width: 640px) {
    #search-modal-backdrop {
        align-items: flex-end !important;
        padding: 0 !important;
        padding-top: 0 !important;
    }
    #search-modal-container {
        max-width: 100% !important;
        width: 100% !important;
        border-radius: 22px 22px 0 0 !important;
        max-height: 88vh !important;
        padding-bottom: var(--ios-safe-bottom);
    }
    /* grabber handle */
    #search-modal-container::before {
        content: "";
        display: block;
        width: 38px; height: 5px;
        background: #d1d5db;
        border-radius: 999px;
        margin: 9px auto 2px;
    }
    .modal-content-transition { transform: translateY(100%) !important; }
    .modal-content-active { transform: translateY(0) !important; }
}

/* ============================================================================
   6. FORM / INPUT iOS niceties (prevent zoom-on-focus, native feel)
   ============================================================================ */
@media (max-width: 1023px) {
    input:not([type=checkbox]):not([type=radio]),
    select, textarea, .input-premium, .ts-control input {
        font-size: 16px !important; /* iOS Safari δεν κάνει zoom με 16px+ */
    }
    /* μεγαλύτερα tap targets σε φόρμες */
    .input-premium, select, input[type=text], input[type=number],
    input[type=email], input[type=password], input[type=date] {
        min-height: 44px;
    }
    button, .btn-primary, [role="button"] { min-height: 44px; }
}

/* ============================================================================
   7. TABLET refinements (768–1023px)  — π.χ. iPad portrait
   ============================================================================ */
@media (min-width: 768px) and (max-width: 1023px) {
    #sidebar { width: 60vw !important; max-width: 360px !important; }
    #ios-tabbar { padding-left: 8vw; padding-right: 8vw; }
    #main-content > main { padding-left: calc(1.5rem + var(--ios-safe-left)); padding-right: calc(1.5rem + var(--ios-safe-right)); }
    /* σε tablet προτιμάμε scroll αντί για card mode στους πίνακες (περισσότερος χώρος) */
}

/* ============================================================================
   8. Misc — fixes για mobile viewport
   ============================================================================ */
@media (max-width: 1023px) {
    /* full dynamic viewport height (διορθώνει το «πηδηχτό» Safari toolbar) */
    .flex.min-h-\[calc\(100vh-4rem\)\] { min-height: 100dvh; }
    /* το global search modal πάνω από το tab bar */
    #search-modal-backdrop { z-index: 200; }
}
