/* ═══════════════════════════════════════════════════════════════
   PWA Mobile CSS — Safe area + Mobile drawer menu
   ═══════════════════════════════════════════════════════════════ */

/* ─── Safe area برای notch (iPhone X+ و اندروید جدید) ──────── */
.pwa-standalone body {
  padding-top: env(safe-area-inset-top, 0);
  padding-bottom: env(safe-area-inset-bottom, 0);
  padding-left: env(safe-area-inset-left, 0);
  padding-right: env(safe-area-inset-right, 0);
}

.pwa-standalone .header {
  padding-top: max(12px, env(safe-area-inset-top, 12px));
}

.pwa-standalone .input-area,
.pwa-standalone .chat-input-wrap {
  padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
}

/* ═══════════════════════════════════════════════════════════════
   🔥 موبایل: استفاده از dvh به جای vh برای جلوگیری از overlap با toolbar
   iOS Safari toolbar پایین صفحه از 100vh کم نمی‌شه، ولی از 100dvh کم می‌شه
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
    html, body {
        height: 100dvh !important;
        overflow: hidden;
    }
    
    .app,
    body > .app,
    #mainApp {
        height: 100dvh !important;
        max-height: 100dvh !important;
        display: flex;
        flex-direction: column;
    }
    
    /* main scroll بشه ولی header و input ثابت بمونن */
    .main,
    .chat-area {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        min-height: 0;
    }
}

@media (max-width: 768px) {
    .chat-input,
    .input-area,
    .chat-input-wrap {
        /* padding-bottom علاوه بر مقدار اصلی، فضای ابزار مرورگر رو اضافه می‌کنه */
        padding-bottom: max(16px, env(safe-area-inset-bottom, 16px)) !important;
    }
    
    /* اطمینان از اینکه main scrollable باشه و input سرجاش بمونه */
    .main,
    .chat-area,
    .conversation {
        /* فضای کافی پایین تا scroll روی input گیر نکنه */
        padding-bottom: 0;
    }
}

/* در iOS Safari (mobile که standalone نیست) — یه padding bottom اضافی
   برای جلوگیری از گیر کردن input زیر نوار ابزار پایین Safari */
@supports (-webkit-touch-callout: none) {
    @media (max-width: 768px) {
        .chat-input,
        .input-area,
        .chat-input-wrap {
            padding-bottom: max(24px, calc(env(safe-area-inset-bottom, 0px) + 16px)) !important;
        }
    }
}

/* ─── scroll bar در standalone ───────────────────────────────── */
.pwa-standalone ::-webkit-scrollbar {
  width: 4px;
}

.pwa-standalone ::-webkit-scrollbar-thumb {
  background: rgba(244, 216, 147, 0.2);
  border-radius: 2px;
}

