/* src/shared/styles.css - REPLACE THE TOP :root BLOCK */

:root {
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (max-width: 768px) {
    /* 🚨 ANDROID ONLY: Chrome/Blink engine */
    /* Android needs the max() hack to clear the 34px status bar */
    @supports not (-webkit-touch-callout: none) {
        :root {
            --safe-top: max(env(safe-area-inset-top, 0px), 34px);
            --safe-bottom: max(env(safe-area-inset-bottom, 0px), 16px);
        }
    }
    
    /* 🚨 iOS ONLY: Apple WebKit engine */
    /* WebKit crashes if we use max(env()), so we feed it pure env() */
    @supports (-webkit-touch-callout: none) {
        :root {
            --safe-top: env(safe-area-inset-top, 0px);
            --safe-bottom: env(safe-area-inset-bottom, 0px);
        }
    }
}

/* --- RESET & BASICS --- */
:host { 
    display: block; 
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: var(--text-main);
}

*, *::before, *::after { 
    box-sizing: border-box; 
}

/* Global wrapper */
/* 🚨 FIX: Replaced env() with CSS Variable for global container */
#main-container, .container { 
    padding: calc(var(--safe-top, 0px) + 20px) 20px calc(var(--safe-bottom, 0px) + 20px) 20px;
    box-sizing: border-box;
    width: 100%;
}

/* --- UNIFIED EDGE ALIGNMENT --- */
.header, .search-row, .card, .section-card, .card-list, .card-list-container {
    width: 100%;
    max-width: 900px;
    margin-left: auto !important;
    margin-right: auto !important;
    box-sizing: border-box;
}

/* --- HEADERS --- */
.header { 
    display: flex; 
    align-items: center; 
    justify-content: space-between; 
    gap: 20px; 
    margin-bottom: 20px; 
}

h2 { 
    margin: 0; 
    font-size: 1.5rem; 
    font-weight: 700; 
    flex: 1; 
    text-align: center; 
    color: var(--text-main);
}

/* --- SEARCH BARS --- */
.search-row { 
    margin-bottom: 20px; 
}

.search-bar { 
    width: 100%; 
    box-sizing: border-box; 
}

/* --- CARDS --- */
.card, .section-card, .card-list-container, .card-list {
    background: var(--bg-panel); 
    border-radius: 12px; 
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 15px var(--shadow);
    margin-bottom: 24px;
    overflow: hidden;
    padding: 0;
}

/* --- STANDARD LIST ROWS --- */
.row, .list-row, .product-row, .movie-row { 
    display: flex; 
    align-items: center; 
    padding: 20px; 
    border-bottom: 1px solid var(--border-color); 
    gap: 20px; 
    transition: background 0.1s; 
    background: transparent;
}

.row:hover, .list-row:hover, .product-row:hover, .movie-row:hover { 
    background: rgba(0,0,0,0.02); 
}

.row:last-child, .list-row:last-child, .product-row:last-child, .movie-row:last-child { 
    border-bottom: none; 
}

.swipe-surface { 
    display: contents; 
}

.list-info, .info { 
    flex: 1; 
    display: flex; 
    flex-direction: row; 
    flex-wrap: wrap; 
    align-items: center; 
    gap: 20px; 
    width: 100%; 
    overflow: hidden;
}

.data-field { 
    flex: 1 1 0%; 
    min-width: 0; 
    display: flex; 
    flex-direction: column;
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis;
}

.field-label { 
    font-size: 0.65rem; 
    text-transform: uppercase; 
    color: var(--text-muted); 
    font-weight: 800; 
    letter-spacing: 0.5px; 
    margin-bottom: 4px; 
    white-space: nowrap;
}

.field-value { 
    font-size: 1rem; 
    font-weight: 500; 
    color: var(--text-main); 
}

.header-label { 
    display: none; 
    font-size: 0.7rem; 
    text-transform: uppercase; 
    color: var(--text-muted); 
    font-weight: 800; 
}

