/* Mobile & Tablet Responsive Adaptation (Feishu Mobile Experience) */

.rail-label {
  display: none;
}

.mobile-only {
  display: none !important;
}

.table-scroll {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Mobile Action Sheet (Touch alternative to desktop hover bar) */
.mobile-sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(31, 35, 41, 0.45);
  z-index: 9500;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fadeIn 0.14s ease-out;
}
.mobile-sheet {
  width: 100%;
  max-width: 520px;
  background: #f5f6f7;
  border-radius: 16px 16px 0 0;
  padding: 10px 12px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
  animation: sheetUp 0.18s ease-out;
}
.mobile-sheet-group {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
}
.mobile-sheet-btn {
  width: 100%;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 500;
  color: var(--text-primary);
  border-bottom: 1px solid var(--border-light);
}
.mobile-sheet-btn:last-child {
  border-bottom: none;
}
.mobile-sheet-btn:active {
  background: var(--bg-hover);
}
.mobile-sheet-btn.danger {
  color: var(--danger);
}
.mobile-sheet-btn.cancel {
  background: #fff;
  border-radius: 12px;
  color: var(--text-secondary);
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes sheetUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

/* ==================== Tablet (<= 1024px) ==================== */
@media (max-width: 1024px) {
  :root {
    --sidebar-w: 256px;
  }

  .chat-drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(340px, 88vw);
    z-index: 600;
    box-shadow: -8px 0 28px rgba(31, 35, 41, 0.16);
  }
}

