/* Mobile and narrow viewport behavior. Single source of truth for max-width: 860px. */

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

  body {
    height: auto;
    background: linear-gradient(180deg, #dbeafe 0%, #eef2f7 34%, #eef2f7 100%);
    overflow-y: auto;
  }

  .auth-screen {
    width: 100%;
    min-height: 100svh;
    padding: 16px;
    place-items: center;
    overflow-y: auto;
  }

  .auth-card {
    width: min(460px, 100%);
    max-width: 100%;
    grid-template-columns: 1fr;
    gap: 0;
    margin: auto;
    border-radius: 24px;
    overflow: hidden;
  }

  .auth-hero,
  .auth-panel {
    padding: 22px;
  }

  .auth-hero {
    min-height: 0;
    background: linear-gradient(135deg, #dbeafe, #f8fafc 72%);
  }

  .auth-logo-wrap {
    width: min(220px, 78%);
    max-width: 100%;
    margin-bottom: 12px;
  }

  .auth-logo {
    max-height: 54px;
  }

  .auth-hero p {
    font-size: 14px;
    line-height: 1.5;
  }

  .auth-tabs {
    margin-bottom: 16px;
  }

  .auth-tab,
  .auth-submit,
  select,
  input {
    min-height: 46px;
  }

  .app {
    width: 100%;
    max-width: 100%;
    grid-template-columns: minmax(0, 1fr);
    gap: 12px;
    height: 100svh;
    min-height: 100svh;
    padding: 10px;
    overflow: hidden;
  }

  .app.auth-hidden {
    display: none;
  }

  .sidebar,
  .chat {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    height: calc(100svh - 20px);
    min-height: 0;
    border-radius: 18px;
    overflow: hidden;
  }

  .app.mobile-chat-active .sidebar {
    display: none;
  }

  .app:not(.mobile-chat-active) .chat {
    display: none;
  }

  .sidebar {
    padding: 12px;
    gap: 10px;
  }

  .brand,
  .brand-actions,
  .account-card,
  .profile-card,
  .section,
  .chat-header,
  .composer,
  .composer-row,
  .modal,
  .modal-panel,
  .row {
    max-width: 100%;
    min-width: 0;
  }

  .brand {
    align-items: center;
    flex-direction: row;
  }

  .brand-logo {
    width: min(112px, 100%);
    max-height: 28px;
  }

  .brand-logo-link,
  .account-email,
  .room-name,
  .notification-title,
  .settings-room-title,
  .chat-header > div,
  .chat-title,
  .chat-subtitle {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  .brand-actions {
    flex-shrink: 0;
    gap: 6px;
  }

  .settings-button,
  .secondary-button,
  .danger-button,
  .load-more-button,
  .send-button {
    min-height: 42px;
    border-radius: 12px;
  }

  .status {
    max-width: 100%;
    min-height: 32px;
    padding: 6px 9px;
    overflow: hidden;
    font-size: 11px;
  }

  #connectionText {
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .profile-card,
  .account-card,
  .section,
  .agent-card,
  .api-key-box {
    border-radius: 15px;
    padding: 11px;
  }

  .account-card {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 8px;
  }

  .account-card .field-label {
    grid-column: 1 / -1;
    margin-bottom: 0;
  }

  .account-email {
    font-size: 13px;
  }

  .profile-card label,
  .section-title,
  .field-label {
    margin-bottom: 8px;
    font-size: 11px;
  }

  .profile-card .notice {
    margin-top: 8px;
    font-size: 12px;
  }

  .scroll-section {
    display: flex;
    flex-direction: column;
    min-height: 0;
    max-height: none;
    overflow: hidden;
  }

  .sidebar > .section:nth-of-type(1) {
    flex: 1 1 auto;
  }

  .sidebar > .section:nth-of-type(2) {
    flex: 0 1 min(24svh, 180px);
  }

  .scroll-section .rooms,
  .scroll-section .notifications {
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .rooms,
  .notifications,
  .settings-room-list,
  .member-list,
  .agent-list {
    gap: 8px;
  }

  .room,
  .notification,
  .settings-room {
    border-radius: 14px;
    padding: 10px;
  }

  .room .notice {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .chat-header {
    align-items: flex-start;
    padding: 14px 16px;
  }

  .chat-header > div {
    flex: 1 1 auto;
  }

  .chat-back-button {
    display: inline-flex;
  }

  .chat-title {
    font-size: 18px;
  }

  .chat-subtitle {
    display: -webkit-box;
    overflow: hidden;
    font-size: 12px;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }

  .messages {
    flex: 1 1 auto;
    min-height: 0;
    padding: 14px 12px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .empty-state {
    min-height: 220px;
    padding: 28px 18px;
  }

  .load-more-wrap {
    margin-bottom: 12px;
  }

  .message-row {
    margin-bottom: 11px;
  }

  .message-bubble {
    min-width: 0;
    max-width: 86%;
    border-radius: 16px 16px 16px 5px;
    padding: 9px 11px;
    overflow-wrap: anywhere;
  }

  .message-row.mine .message-bubble {
    border-radius: 16px 16px 5px 16px;
  }

  .meta {
    gap: 8px;
    font-size: 11px;
  }

  .text {
    font-size: 14px;
  }

  .typing-indicator {
    min-height: 24px;
    padding: 0 14px 8px;
    font-size: 12px;
  }

  .composer {
    flex: 0 0 auto;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  }

  .composer-row {
    gap: 8px;
  }

  .composer-row input,
  .composer-row textarea {
    min-width: 0;
  }


  .scroll-to-bottom-button {
    bottom: calc(var(--composer-height, 100px) + 24px);
    width: 42px;
    height: 42px;
    font-size: 23px;
  }

  .send-button {
    flex: 0 0 auto;
    padding: 0 14px;
  }

  .modal-backdrop {
    padding: 0;
  }

  .modal-backdrop.open {
    align-items: stretch;
    justify-content: stretch;
  }

  .modal {
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 100%;
    height: 100svh;
    max-height: none;
    border: 0;
    border-radius: 0;
    overflow-y: auto;
  }

  .modal-panel {
    max-height: none;
    padding: 16px;
    overflow: visible;
  }

  .modal-side {
    border-right: 0;
    border-bottom: 1px solid var(--line);
    margin-bottom: 120px;
  }

  .modal-side > .row:first-child {
    position: sticky;
    top: 0;
    z-index: 5;
    margin: -16px -16px 12px;
    padding: calc(14px + env(safe-area-inset-top)) 16px 12px;
    background: rgba(248, 250, 252, 0.96);
    border-bottom: 1px solid var(--line);
    backdrop-filter: blur(12px);
  }

  .settings-room-list {
    max-height: 26svh;
    overflow-y: auto;
    padding-right: 2px;
    -webkit-overflow-scrolling: touch;
  }

  .modal h2 {
    font-size: 20px;
  }

  .modal h3 {
    font-size: 18px;
  }

  .form-actions {
    align-items: stretch;
    flex-direction: column-reverse;
  }

  .form-actions > .row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
  }

  .form-actions > .row .settings-button,
  .form-actions > .row .secondary-button,
  .form-actions > .row .danger-button,
  .form-actions > .settings-button,
  .form-actions > .secondary-button,
  .form-actions > .danger-button,
  #createAgentButton,
  #deleteRoomButton {
    width: 100%;
  }

  .member-option {
    min-height: 46px;
    padding: 11px 12px;
  }

  .agent-management {
    margin-top: 22px;
    padding-top: 18px;
  }

  .agent-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}