/* --- ICONS --- */
.icon-box, .list-avatar { 
    width: 44px; 
    height: 44px; 
    min-width: 44px;
    background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
    color: #fff; 
    border-radius: 12px; 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    font-weight: 700; 
    font-size: 1.2rem; 
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.icon-box svg, .list-avatar svg { 
    width: 24px; 
    height: 24px; 
    fill: currentColor; 
    display: block; 
}

/* --- TABLE VIEW GLOBAL OVERRIDES --- */
:host([layout="table"]) .list-row, 
:host([layout="table"]) .product-row, 
:host([layout="table"]) .movie-row { 
    padding: 10px 20px; 
    align-items: center; 
}
:host([layout="table"]) .field-label { 
    display: none !important; 
} 
:host([layout="table"]) .header-label { 
    display: block !important; 
} 
:host([layout="table"]) .list-info, :host([layout="table"]) .info { 
    flex-wrap: nowrap !important; 
}
:host([layout="table"]) .data-field { 
    flex: 1 1 0%; 
    min-width: 0; 
    margin-bottom: 0 !important; 
}
:host([layout="table"]) .data-field.col-id { 
    flex: 0 0 50px; 
    min-width: 50px; 
}
:host([layout="table"]) .data-field.col-price { 
    flex: 0 0 100px; 
    min-width: 100px; 
}
:host([layout="table"]) .data-field.col-type { 
    flex: 0 0 100px; 
    min-width: 100px; 
}
:host([layout="table"]) .data-field.col-status { 
    flex: 0 0 100px; 
    min-width: 100px; 
}
:host([layout="table"]) .data-field.col-year { 
    flex: 0 0 80px; 
    min-width: 80px; 
}
:host([layout="table"]) .data-field.col-rating { 
    flex: 0 0 80px; 
    min-width: 80px; 
}
:host([layout="table"]) .data-field.col-watched { 
    flex: 0 0 80px; 
    min-width: 80px; 
}
:host([layout="table"]) .data-field.col-role { 
    flex: 0 0 100px; 
    min-width: 100px; 
}

:host([layout="table"]) .table-header { 
    background: rgba(0,0,0,0.03); 
    border-bottom: 2px solid var(--border-color); 
    pointer-events: none; 
    padding: 12px 20px; 
    display: flex !important; 
    align-items: center; 
    gap: 20px;
}

:host([layout="table"]) .icon-box, :host([layout="table"]) .list-avatar { 
    width: 32px; 
    height: 32px; 
    min-width: 32px; 
    font-size: 0.9rem; 
    border-radius: 8px; 
    flex-shrink: 0 !important;
}

.actions { 
    display: flex; 
    gap: 10px; 
    justify-content: center; 
    align-items: center; 
}

:host([layout="table"]) .actions, :host([layout="table"]) .actions-header {
    width: 100px !important; 
    min-width: 100px !important; 
    flex-shrink: 0 !important;
    justify-content: center !important; 
    text-align: center !important;
}

/* --- BUTTONS --- */
.btn-back, .btn-circle {
    width: 44px; 
    height: 44px; 
    min-width: 44px; 
    border-radius: 50%; 
    border: none;
    background: var(--bg-panel); 
    color: var(--text-main);
    display: flex; 
    align-items: center; 
    justify-content: center;
    cursor: pointer; 
    box-shadow: 0 4px 12px var(--shadow);
    transition: transform 0.2s; 
    flex-shrink: 0; 
    padding: 0;
}

.btn-back:hover, .btn-circle:hover { 
    transform: scale(1.1); 
}

.btn-back svg, .btn-circle svg { 
    width: 24px; 
    height: 24px; 
    fill: currentColor; 
}

.btn, .btn-primary, .btn-secondary, .btn-danger {
    padding: 10px 20px; 
    border-radius: 8px; 
    font-weight: 600; 
    cursor: pointer; 
    border: none; 
    font-size: 0.95rem; 
    transition: transform 0.1s, opacity 0.2s;
    display: inline-flex; 
    align-items: center; 
    justify-content: center; 
    text-decoration: none;
}

.btn:hover { 
    opacity: 0.9; 
    transform: translateY(-1px); 
}

.btn-primary { 
    background: var(--accent-color); 
    color: #fff; 
    box-shadow: 0 4px 10px rgba(0,0,0,0.2); 
    width: 100%; 
}

.btn-secondary { 
    background: var(--bg-app); 
    border: 1px solid var(--border-color); 
    color: var(--text-main); 
}

.btn-danger { 
    background: #dc3545; 
    color: white; 
}

.btn-icon {
    width: 38px; 
    height: 38px; 
    padding: 0; 
    border-radius: 50%;
    color: var(--text-muted); 
    background: transparent; 
    border: none;
    cursor: pointer; 
    display: flex; 
    align-items: center; 
    justify-content: center;
    transition: background-color 0.2s, color 0.2s; 
    flex-shrink: 0;
}

.btn-icon svg { 
    width: 20px; 
    height: 20px; 
    fill: currentColor; 
}

.btn-icon:hover { 
    background-color: rgba(0,0,0,0.05); 
    color: var(--text-main); 
}

.btn-icon.edit:hover { 
    color: #3498db; 
    background: rgba(52, 152, 219, 0.1); 
}

.btn-icon.delete:hover { 
    color: #e74c3c; 
    background: rgba(231, 76, 60, 0.1); 
}

/* --- FORMS GLOBALS --- */
.form-body { 
    padding: 30px; 
    position: relative; 
    min-height: 200px; 
}

.form-actions { 
    background: rgba(0,0,0,0.02); 
    padding: 20px 30px; 
    display: flex; 
    justify-content: flex-end; 
    gap: 15px; 
    border-top: 1px solid var(--border-color); 
}

.standard-group, .form-group { 
    margin-bottom: 20px; 
}

.helper-text { 
    font-size: 0.75rem; 
    color: var(--text-muted); 
    margin-top: 4px; 
    font-style: italic; 
    opacity: 0.8; 
}

label { 
    display: block; 
    margin-bottom: 8px; 
    font-weight: 600; 
    font-size: 0.85rem; 
    color: var(--text-muted); 
    text-transform: uppercase; 
    letter-spacing: 0.5px; 
}

input[type="number"]::-webkit-inner-spin-button, 
input[type="number"]::-webkit-outer-spin-button { 
    -webkit-appearance: none; 
    margin: 0; 
}

input[type="number"] { 
    -moz-appearance: textfield; 
}

input[type="text"], 
input[type="email"], 
input[type="password"], 
input[type="tel"], 
input[type="number"], 
select, 
textarea { 
    width: 100%; 
    padding: 14px 18px; 
    border-radius: 12px; 
    border: 1px solid var(--border-color); 
    background: var(--input-bg); 
    color: var(--text-main); 
    font-size: 1rem;
    box-shadow: inset 0 2px 4px var(--shadow); 
    transition: border-color 0.2s; 
    outline: none; 
    box-sizing: border-box;
    font-family: inherit;
}

input:focus, select:focus, textarea:focus { 
    border-color: var(--accent-color); 
}

select {
    appearance: none;
    background-image: url('data:image/svg+xml;utf8,<svg fill="%23888" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M7 10l5 5 5-5z"/></svg>');
    background-repeat: no-repeat;
    background-position: right 15px center;
    cursor: pointer;
    padding-right: 40px; 
}

option { 
    background: var(--bg-panel); 
    color: var(--text-main); 
    padding: 10px; 
}

input:disabled, select:disabled, textarea:disabled { 
    opacity: 0.6; 
    cursor: not-allowed; 
    background-color: rgba(255, 255, 255, 0.05) !important;
    color: var(--text-main); 
    -webkit-text-fill-color: var(--text-main); 
    border-style: dashed;
}

/* --- PAGER --- */
.pager { 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
    padding: 15px 25px; 
    border-top: 1px solid var(--border-color); 
    color: var(--text-main); 
}

.btn-pager { 
    background: var(--bg-panel); 
    border: 1px solid var(--border-color); 
    color: var(--text-main); 
    width: 44px; 
    height: 44px; 
    border-radius: 50%; 
    cursor: pointer; 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    transition: all 0.2s ease; 
    box-shadow: 0 2px 8px var(--shadow); 
    padding: 0; 
}

.btn-pager:hover:not(:disabled) { 
    transform: scale(1.1); 
    color: var(--accent-color); 
    border-color: var(--accent-color); 
}

.btn-pager:disabled { 
    opacity: 0.3; 
    cursor: not-allowed; 
    box-shadow: none; 
    filter: grayscale(1); 
}

.btn-pager svg { 
    width: 24px; 
    height: 24px; 
    fill: currentColor; 
} 

.page-info { 
    font-weight: 700; 
    font-size: 0.95rem; 
}

.status-msg { 
    padding: 40px; 
    text-align: center; 
    color: var(--text-muted); 
    font-style: italic; 
}

.status-msg.error, .empty.error { 
    color: #e74c3c; 
    font-style: normal; 
}

:host([loaded]) .status-msg:not(.error) { 
    display: none; 
}

/* ======================================================================== */
/* --- RESPONSIVE GLOBALS (NATIVE MOBILE FEEL)                              */
/* ======================================================================== */
@media (max-width: 768px) {
    #main-container, .container { 
        padding: 0 !important; 
    }

    /* 🚨 FIX: Centered with calc() to prevent off-screen bleeding */
    .card:not(.card-list), .section-card {
        width: calc(100% - 30px) !important;
        margin: 0 auto 15px auto !important;
        border-radius: 16px !important; 
        border: 1px solid var(--border-color) !important;
        box-shadow: 0 4px 12px var(--shadow) !important;
    }

    /* Data lists and tables MUST go edge-to-edge to maximize horizontal space */
    .card-list, .card-list-container {
        border-radius: 0 !important; 
        border-left: none !important; 
        border-right: none !important;
        box-shadow: none !important; 
        margin: 0 0 15px 0 !important; 
        border-bottom: 1px solid var(--border-color) !important;
    }

    /* 🚨 FIX: Replaced env() with var(--safe-top) everywhere to bypass WebKit bugs */
    .header {
        padding: calc(var(--safe-top) + 15px) 15px 15px 15px !important; 
        margin-bottom: 0; 
        background: var(--bg-app) !important;
        border-bottom: none !important;
        position: sticky; 
        top: 0; 
        z-index: 100;
    }

    .header .btn-back, .header .btn-circle {
        background: var(--bg-panel);
        box-shadow: 0 2px 8px var(--shadow);
        width: 44px;
        height: 44px;
        border-radius: 50%;
        color: var(--text-main);
        justify-content: center; 
    }
    
    .header .btn-back:hover, .header .btn-circle:hover {
        transform: scale(1.05);
        opacity: 0.9;
    }
    
    .header .btn-back svg, .header .btn-circle svg {
        width: 24px;
        height: 24px;
    }

    .search-row {
        padding: 10px 15px; 
        background: var(--bg-panel); 
        margin-bottom: 0;
        border-bottom: 1px solid var(--border-color);
    }
    
    /* 🚨 FIX: Replaced env() with var(--safe-top) */
    :host > .search-row:first-child,
    .container > .search-row:first-child {
        padding-top: calc(var(--safe-top) + 10px);
    }
    
    .form-body { 
        padding: 20px 15px; 
    }
    
    .form-actions { 
        padding: 15px; 
    }
    
    .list-row .list-avatar, .list-row .icon-box { 
        display: none !important; 
    }

    /* NATIVE INBOX LAYOUT (STANDARD CARD VIEW) */
    :host(:not([layout="table"])) .list-info {
        display: flex !important; 
        flex-direction: column !important;
        align-items: flex-start !important; 
        justify-content: center !important;
        padding: 12px 0; 
        position: relative; 
        width: 100%; 
        gap: 6px !important;
    }
    
    :host(:not([layout="table"])) .data-field {
        width: 100%; 
        margin: 0 !important;
        white-space: nowrap; 
        overflow: hidden; 
        text-overflow: ellipsis; 
        padding-right: 70px;
    }
    
    :host(:not([layout="table"])) .data-field:first-child .field-label { 
        display: none !important; 
    }
    
    :host(:not([layout="table"])) .data-field:first-child .field-value {
        font-size: 1.15rem; 
        font-weight: 700; 
        color: var(--text-main); 
        display: block;
    }

    :host(:not([layout="table"])) .data-field.col-role,
    :host(:not([layout="table"])) .data-field.col-status,
    :host(:not([layout="table"])) .data-field.col-stage {
        position: absolute; 
        top: 12px; 
        right: 0; 
        width: auto; 
        padding-right: 0;
    }
    
    :host(:not([layout="table"])) .data-field.col-role .field-label,
    :host(:not([layout="table"])) .data-field.col-status .field-label,
    :host(:not([layout="table"])) .data-field.col-stage .field-label { 
        display: none !important; 
    }

    :host(:not([layout="table"])) .data-field:not(:first-child):not(.col-role):not(.col-status):not(.col-stage) {
        display: grid !important; 
        grid-template-columns: 85px 1fr; 
        align-items: baseline; 
        gap: 10px; 
        white-space: normal; 
    }
    
    :host(:not([layout="table"])) .data-field:not(:first-child):not(.col-role):not(.col-status):not(.col-stage) .field-label {
        display: block !important; 
        font-size: 0.75rem; 
        color: var(--text-muted); 
        opacity: 0.8; 
        margin: 0 !important; 
        text-transform: capitalize; 
        text-align: right; 
    }
    
    :host(:not([layout="table"])) .data-field:not(:first-child):not(.col-role):not(.col-status):not(.col-stage) .field-value {
        font-size: 0.95rem; 
        color: var(--text-main); 
        opacity: 0.9;
        white-space: nowrap; 
        overflow: hidden; 
        text-overflow: ellipsis;
    }

    :host(:not([layout="table"])) .data-field.col-description .field-value,
    :host(:not([layout="table"])) .data-field.col-summary .field-value {
        white-space: normal; 
        display: -webkit-box; 
        -webkit-line-clamp: 2; 
        -webkit-box-orient: vertical;
    }

    /* HORIZONTAL SCROLL (TABLE VIEW) */
    :host([layout="table"]) #list-body {
        overflow-x: auto; 
        -webkit-overflow-scrolling: touch; 
        width: 100%; 
        scrollbar-width: none;
    }
    
    :host([layout="table"]) #list-body::-webkit-scrollbar { 
        display: none; 
    }
    
    :host([layout="table"]) .table-header { 
        display: flex !important; 
        width: max-content; 
        min-width: 100%; 
    } 
    
    :host([layout="table"]) .list-row { 
        width: max-content; 
        min-width: 100%; 
        display: block !important; 
    }
    
    :host([layout="table"]) .list-info {
        display: flex !important; 
        flex-direction: row !important; 
        flex-wrap: nowrap !important;
        overflow: visible !important; 
        padding: 8px 0; 
        width: auto !important; 
        gap: 0 !important; 
    }
    
    :host([layout="table"]) .data-field { 
        width: 140px !important; 
        flex: 0 0 140px !important; 
        margin-bottom: 0 !important; 
        padding-right: 15px; 
        box-sizing: border-box;
    }
    
    :host([layout="table"]) .data-field.col-email,
    :host([layout="table"]) .data-field.col-title,
    :host([layout="table"]) .data-field.col-name,
    :host([layout="table"]) .data-field.col-description { 
        width: 200px !important; 
        flex: 0 0 200px !important; 
    }

    :host([layout="table"]) .field-label { display: none !important; }
    :host([layout="table"]) .header-label { display: block !important; }
    :host([layout="table"]) .actions-header { display: none !important; }

    /* SWIPE-TO-ACTION PHYSICS */
    .list-row:not(.table-header) {
        position: relative; 
        padding: 0 !important; 
        overflow: hidden;
        border-bottom: 1px solid var(--border-color) !important; 
        background: var(--bg-panel);
    }
    
    .swipe-surface {
        display: flex !important; 
        position: relative; 
        z-index: 2; 
        background: var(--bg-panel);
        align-items: center; 
        width: 100%; 
        padding: 15px 20px; 
        box-sizing: border-box; 
        transition: transform 0.25s cubic-bezier(0.2, 0.8, 0.2, 1); 
        gap: 15px;
    }
    
    .swipe-surface.swiping { 
        transition: none; 
    }

    :host([layout="table"]) .actions, .actions {
        position: absolute !important; 
        top: 0; left: 0; right: 0; bottom: 0; 
        z-index: 1;
        display: flex !important; 
        justify-content: space-between; 
        align-items: center;
        padding: 0; 
        width: 100% !important; 
        background: transparent; 
    }
    
    .actions .btn-icon { 
        height: 100%; 
        width: 80px; 
        border-radius: 0; 
        color: white; 
        background: transparent; 
    }
    
    .actions .btn-icon svg { 
        width: 28px; 
        height: 28px; 
    }
    
    .actions .btn-icon:hover { 
        background: rgba(255, 255, 255, 0.1) !important; 
        color: white !important; 
    }
    
    .actions .edit { background: #3498db; }
    .actions .delete { background: #e74c3c; margin-left: auto; }
    .actions .swipe-left-group { position: absolute; left: 0; }
    .actions .swipe-right-group { position: absolute; right: 0; }

    /* ---------------------------------------------------- */
    /* CALENDAR FIXES (Responsive Square Grid)              */
    /* ---------------------------------------------------- */
    .calendar-grid {
        display: grid;
        grid-template-columns: repeat(7, 1fr) !important;
        gap: 4px !important;
        margin: 0 15px !important;
    }
    
    .calendar-day, .day-cell, .event-cell, [class*="calendar-cell"] {
        min-height: 44px !important;
        height: auto !important;
        aspect-ratio: 1 / 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: flex-start;
        padding: 4px !important;
        border-radius: 8px !important;
    }
}