/* ===========================================
   SoulStream Mobile Styles - Consolidated
   Combines: mobile.css, mobile-header.css, 
   mobile-sidebar.css, mobile-optimizations.css
   =========================================== */

/* --- CSS Variables for Consistency --- */
:root {
  --mobile-header-height: 56px;
  --mobile-sidebar-width: 280px;
  --mobile-touch-target: 44px;
  --mobile-glass-blur: 10px;
  --mobile-glass-bg: rgba(255, 255, 255, 0.95);
  --mobile-transition: 0.3s ease;
  --mobile-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

/* --- Prevent Horizontal Scrolling --- */
html, body {
  overflow-x: hidden;
  width: 100%;
  position: relative;
}

/* --- Mobile Header Component --- */
.mobile-header {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--mobile-header-height);
  background: var(--mobile-glass-bg);
  backdrop-filter: blur(var(--mobile-glass-blur));
  -webkit-backdrop-filter: blur(var(--mobile-glass-blur));
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  z-index: 1200;
  padding: 0 1rem;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--mobile-shadow);
}

.mobile-header-title {
  font-size: 1.125rem;
  font-weight: 600;
  color: #333;
  flex: 1;
  text-align: center;
}

.mobile-model-selector {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  background: rgba(74, 144, 226, 0.1);
  border: 1px solid rgba(74, 144, 226, 0.2);
  border-radius: 20px;
  cursor: pointer;
  transition: all var(--mobile-transition);
  min-height: 36px;
  max-width: 150px;
}

.mobile-model-selector:hover {
  background: rgba(74, 144, 226, 0.15);
  border-color: rgba(74, 144, 226, 0.3);
}

.mobile-model-selector .model-icon {
  font-size: 1rem;
}

.mobile-model-selector .model-name {
  font-size: 0.875rem;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* --- Hamburger Menu Component --- */
.hamburger-menu {
  display: none;
  background-color: transparent;
  border: none;
  padding: 10px;
  cursor: pointer;
  position: relative;
  z-index: 1201;
  color: #333;
  border-radius: 4px;
  transition: all var(--mobile-transition);
}

.hamburger-menu.active {
  padding: 8px;
  width: 30px;
  height: 30px;
}

.hamburger-menu span {
  display: block;
  width: 24px;
  height: 2px;
  margin: 5px 0;
  background-color: #333;
  transition: all var(--mobile-transition);
}

/* Animate hamburger to X */
.hamburger-menu.active span:nth-child(1) {
  transform: rotate(45deg) translate(3px, 3px);
  width: 18px;
}

.hamburger-menu.active span:nth-child(2) {
  opacity: 0;
}

.hamburger-menu.active span:nth-child(3) {
  transform: rotate(-45deg) translate(3px, -3px);
  width: 18px;
}

/* --- Menu Overlay --- */
.menu-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1250;
  transition: opacity var(--mobile-transition);
}

.menu-overlay.active {
  display: block;
}

/* --- Mobile Bottom Navbar --- */
.mobile-bottom-navbar {
  display: none;
}

