/* Community3D — Mobile + Touch Optimierung
 *
 * Zweck:
 * - iOS-Zoom beim Fokus auf Inputs/Textareas verhindern (font-size >= 16px)
 * - Panels/Dialoge auf Mobile als Bottom-Sheet darstellen
 * - Touch-Targets >= 44x44
 *
 * Wird in index.html, login.html, register.html, profile.html, callback.html geladen.
 */

/* ---------- App-Shell: Layout ist fixiert, nichts bounced ---------- */
/* Gilt NUR fuer die Haupt-App (body ohne .auth-page) — Auth/Profil-Seiten scrollen normal. */
html:not(:has(body.auth-page)) {
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
}
body:not(.auth-page) {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: auto;
  touch-action: none;
}
/* Canvas bleibt touch-action: none (schon inline gesetzt) */

/* Scrollbare Innen-Container brauchen Touch-Pan + contained Overscroll */
#chatMessages,
#c3dRoomChatMessages,
#c3dDmMessages,
.c3d-panel-body,
#c3dGalleryGrid,
#c3dQList,
#c3dRsBody,
#videoDetail,
#newsDetail,
#newsCommentsList {
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}

/* Jedes fixed Panel selbst darf auch pannen (damit Input-Fokus den Inhalt scrollen kann) */
#chat, #c3dRoomChat, #c3dDmPanel, #c3dGalleryPanel, #c3dQPanel, #c3dRsPanel,
.c3d-panel-wrap, #videoDetail, #newsDetail, #c3dProfilePopup,
#c3dSrDialog, #c3dSpDialog, #c3dSmDialog {
  touch-action: manipulation;
  overscroll-behavior: contain;
}

/* Kein horizontales Scrollen, nirgends */
body, #c3dPanels, #c3dActionRail {
  max-width: 100vw;
}

/* ---------- Global: kein Auto-Zoom auf Inputs (iOS) ---------- */
/* iOS-Safari zoomt rein wenn focused-input computed font-size < 16px hat.
   !important weil die UI-Komponenten eigene ID-Selektoren mit 13-14px setzen. */
input,
textarea,
select {
  font-size: 16px !important;
}
button {
  font-size: 16px;
}

/* Damit greift die 16px-Regel fuer chatInput, DM, Comment, Room-Chat, Dialoge,
   Post-Input — egal ob ID-Selektor oder Inline-Style. */

/* ---------- Tap-Highlight entfernen ---------- */
button,
.c3d-rail-btn,
.c3d-bubble,
.news-btn,
.auth-btn {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Hamburger-Menue + Profil-Label sind Mobile-only. Desktop unsichtbar. */
#c3dRailHamburger { display: none; }
#userHud .hud-profil-label { display: none; }

/* ================================================================
   EXKLUSIV-LOGIK (nur Mobile): nur EIN Bottom-Element zur Zeit,
   UserHud immer klickbar, keine Icon-Ueberlappungen.
   ================================================================ */
@media (max-width: 640px), (pointer: coarse) and (max-width: 1024px) {

  /* UserHud ueber DM (250), unter Popup (300) — damit nix drueber klebt */
  #userHud { z-index: 260 !important; }

  /* Close-Buttons gross genug fuer Touch */
  #newsDetailClose,
  #videoDetailClose,
  .c3d-panel-close {
    min-width: 36px !important;
    min-height: 36px !important;
  }

  /* DM offen → alles andere unten aus */
  body.dm-open #c3dBubbles,
  body.dm-open #c3dPanels,
  body.dm-open #c3dRoomChat,
  body.dm-open #c3dRoomChatBubble,
  body.dm-open #npcFeed {
    display: none !important;
  }

  /* Room-Chat offen (nicht collapsed) → Social-Widgets aus */
  body:not(.room-chat-collapsed) #c3dBubbles,
  body:not(.room-chat-collapsed) #c3dPanels {
    display: none !important;
  }

  /* Fullscreen-Modals (Gallery/Q&A/Settings) → Bottom-Elemente aus */
  body.gallery-open #c3dBubbles,
  body.gallery-open #c3dRoomChatBubble,
  body.gallery-open #c3dRoomChat,
  body.q-open #c3dBubbles,
  body.q-open #c3dRoomChatBubble,
  body.q-open #c3dRoomChat,
  body.rs-open #c3dBubbles,
  body.rs-open #c3dRoomChatBubble,
  body.rs-open #c3dRoomChat {
    display: none !important;
  }

  /* Profil-Popup offen → Bottom-Elemente aus */
  body.profile-popup-open #c3dBubbles,
  body.profile-popup-open #c3dRoomChatBubble,
  body.profile-popup-open #c3dRoomChat {
    display: none !important;
  }

  /* Dialoge (Sub-Raum / Space / Mod / Loeschen) → Bottom-Elemente aus */
  body.subroom-create-open #c3dBubbles,
  body.subroom-create-open #c3dRoomChatBubble,
  body.subroom-create-open #c3dRoomChat,
  body.space-create-open #c3dBubbles,
  body.space-create-open #c3dRoomChatBubble,
  body.space-create-open #c3dRoomChat,
  body.mod-edit-open #c3dBubbles,
  body.mod-edit-open #c3dRoomChatBubble,
  body.mod-edit-open #c3dRoomChat,
  body.mod-del-open #c3dBubbles,
  body.mod-del-open #c3dRoomChatBubble,
  body.mod-del-open #c3dRoomChat,
  body.sm-edit-open #c3dBubbles,
  body.sm-edit-open #c3dRoomChatBubble,
  body.sm-edit-open #c3dRoomChat,
  body.sm-del-open #c3dBubbles,
  body.sm-del-open #c3dRoomChatBubble,
  body.sm-del-open #c3dRoomChat {
    display: none !important;
  }
}

