/* ============================================================
   Seed Code Chat — Mobile refinements
   ------------------------------------------------------------
   Android / small-screen hardening on top of the existing layout:
   safe areas, touch targets, touch behaviour, keyboard posture.
   Every rule is guarded by a media query or feature query so
   desktop rendering is untouched.
   ============================================================ */

/* ---- Prevent double-tap zoom on interactive controls ---- */
@media (pointer: coarse) {
  button,
  .btn,
  .icon-btn,
  .model-btn,
  .suggestion,
  .conv-item,
  .side-nav-item,
  .picker-item,
  .sn-btn,
  .provider-card,
  .seg-btn {
    touch-action: manipulation;
  }
}

/* ---- Accessible focus rings (keyboard + assistive tech) ----
   Visible on every interactive control, never on mouse-click. */
:focus-visible {
  outline: 2px solid var(--brand-strong);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

.btn:focus-visible,
.icon-btn:focus-visible,
.model-btn:focus-visible,
.sn-btn:focus-visible,
.side-nav-item:focus-visible,
.picker-item:focus-visible,
.provider-card:focus-visible,
.conv-item:focus-visible,
.seg-btn:focus-visible,
.suggestion:focus-visible {
  outline: 2px solid var(--brand-strong);
  outline-offset: 2px;
}

/* ---- Buttons: consistent pressed/hover micro-feedback ---- */
.btn:active:not(:disabled) {
  transform: translateY(1px) scale(0.99);
}

.icon-btn:active {
  transform: scale(0.92);
}

.side-nav-item:active,
.sn-btn:active,
.picker-item:active {
  background: var(--surface-3);
}

/* Empty-state icon gets a quiet entrance instead of static render */
.empty-state .es-icon {
  animation: sc-scale-in var(--t-med) var(--ease-out) both;
}

/* ---- Toasts: enter from below on phones (thumb-friendly) ---- */
@keyframes sc-toast-in-up {
  from { opacity: 0; transform: translateY(14px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 600px) {
  .toast {
    animation: sc-toast-in-up 200ms var(--ease-out);
    left: 16px;
    right: 16px;
    min-width: 0;
  }

  .toast-region {
    left: 0;
    right: 0;
    align-items: stretch;
  }

  .toast.out {
    transform: translateY(10px);
  }
}

/* ---- History/provider cards: soft entrance without layout shift ---- */
.history-card,
.provider-card {
  animation: sc-fade-up var(--t-med) var(--ease-out) both;
}

.history-card:nth-child(1) { animation-delay: 0ms; }
.history-card:nth-child(2) { animation-delay: 30ms; }
.history-card:nth-child(3) { animation-delay: 60ms; }
.history-card:nth-child(4) { animation-delay: 90ms; }
.history-card:nth-child(5) { animation-delay: 120ms; }

.provider-card:nth-child(2) { animation-delay: 30ms; }
.provider-card:nth-child(3) { animation-delay: 60ms; }
.provider-card:nth-child(4) { animation-delay: 90ms; }
.provider-card:nth-child(5) { animation-delay: 120ms; }

/* ---- Show message actions on touch devices (no hover) ---- */
@media (hover: none) {
  .msg-actions {
    opacity: 1;
  }

  .suggestion:hover {
    transform: none;
  }
}

/* ---- Touch targets at least 40px tall on small screens ---- */
@media (max-width: 600px) {
  .icon-btn {
    width: 40px;
    height: 40px;
  }

  .model-btn {
    min-height: 40px;
  }

  .btn-send {
    width: 44px;
    height: 44px;
  }

  .composer textarea {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
  }

  .header-model .model-btn {
    padding: 6px 10px;
  }

  /* Prevent horizontal overflow from long words/code inside messages */
  .markdown pre {
    max-width: 100%;
  }

  .message.user {
    max-width: 92%;
  }
}

/* ---- Very small screens: keep the header from overflowing.
   The header model selector is duplicated in the composer, so hide it
   here to give the title room and keep the settings button accessible. */
@media (max-width: 380px) {
  .header-model {
    display: none;
  }

  .welcome h1 {
    font-size: var(--fs-24);
  }

  .welcome .w-sub {
    font-size: var(--fs-14);
  }
}

/* ---- Inputs: 16px on touch screens prevents iOS auto-zoom.
   Desktop keeps the shared 14px scale (guard keeps that true). */
@media (pointer: coarse) {
  .input,
  .select,
  .textarea,
  .sidebar-search input {
    font-size: 16px;
  }
}

/* ---- Glass chrome (subtle, with solid fallbacks) ----
   Header, composer card, mobile drawer and dialogs get a soft blur
   finish. Every declaration is behind @supports so engines without
   backdrop-filter keep fully opaque, readable surfaces. */
.main-header {
  background: var(--glass-header, var(--bg));
}

.composer {
  background: var(--glass-panel, var(--surface-2));
  box-shadow: var(--shadow-2), inset 0 1px 0 rgba(255, 255, 255, 0.03);
}

.dialog-box,
.menu,
.toast {
  background: var(--glass-dialog, var(--surface-2));
}@supports (backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px)) {
  .main-header {
    backdrop-filter: blur(16px) saturate(1.35);
    -webkit-backdrop-filter: blur(16px) saturate(1.35);
  }

  .composer {
    backdrop-filter: blur(14px) saturate(1.3);
    -webkit-backdrop-filter: blur(14px) saturate(1.3);
  }

  .dialog-box,
  .menu,
  .toast {
    backdrop-filter: blur(18px) saturate(1.4);
    -webkit-backdrop-filter: blur(18px) saturate(1.4);
  }

  .sidebar {
    backdrop-filter: blur(18px) saturate(1.3);
    -webkit-backdrop-filter: blur(18px) saturate(1.3);
    background: var(--glass-panel, var(--surface));
  }

  @media (max-width: 900px) {
    /* Glass drawer: only the mobile slide-over gets the blur finish.
       The desktop sidebar stays a solid surface for readability. */
    .sidebar {
      background: var(--glass-panel, var(--surface));
    }
  }

  /* Glass card lift for auth/profile/history surfaces (kept subtle) */
  .auth-card {
    background: var(--glass-panel, var(--surface-2));
    backdrop-filter: blur(18px) saturate(1.35);
    -webkit-backdrop-filter: blur(18px) saturate(1.35);
  }
}

/* ---- Guard menus/toasts from overflowing narrow screens ---- */
@media (max-width: 480px) {
  .menu {
    min-width: 0;
    max-width: calc(100vw - 16px);
  }

  .toast {
    min-width: 0;
    max-width: calc(100vw - 32px);
  }

  .picker-panel {
    width: calc(100vw - 24px);
  }

  /* Dialogs: keep long content reachable on small screens.
     On phones the dialog docks to the bottom (bottom-sheet posture).
     Desktop keeps the centered layout from components.css. */
  .dialog {
    padding: 12px;
    align-items: flex-end;
  }

  .dialog-box {
    max-height: min(86dvh, 86vh);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: var(--sab, 0px);
  }

  .dialog .dialog-foot {
    padding-bottom: calc(14px + var(--sab, 0px) / 2);
  }

  /* Model picker gets bottom-sheet proportions on phones */
  .picker-list {
    max-height: min(56dvh, 56vh);
  }

  /* Long values (emails, model ids, URLs) must wrap, not push width */
  .hc-preview,
  .user-sub,
  .pc-desc,
  .sr-desc {
    overflow-wrap: anywhere;
  }

  .profile-details dd {
    white-space: normal;
    overflow-wrap: anywhere;
  }
}

/* ---- Settings page nav on phones: horizontally scrollable chips.
   Replaces the wrap flow so 320px screens get one tidy row. */
@media (max-width: 720px) {
  .settings-page-nav {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 12px;
  }

  .settings-page-nav::-webkit-scrollbar {
    display: none;
  }

  .sn-btn {
    flex: none;
  }

  /* Range rows stack under their label instead of squeezing */
  .setting-row {
    flex-wrap: wrap;
  }

  .setting-row .range-row {
    width: 100%;
  }
}

/* ---- Extra-narrow phones: stack header controls cleanly ---- */
@media (max-width: 340px) {
  .header-right {
    gap: 2px;
  }

  .model-btn .model-name {
    max-width: 92px;
  }
}

/* ---- Prevent content selection on UI chrome ---- */
@media (pointer: coarse) {
  .icon-btn,
  .btn,
  .model-btn,
  .suggestion,
  .conv-item,
  .side-nav-item,
  .picker-item,
  .sn-btn,
  .provider-card,
  .seg-btn,
  .msg-actions,
  .composer-bar {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
  }
}

/* ---- Overscroll containment (no elastic page bounce) ---- */
.main,
.conv-list,
.page-view {
  overscroll-behavior-y: contain;
}

html {
  overscroll-behavior: none;
}

/* ---- Sidebar drawer: full-height glass panel with its own safe areas.
   Applied at all widths guarded by the ≤900px drawer breakpoint in
   layout.css; position:fixed here keeps it out of the grid flow. */
@media (max-width: 900px) {
  .sidebar {
    padding-top: calc(env(safe-area-inset-top, 0px));
    padding-bottom: calc(env(safe-area-inset-bottom, 0px));
    width: min(85vw, 320px);
  }
}

/* ---- Safe-area fallbacks ----
   Capacitor 8 (edge-to-edge) exposes real insets through
   env(safe-area-inset-*) via viewport-fit=cover; the app shell
   (header/app/composer) applies them in layout.css. These rules are
   defense-in-depth for hosts where the grid padding is not enough. */
@supports (padding: env(safe-area-inset-bottom)) {
  .toast-region {
    padding-bottom: env(safe-area-inset-bottom, 0px);
    padding-right: env(safe-area-inset-right, 0px);
  }
}

/* ---- Dynamic viewport height helpers ----
   On mobile browsers the address bar can overlap a fixed 100vh.
   Prefer dvh when available for the app shell. */
@supports (height: 100dvh) {
  html {
    height: 100%;
  }

  body,
  #app {
    height: 100dvh;
  }
}

/* ---- Touch targets on phones (≥40px) ---- */
@media (pointer: coarse) and (max-width: 720px) {
  .eye-btn {
    width: 38px;
    height: 38px;
  }

  .check-line input {
    width: 17px;
    height: 17px;
  }

  .menu-item,
  .picker-item {
    padding-top: 11px;
    padding-bottom: 11px;
  }

  .history-card,
  .conv-item {
    min-height: 44px;
  }

  .side-nav-item {
    min-height: 42px;
  }

  .btn-lg {
    min-height: 48px;
  }
}

/* ---- Reduced motion never blocked ---- */
@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
}

