/* ─── MOBILE RESPONSIVE  (≤ 768px) ────────────────────────────────────────── */
@media (max-width: 768px) {

  /* ── Shell ─────────────────────────────────────── */
  .app-shell {
    max-width: 100%;
    position: relative;
    overflow: hidden;
  }

  /* ── Sidebar: full-width, slide-in panel ────────── */
  .sidebar {
    position: absolute;
    inset: 0;
    width: 100%;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: none;
  }
  .sidebar.sidebar-active {
    transform: translateX(0);
    pointer-events: auto;
  }

  /* ── Chat panel: full-width, hidden until active ── */
  .chat-panel {
    position: absolute;
    inset: 0;
    width: 100%;
    transform: translateX(100%);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: none;
  }
  .chat-panel.panel-active {
    transform: translateX(0);
    pointer-events: auto;
  }

  #simRightPanel {
    position: absolute;
    inset: 0;
    width: 100%;
    z-index: 25;
    transform: translateX(100%);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: none;
  }
  #simRightPanel.sim-panel-active {
    transform: translateX(0);
    pointer-events: auto;
  }

  /* ── Off-canvas panels must not be focusable ───── */
  /* An inactive panel sits at translateX(100%), which extends .app-shell's
     scrollWidth to 780px. .app-shell is overflow:hidden, which blocks user
     scrolling but NOT the browser's own scroll-into-view — so focusing any
     control inside an off-canvas panel scrolled the shell across by 227px and
     it could never scroll back, leaving both panels clipped side by side.
     visibility:hidden takes them out of the focus order entirely. The delayed
     visibility transition keeps the panel painted while it slides away. */
  .chat-panel,
  #simRightPanel {
    visibility: hidden;
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1),
                visibility 0s linear 0.28s;
  }
  .chat-panel.panel-active,
  #simRightPanel.sim-panel-active {
    visibility: visible;
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1),
                visibility 0s linear 0s;
  }

  /* ── Back button: show on mobile ───────────────── */
  .back-to-sidebar {
    display: flex !important;
  }

  /* ── Topbar tweaks ─────────────────────────────── */
  .topbar {
    padding: 10px 8px 10px;
    padding-top: calc(10px + env(safe-area-inset-top));
    gap: 6px;
  }
  .topbar-coins { padding: 2px 6px; font-size: 11px; margin-right: 0; }
  .status-info .name { font-size: 15px; }
  .online { font-size: 11px; }

  /* ── Input bar ─────────────────────────────────── */
  .input-bar {
    padding: 8px 8px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
    gap: 6px;
  }
  .input-bar textarea, .input-bar input {
    font-size: 16px; /* prevents iOS zoom on focus */
  }

  /* ── Chat bubbles ──────────────────────────────── */
  .msg { max-width: 88%; font-size: 14px; }

  /* ── User messages: push to rightmost edge ─────── */
  .chat { padding-right: 8px; }
  .msg.user { margin-right: -4px; }

  /* ── Screens: full-screen on mobile ────────────── */
  .screen {
    position: fixed !important;
    inset: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    z-index: 30;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* ── Modals: bottom sheet style ────────────────── */
  .modal-overlay {
    align-items: flex-end !important;
    padding: 0 !important;
  }
  .modal-card {
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 20px 20px 0 0 !important;
    max-height: 92vh;
    overflow-y: auto;
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
  }

  /* ── Export modal ──────────────────────────────── */
  .modal-content {
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 20px 20px 0 0 !important;
    max-height: 92vh !important;
    flex-direction: column !important;
  }

  /* ── Sidebar bottom bar ────────────────────────── */
  .sidebar-bottom-bar {
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }

  /* ── Companion list items: bigger tap targets ──── */
  .companion-item { min-height: 68px; }

  /* ── Segment tabs: scrollable ───────────────────── */
  .segment-tabs {
    overflow-x: auto;
    flex-wrap: nowrap !important;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .segment-tabs::-webkit-scrollbar { display: none; }

  /* ── Select bar ────────────────────────────────── */
  #selectBar { padding: 10px 12px; }

  /* ── Profile face picker: wider buttons ─────────── */
  #profileFaceGrid button { width: 56px; height: 56px; }

  /* ── Settings rows ─────────────────────────────── */
  .settings-row { min-height: 52px; }

  /* ── GIF picker ────────────────────────────────── */
  .gif-picker { max-height: 50vh; }

  /* ── Topbar icon buttons: slightly bigger hit area  */
  .icon-btn { width: 40px; height: 40px; }

  /* ── Video/call screen ─────────────────────────── */
  #videoScreen .call-controls { padding-bottom: calc(20px + env(safe-area-inset-bottom)); }
}

/* ── Very small screens (iPhone SE etc.) ─────────── */
@media (max-width: 375px) {
  .topbar-actions { gap: 0; }
  .icon-btn { width: 36px; height: 36px; }
  .msg { max-width: 93%; font-size: 13.5px; }
}

/* iOS Safari zooms the whole page in whenever a focused input has a font-size
   under 16px, and it never zooms back out afterwards. Every field here sat at
   14-15px, so tapping one — the login form, the search box — left the app stuck
   at ~110% (16/14.5 = 1.10) with the page no longer fitting the screen, and the
   only way back was to pinch out manually.
   The fix is the font-size itself, not `maximum-scale=1`: that would disable
   pinch-zoom for everyone, and iOS has ignored it since iOS 10 anyway. IDs are
   listed explicitly because an ID selector outranks a bare `input` one. */
@media (pointer: coarse) {
  /* !important because app-shell.html sets font-size inline on 14 of these
     fields, and an inline style outranks any selector — the first version of
     this rule was silently losing to them and the page still zoomed. It also
     covers any input added inline later. */
  input, textarea, select,
  #input, .gif-search-input, .modal-input,
  .sim-compose-input, .sim-agent-input,
  .svc-inputbox, .svc-textarea {
    font-size: 16px !important;
  }
}