/* ---------- Mobile-spezifisch ---------- */
@media (max-width: 640px), (pointer: coarse) and (max-width: 1024px) {

  /* Auth-Seiten: Karten kleiner, randlose Ansicht */
  .auth-wrap { padding: 16px 12px !important; }
  .auth-card { padding: 22px 18px 20px !important; border-radius: 12px !important; }
  .auth-title { font-size: 22px !important; }

  /* Profil: Preset-Grid kompakter */
  .preset-grid { grid-template-columns: repeat(4, 1fr) !important; gap: 8px !important; }

  /* Raum-HUD oben mittig: dezenter */
  #roomHeader { padding: 3px 10px !important; }
  #roomTitle  { font-size: 12px !important; }

  /* Hamburger-Button oben links */
  #c3dRailHamburger {
    display: flex !important;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 10px;
    left: 10px;
    width: 44px;
    height: 44px;
    z-index: 70;
    background: rgba(8, 12, 20, 0.6);
    border: 1px solid rgba(255,255,255,0.14);
    color: #fff;
    border-radius: 10px;
    font-size: 22px;
    cursor: pointer;
    backdrop-filter: blur(6px);
    padding: 0;
  }
  body.rail-open #c3dRailHamburger {
    background: rgba(90, 220, 140, 0.25);
    border-color: rgba(90, 220, 140, 0.5);
  }

  /* Action-Rail: default versteckt — nur via Hamburger */
  #c3dActionRail {
    display: none !important;
  }

  /* Menue offen → Rail als vertikale Spalte unter Hamburger */
  body.rail-open #c3dActionRail {
    display: flex !important;
    flex-direction: column !important;
    top: 60px !important;
    left: 10px !important;
    right: auto !important;
    transform: none !important;
    max-width: calc(100vw - 20px) !important;
    padding: 6px !important;
    gap: 4px !important;
    flex-wrap: nowrap !important;
    align-items: stretch !important;
    overflow: visible !important;
  }

  /* Jeder sichtbare Rail-Button: Icon links + Label-Badge rechts daneben */
  body.rail-open .c3d-rail-btn.visible {
    width: auto !important;
    min-width: 210px !important;
    height: 46px !important;
    padding: 0 10px 0 12px !important;
    justify-content: flex-start !important;
    gap: 14px !important;
    flex-direction: row !important;
    border-radius: 10px !important;
    font-size: 20px !important;
  }

  /* data-label Pseudo als permanenter Badge rechts neben Icon */
  body.rail-open .c3d-rail-btn::after {
    content: attr(data-label) !important;
    display: inline-block !important;
    position: static !important;
    opacity: 1 !important;
    transform: none !important;
    top: auto !important; left: auto !important;
    background: rgba(255,255,255,0.1) !important;
    border: 1px solid rgba(255,255,255,0.15) !important;
    color: #fff !important;
    font-size: 13px !important;
    font-weight: 500 !important;
    padding: 4px 10px !important;
    border-radius: 12px !important;
    line-height: 1 !important;
    white-space: nowrap !important;
    pointer-events: none !important;
  }

  /* UserHud kompakt: nur Avatar + "Profil" */
  #userHud .hud-name,
  #userLogout {
    display: none !important;
  }
  #userHud .hud-profil-label {
    display: inline-block !important;
    font-size: 13px !important;
    padding: 2px 0;
  }
  #userHud {
    gap: 8px !important;
    padding: 4px 10px 4px 4px !important;
  }

  /* Rechts unten Bubbles: etwas kleiner, tiefer anliegend */
  #c3dBubbles {
    right: 10px !important; bottom: 10px !important;
    gap: 6px !important;
  }
  .c3d-bubble {
    width: 48px !important; height: 48px !important;
    font-size: 20px !important;
  }

  /* Widget-Panels (Feed / Freunde / Chats): Bottom-Sheet */
  #c3dPanels {
    right: 0 !important; left: 0 !important;
    top: auto !important; bottom: 72px !important;
    max-width: 100vw !important;
    padding: 0 8px !important;
    flex-direction: column !important;
  }
  .c3d-panel-wrap {
    width: 100% !important; max-width: 100% !important;
    max-height: 70dvh !important;
    border-radius: 14px !important;
  }
  .c3d-panel-body {
    min-height: 120px !important;
    max-height: calc(70dvh - 48px) !important;
  }

  /* Legacy Video-Chat rechts oben → Bottom-Sheet */
  #chat {
    left: 0 !important; right: 0 !important;
    top: auto !important; bottom: 0 !important;
    width: 100vw !important;
    max-width: 100vw !important;
    max-height: 75dvh !important;
    border-radius: 14px 14px 0 0 !important;
    padding-bottom: env(safe-area-inset-bottom) !important;
  }
  #chatMessages { max-height: 50dvh !important; }

  /* Raum-Chat (links unten) → Bottom-Sheet */
  #c3dRoomChat {
    left: 0 !important; right: 0 !important;
    bottom: 0 !important;
    width: 100vw !important;
    max-width: 100vw !important;
    max-height: 60dvh !important;
    border-radius: 14px 14px 0 0 !important;
    padding-bottom: env(safe-area-inset-bottom) !important;
  }
  #c3dRoomChatMessages { max-height: 44dvh !important; }
  #c3dRoomChatBubble {
    left: 10px !important; bottom: 10px !important;
    width: 48px !important; height: 48px !important;
  }

  /* DM-Panel → Bottom-Sheet */
  #c3dDmPanel {
    left: 0 !important; right: 0 !important;
    top: auto !important; bottom: 0 !important;
    width: 100vw !important;
    max-width: 100vw !important;
    height: 85dvh !important;
    max-height: 85dvh !important;
    border-radius: 14px 14px 0 0 !important;
    padding-bottom: env(safe-area-inset-bottom) !important;
  }
  #c3dDmInput { padding: 10px 12px !important; }

  /* Gallery → Fullscreen */
  #c3dGalleryPanel {
    left: 0 !important; top: 0 !important;
    transform: none !important;
    width: 100vw !important; height: 100dvh !important;
    max-width: 100vw !important; max-height: 100dvh !important;
    border-radius: 0 !important;
  }

  /* Gallery-Raster: 3 Spalten, kein Hover-Scale (der Bilder in Nachbarzelle schiebt),
     Overlay immer sichtbar damit die S/M/L/Del-Buttons tappbar sind */
  #c3dGalleryBody {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)) !important;
    gap: 8px !important;
    padding: 12px !important;
  }
  .c3d-gal-item {
    transform: none !important;
  }
  .c3d-gal-item:hover {
    transform: none !important;
    border-color: rgba(255,255,255,0.1) !important;
  }
  .c3d-gal-item:active {
    transform: none !important;
  }
  .c3d-gal-item-overlay {
    opacity: 1 !important;
    background: linear-gradient(180deg, transparent 30%, rgba(0,0,0,0.85)) !important;
    padding: 4px !important;
  }
  .c3d-gal-action {
    padding: 6px 4px !important;
    font-size: 11px !important;
    min-height: 30px;
    border-radius: 5px !important;
  }
  .c3d-gal-badge {
    top: 4px !important;
    left: 4px !important;
    padding: 2px 6px !important;
    font-size: 9px !important;
  }
  .c3d-gal-source {
    font-size: 9px !important;
    margin-bottom: 3px !important;
  }

  /* Q&A → Fullscreen */
  #c3dQPanel {
    left: 0 !important; top: 0 !important;
    transform: none !important;
    width: 100vw !important; height: 100dvh !important;
    max-width: 100vw !important; max-height: 100dvh !important;
    border-radius: 0 !important;
  }

  /* Profil-Popup → zentriert, full-width */
  #c3dProfilePopup {
    left: 12px !important; right: 12px !important;
    transform: translateY(-50%) !important;
    top: 50% !important;
    width: auto !important;
    max-width: calc(100vw - 24px) !important;
  }

  /* Kleine Dialoge zentriert halten, aber breiter */
  #c3dSrDialog, #c3dSpDialog, #c3dSmDialog {
    width: calc(100vw - 24px) !important;
    max-width: calc(100vw - 24px) !important;
  }

  /* Room-Settings-Panel → Fullscreen */
  #c3dRsPanel {
    left: 0 !important; top: 0 !important;
    transform: none !important;
    width: 100vw !important; height: 100dvh !important;
    max-width: 100vw !important; max-height: 100dvh !important;
    border-radius: 0 !important;
  }

  /* Room-Presence (links) auf Mobile ausblenden — Platz zu knapp */
  #c3dPresencePanel { display: none !important; }

  /* News-Detail rechts → Bottom-Sheet */
  #newsDetail {
    left: 0 !important; right: 0 !important;
    top: auto !important; bottom: 0 !important;
    width: 100vw !important;
    max-width: 100vw !important;
    max-height: 85dvh !important;
    border-radius: 14px 14px 0 0 !important;
    padding: 14px !important;
    padding-bottom: calc(14px + env(safe-area-inset-bottom)) !important;
  }

  /* Video-Detail: schon 92vw — nur Rand abschwaechen */
  #videoDetail {
    max-height: 92dvh !important;
    padding: 10px !important;
  }

  /* NPC-Feed unten links: auf Mobile ausblenden */
  #npcFeed { display: none !important; }


  /* HUD oben links etwas kleiner */
  #hud { font-size: 11px !important; padding: 6px 10px !important; }

  /* Touch-Targets: Inputs in Formen groesser */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="search"],
  input[type="tel"],
  input[type="url"],
  textarea {
    min-height: 40px;
    padding: 10px 12px !important;
  }

  /* Send-Buttons in Chats: groesser anfassbar */
  #chatForm button,
  #c3dDmSend, #c3dRoomChatSend {
    min-width: 56px !important;
    padding: 10px 14px !important;
  }
}

