/* ==========================================================================
   AISA SANCTUARY - MOBILE UI & APP-LIKE EXPERIENCE (V1.0)
   Tối ưu chuyên biệt cho trải nghiệm AI Companion trên di động (< 768px)
   Dựa trên ngôn ngữ thiết kế MHEnt. Universe Mobile Standard
   ========================================================================== */

/* ==============================================================
   0. THIẾT LẬP NỀN TẢNG & TOUCH OPTIMIZATIONS
   ============================================================== */
@media screen and (max-width: 768px) {
  * {
    -webkit-tap-highlight-color: transparent;
  }

  html, body {
    height: 100% !important;
    height: 100dvh !important;
    overflow: hidden !important;
  }

  .sanctuary-stage {
    display: flex !important;
    flex-direction: column !important;
    height: calc(100dvh - 52px) !important;
    position: relative !important;
    overflow: hidden !important;
  }

  /* Ẩn sidebar phải mặc định trên mobile */
  .sanctuary-sidebar-right {
    display: none !important;
  }
}

/* ==============================================================
   1. THANH ĐIỀU HƯỚNG ĐÁY CHUẨN APP (AISA BOTTOM NAV)
   ============================================================== */
.aisa-bottom-nav {
  display: none;
}

@media screen and (max-width: 768px) {
  .aisa-bottom-nav {
    display: flex !important;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 60px;
    background: rgba(8, 12, 20, 0.94) !important;
    backdrop-filter: blur(25px);
    -webkit-backdrop-filter: blur(25px);
    border-top: 1px solid rgba(255, 255, 255, 0.1) !important;
    justify-content: space-around;
    align-items: center;
    z-index: 99999;
    box-shadow: 0 -4px 25px rgba(0, 0, 0, 0.5);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    box-sizing: content-box;
  }

  .aisa-nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-muted, #64748b);
    font-family: inherit;
    font-size: 11px;
    font-weight: 700;
    gap: 3px;
    height: 100%;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    position: relative;
    user-select: none;
  }

  .aisa-nav-item .nav-icon {
    font-size: 19px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  }

  /* Active states */
  .aisa-nav-item.active {
    font-weight: 900;
  }

  .aisa-nav-item.active .nav-icon {
    transform: scale(1.2) translateY(-2px);
    animation: navIconBounce 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
  }

  .aisa-nav-item.active::after {
    content: '';
    width: 4px;
    height: 4px;
    border-radius: 50%;
    position: absolute;
    bottom: 5px;
  }

  .aisa-nav-item[data-tab="chat"].active { color: #38bdf8; }
  .aisa-nav-item[data-tab="chat"].active::after { background: #38bdf8; box-shadow: 0 0 6px #38bdf8; }

  .aisa-nav-item[data-tab="harmony"].active { color: #f472b6; }
  .aisa-nav-item[data-tab="harmony"].active::after { background: #f472b6; box-shadow: 0 0 6px #f472b6; }

  .aisa-nav-item[data-tab="echo"].active { color: #a855f7; }
  .aisa-nav-item[data-tab="echo"].active::after { background: #a855f7; box-shadow: 0 0 6px #a855f7; }

  .aisa-nav-item[data-tab="scope"].active { color: #ec4899; }
  .aisa-nav-item[data-tab="scope"].active::after { background: #ec4899; box-shadow: 0 0 6px #ec4899; }

  .aisa-nav-item[data-tab="menu"].active { color: #cbd5e1; }
  .aisa-nav-item[data-tab="menu"].active::after { background: #cbd5e1; box-shadow: 0 0 6px #cbd5e1; }

  .aisa-nav-item:active {
    transform: scale(0.88);
  }
}

/* ==============================================================
   2. TINH CHỈNH HEADER SANCTUARY TRÊN MOBILE
   ============================================================== */
@media screen and (max-width: 768px) {
  .sanctuary-header {
    height: 52px !important;
    padding: 0 12px !important;
    box-sizing: border-box !important;
  }

  .brand-logo-img {
    width: 28px !important;
    height: 28px !important;
  }

  .brand-info h1 {
    font-size: 15px !important;
  }

  .brand-info span {
    font-size: 9px !important;
  }

  /* Ẩn các tabs phạm vi trên topbar vì đã có nút Không Gian ở Bottom Nav */
  .header-scope-tabs {
    display: none !important;
  }

  .header-actions {
    gap: 6px !important;
  }

  .btn-ambient-toggle span:not(.badge-counter),
  #btn-open-memory span:not(.badge-counter) {
    display: none !important;
  }

  .btn-header-action {
    padding: 6px 8px !important;
    min-width: 34px !important;
    height: 34px !important;
  }

  .header-user-badge .user-badge-name {
    display: none !important;
  }
}

/* ==============================================================
   3. SIDEBAR LEFT - MENU TRƯỢT TRÁI (LEFT DRAWER V2.0)
   ============================================================== */
@media screen and (max-width: 768px) {
  /* Lớp phủ mờ (Overlay) */
  .mhent-aisa-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(5, 7, 11, 0.7);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    z-index: 1000000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
  }
  .mhent-aisa-overlay.show {
    opacity: 1;
    pointer-events: auto;
  }

  .sanctuary-sidebar-left {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 84% !important;
    max-width: 320px !important;
    height: 100% !important;
    height: 100dvh !important;
    background: rgba(14, 20, 32, 0.98) !important;
    backdrop-filter: blur(28px) !important;
    -webkit-backdrop-filter: blur(28px) !important;
    border-right: 1px solid rgba(255, 255, 255, 0.12) !important;
    box-shadow: none !important;
    z-index: 1000001 !important;
    transform: translateX(-100%) !important;
    transition: transform 0.35s cubic-bezier(0.2, 0.8, 0.2, 1), box-shadow 0.35s ease !important;
    display: flex !important;
    flex-direction: column !important;
    padding: 20px 16px !important;
    overflow-y: auto !important;
  }

  /* Khi mở Sidebar */
  .sanctuary-sidebar-left.open-mobile {
    transform: translateX(0) !important;
    box-shadow: 20px 0 45px rgba(0, 0, 0, 0.6) !important;
  }

  .btn-new-session {
    min-height: 44px !important;
    font-size: 14px !important;
    border-radius: 14px !important;
  }

  .sidebar-session-item {
    min-height: 44px !important;
    padding: 10px 12px !important;
    border-radius: 12px !important;
    font-size: 13.5px !important;
  }
}

/* ==============================================================
   4. CHAT STREAM & INPUT STATION TRÊN MOBILE
   ============================================================== */
@media screen and (max-width: 768px) {
  .sanctuary-chat-center {
    height: 100% !important;
    display: flex !important;
    flex-direction: column !important;
    padding-bottom: 60px !important; /* Chừa chỗ cho Bottom Nav */
    box-sizing: border-box !important;
    overflow: hidden !important;
  }

  .chat-stream-header {
    padding: 8px 12px !important;
    font-size: 12px !important;
    flex-shrink: 0 !important;
  }

  .chat-messages-wrap {
    flex: 1 !important;
    padding: 12px 10px 10px 10px !important;
    gap: 12px !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  .chat-message-row {
    max-width: 100% !important;
    gap: 8px !important;
  }

  .user-row {
    max-width: 90% !important;
  }

  .assistant-avatar {
    width: 32px !important;
    height: 32px !important;
    font-size: 1.1rem !important;
  }

  .message-bubble {
    padding: 11px 14px !important;
    font-size: 14px !important;
    border-radius: 18px !important;
    line-height: 1.55 !important;
  }

  /* Khung Input Station sát Bottom Nav */
  .chat-input-station {
    padding: 6px 8px 8px 8px !important;
    background: rgba(5, 7, 11, 0.95) !important;
    backdrop-filter: blur(20px) !important;
    -webkit-backdrop-filter: blur(20px) !important;
    border-top: 1px solid rgba(255, 255, 255, 0.08) !important;
    flex-shrink: 0 !important;
  }

  .quick-prompts-bar {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    gap: 6px !important;
    padding-bottom: 6px !important;
    scroll-snap-type: x mandatory !important;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  .quick-prompts-bar::-webkit-scrollbar {
    display: none;
  }

  .quick-prompt-btn {
    white-space: nowrap !important;
    flex-shrink: 0 !important;
    padding: 5px 10px !important;
    font-size: 11.5px !important;
    border-radius: 99px !important;
    scroll-snap-align: start;
  }

  .input-box-wrapper {
    padding: 4px 8px !important;
    border-radius: 18px !important;
    gap: 6px !important;
  }

  .chat-input-textarea {
    font-size: 14px !important;
    padding: 6px 2px !important;
    min-height: 36px !important;
    max-height: 110px !important;
  }

  .btn-tool-icon {
    width: 32px !important;
    height: 32px !important;
    font-size: 1rem !important;
  }

  .btn-send-message {
    height: 32px !important;
    padding: 0 12px !important;
    font-size: 12px !important;
    gap: 4px !important;
  }
}

/* ==============================================================
   5. MODALS & POPUPS CĂN GIỮA (MEMORY VAULT & SETTINGS)
   ============================================================== */
@media screen and (max-width: 768px) {
  .modal-overlay, .gatekeeper-overlay {
    align-items: center !important;
    justify-content: center !important;
    padding: 16px !important;
    box-sizing: border-box !important;
  }

  .modal-card, .gatekeeper-card {
    width: calc(100vw - 32px) !important;
    max-width: 440px !important;
    margin: auto !important;
    border-radius: 22px !important;
    padding: 22px 18px !important;
    max-height: 85vh !important;
    overflow-y: auto !important;
    box-shadow: 0 20px 45px rgba(0, 0, 0, 0.6) !important;
    position: relative !important;
  }

  .gatekeeper-card {
    padding: 24px 18px !important;
  }

  .gate-input-wrapper {
    min-height: 44px !important;
  }

  .btn-gate-submit, .btn-gate-google {
    min-height: 44px !important;
  }
}

/* ==============================================================
   6. MODAL CHỌN KHÔNG GIAN (SCOPE PICKER POPUP)
   ============================================================== */
.scope-picker-modal {
  display: none;
}
.scope-picker-modal.show {
  display: flex !important;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(5, 7, 11, 0.7);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 1000010;
  align-items: center;
  justify-content: center;
  padding: 16px;
  box-sizing: border-box;
}

.scope-picker-box {
  width: calc(100vw - 32px);
  max-width: 380px;
  background: rgba(14, 20, 32, 0.98);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 22px;
  padding: 22px 18px;
  box-shadow: 0 20px 45px rgba(0, 0, 0, 0.6);
  display: flex;
  flex-direction: column;
  gap: 10px;
  animation: appPageSlideUp 0.28s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

.scope-picker-title {
  font-size: 16px;
  font-weight: 800;
  color: var(--text-high);
  text-align: center;
  margin-bottom: 4px;
}

.scope-picker-option {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  color: var(--text-high);
  cursor: pointer;
  font-size: 14px;
  font-weight: 700;
  transition: all 0.2s ease;
}

.scope-picker-option:active {
  background: rgba(236, 72, 153, 0.15);
  border-color: rgba(236, 72, 153, 0.4);
  color: #ec4899;
}

.scope-picker-option.active {
  border-color: #ec4899;
  background: rgba(236, 72, 153, 0.12);
}

/* ==============================================================
   7. HIỆU ỨNG CHUYỂN CẢNH APP NATIVE
   ============================================================== */
@keyframes navIconBounce {
  0% { transform: scale(1) translateY(0); }
  40% { transform: scale(1.3) translateY(-6px); }
  70% { transform: scale(1.08) translateY(-1px); }
  100% { transform: scale(1.18) translateY(-2px); }
}

@keyframes appPageSlideUp {
  0% {
    opacity: 0;
    transform: translateY(16px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}