/* --- Main Mobile Styles (max-width: 768px) --- */
@media (max-width: 768px) {
  /* Show mobile-specific elements */
  .mobile-header {
    display: flex;
  }
  
  
  /* Hide desktop elements */
  .sidebar-toggle {
    display: none;
  }
  
  .selectors-container.desktop-only {
    display: none !important;
  }

  /* Sidebar mobile configuration */
  .sidebar {
    position: fixed;
    left: calc(-1 * var(--mobile-sidebar-width));
    top: 0;
    bottom: 0;
    width: var(--mobile-sidebar-width);
    background-color: #f5f5f7;
    transition: left var(--mobile-transition), transform var(--mobile-transition);
    z-index: 1300;
    box-shadow: none;
    display: flex;
    flex-direction: column;
    will-change: transform, left;
    padding-top: var(--mobile-header-height);
  }

  .sidebar.active {
    left: 0;
    box-shadow: 2px 0 15px rgba(0, 0, 0, 0.2);
  }
  
  /* Sidebar header mobile optimization */
  .sidebar-header {
    padding: 1rem;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: var(--mobile-glass-bg);
    backdrop-filter: blur(var(--mobile-glass-blur));
  }
  
  .home-link {
    display: none; /* Hide logo in mobile sidebar */
  }
  
  .new-chat-btn {
    min-height: var(--mobile-touch-target);
    font-size: 1rem;
    font-weight: 600;
    box-shadow: 0 2px 8px rgba(74, 144, 226, 0.2);
    transition: all var(--mobile-transition);
  }
  
  .new-chat-btn:active {
    transform: scale(0.98);
    box-shadow: 0 1px 4px rgba(74, 144, 226, 0.2);
  }
  
  /* Conversations list mobile optimization */
  .conversations-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
    -webkit-overflow-scrolling: touch;
  }
  
  .conversation-item {
    min-height: 56px;
    padding: 0.875rem 1rem;
    margin-bottom: 0.5rem;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 12px;
    border: 1px solid rgba(0, 0, 0, 0.05);
    transition: all 0.2s ease;
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: rgba(74, 144, 226, 0.2);
  }
  
  .conversation-item:active {
    background: rgba(74, 144, 226, 0.1);
    border-color: rgba(74, 144, 226, 0.2);
  }
  
  .conversation-item.active {
    background: rgba(74, 144, 226, 0.15);
    border-color: rgba(74, 144, 226, 0.3);
    font-weight: 500;
  }
  
  /* Sidebar actions mobile optimization */
  .sidebar-actions {
    display: flex;
    flex-direction: column;
    gap: 1.1rem;
    margin-top: 2.5rem;
    padding: 1rem;
    border-top: 1px solid rgba(0, 0, 0, 0.1);
    background: var(--mobile-glass-bg);
    backdrop-filter: blur(var(--mobile-glass-blur));
  }
  
  .sidebar-action-btn {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.875rem 1rem;
    min-height: 48px;
    width: 100%;
    background: rgba(0, 0, 0, 0.02);
    border: 1px solid rgba(0, 0, 0, 0.08);
    border-radius: 12px;
    text-decoration: none;
    color: #333;
    font-size: 1rem;
    font-weight: 500;
    transition: all 0.2s ease;
    touch-action: manipulation;
  }
  
  .sidebar-action-btn:active {
    background: rgba(0, 0, 0, 0.05);
    transform: scale(0.98);
  }
  
  /* Logout button special styling */
  .sidebar-action-btn.logout-btn {
    margin-top: 1.25rem !important;
    background: #f8e9e9;
    color: #b23a48;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(178,58,72,0.06);
    transition: background 0.2s, box-shadow 0.2s;
  }
  
  .sidebar-action-btn.logout-btn:hover,
  .sidebar-action-btn.logout-btn:focus {
    background: #ffeaea;
    color: #a12a38;
    box-shadow: 0 4px 16px rgba(178,58,72,0.12);
  }

  /* Main content adjustments */
  .chat-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
  }
  
  .main-content {
    margin-left: 0;
    width: 100%;
    overflow-x: hidden;
    flex: 1;
    display: flex;
    flex-direction: column;
    padding-top: var(--mobile-header-height);
    padding-bottom: 5rem; /* Space for bottom navbar */
  }
  
  .chat-messages {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-top: 1rem !important;
    padding-bottom: 5rem !important; /* Space for bottom navbar */
    padding-bottom: calc(5rem + env(safe-area-inset-bottom, 0));
  }
  
  /* Chat input container mobile optimization */
  .chat-input-container {
    position: relative;
    margin-bottom: 0;
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0));
    background-color: #ffffff;
    box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.05);
  }
  
  .chat-input {
    font-size: 16px !important; /* Prevent iOS zoom */
    min-height: var(--mobile-touch-target);
  }
  
  /* Mobile selectors */
  .selectors-container {
    flex-direction: row;
    gap: 0.5rem;
    padding: 0.5rem 0.5rem 0.25rem 0.5rem;
    min-height: 48px;
    flex-wrap: wrap;
  }
  
  .selector-icon {
    min-width: 120px;
    min-height: var(--mobile-touch-target);
    font-size: 0.95rem;
    padding: 0.75rem 0.5rem;
  }
  
  /* Touch targets */
  .send-button,
  .thinking-toggle,
  .photo-upload-button {
    min-width: var(--mobile-touch-target);
    min-height: var(--mobile-touch-target);
    font-size: 1rem;
    touch-action: manipulation;
  }
  
  .input-wrapper {
    gap: 0.5rem;
  }
  
  /* Message optimizations */
  .message {
    padding: 0.75rem 0;
    min-height: 3rem;
    overflow: visible;
  }
  
  .message-content {
    gap: 0.5rem;
    overflow: visible;
    padding: 0 0.5rem;
  }
  
  .message-text {
    min-height: 2.5rem;
    overflow: visible;
    display: flex;
    flex-direction: column;
  }
  
  .message-buttons {
    flex-direction: column;
    gap: 0.35rem;
    align-items: flex-start;
    margin-top: 0.5rem;
    width: 100%;
    display: flex;
    flex-wrap: wrap;
  }
  
  .refresh-button, .sources-button {
    font-size: 0.875rem;
    padding: 0.5rem 0.75rem;
    min-height: 36px;
    min-width: 80px;
    flex: 0 0 auto;
    white-space: nowrap;
  }
  
  /* Sources list mobile optimization */
  .sources-list {
    grid-template-columns: 1fr;
  }
  
  /* Code blocks mobile optimization */
  pre {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  /* Thinking content mobile optimization */
  .thinking-content {
    max-width: 100%;
    word-wrap: break-word;
    overflow-wrap: break-word;
  }
  
  /* Modal z-index fixes */
  .settings-modal-overlay,
  .memory-suggestion-modal-overlay,
  .sources-modal-overlay {
    z-index: 9999;
  }
  
  /* Dropdown positioning fixes */
  .model-dropdown-container,
  .prompt-dropdown-container {
    position: absolute;
    top: 100%;
    left: 0;
    right: auto;
    z-index: 1000;
  }
  
  .model-dropdown,
  .prompt-dropdown {
    max-width: 90vw;
    font-size: 16px;
  }
  
  /* Memory bell positioning */
  #memoryBellContainer {
    position: relative;
    z-index: 100;
  }
  
  .memory-dropdown {
    position: fixed;
    top: auto;
    right: 1rem;
    left: 1rem;
    bottom: auto;
    max-height: 60vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  /* Mobile Bottom Navbar */
  .mobile-bottom-navbar {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    padding: 0.75rem 0;
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0));
    z-index: 1000;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.1);
  }
  
  .navbar-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 0.5rem 0.25rem;
    cursor: pointer;
    color: rgba(255, 255, 255, 0.7);
    transition: all 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
    text-decoration: none;
    min-height: 48px;
    border-radius: 12px;
    margin: 0 0.25rem;
    position: relative;
    overflow: hidden;
  }
  
  .navbar-item::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0);
    border-radius: 12px;
    transition: all var(--mobile-transition);
    z-index: -1;
  }
  
  .navbar-item:hover::before,
  .navbar-item:focus::before {
    background: rgba(255, 255, 255, 0.1);
  }
  
  .navbar-item.active::before {
    background: rgba(255, 255, 255, 0.2);
    box-shadow: 0 0 20px rgba(255, 255, 255, 0.2);
  }
  
  .navbar-item svg {
    width: 24px;
    height: 24px;
    margin-bottom: 0.25rem;
    transition: all var(--mobile-transition);
    stroke-width: 1.5;
  }
  
  .navbar-item span {
    font-size: 0.75rem;
    font-weight: 500;
    text-align: center;
    line-height: 1;
    white-space: nowrap;
    transition: all var(--mobile-transition);
  }
  
  .navbar-item:hover,
  .navbar-item:focus {
    color: rgba(255, 255, 255, 0.9);
    transform: translateY(-2px);
  }
  
  .navbar-item.active {
    color: #ffffff;
    transform: translateY(-2px);
  }
  
  .navbar-item.active svg {
    stroke-width: 2;
    filter: drop-shadow(0 0 8px rgba(255, 255, 255, 0.3));
  }
  
  .navbar-item.active span {
    font-weight: 600;
    text-shadow: 0 0 8px rgba(255, 255, 255, 0.3);
  }
}

