/* Mobile overrides for chat (<= 820px) */
@media (max-width: 820px) {
  :root { --mobile-input-bar-height: 140px; } /* konservativ, umfasst Eingabe + Buttonleiste */
  .desktop-only { display: none; }
  .mobile-only { display: block; }
  /* Erzwinge Einspalten-Layout: kein Grid, keine zweite Spalte */
  .main-layout,
  .main-layout.sidebar-standard-layout {
    display: flex !important;
    flex-direction: column !important;
    grid-template-areas: 'header' 'content' 'footer' !important;
    grid-template-columns: 1fr !important;
    grid-template-rows: auto 1fr auto !important;
    width: 100% !important;
    max-width: 100% !important;
    height: 100vh !important; /* volle Höhe für Flex-Layout */
  }
  .chat-header, #chat-container, .chat-input-footer {
    grid-area: unset !important;
    width: 100% !important;
    max-width: 100% !important;
  }

  /* Chat-Container soll den verfügbaren Platz ausfüllen und Kind darf scrollen */
  #chat-container {
    display: flex !important;
    flex-direction: column !important;
    flex: 1 1 auto !important;
    min-height: 0 !important;
    overflow: hidden !important;
  }

  /* Nachrichtenbereich: keine harte Max-Höhe; füllt den Platz und scrollt */
  #chat-messages {
    /* Platz für fixe Eingabeleiste (inkl. Buttons), plus kleiner Puffer */
    padding-bottom: calc(var(--mobile-input-bar-height) + 12px) !important;
    flex: 1 1 auto !important;
    min-height: 0 !important;
    max-height: 82% !important; /* überschreibt 50vh aus messages.css */
    height: auto !important;
    overflow-y: auto !important;
  }

  /* Nachrichten-Container: immer linksbündig stapeln (mobil) */
  #chat-container #chat-messages {
    display: flex !important;
    flex-direction: column !important;
    align-items: flex-start !important; /* Bubbles starten links */
    text-align: left !important;
  }

  /* Stabilisiere Username-/Nachrichtenzeilen (aus altem mobile.css übernommen) */
  .message-username-row {
    display: inline-flex !important;
    align-items: center !important;
    gap: 6px !important;
    min-height: 22px !important;
    line-height: 1.2 !important;
    width: auto !important;
    max-width: 100%;
  }
  /* Nachricht-Bubbles: nicht zeilenfüllend, links beginnen */
  .message,
  .chat-message {
    display: inline-block !important;     /* Bubble-Look statt voller Zeile */
    width: auto !important;               /* nur so breit wie Inhalt */
    max-width: 90% !important;            /* lange Texte umbrechen */
    text-align: left !important;          /* Text linksbündig */
    margin-left: 0 !important;            /* nicht zentrieren */
    box-sizing: border-box;
  }
  /* Inhalt innerhalb der Nachricht sauber umbrechen lassen */
  .message > span,
  .message > strong,
  .message > div {
    display: inline; /* Username, Doppelpunkt, Text in einer Zeile */
    white-space: normal;
    word-break: break-word;
  }

  /* Kleine Icon-Optimierungen aus chatarea.css (mobil) */
  .user-icon, .gender-icon, .torch-icon {
    display: inline-block !important;
    vertical-align: middle !important;
    width: 16px !important;
    height: 16px !important;
    margin-right: 2px !important;
    margin-left: 0 !important;
    line-height: 1 !important;
    object-fit: contain !important;
    max-height: 18px !important;
  }

  .mobile-input-bar-wrapper {
    position: fixed !important; left: 0 !important; bottom: 0 !important; width: 100vw; max-width: 100vw;
    background: #fff !important; z-index: 4900 !important; box-shadow: 0 -2px 8px rgba(0,0,0,0.1);
    border-radius: 0; border: none !important; display: flex; flex-direction: column; gap: 0; padding: 0;
    min-height: var(--mobile-input-bar-height) !important; /* Referenzhöhe für Padding oben */
  }

  .mobile-message-input-row {
    display: flex; flex-direction: row; align-items: center; padding: 8px; border-bottom: 1px solid #eee; background: #fff; min-height: 48px;
  }
  .mobile-message-input-row input[type="text"] {
    flex: 1 1 auto; min-width: 0; margin-right: 8px; border-radius: 8px; border: 1.5px solid #bbb; padding: 8px;
  }
  .mobile-message-input-row button {
    background: #4a90e2; color: white; border: none; border-radius: 6px; padding: 8px 16px; cursor: pointer;
  }

  .mobile-message-bar {
    background: #f8fafc; border-radius: 8px 8px 0 0; border: 1px solid #e0e7ef; box-shadow: 0 1px 4px rgba(178, 194, 217, 0.13);
    padding: 0 2px; min-height: 44px; display: flex !important; flex-direction: row !important; align-items: center !important; justify-content: space-between !important;
  }

  .mobile-button-fix {
    background: #e0f7fa; border: 1px solid #b2ebf2; border-radius: 6px; min-width: 36px; min-height: 36px; margin: 0 2px;
    display: inline-flex !important; flex-direction: column !important; align-items: center !important; justify-content: center !important; cursor: pointer; font-size: 0.75em;
  }
  .btn-mobile-logout { background: #fff !important; border: 1px solid #e0e7ef !important; color: #c0392b; }
  .btn-mobile-hamburger { background: #f3f6fb !important; border: 1px solid #e0e7ef !important; color: #3b4a5a; }

  #mobile-current-room {
    flex: 1 1 auto; text-align: center; font-weight: 500; color: #1a237e; font-size: 0.98em; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    background: #f0f4ff; border-radius: 5px; padding: 0 5px; margin: 0 2px; height: 36px; display: flex; align-items: center; justify-content: center;
  }
  .btn-label { font-size: 0.75em; margin-top: 2px; }

  /* Sidebar: auf Mobile standardmäßig ausblenden, nur als Overlay öffnen */
  .sidebar {
    display: none !important;
    position: fixed !important;
    left: -9999px !important; /* aus Layout entfernen */
    width: 0 !important;
    min-width: 0 !important;
    max-width: 0 !important;
    height: 0 !important;
    min-height: 0 !important;
    max-height: 0 !important;
    opacity: 0 !important;
    visibility: hidden !important;
    pointer-events: none !important;
  }
  /* Auf echten Mobilgeräten NIE Sidebar anzeigen (auch nicht per Klasse) */
  body.mobile-sidebar-open .sidebar { display: none !important; }
  body.mobile-sidebar-open .sidebar .sidebar-close-btn {
    display: block !important;
    position: absolute;
    top: 8px;
    left: 8px;
    width: 36px;
    height: 36px;
    border-radius: 18px;
    background: #f5f7fa;
    border: 1px solid #dee2e6;
    color: #333;
  }

  /* Smiley-Popup mittig platzieren (mobil) */
  .smiley-container {
    position: fixed !important;
    top: 50% !important;
    left: 50% !important;
    transform: translate(-50%, -50%) !important;
    width: 90% !important;
    max-width: 520px !important;
    max-height: 70vh !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    border-radius: 12px !important;
  }

  /* Generischer Fallback für Popups auf kleinen Screens */
  .popup {
    max-width: 95vw !important;
    max-height: 90vh !important;
    overflow: auto !important;
    box-sizing: border-box !important;
  }
}
/* Mobile/Tablet chat styles */