/* ==================== Mobile (<= 768px) ==================== */
@media (max-width: 768px) {
  :root {
    --header-h: 52px;
  }

  .mobile-only {
    display: inline-flex !important;
  }

  /* Prevent iOS Safari zoom on inputs */
  .input, .select, .textarea, .composer-textarea, .search-box input {
    font-size: 16px;
  }

  /* Auth card mobile sizing */
  #auth-view {
    padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  }
  .auth-card {
    padding: 26px 20px 22px;
    border-radius: 14px;
  }

  /* Convert left nav rail into Feishu Mobile Bottom Tab Bar */
  #app-shell {
    flex-direction: column-reverse;
  }

  #app-shell.mobile-in-chat .nav-rail {
    display: none;
  }

  .nav-rail {
    width: 100%;
    height: calc(56px + env(safe-area-inset-bottom));
    padding: 4px 6px env(safe-area-inset-bottom);
    flex-direction: row;
    align-items: center;
    justify-content: space-around;
    background: #fff;
    border-right: none;
    border-top: 1px solid var(--border);
    z-index: 40;
  }

  .rail-nav {
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
    gap: 0;
    flex: 3;
    height: 100%;
  }

  .rail-item {
    flex: 1;
    width: auto;
    height: 46px;
    border-radius: 8px;
    gap: 2px;
  }
  .rail-item svg {
    width: 20px;
    height: 20px;
  }
  .rail-item.active {
    background: transparent;
    color: var(--brand);
  }
  .rail-label {
    display: block;
    font-size: 10px;
    line-height: 12px;
    font-weight: 500;
  }
  .rail-unread {
    top: 2px;
    right: calc(50% - 20px);
  }

  /* Move profile avatar button into the rightmost bottom tab ("我的") */
  .rail-avatar-btn {
    order: 2;
    flex: 1;
    margin-bottom: 0;
    height: 46px;
    padding: 0;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    color: var(--text-secondary);
  }
  .rail-avatar-btn .avatar {
    width: 24px;
    height: 24px;
    font-size: 10px;
  }
  .rail-avatar-btn .presence-dot {
    width: 8px;
    height: 8px;
    border-width: 1.5px;
  }

  .rail-footer {
    display: none;
  }

  /* Workspace fills area above bottom tab bar */
  .workspace-view {
    height: 0;
    flex: 1;
    min-height: 0;
  }

  /* Single-column drill-down for Messages */
  #view-messages .conv-sidebar {
    width: 100%;
    flex: 1;
    border-right: none;
  }
  #view-messages .chat-main {
    display: none;
    width: 100%;
  }
  #view-messages.mobile-chat-open .conv-sidebar {
    display: none;
  }
  #view-messages.mobile-chat-open .chat-main {
    display: flex;
  }

  .conv-sidebar-top {
    padding: 8px 12px;
  }
  .conv-item {
    padding: 12px 6px 12px 10px;
  }
  .conv-tail-btn {
    width: 32px;
    height: 38px;
    opacity: 0.72;
  }
  .conv-act-btn {
    padding: 0 16px;
    font-size: 13.5px;
  }

  /* Mobile Chat Header */
  .chat-header {
    padding: 0 8px 0 6px;
    gap: 6px;
  }
  .chat-header-left {
    gap: 6px;
    overflow: hidden;
  }
  .chat-header-left .avatar {
    width: 32px;
    height: 32px;
    font-size: 12px;
  }
  .chat-header-title {
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .chat-header-sub {
    font-size: 11px;
  }
  .chat-header-actions {
    gap: 4px;
  }

  /* Mobile Message Timeline */
  .msg-timeline {
    padding: 10px 10px 8px;
  }
  .msg-row {
    padding: 5px 4px;
    gap: 8px;
  }
  .msg-row:hover {
    background: transparent;
  }
  .msg-hover-actions {
    display: none !important;
  }
  .msg-body-col {
    max-width: 82%;
  }
  .msg-bubble {
    padding: 9px 12px;
    font-size: 15px;
    line-height: 1.48;
  }
  .msg-image-thumb {
    max-width: min(220px, 64vw);
    max-height: 220px;
  }

  /* Mobile Composer */
  .composer-wrap {
    padding: 4px 10px calc(10px + env(safe-area-inset-bottom));
  }
  .composer-hint {
    display: none;
  }
  .composer-toolbar {
    padding: 4px 8px;
  }
  .composer-input-row {
    padding: 6px 8px 8px;
    gap: 8px;
  }
  .composer-textarea {
    min-height: 36px;
    max-height: 110px;
  }
  .composer-voice-bar {
    padding: 8px 10px;
    gap: 6px;
    min-height: 52px;
  }
  .voice-rec-label {
    display: none;
  }
  .voice-bubble {
    max-width: min(220px, 68vw);
  }
  .emoji-popover {
    width: calc(100vw - 24px);
    left: 0;
    bottom: 42px;
  }

  /* Full-screen Mobile Right Drawer */
  .chat-drawer {
    position: fixed;
    inset: 0;
    width: 100%;
    z-index: 8000;
    border-left: none;
    padding-bottom: env(safe-area-inset-bottom);
  }

  /* Mobile Contacts */
  .contacts-layout {
    flex-direction: column;
  }
  .contacts-groups-col {
    width: 100%;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 10px 12px 8px;
    overflow: visible;
  }
  .contacts-col-title {
    padding: 0 2px 6px;
  }
  #contacts-group-list {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
  }
  .contact-group-item {
    flex-shrink: 0;
    padding: 6px 11px;
    margin-bottom: 0;
    gap: 6px;
    border: 1px solid var(--border-light);
    background: var(--bg-subtle);
  }
  .contact-group-item.active {
    border-color: #c2d4ff;
  }
  .contacts-header {
    height: auto;
    padding: 10px 12px;
    flex-wrap: wrap;
    gap: 8px;
  }
  .contacts-header .search-box {
    max-width: none !important;
    width: 100%;
  }
  .contacts-grid {
    padding: 12px;
    grid-template-columns: 1fr;
    gap: 10px;
  }

  /* Mobile Admin Console */
  .admin-layout {
    flex-direction: column;
  }
  .admin-sidebar {
    width: 100%;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 10px 12px 8px;
    gap: 8px;
  }
  .admin-sidebar-title {
    padding: 0 2px 6px;
    margin-bottom: 2px;
    font-size: 14px;
  }
  .admin-nav-group-label {
    display: none;
  }
  #admin-nav-list {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
  }
  .admin-nav-btn {
    width: auto;
    flex-shrink: 0;
    white-space: nowrap;
    padding: 7px 12px;
    border: 1px solid var(--border-light);
    background: var(--bg-subtle);
  }
  .admin-nav-btn.active {
    border-color: #c2d4ff;
  }
  .admin-nav-btn.active::before {
    display: none;
  }
  .admin-content {
    padding: 14px 12px;
  }
  .admin-panel-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
    margin-bottom: 14px;
  }
  .admin-card, .ops-card {
    padding: 14px;
  }
  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }
  .kpi-card {
    padding: 12px 14px;
  }
  .kpi-value {
    font-size: 19px;
  }
  .ops-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  .ops-metrics-row {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }
  .ops-metric {
    padding: 8px 9px;
  }
  .ops-metric-val {
    font-size: 13px;
  }
  .data-table {
    min-width: 560px;
  }
  .setting-item {
    gap: 12px;
  }
  .setting-item.setting-item-stack {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .setting-item.setting-item-stack .setting-item-controls {
    align-items: flex-start;
    width: 100%;
  }
  .setting-item input[type="range"] {
    width: 100% !important;
    max-width: 100%;
    flex-shrink: 0;
  }
  .trace-dropzone {
    padding: 22px 14px;
  }

  /* Mobile Modals -> Flush native bottom sheet with iOS safe-area inset */
  .modal-backdrop {
    padding: 12px 0 0;
    align-items: flex-end;
  }
  .modal-card {
    max-width: 100%;
    max-height: 90dvh;
    border-radius: 16px 16px 0 0;
  }
  .modal-header {
    padding: 14px 16px;
  }
  .modal-body {
    padding: 14px 16px;
  }
  .modal-body:last-child {
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
  }
  .modal-footer {
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  }
  .pf-hero {
    padding: 14px;
    gap: 12px;
  }
  .pf-section {
    padding: 12px 14px;
  }

  /* Mobile Lightbox */
  .lightbox-topbar {
    height: auto;
    min-height: 50px;
    padding: 8px 12px;
    flex-wrap: wrap;
    gap: 8px;
  }
  .lightbox-actions {
    gap: 6px;
    flex-wrap: wrap;
  }
  .lightbox-btn {
    padding: 6px 9px;
    font-size: 12px;
  }
  .lightbox-img {
    max-width: 96vw;
    max-height: 78dvh;
  }

  /* Toast positioned safely */
  #toast-container {
    width: calc(100vw - 32px);
    align-items: center;
  }
  .toast {
    max-width: 100%;
    text-align: center;
  }
}