/* ─── select کردن متن در standalone ──────────────────────────── */
.pwa-standalone button,
.pwa-standalone .lang-option,
.pwa-standalone .slot-card,
.pwa-standalone .header {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.pwa-standalone .message,
.pwa-standalone .chat-message,
.pwa-standalone input,
.pwa-standalone textarea {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

/* ─── tap highlight در موبایل ──────────────────────────────── */
.pwa-mobile button,
.pwa-mobile .lang-option,
.pwa-mobile .slot-card,
.pwa-mobile a {
  -webkit-tap-highlight-color: rgba(244, 216, 147, 0.1);
}

/* ═══════════════════════════════════════════════════════════════
   🆕 Mobile Drawer Menu
   ═══════════════════════════════════════════════════════════════ */

.mobile-menu-btn {
    display: none;
    background: transparent;
    border: 1px solid rgba(244, 216, 147, 0.2);
    border-radius: 8px;
    width: 36px;
    height: 36px;
    padding: 0;
    cursor: pointer;
    color: var(--gold-bright, #f4d893);
    align-items: center;
    justify-content: center;
    margin-left: auto;
    margin-right: 8px;
    transition: background 0.2s, border-color 0.2s;
}

.mobile-menu-btn:hover,
.mobile-menu-btn:active {
    background: rgba(244, 216, 147, 0.08);
    border-color: rgba(244, 216, 147, 0.4);
}

.mobile-menu-btn svg {
    width: 22px;
    height: 22px;
}

.mobile-drawer {
    position: fixed;
    inset: 0;
    z-index: 3000;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.mobile-drawer.hidden {
    display: none;
}

.mobile-drawer.open {
    opacity: 1;
    pointer-events: auto;
}

.mobile-drawer-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

.mobile-drawer-panel {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(320px, 85vw);
    background: var(--bg-deep, #0d0e14);
    border-left: 1px solid rgba(244, 216, 147, 0.12);
    box-shadow: -8px 0 32px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
    padding-top: env(safe-area-inset-top, 0);
    padding-bottom: env(safe-area-inset-bottom, 0);
}

html[dir="rtl"] .mobile-drawer-panel {
    right: auto;
    left: 0;
    border-left: none;
    border-right: 1px solid rgba(244, 216, 147, 0.12);
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.5);
    transform: translateX(-100%);
}

.mobile-drawer.open .mobile-drawer-panel {
    transform: translateX(0);
}

.mobile-drawer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid rgba(244, 216, 147, 0.08);
}

.mobile-drawer-title {
    font-size: 14px;
    color: var(--gold, #c9a961);
    letter-spacing: 0.1em;
    text-transform: uppercase;
    font-weight: 500;
}

.mobile-drawer-close {
    background: transparent;
    border: none;
    color: var(--ink-secondary, #888);
    cursor: pointer;
    padding: 8px;
    margin: -8px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.mobile-drawer-close:hover,
.mobile-drawer-close:active {
    color: var(--ink-primary, #fff);
    background: rgba(255, 255, 255, 0.05);
}

.mobile-drawer-close svg {
    width: 20px;
    height: 20px;
}

.mobile-drawer-body {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* استایل آیتم‌های داخل drawer */
.mobile-drawer-body .identity-display {
    padding: 12px;
    background: rgba(244, 216, 147, 0.04);
    border: 1px solid rgba(244, 216, 147, 0.1);
    border-radius: 10px;
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
}

.mobile-drawer-body .goals-badge {
    width: 100%;
    justify-content: flex-start;
    padding: 12px;
}

.mobile-drawer-body .user-email-badge {
    display: flex !important;
    padding: 12px;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 10px;
    font-size: 13px;
    color: var(--ink-secondary, #888);
    word-break: break-all;
}

.mobile-drawer-body .tts-group {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px;
    background: rgba(244, 216, 147, 0.04);
    border-radius: 10px;
}

/* ═══════════════════════════════════════════════════════════════
   Mobile breakpoint — اصلاح header در موبایل
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
    .mobile-menu-btn {
        display: inline-flex;
    }
    
    .header {
        flex-wrap: nowrap !important;
        overflow: hidden !important;
        padding: 12px !important;
        gap: 8px !important;
        align-items: center !important;
    }
    
    /* آیتم‌هایی که میرن توی drawer */
    .header > #identityBar,
    .header > #userEmailBadge {
        display: none !important;
    }
    
    /* 🔥 moodBar: همیشه visible روی header
       توجه: selfa-logo.js یه wrapper .logo-mood-wrap اضافه می‌کنه دور moodBar
       پس باید descendant selector (.header #moodBar) باشه نه > (child selector) */
    html body .header #moodBar,
    html body .header #moodBar.hidden,
    html body .header .mood-bar,
    html body .header .mood-bar.hidden,
    html body .logo-mood-wrap #moodBar,
    html body .logo-mood-wrap #moodBar.hidden {
        display: flex !important;
        flex-shrink: 1;
        min-width: 0;
        opacity: 1 !important;
        visibility: visible !important;
    }
    
    /* logo-mood-wrap باید flex بشه که logo + moodBar کنار هم باشن */
    html body .header .logo-mood-wrap {
        display: flex !important;
        align-items: center;
        gap: 8px;
        flex-shrink: 1;
        min-width: 0;
    }
    
    /* moodBar داخل compact شه */
    .header > #moodBar .mood-indicator {
        padding: 4px 10px !important;
        gap: 6px !important;
    }
    
    .header > #moodBar .mood-label {
        font-size: 11px !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 90px;
    }
    
    .header > #moodBar .mood-dot-svg {
        width: 18px !important;
        height: 18px !important;
        flex-shrink: 0;
    }
    
    .header .logo-tagline {
        display: none !important;
    }
    
    /* header-actions در موبایل پنهان (آیکون‌های مهمش بیرون میان) */
    .header .header-actions {
        display: none !important;
    }
    
    /* 🔥 Settings: همیشه visible روی header */
    .header > #settingsBtn,
    .header > .settings-with-ring {
        display: inline-flex !important;
        flex-shrink: 0;
        align-items: center;
        justify-content: center;
    }
    
    /* 🔥 Logout: همیشه visible روی header */
    .header > #logoutBtn,
    .header > .logout-btn {
        display: inline-flex !important;
        flex-shrink: 0;
        align-items: center;
        justify-content: center;
    }
    
    .header .logo {
        flex-shrink: 1;
        min-width: 0;
    }
    
    .mobile-menu-btn {
        order: 99;
    }
}

@media (max-width: 400px) {
    /* در گوشی‌های خیلی کوچک، logout میره داخل drawer
       ولی settings و moodBar همیشه روی header می‌مونن */
    .header > #logoutBtn,
    .header > .logout-btn {
        display: none !important;
    }
    
    .header > #moodBar .mood-label {
        font-size: 10px !important;
        max-width: 70px;
    }
}
