/**
 * EmailCore Admin — mobile layout fix (≤768px)
 * Keeps desktop styles untouched and overrides only the mobile shell.
 */

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 99;
  opacity: 0;
  transition: opacity 0.25s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

@media (max-width: 768px) {
  html,
  body {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
  }

  html {
    font-size: 16px;
  }

  .admin-app {
    display: block;
    width: 100%;
    max-width: 100%;
    min-height: 100vh;
    padding-inline: 0;
    overflow-x: hidden;
  }

  .main-wrap,
  [dir="ltr"] .main-wrap,
  [dir="rtl"] .main-wrap {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    margin-left: 0 !important;
    margin-right: 0 !important;
    margin-inline-start: 0 !important;
    margin-inline-end: 0 !important;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    inset-inline-start: 0;
    inset-inline-end: auto;
    width: min(86vw, 320px);
    max-width: 86vw;
    z-index: 101;
    box-shadow: var(--shadow);
    transform: translateX(-105%) !important;
    transition: transform 0.32s cubic-bezier(0.4, 0, 0.2, 1);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  [dir="rtl"] .sidebar {
    inset-inline-start: auto;
    inset-inline-end: 0;
    transform: translateX(105%) !important;
  }

  .sidebar.sidebar--open,
  body.sidebar-open .sidebar,
  .admin-app.sidebar-open .sidebar,
  [dir="rtl"] .sidebar.sidebar--open,
  [dir="rtl"] body.sidebar-open .sidebar,
  [dir="rtl"] .admin-app.sidebar-open .sidebar {
    transform: translateX(0) !important;
  }

  .sidebar-backdrop.sidebar-backdrop--visible {
    display: block;
    opacity: 1;
  }

  body.sidebar-open {
    overflow: hidden;
    touch-action: none;
  }

  .sidebar__header {
    padding-top: max(1rem, env(safe-area-inset-top));
    padding-inline: 1rem;
  }

  .sidebar__link {
    min-height: 48px;
    padding: 0.85rem 1rem;
    font-size: 1rem;
  }

  .sidebar__icon {
    font-size: 1.25rem;
    width: 1.85rem;
  }

  .topbar,
  .content,
  .panel,
  .stats-grid,
  .stat-card,
  .table-wrap,
  .settings-table-wrap {
    max-width: 100%;
  }

  .topbar {
    width: 100%;
    display: grid;
    grid-template-columns: auto 1fr auto auto;
    grid-template-rows: auto auto;
    align-items: center;
    gap: 0.5rem 0.55rem;
    padding: max(0.55rem, env(safe-area-inset-top)) 0.85rem 0.65rem;
    position: sticky;
    top: 0;
    z-index: 50;
  }

  .topbar__toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    grid-column: 1;
    grid-row: 1;
    min-width: 46px;
    min-height: 46px;
    padding: 0.5rem;
    cursor: pointer;
    touch-action: manipulation;
    pointer-events: auto;
  }

  .topbar__title {
    grid-column: 2;
    grid-row: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 1.05rem;
  }

  .topbar__logout {
    grid-column: 3;
    grid-row: 1;
    min-height: 46px;
    padding: 0.5rem 0.8rem;
    margin-inline-start: 0;
    font-size: 0.875rem;
  }

  .topbar__lang {
    grid-column: 4;
    grid-row: 1;
    min-height: 46px;
    padding: 0.5rem 0.65rem;
    font-size: 0.875rem;
  }

  .topbar__lang .lang-opt {
    padding: 0.2rem 0.35rem;
    min-width: 1.75rem;
    text-align: center;
  }

  .topbar__actions {
    grid-column: 1 / -1;
    grid-row: 2;
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 0.45rem;
    margin-inline-end: 0;
  }

  .content {
    width: 100%;
    padding: 0.85rem;
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
  }

  .panel {
    padding: 1rem;
  }

  .stats-grid,
  .form-grid {
    grid-template-columns: 1fr;
    gap: 0.65rem;
  }

  .stat-card {
    width: 100%;
    min-width: 0;
    padding: 1rem 1.1rem;
    min-height: 76px;
  }

  .quick-actions,
  .form-actions,
  .generate-form-actions,
  .filters-bar,
  .backup-toolbar,
  .panel__header-row {
    flex-direction: column;
    align-items: stretch;
  }

  .quick-actions .btn,
  .form-actions .btn,
  .generate-form-actions .btn,
  .backup-toolbar .btn {
    width: 100%;
  }

  .sessions-tabs,
  .outlook-tabs,
  .filter-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
  }

  .sessions-tab,
  .outlook-tab,
  .filter-tabs .btn {
    flex-shrink: 0;
    white-space: nowrap;
  }

  /* Admin member messages — mobile conversation layout */
  .msg-layout {
    display: block !important;
    width: 100%;
    max-width: 100%;
    min-height: calc(100dvh - 92px);
  }

  .msg-sidebar,
  .msg-main {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }

  .msg-sidebar {
    padding: 1rem;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--bg-card);
  }

  .msg-layout--chat-open .msg-sidebar {
    display: none !important;
  }

  .msg-main {
    display: none !important;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--bg-card);
    min-height: calc(100dvh - 112px);
    overflow: hidden;
  }

  .msg-layout--chat-open .msg-main {
    display: flex !important;
    flex-direction: column;
  }

  .msg-sidebar__head {
    padding-bottom: 0.75rem;
    margin-bottom: 0.75rem;
    border-bottom: 1px solid var(--border);
  }

  .msg-sidebar__head .panel__title {
    font-size: 1.1rem;
    margin-bottom: 0.25rem;
  }

  .msg-sidebar__head .panel__desc {
    font-size: 0.85rem;
    line-height: 1.45;
    color: var(--text-muted);
  }

  .msg-thread-list {
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
    max-height: calc(100dvh - 250px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 0.15rem 0.1rem 0.25rem;
  }

  .msg-thread-item {
    width: 100%;
    min-height: 74px;
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.15rem 0.5rem;
    align-items: center;
    padding: 0.8rem 0.9rem;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: rgba(15, 20, 25, 0.55);
    color: var(--text);
    text-align: start;
  }

  .msg-thread-item--active {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px var(--accent-glow);
  }

  .msg-thread-item__name {
    grid-column: 1;
    font-weight: 800;
    font-size: 0.96rem;
    letter-spacing: 0.01em;
  }

  .msg-unread-badge {
    grid-column: 2;
    justify-self: end;
    min-width: 1.35rem;
    height: 1.35rem;
    border-radius: 999px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--accent);
    color: #042f2e;
    font-size: 0.72rem;
    font-weight: 800;
  }

  .msg-thread-item__preview,
  .msg-thread-item__meta {
    grid-column: 1 / -1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--text-muted);
  }

  .msg-thread-item__preview {
    font-size: 0.88rem;
  }

  .msg-thread-item__meta {
    font-size: 0.78rem;
  }

  .msg-chat {
    display: flex;
    flex-direction: column;
    min-height: calc(100dvh - 112px);
    width: 100%;
  }

  .msg-chat__head {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 0.5rem;
    align-items: center;
    padding: 0.75rem;
    border-bottom: 1px solid var(--border);
    background: rgba(18, 26, 38, 0.96);
    position: sticky;
    top: 0;
    z-index: 2;
  }

  .msg-mobile-back {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    width: 42px;
    min-width: 42px;
    height: 42px;
    padding: 0;
    border-radius: 12px;
    font-size: 1.1rem;
  }

  .msg-chat__title {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 1.05rem;
  }

  .msg-chat__mod {
    grid-column: 1 / -1;
    display: flex;
    gap: 0.4rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-top: 0.15rem;
  }

  .msg-chat__mod .btn {
    flex: 0 0 auto;
    white-space: nowrap;
    min-height: 36px;
  }

  .msg-chat__messages {
    flex: 1;
    min-height: 0;
    max-height: calc(100dvh - 250px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 0.85rem;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
  }

  .msg-bubble {
    max-width: 88%;
    padding: 0.7rem 0.8rem;
    border-radius: 14px;
    word-break: break-word;
  }

  .msg-bubble--in {
    align-self: flex-start;
    background: rgba(45, 58, 79, 0.75);
    border-bottom-left-radius: 5px;
  }

  .msg-bubble--out {
    align-self: flex-end;
    background: rgba(20, 184, 166, 0.17);
    border: 1px solid rgba(20, 184, 166, 0.35);
    border-bottom-right-radius: 5px;
  }

  .msg-bubble__text {
    font-size: 0.92rem;
    line-height: 1.45;
  }

  .msg-bubble__time {
    display: block;
    margin-top: 0.35rem;
    color: var(--text-muted);
    font-size: 0.7rem;
  }

  .msg-bubble__image,
  .msg-bubble__audio {
    max-width: 100%;
  }

  .msg-compose {
    position: sticky;
    bottom: 0;
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.55rem;
    align-items: end;
    padding: 0.75rem;
    border-top: 1px solid var(--border);
    background: rgba(18, 26, 38, 0.98);
  }

  .msg-compose__input {
    min-height: 46px;
    max-height: 120px;
    resize: vertical;
  }

  .msg-compose__tools {
    display: flex;
    gap: 0.35rem;
    align-items: center;
  }

  .msg-compose__tools .btn,
  .msg-compose__attach {
    min-width: 40px;
    min-height: 40px;
    padding: 0.45rem 0.55rem;
  }
}