/* ============================================================
   Provider settings: reflow for phone widths (320–430px)
   Cards become flex-wrap rows; name/badges/status wrap instead of
   clipping; the detail dialog fields never exceed their container.
   ============================================================ */

@media (max-width: 600px) {
  .provider-card {
    flex-wrap: wrap;
    gap: 10px 12px;
    padding: 12px;
  }

  .provider-card .pc-info {
    flex: 1 1 160px;
    min-width: 0;
  }

  .provider-card .pc-name {
    flex-wrap: wrap;
    row-gap: 4px;
  }

  .provider-card .pc-name span {
    max-width: 100%;
    overflow-wrap: anywhere;
  }

  /* Description wraps to a second line instead of ellipsis-carrying
     long adapter text on phones */
  .provider-card .pc-desc {
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  .provider-card .pc-status {
    flex-direction: row;
    align-items: center;
    margin-left: auto;
  }

  /* Detail form rows stack; fixed 220px range width removed */
  .range-row {
    min-width: 0 !important;
    width: 100%;
  }

  .setting-row > .switch,
  .setting-row > .select,
  .setting-row > .segmented {
    max-width: 100%;
  }

  .segmented {
    width: 100%;
    justify-content: stretch;
  }

  .segmented .seg-btn {
    flex: 1;
    text-align: center;
  }

  /* Dialog bottom-sheet posture gets breathing room + safety */
  .dialog {
    padding: 10px;
    align-items: flex-end;
  }

  .dialog-box {
    max-width: 100%;
    max-height: min(88dvh, 88vh);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: var(--sab, 0px);
  }

  /* Provider detail inner rows: model chips wrap safely */
  .model-chip {
    flex-wrap: wrap;
    row-gap: 2px;
  }

  .model-chip .mc-name,
  .model-chip > span:last-of-type {
    overflow-wrap: anywhere;
  }

  .pf-head {
    flex-wrap: wrap;
  }

  .pf-head h3 {
    overflow-wrap: anywhere;
  }

  /* Export/action rows: full-width tappable buttons */
  .export-actions .btn {
    flex: 1 1 auto;
  }
}

/* Model list inside provider dialog: scroll area capped, keeps the
   Save button reachable without page-level scrolling */
@media (max-width: 600px) {
  [data-pf-modellist] {
    max-height: 180px;
  }
}

/* ============================================================
   Skeleton → content: containers reserve space so the swap does
   not jump. Skeletons are removed by JS as soon as data arrives.
   ============================================================ */

.sk-swap {
  min-height: 1px; /* containers with only skeletons still size */
}

/* Settings nav chips: skeleton state while auth/avatars resolve */
.sk-chip {
  height: 34px;
  width: 110px;
  border-radius: var(--r-pill);
  border: 1px solid var(--border);
}

/* ============================================================
   Accessibility helpers
   ============================================================ */

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* Skip link: first tab stop on every page */
.skip-link {
  position: fixed;
  top: -48px;
  left: 12px;
  z-index: 300;
  padding: 10px 16px;
  border-radius: var(--r-md);
  background: var(--brand);
  color: #062A15;
  font-weight: 700;
  font-size: var(--fs-14);
  transition: top var(--t-fast) var(--ease-out);
}

.skip-link:focus {
  top: 12px;
}

/* Long model/provider names anywhere: wrap, never blow out width */
.pc-name,
.pi-name,
.mc-name,
.model-name,
.user-name {
  overflow-wrap: anywhere;
}
