/* iPad / touch layer (2026-09-08).
 *
 * Loaded LAST so it can correct the desktop stylesheets without either of
 * them needing to know it exists. Everything here is inside a media query -
 * on a mouse-driven desktop this file changes nothing at all.
 *
 * The existing breakpoints (700/820/860/900px) already reflow the layout
 * for iPad portrait at 768px. What they do not handle is the part that has
 * nothing to do with WIDTH: iOS Safari's specific behaviours, and the fact
 * that a finger is a much less precise pointer than a cursor. iPad
 * landscape is 1024px wide, so it misses every one of those breakpoints
 * and gets the full desktop layout - correct for the layout, wrong for the
 * hit targets, which is why these rules key off `pointer: coarse` rather
 * than a width.
 */

/* ---------------------------------------------------------------------
 * 1. Stop iOS zooming the page when a field is focused.
 *
 * Mobile Safari auto-zooms on focus for any input whose font-size is
 * under 16px, then leaves the viewport scaled - the layout appears to
 * "jump and stay broken" after tapping the message box. There is no
 * property to disable this; the only fix is to meet the threshold.
 * (Doing it with maximum-scale=1 in the viewport meta instead would also
 * work, but that disables pinch-zoom entirely, which breaks accessibility
 * for everyone.)
 * ------------------------------------------------------------------ */
@media (pointer: coarse) {
  input,
  textarea,
  select,
  button {
    font-size: max(16px, 1em);
  }
}

@media (pointer: coarse) {
  /* -------------------------------------------------------------------
   * 2. Touch targets.
   *
   * Apple's own guidance is 44x44pt minimum. min-height (not height) so
   * nothing that is already larger gets shrunk, and so a control that
   * grows with its content still can.
   * ---------------------------------------------------------------- */
  button,
  .btn,
  [role="button"],
  .persona-rail button,
  .chat-menu button,
  nav a {
    min-height: 44px;
    min-width: 44px;
  }

  /* Inline text links inside prose must NOT get 44px of height - that
   * would break paragraph line spacing wherever one appears mid-sentence.
   * Padding widens the tap area without touching layout height. */
  p a,
  li a,
  .message a {
    min-height: 0;
    min-width: 0;
    padding: 0.15em 0.1em;
  }

  /* -------------------------------------------------------------------
   * 3. Kill the grey flash on tap.
   *
   * iOS paints a translucent grey box over any tapped element. On custom
   * controls that already have their own :active state it reads as a
   * rendering glitch rather than feedback.
   * ---------------------------------------------------------------- */
  * {
    -webkit-tap-highlight-color: transparent;
  }

  /* Hover styles are actively harmful on touch: iOS emulates :hover on
   * first tap, so a button can require two taps and appear stuck in a
   * highlighted state afterward. Anything relying on hover to REVEAL
   * something must show it unconditionally here. */
  .hover-reveal,
  .persona-card .actions,
  .message .actions {
    opacity: 1;
    visibility: visible;
  }
}

/* ---------------------------------------------------------------------
 * 4. The 100vh problem.
 *
 * On iOS, 100vh is the height of the viewport with the browser chrome
 * HIDDEN, which is taller than what is actually visible while the URL bar
 * is showing. Any full-height flex column sized with 100vh therefore has
 * its bottom - in this app, the message composer - pushed below the fold
 * until you scroll. dvh ("dynamic viewport height") tracks the real
 * visible area and resizes as the chrome collapses.
 *
 * Guarded by @supports rather than applied blindly: dvh is well supported
 * on iOS 15.4+ / modern Chrome, and on anything older this block is
 * skipped and the existing 100vh rules stand unchanged.
 * ------------------------------------------------------------------ */
@supports (height: 100dvh) {
  @media (pointer: coarse) {
    html,
    body,
    .shell,
    .app-shell,
    .chat-layout,
    .chat-main {
      min-height: 100dvh;
    }

    .chat-layout,
    .chat-main {
      max-height: 100dvh;
    }
  }
}

@media (pointer: coarse) {
  /* -------------------------------------------------------------------
   * 5. Notch / home-indicator safe areas.
   *
   * An iPad without a home button reserves a strip at the bottom for the
   * indicator. Without this the composer sits underneath it and its
   * bottom edge is not reliably tappable. env() returns 0 on hardware
   * with no inset, so this costs nothing elsewhere.
   * ---------------------------------------------------------------- */
  body {
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }

  .composer,
  .chat-composer,
  .message-input-row {
    padding-bottom: calc(0.5rem + env(safe-area-inset-bottom));
  }

  /* -------------------------------------------------------------------
   * 6. Momentum scrolling, and containing it.
   *
   * overscroll-behavior stops a scroll that reaches the end of the
   * message list from continuing into the page behind it and rubber-
   * banding the whole app - the effect that makes a chat feel like it is
   * "sliding off" on iOS.
   * ---------------------------------------------------------------- */
  .messages,
  .message-list,
  .panel-body,
  .scrollable {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  /* Long generated prompts and model filenames have no spaces to break
   * on and will otherwise force the whole column wider than the screen. */
  .message,
  .prompt,
  code {
    overflow-wrap: anywhere;
  }
}

/* ---------------------------------------------------------------------
 * 7. iPad landscape (1024px) misses every existing max-width breakpoint,
 * so it renders the desktop layout. That is mostly right - it is a big
 * screen - but the fixed side rails were sized against a mouse and eat
 * too much of it. Narrow them rather than collapsing them, since there is
 * genuinely room for both here.
 * ------------------------------------------------------------------ */
@media (pointer: coarse) and (min-width: 901px) and (max-width: 1180px) {
  .persona-rail,
  .sidebar {
    max-width: 220px;
  }
}
