/* ═══════════════════════════════════════════════════════
   MOBILE.CSS — Complete Mobile UI for Nexus Messenger
   Rewritten for proper touch-first experience
═══════════════════════════════════════════════════════ */

/* ── HIDE MOBILE-ONLY ELEMENTS ON DESKTOP ── */
.mobile-header,
.mobile-overlay,
.mobile-bottom-nav {
  display: none !important;
}

/* ══════════════════════════════════════════════════════
   MOBILE BREAKPOINT (< 768px)
══════════════════════════════════════════════════════ */
@media (max-width: 768px) {

  /* ── GLOBAL RESETS ── */
  * { -webkit-tap-highlight-color: transparent; }
  body { overflow: hidden; position: fixed; width: 100%; height: 100%; }

  #app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    width: 100vw;
    overflow: hidden;
    position: relative;
  }

  /* ── MOBILE HEADER ── */
  .mobile-header {
    display: flex !important;
    align-items: center;
    justify-content: space-between;
    padding: 0 12px;
    height: 48px;
    min-height: 48px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--bg-tertiary);
    position: relative;
    z-index: 50;
    flex-shrink: 0;
  }

  .mobile-hamburger {
    width: 40px;
    height: 40px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 5px;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    -webkit-tap-highlight-color: transparent;
  }
  .mobile-hamburger:active { background: var(--bg-hover); }
  .mobile-hamburger span {
    width: 20px; height: 2px;
    background: var(--text-normal);
    border-radius: 2px;
    transition: transform 0.3s, opacity 0.3s;
    display: block;
  }
  .mobile-hamburger.active span:nth-child(1) { transform: rotate(45deg) translate(5px, 5px); }
  .mobile-hamburger.active span:nth-child(2) { opacity: 0; }
  .mobile-hamburger.active span:nth-child(3) { transform: rotate(-45deg) translate(5px, -5px); }

  .mobile-title {
    font-weight: 700;
    font-size: 16px;
    color: var(--text-header);
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    pointer-events: none;
  }

  /* ── OVERLAY ── */
  .mobile-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 90;
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
  }
  .mobile-overlay.active { display: block !important; }

  /* ── SERVER RAIL (left drawer) ── */
  .server-rail {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: 72px;
    z-index: 102;
    transform: translateX(-100%);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    padding-top: 12px;
    padding-bottom: env(safe-area-inset-bottom, 12px);
    will-change: transform;
  }
  .server-rail.mobile-open {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.4);
  }
  .rail-item {
    width: 44px !important;
    height: 44px !important;
    font-size: 13px !important;
  }

  /* ── SIDE PANEL (channels/DMs — slides in next to rail) ── */
  .side-panel {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: 280px;
    z-index: 101;
    transform: translateX(-100%);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
    will-change: transform;
    background: var(--bg-secondary);
  }
  .side-panel.mobile-open {
    transform: translateX(72px);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.3);
  }

  .panel-header {
    padding: 10px 12px !important;
    min-height: 48px;
  }
  .panel-body {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* channel items — larger touch targets */
  .ch-item {
    padding: 10px 12px 10px 16px !important;
    min-height: 40px;
    font-size: 15px !important;
  }
  .ch-category { padding: 8px 12px 4px 16px !important; }
  .ch-category-name { font-size: 12px !important; }

  /* DM items in side panel */
  .dm-item { padding: 10px 12px !important; min-height: 44px; }
  .dm-name { font-size: 15px !important; }

  /* User area at panel bottom */
  .user-area {
    padding: 8px 10px !important;
    min-height: 52px;
    border-top: 1px solid var(--bg-tertiary);
    flex-shrink: 0;
    padding-bottom: max(8px, env(safe-area-inset-bottom)) !important;
  }
  .ua-btn { width: 36px; height: 36px; min-height: 36px; min-width: 36px; }

  /* ── MAIN CONTENT ── */
  .main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    width: 100% !important;
    margin: 0 !important;
    min-height: 0;
  }

  /* ── TOP BAR ── */
  .top-bar {
    height: 44px;
    min-height: 44px;
    padding: 0 8px !important;
    gap: 6px !important;
    flex-shrink: 0;
  }
  .top-icon { font-size: 16px !important; }
  .top-channel-name { font-size: 14px !important; max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .top-sep { display: none !important; }
  .top-topic { display: none !important; }
  .top-btn { width: 36px; height: 36px; min-width: 36px; min-height: 36px; }

  /* ── FRIENDS HEADER ── */
  .friends-header {
    height: auto !important;
    min-height: 44px;
    padding: 8px 12px !important;
    flex-wrap: wrap;
    gap: 8px !important;
  }
  .friends-header-icon { flex-shrink: 0; }
  .friends-header-title { font-size: 14px !important; }
  .fh-divider { display: none !important; }
  .friends-nav {
    flex-wrap: wrap;
    gap: 4px !important;
    order: 10;
    width: 100%;
  }
  .fnav-btn {
    padding: 6px 10px !important;
    font-size: 13px !important;
    border-radius: 16px !important;
    flex-shrink: 0;
  }
  .friends-search-bar { padding: 8px 12px !important; }
  .friend-row { padding: 10px 12px !important; gap: 10px !important; margin: 0 4px !important; }
  .friend-btn { width: 40px; height: 40px; min-width: 40px; }
  .add-friend-page { padding: 16px !important; }
  .add-friend-page h2 { font-size: 18px !important; }
  .add-friend-form { flex-direction: column; }
  .add-friend-form input { padding: 14px 16px !important; font-size: 16px !important; }
  .add-friend-form button { margin: 8px !important; padding: 12px !important; }

  /* ── CHAT AREA ── */
  .chat-wrapper {
    flex: 1;
    overflow: hidden;
    position: relative;
  }
  .chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    width: 100%;
  }

  /* Messages */
  .messages-container {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  .channel-welcome { padding: 24px 16px 16px !important; }
  .cw-icon { width: 48px !important; height: 48px !important; font-size: 24px !important; }
  .cw-title { font-size: 20px !important; }
  .cw-desc { font-size: 14px !important; }

  /* Message groups */
  .msg-group {
    padding: 2px 12px 2px 56px !important;
  }
  .msg-group.first {
    padding-top: 12px !important;
  }

  .msg-avatar-col {
    left: 12px !important;
    top: 12px !important;
  }
  .msg-avatar {
    width: 36px !important;
    height: 36px !important;
    font-size: 14px !important;
  }

  .msg-timestamp-small {
    left: 12px !important;
    width: 36px !important;
    font-size: 9px !important;
  }

  .msg-author { font-size: 14px !important; }
  .msg-time { font-size: 11px !important; }
  .msg-text { font-size: 15px !important; line-height: 1.45 !important; }

  /* Reply bars */
  .msg-reply-bar { font-size: 12px !important; gap: 6px !important; }

  /* Action toolbar — show on long-press area */
  .msg-actions {
    position: absolute;
    right: 8px !important;
    top: -16px !important;
    padding: 2px !important;
    gap: 0 !important;
    border-radius: 8px !important;
    opacity: 0;
    pointer-events: none;
  }
  .msg-group:active .msg-actions,
  .msg-group:focus-within .msg-actions {
    opacity: 1;
    pointer-events: all;
  }
  .msg-action { width: 32px !important; height: 32px !important; }
  .msg-quick-reactions { display: none !important; }

  /* Reactions */
  .msg-reactions { margin-top: 4px !important; gap: 4px !important; }
  .reaction { padding: 4px 8px !important; font-size: 13px !important; border-radius: 12px !important; }

  /* Attachments */
  .att-image { max-width: calc(100vw - 80px) !important; max-height: 240px !important; }
  .att-file { max-width: calc(100vw - 80px) !important; padding: 10px 12px !important; }
  .att-gallery .att-image { max-width: calc(50vw - 50px) !important; max-height: 160px !important; }

  /* Date divider */
  .date-divider { padding: 12px 12px 6px !important; font-size: 11px !important; }

  /* ── INPUT AREA ── */
  .input-area {
    padding: 0 8px 8px !important;
    flex-shrink: 0;
    padding-bottom: max(8px, env(safe-area-inset-bottom)) !important;
  }

  .input-box {
    border-radius: 20px !important;
    min-height: 44px;
    background: var(--bg-tertiary) !important;
  }
  .reply-strip + .input-box {
    border-radius: 0 0 20px 20px !important;
  }
  .reply-strip {
    border-radius: 20px 20px 0 0 !important;
    padding: 6px 12px !important;
    font-size: 12px !important;
  }

  .input-attach-btn {
    width: 40px !important;
    height: 44px !important;
    padding-left: 4px;
  }
  .msg-input {
    font-size: 16px !important;
    padding: 11px 4px !important;
    min-height: 22px;
    max-height: 120px !important;
  }
  .input-actions { padding: 4px 6px 4px 0 !important; }
  .input-icon-btn { width: 36px !important; height: 36px !important; }
  .gif-btn { margin-bottom: 4px !important; }

  .file-chips { padding: 6px 0 0 12px !important; }
  .file-chip { font-size: 12px !important; }

  /* Typing bar */
  .typing-bar { padding: 0 12px !important; height: 20px !important; font-size: 11px !important; }

  /* ── EMOJI PICKER ── */
  .emoji-picker {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    top: auto !important;
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 16px 16px 0 0 !important;
    max-height: 50vh;
    z-index: 200 !important;
  }
  .emoji-grid {
    grid-template-columns: repeat(8, 1fr) !important;
    max-height: 35vh !important;
  }
  .e-btn { width: 38px !important; height: 38px !important; font-size: 22px !important; }

  /* Emoji quick picker */
  .emoji-quick-picker {
    position: fixed !important;
    bottom: 60px !important;
    left: 50% !important;
    transform: translateX(-50%) !important;
    right: auto !important;
  }

  /* GIF picker */
  #gif-picker {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    top: auto !important;
    width: 100% !important;
    max-height: 60vh !important;
    border-radius: 16px 16px 0 0 !important;
    z-index: 200 !important;
  }

  /* ── MEMBER LIST (right drawer) ── */
  .member-list {
    position: fixed !important;
    right: 0;
    top: 0;
    bottom: 0;
    width: 280px;
    max-width: 80vw;
    z-index: 101;
    transform: translateX(100%);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    will-change: transform;
    background: var(--bg-secondary) !important;
    border-left: 1px solid var(--bg-tertiary);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: block !important;
  }
  .member-list.mobile-open {
    transform: translateX(0);
    box-shadow: -4px 0 24px rgba(0, 0, 0, 0.4);
  }
  .ml-item { padding: 8px 12px !important; min-height: 44px; }

  /* ── WELCOME STATE ── */
  .welcome-state {
    padding: 32px 24px !important;
    text-align: center;
  }
  .ws-icon {
    width: 64px !important; height: 64px !important;
    font-size: 28px !important;
  }

  /* ── MODALS ── */
  .modal-overlay {
    align-items: flex-end !important;
    padding: 0 !important;
  }
  #modal-box,
  .modal {
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
    border-radius: 16px 16px 0 0 !important;
    max-height: 90vh !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }
  .modal-inner { padding: 20px 16px !important; }
  .modal-title { font-size: 18px !important; }
  .modal-input {
    font-size: 16px !important;
    padding: 12px !important;
    border-radius: 10px !important;
  }
  .modal-select {
    font-size: 16px !important;
    padding: 12px !important;
  }
  .modal-btns {
    flex-direction: column-reverse !important;
    gap: 8px !important;
    padding-bottom: env(safe-area-inset-bottom, 8px) !important;
  }
  .modal-btns button,
  .modal-btns .btn {
    width: 100% !important;
    padding: 14px !important;
    border-radius: 12px !important;
    font-size: 15px !important;
  }

  /* ── SETTINGS PAGE (full screen) ── */
  .settings-page {
    flex-direction: column !important;
    position: fixed !important;
    inset: 0 !important;
    z-index: 500 !important;
  }
  .settings-sidebar,
  .settings-nav {
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    max-height: 40vh !important;
    border-right: none !important;
    border-bottom: 1px solid var(--bg-tertiary) !important;
    overflow-x: auto !important;
    overflow-y: auto !important;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
  }
  .settings-nav-items {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 4px;
    padding: 8px;
  }
  .settings-nav-item,
  .settings-link {
    padding: 8px 14px !important;
    border-radius: 16px !important;
    font-size: 13px !important;
    white-space: nowrap;
    flex-shrink: 0;
    min-height: 36px;
  }
  .settings-content,
  .settings-panel {
    flex: 1;
    width: 100% !important;
    max-width: 100% !important;
    padding: 16px !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }
  .settings-close-btn {
    position: fixed !important;
    top: 12px !important;
    right: 12px !important;
    z-index: 510 !important;
    width: 40px !important;
    height: 40px !important;
  }
  .settings-input,
  .settings-textarea {
    font-size: 16px !important;
    padding: 12px !important;
  }

  /* Server settings */
  .ss-page {
    flex-direction: column !important;
  }
  .ss-sidebar,
  .ss-nav {
    width: 100% !important;
    max-height: 30vh;
    overflow-x: auto;
    border-right: none !important;
    border-bottom: 1px solid var(--bg-tertiary);
  }
  .ss-content { width: 100% !important; padding: 16px !important; }

  /* ── CONTEXT MENU ── */
  .ctx-menu {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    top: auto !important;
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 16px 16px 0 0 !important;
    padding-bottom: env(safe-area-inset-bottom, 8px) !important;
    max-height: 60vh;
    overflow-y: auto;
    animation: slideUp 0.2s ease-out;
  }
  .ctx-item {
    padding: 14px 20px !important;
    font-size: 15px !important;
    min-height: 44px !important;
  }

  /* ── TOASTS ── */
  .toasts {
    bottom: 70px !important;
    top: auto !important;
    left: 12px !important;
    right: 12px !important;
  }
  .toast, .toast-modern {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    border-radius: 12px !important;
    right: 0 !important;
  }

  /* ── PROFILE POPOUT ── */
  #profile-popout .profile-popout,
  .profile-popout {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    top: auto !important;
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 16px 16px 0 0 !important;
    max-height: 70vh;
    overflow-y: auto;
    transform: none !important;
    animation: slideUp 0.25s ease-out;
  }

  /* ── VOICE CHANNEL VIEW ── */
  .voice-view { padding: 12px !important; }
  .vc-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }
  .vc-tile {
    height: auto !important;
    min-height: 120px;
    border-radius: 12px !important;
  }
  .vc-controls {
    flex-wrap: wrap;
    gap: 8px !important;
    padding: 12px 8px !important;
    justify-content: center;
  }
  .vc-ctrl-btn {
    width: 50px !important;
    height: 50px !important;
    border-radius: 50% !important;
    flex-direction: column;
  }
  .vc-ctrl-btn svg { width: 22px; height: 22px; }
  .vc-ctrl-btn span { font-size: 9px !important; }

  /* ── DM CALL OVERLAY ── */
  .dm-call-overlay,
  .call-overlay,
  #dm-call-overlay {
    padding: 16px !important;
  }
  .dm-call-actions,
  .call-controls {
    gap: 16px !important;
    flex-wrap: wrap;
    justify-content: center;
  }
  .call-btn, .dm-call-btn {
    width: 54px !important;
    height: 54px !important;
  }

  /* ── INVITE PAGE ── */
  .invite-page {
    padding: 24px 16px !important;
  }
  .invite-card {
    width: 100% !important;
    max-width: 100% !important;
    padding: 20px 16px !important;
  }

  /* ── LIGHTBOX ── */
  .lightbox {
    padding: 16px !important;
  }
  .lightbox img {
    max-width: 100% !important;
    max-height: 85vh !important;
    border-radius: 8px;
  }

  /* ── THREAD PANEL ── */
  .thread-panel {
    position: fixed !important;
    inset: 0 !important;
    width: 100% !important;
    z-index: 110;
    border-radius: 0 !important;
  }
  .thread-header { padding: 12px 16px !important; min-height: 48px; }
  .thread-input {
    font-size: 16px !important;
    padding: 12px !important;
  }

  /* ── PINS PANEL ── */
  .pins-panel {
    position: fixed !important;
    inset: 0 !important;
    width: 100% !important;
    z-index: 110;
    border-radius: 0 !important;
    overflow-y: auto;
  }

  /* ── BOTTOM NAV ── */
  .mobile-bottom-nav {
    display: flex !important;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(56px + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: var(--bg-tertiary);
    border-top: 1px solid rgba(255, 255, 255, 0.06);
    z-index: 50;
    justify-content: space-around;
    align-items: center;
    flex-shrink: 0;
  }

  .mobile-nav-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    padding: 6px 4px;
    color: var(--text-muted);
    font-size: 10px;
    font-weight: 600;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: color 0.15s;
    position: relative;
  }
  .mobile-nav-btn:active { transform: scale(0.92); }
  .mobile-nav-btn.active { color: var(--text-header); }
  .mobile-nav-btn.active::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 24px;
    height: 2px;
    background: var(--brand);
    border-radius: 0 0 2px 2px;
  }
  .mobile-nav-btn svg { width: 22px; height: 22px; }

  /* Ensure content does not hide behind bottom nav */
  .main-content {
    padding-bottom: 0 !important;
    height: calc(100vh - 48px - 56px - env(safe-area-inset-bottom, 0px));
    height: calc(100dvh - 48px - 56px - env(safe-area-inset-bottom, 0px));
  }

  /* ── SERVER DROPDOWN ── */
  .server-dropdown {
    position: fixed !important;
    top: auto !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    width: 100% !important;
    border-radius: 16px 16px 0 0 !important;
    max-height: 60vh;
    z-index: 200 !important;
    padding-bottom: env(safe-area-inset-bottom, 8px) !important;
    animation: slideUp 0.2s ease-out;
  }

  /* ── SCROLLBAR HIDE ── */
  ::-webkit-scrollbar { width: 0 !important; height: 0 !important; }
  * { scrollbar-width: none; }

  /* ── TOUCH HELPERS ── */
  button, .btn, .rail-item, .ch-item, .dm-item, .ml-item, .friend-row,
  .ctx-item, .fnav-btn, .mobile-nav-btn, .top-btn, .ua-btn, .vc-ctrl-btn {
    -webkit-tap-highlight-color: transparent;
  }

  /* Ensure all interactive elements have minimum touch target */
  .ch-item, .dm-item, .ml-item, .friend-row {
    min-height: 44px;
    display: flex;
    align-items: center;
  }

  /* ── ANIMATIONS ── */
  @keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
  }

  @keyframes slideDown {
    from { transform: translateY(-100%); }
    to { transform: translateY(0); }
  }
}

/* ══════════════════════════════════════════════════════
   TABLET (768px - 1024px)
══════════════════════════════════════════════════════ */
@media (min-width: 768px) and (max-width: 1024px) {
  .server-rail { width: 64px; min-width: 64px; }
  .side-panel { width: 220px; }
  .member-list { width: 220px; min-width: 220px; }
}

/* ══════════════════════════════════════════════════════
   LANDSCAPE PHONES
══════════════════════════════════════════════════════ */
@media (max-width: 768px) and (max-height: 500px) and (orientation: landscape) {
  .mobile-header { height: 40px !important; min-height: 40px !important; padding: 0 8px !important; }
  .mobile-bottom-nav { height: 44px !important; }
  .mobile-nav-btn span { display: none; }
  .msg-group.first { padding-top: 8px !important; }
  .channel-welcome { padding: 12px !important; }
  .cw-title { font-size: 16px !important; }
  .vc-grid { grid-template-columns: repeat(3, 1fr) !important; }
}