/* ================================================================
   GAST-MODUS — Features ausblenden die Gäste nicht nutzen dürfen.
   Greift auf allen Screensizes (Desktop + Mobile).
   ================================================================ */
body.is-guest #railCreateSpace,
body.is-guest #railCreateSub,
body.is-guest #railEditSub,
body.is-guest #railDeleteSub,
body.is-guest #railViewAtmo,
body.is-guest #railViewFurniture,
body.is-guest #railViewMusic,
body.is-guest #railViewGuests,
body.is-guest #railEditFurniture,
body.is-guest #railEditDisplays {
  display: none !important;
}

/* Post-Compose im Feed verstecken */
body.is-guest .c3d-post-compose,
body.is-guest .c3d-feed-filter {
  display: none !important;
}

/* Galerie: Upload verstecken */
body.is-guest .c3d-gal-upload,
body.is-guest #c3dGalleryFile {
  display: none !important;
}

/* Social-Actions im Profil-Popup: Gäste können keine Freundschaften/Follows */
body.is-guest #c3dPpFriend,
body.is-guest #c3dPpFriendAccept,
body.is-guest #c3dPpFriendDecline,
body.is-guest #c3dPpFriendCancel,
body.is-guest #c3dPpFriendRemove,
body.is-guest #c3dPpFollow,
body.is-guest #c3dPpUnfollow {
  display: none !important;
}

/* Feed-/Freunde-Widgets gehen für Gäste nicht (Posts+Freundschaft nicht möglich) */
body.is-guest .c3d-bubble[data-panel="feed"],
body.is-guest .c3d-bubble[data-panel="friends"] {
  display: none !important;
}

/* Info-Banner für Gast */
body.is-guest #userHud {
  border-color: rgba(255, 200, 100, 0.45) !important;
}

/* Wenn Zeit knapp: Timer pulsiert */
#c3dGuestTimer.urgent {
  animation: c3d-timer-pulse 1s ease-in-out infinite;
  color: #ff8844 !important;
  background: rgba(255, 130, 80, 0.2) !important;
  border-color: rgba(255, 130, 80, 0.5) !important;
}
@keyframes c3d-timer-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* ---------- Touch-First Hover-Fix ---------- */
@media (hover: none) {
  /* Hover-Effekte zuruecknehmen damit Touch nicht "klebt" */
  .c3d-rail-btn:hover { background: transparent; }
  .c3d-bubble:hover { background: rgba(90, 220, 140, 0.2); }
  .news-btn:hover { background: rgba(255,255,255,0.1); }
}