@media (max-width: 820px) {
  /* Add mobile-specific spacings or overrides here */
  /* Overlay-Scroll-Fixes für mobile Sidebar/Menu (aus altem mobile.css) */
  #mobile-sidebar-overlay, #mobile-menu-overlay {
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    max-height: 100vh !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: flex-end !important;
    justify-content: flex-end !important;
  }
  #mobile-sidebar-content, #mobile-menu-content {
    overflow-y: auto !important;
    max-height: 90vh !important;
    width: 100vw !important;
    box-sizing: border-box !important;
    display: flex !important;
    flex-direction: column !important;
  }
}

/* Tablet-Schwelle: Sidebar ausblenden und Einspalten-Layout erzwingen */
@media (max-width: 1024px) {
  .main-layout,
  .main-layout.sidebar-standard-layout {
    display: flex !important;
    flex-direction: column !important;
    grid-template-areas: 'header' 'content' 'footer' !important;
    grid-template-columns: 1fr !important;
    grid-template-rows: auto 1fr auto !important;
    width: 100% !important;
    max-width: 100% !important;
  }
  .chat-header, #chat-container, .chat-input-footer {
    grid-area: unset !important;
    width: 100% !important;
    max-width: 100% !important;
  }
  .sidebar {
    display: none !important;
    position: fixed !important;
    left: -9999px !important;
    width: 0 !important;
    min-width: 0 !important;
    max-width: 0 !important;
    height: 0 !important;
    min-height: 0 !important;
    max-height: 0 !important;
    opacity: 0 !important;
    visibility: hidden !important;
    pointer-events: none !important;
  }
  body.mobile-sidebar-open .sidebar { display: none !important; }
}