/* --- Bottom Sheet Component --- */
.bottom-sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 1100;
  display: none;
}

.bottom-sheet.active {
  display: block;
}

.bottom-sheet-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity var(--mobile-transition);
}

.bottom-sheet.active .bottom-sheet-backdrop {
  opacity: 1;
}

.bottom-sheet-content {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  max-height: 80vh;
  overflow: hidden;
}

.bottom-sheet.active .bottom-sheet-content {
  transform: translateY(0);
}

.bottom-sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.bottom-sheet-header h3 {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0;
  color: #333;
}

.bottom-sheet-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: #666;
  cursor: pointer;
  padding: 0.25rem;
  line-height: 1;
  border-radius: 4px;
  transition: all 0.2s ease;
}

.bottom-sheet-close:hover {
  background: rgba(0, 0, 0, 0.05);
  color: #333;
}

.bottom-sheet-body {
  padding: 1rem;
  overflow-y: auto;
  max-height: calc(80vh - 80px);
}

/* Swipe handle */
.bottom-sheet-content::before {
  content: '';
  position: absolute;
  top: 0.5rem;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 4px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 2px;
}

/* Model options in bottom sheet */
.model-options {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.model-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem;
  background: #f5f5f7;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  min-height: 60px;
}

.model-option:hover {
  background: #e8e8ea;
}

.model-option.active {
  background: #e3f2fd;
  border: 1px solid #4a90e2;
}

.model-option-info {
  flex: 1;
}

.model-option-name {
  font-size: 1rem;
  font-weight: 500;
  color: #333;
  margin-bottom: 0.25rem;
}

.model-option-provider {
  font-size: 0.875rem;
  color: #666;
}

.model-option-check {
  font-size: 1.25rem;
  color: #4a90e2;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.model-option.active .model-option-check {
  opacity: 1;
}

/* --- Extra Small Devices (max-width: 480px) --- */
@media (max-width: 480px) {
  .selectors-container {
    flex-direction: row;
    gap: 0.5rem;
    padding: 0.5rem 0.5rem 0.25rem 0.5rem;
    min-height: 48px;
    flex-wrap: wrap;
  }
  
  .selector-icon {
    min-width: 100px;
    min-height: var(--mobile-touch-target);
    font-size: 0.9rem;
    padding: 0.5rem 0.4rem;
    flex: 1;
  }
  
  .send-button,
  .thinking-toggle,
  .photo-upload-button {
    min-width: var(--mobile-touch-target);
    min-height: var(--mobile-touch-target);
    font-size: 0.95rem;
  }
  
  .input-wrapper {
    gap: 0.5rem;
  }
  
  .avatar {
    width: 40px !important;
    height: 40px !important;
    font-size: 1rem !important;
  }
}

/* --- Very Small Devices (max-width: 375px) --- */
@media (max-width: 375px) {
  .selectors-container {
    gap: 0.5rem;
    padding: 0.25rem 0.25rem 0.1rem 0.25rem;
    min-height: var(--mobile-touch-target);
  }
  
  .selector-icon {
    min-width: 80px;
    min-height: var(--mobile-touch-target);
    font-size: 0.85rem;
    padding: 0.5rem 0.3rem;
  }
  
  .send-button,
  .thinking-toggle,
  .photo-upload-button {
    min-width: var(--mobile-touch-target);
    min-height: var(--mobile-touch-target);
    font-size: 0.9rem;
  }
  
  .input-wrapper {
    gap: 0.25rem;
  }
  
  .message-content {
    padding: 0 0.25rem;
  }
  
  /* Hide thinking toggle on very small screens */
  .thinking-toggle {
    display: none;
  }
}

/* --- Landscape Mode Optimizations --- */
@media (max-width: 768px) and (orientation: landscape) {
  .selectors-container {
    padding: 0.25rem 0.5rem;
  }
  
  .chat-input-container {
    padding: 0.5rem;
  }
  
  .input-wrapper {
    padding: 0.25rem;
  }
  
  .message-timestamp {
    display: none;
  }
  
  .avatar {
    width: 32px !important;
    height: 32px !important;
  }
}

/* --- Accessibility Enhancements --- */
@media (prefers-contrast: high) {
  .send-button:disabled {
    opacity: 0.5;
  }
  
  .chat-input {
    border: 2px solid #333;
  }
  
  .selector-icon {
    border: 2px solid #333;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}

/* --- Platform-Specific Fixes --- */
/* iOS-specific fixes */
@supports (-webkit-touch-callout: none) {
  .chat-messages {
    -webkit-overflow-scrolling: touch;
    overflow-y: scroll;
  }
  
  .chat-input-container {
    position: relative;
    transform: translate3d(0, 0, 0);
  }
}

/* Android-specific fixes */
@media screen and (-webkit-min-device-pixel-ratio: 0) and (min-resolution: .001dpcm) {
  .chat-container {
    height: 100vh;
    height: calc(var(--vh, 1vh) * 100);
  }
}

/* PWA and standalone mode optimizations */
@media (display-mode: standalone) {
  .chat-container {
    padding-top: env(safe-area-inset-top, 0);
  }
  
  .hamburger-menu {
    top: calc(10px + env(safe-area-inset-top, 0));
  }
  
  .mobile-settings-button {
    top: calc(10px + env(safe-area-inset-top, 0));
  }
}

/* Keyboard visible state */
body.keyboard-visible {
  .chat-messages {
    max-height: 50vh;
  }
  
  .selectors-container {
    opacity: 0.7;
  }
}

/* --- Loading States --- */
.conversations-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  color: #666;
}

.loading-spinner {
  width: 24px;
  height: 24px;
  border: 3px solid rgba(74, 144, 226, 0.2);
  border-top-color: #4a90e2;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* --- Empty States --- */
.conversations-empty {
  text-align: center;
  padding: 2rem 1rem;
  color: #666;
  font-size: 0.9rem;
}

.conversations-empty p {
  margin-bottom: 1rem;
}