/* =====================================================================
   Hooshi V6 — Responsive & UI hardening
   Mobile-first fixes for chat, dashboards, admin tables and public pages.
   ===================================================================== */

:root {
  --app-viewport-height: 100dvh;
  --shell-max: 1480px;
  --content-max: 1240px;
  --chat-max: 900px;
  --mobile-gutter: 14px;
  --topbar-h: 68px;
}

html { min-width: 320px; }
html, body { max-width: 100%; }
body { overflow-x: clip; }
img, video, canvas, svg, iframe { max-width: 100%; }
button, input, textarea, select { min-width: 0; }

/* ---------- App shell ---------- */
.app-shell {
  width: 100%;
  max-width: var(--shell-max);
  margin-inline: auto;
}
.app-main,
.app-content,
.app-content > *,
.panel,
.glass,
.dash-grid,
.dash-2col,
.form-grid-2 { min-width: 0; }

.app-topbar {
  min-height: var(--topbar-h);
  background: color-mix(in srgb, var(--bg-solid) 74%, transparent);
  border-bottom: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
  -webkit-backdrop-filter: blur(22px) saturate(145%);
  backdrop-filter: blur(22px) saturate(145%);
}
.app-topbar h1 {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.app-content > :not(.chat-shell) {
  width: min(100%, var(--content-max));
  margin-inline: auto;
}

.app-sidebar {
  background:
    linear-gradient(180deg, color-mix(in srgb, var(--bg-solid) 91%, transparent), color-mix(in srgb, var(--bg-solid) 78%, transparent));
  -webkit-backdrop-filter: blur(24px) saturate(135%);
  backdrop-filter: blur(24px) saturate(135%);
  overscroll-behavior: contain;
  scrollbar-width: thin;
}
.side-nav a { min-height: 44px; }
.side-foot { padding-bottom: max(10px, env(safe-area-inset-bottom)); }

/* ---------- Universal cards and controls ---------- */
.panel,
.stat-card,
.tool-card,
.feature,
.price-card,
.guide-sec,
.admin-hero {
  border-color: color-mix(in srgb, var(--glass-brd) 74%, transparent);
}
.panel,
.stat-card,
.admin-elevated {
  box-shadow: 0 20px 55px -43px rgba(31, 21, 70, .62);
}
.input, .select, .textarea, textarea, input, select { max-width: 100%; }
.actions, .admin-filters, .row-flex, .between { min-width: 0; }
.copy-field, .mono { overflow-wrap: anywhere; word-break: break-word; }

/* Scrollable tables never expand the viewport. */
.adm-table-wrap {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}
.adm-table { min-width: 720px; }
.adm-table th, .adm-table td { vertical-align: middle; }

/* ---------- Chat: fixed composer, independent message scroll ---------- */
.page-chat {
  height: var(--app-viewport-height, 100dvh);
  min-height: 0;
  overflow: hidden;
  overscroll-behavior: none;
}
.page-chat .app-shell,
.page-chat .app-main {
  height: var(--app-viewport-height, 100dvh);
  max-height: var(--app-viewport-height, 100dvh);
  min-height: 0;
  overflow: hidden;
}
.page-chat .app-content {
  min-height: 0;
  overflow: hidden;
  contain: size layout;
}
.page-chat .chat-shell {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  overflow: hidden;
  isolation: isolate;
}
.chat-stream {
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior-y: contain;
  scrollbar-gutter: stable;
  scroll-padding-block: 24px;
  padding: clamp(18px, 3vw, 36px) clamp(14px, 3vw, 34px) 22px;
}
.chat-inner,
.composer-inner {
  width: min(var(--chat-max), 100%);
  min-width: 0;
}
.chat-inner { padding-inline: 2px; }
.chat-composer {
  position: relative;
  z-index: 30;
  flex: none;
  min-width: 0;
  padding: 10px clamp(12px, 3vw, 34px) max(12px, env(safe-area-inset-bottom));
  background:
    linear-gradient(180deg, transparent 0, color-mix(in srgb, var(--bg-solid) 86%, transparent) 22%, var(--bg-solid) 100%);
  border-top: 1px solid color-mix(in srgb, var(--line) 66%, transparent);
  -webkit-backdrop-filter: blur(22px) saturate(140%);
  backdrop-filter: blur(22px) saturate(140%);
}
.chat-composer::before {
  content: "";
  position: absolute;
  inset: -28px 0 auto;
  height: 28px;
  pointer-events: none;
  background: linear-gradient(180deg, transparent, color-mix(in srgb, var(--bg-solid) 72%, transparent));
}
.chat-context-bar {
  min-width: 0;
  margin: 0 0 8px;
  border-radius: 16px;
  padding: 9px 11px;
}
.site-switch { min-width: 0; }
.site-switch > span:last-child { min-width: 0; }
.site-switch b, .site-switch small { overflow-wrap: anywhere; }
.context-picker { width: min(220px, 100%); }

.composer-box {
  width: 100%;
  min-width: 0;
  min-height: 58px;
  display: grid !important;
  grid-template-columns: minmax(0, 1fr) 46px;
  align-items: end !important;
  gap: 10px;
  padding: 8px 9px 8px 16px !important;
  border: 1px solid color-mix(in srgb, var(--violet) 16%, var(--glass-brd));
  background: color-mix(in srgb, var(--bg-solid) 78%, transparent) !important;
  box-shadow:
    0 20px 56px -34px rgba(58, 32, 116, .62),
    inset 0 1px 0 rgba(255,255,255,.46) !important;
  -webkit-backdrop-filter: blur(20px) saturate(145%);
  backdrop-filter: blur(20px) saturate(145%);
}
.composer-box:focus-within {
  border-color: color-mix(in srgb, var(--violet) 55%, transparent);
  box-shadow:
    0 22px 60px -34px rgba(83, 49, 165, .72),
    0 0 0 4px color-mix(in srgb, var(--violet) 10%, transparent) !important;
}
.composer-box textarea {
  width: 100%;
  min-width: 0;
  max-height: min(180px, 28dvh);
  overflow-y: auto;
  padding: 7px 1px 6px;
  line-height: 1.85;
  scrollbar-width: thin;
}
.composer-send {
  width: 46px;
  height: 46px;
  align-self: end;
  touch-action: manipulation;
}
.composer-foot {
  min-width: 0;
  gap: 10px;
}
.composer-foot > span:first-child { min-width: 0; }

.msg,
.chat-typing { min-width: 0; max-width: 100%; }
.msg .bubble,
.chat-typing .bubble {
  min-width: 0;
  max-width: min(100%, 780px);
  overflow: hidden;
}
.msg.bot .bubble { width: fit-content; }
.msg.user .bubble { max-width: min(82%, 680px); }
.msg-body { min-width: 0; overflow-wrap: anywhere; }
.msg-body > :first-child { margin-top: 0; }
.msg-body > :last-child { margin-bottom: 0; }
.msg-body img { border-radius: 14px; height: auto; }
.msg-body table { min-width: 560px; }
.msg-body pre, .code-block { max-width: 100%; }
.code-block pre { max-width: 100%; }
.tool-chips, .evidence-bar, .evidence-sources { max-width: 100%; }
.tool-chip, .evidence-source { min-width: 0; }
.evidence-source { max-width: min(220px, 100%); }
.msg-actions { min-height: 28px; }

/* ---------- Public pages ---------- */
.site-header .bar { max-width: 100%; }
.hero .container { position: relative; }
.hero h1 { text-wrap: balance; }
.hero .lead { text-wrap: pretty; }
.footer-grid > * { min-width: 0; }
.guide-nav { position: sticky; top: 78px; z-index: 20; }

/* ---------- Tablet ---------- */
@media (max-width: 1180px) {
  :root { --content-max: 100%; }
  .dash-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .live-admin-metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .tools-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 1024px) {
  .app-shell { max-width: none; }
  .app-main { width: 100%; }
  .app-sidebar {
    width: min(330px, 88vw);
    height: var(--app-viewport-height, 100dvh);
    padding-top: max(18px, env(safe-area-inset-top));
  }
  .app-sidebar.open { transform: translateX(0); }
  body.sidebar-open { overflow: hidden; }
  .sidebar-backdrop { -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px); }
  .app-topbar { position: sticky; inset-block-start: 0; }
  .page-chat .app-topbar { min-height: 60px; padding-block: 9px; }
  .page-chat .chat-welcome { padding-top: clamp(26px, 7vh, 60px); }
}

@media (max-width: 860px) {
  .feature-row, .plan-grid, .dash-2col, .form-grid-2,
  .guide-hero-v5, .guide-dual, .privacy-grid, .ask-examples {
    grid-template-columns: minmax(0, 1fr) !important;
  }
  .admin-hero { flex-direction: column; align-items: stretch; }
  .knowledge-orb { width: 100%; height: auto; min-height: 92px; border-radius: 22px; }
  .customer-flow, .knowledge-flow {
    grid-template-columns: minmax(0,1fr) !important;
  }
  .customer-flow > i, .knowledge-flow > i { transform: rotate(-90deg); justify-self: center; }
  .guide-nav {
    top: 66px;
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .guide-nav::-webkit-scrollbar { display: none; }
  .guide-nav a { white-space: nowrap; }
}

/* ---------- Mobile ---------- */
@media (max-width: 640px) {
  :root {
    --mobile-gutter: 11px;
    --topbar-h: 58px;
  }
  .container, .container-sm { width: min(100% - 24px, 1180px); }
  .app-content { padding: 12px var(--mobile-gutter); }
  .app-topbar { padding: 9px 11px; gap: 9px; }
  .app-topbar h1 { font-size: 15px; }
  .app-topbar .icon-btn { width: 40px; height: 40px; }
  .app-sidebar { width: min(318px, 90vw); }

  .dash-grid, .live-admin-metrics, .tools-grid, .steps,
  .footer-grid, .score-grid, .chunk-cloud {
    grid-template-columns: minmax(0, 1fr) !important;
  }
  .stat-card, .panel, .admin-hero, .guide-sec { border-radius: 18px; }
  .panel { padding: 16px 14px; }
  .panel-head { align-items: flex-start; flex-wrap: wrap; gap: 8px; }
  .admin-filters { display: grid; grid-template-columns: 1fr; width: 100%; }
  .admin-filters .input, .admin-filters .select, .admin-filters .btn { width: 100%; }
  .actions { flex-wrap: wrap; }
  .actions > *, .row-flex.mobile-stack > * { flex: 1 1 auto; }
  .btn.lg { width: 100%; }

  .site-header { padding: 8px 0; }
  .site-header .bar { padding: 8px 9px; border-radius: 18px; }
  .brand-logo { font-size: 16px; gap: 8px; }
  .brand-logo .mark { width: 36px; height: 36px; }
  .brand-logo small { display: none; }
  .header-actions { gap: 6px; }
  .header-actions .btn { padding-inline: 12px; }
  .hero { padding-top: 58px; }
  .hero-cta { flex-direction: column; align-items: stretch; }
  .hero-cta .btn { width: 100%; }
  .hero-stats { gap: 22px; }
  .live-strip-flow { justify-content: flex-start; overflow-x: auto; width: 100%; padding-bottom: 2px; }

  .page-chat .app-content { padding: 0; }
  .chat-stream {
    padding: 14px 10px 18px;
    scrollbar-gutter: auto;
  }
  .chat-inner { gap: 17px; }
  .chat-welcome { padding: 22px 8px 18px; margin-top: 0 !important; }
  .chat-welcome .wl-orb { width: 62px; height: 62px; border-radius: 20px; margin-bottom: 15px; }
  .chat-welcome h2 { font-size: clamp(20px, 6vw, 25px); line-height: 1.55; }
  .chat-welcome p { font-size: 13px; line-height: 1.85; }
  .suggestions { margin-top: 20px; gap: 8px; }
  .suggestion { padding: 12px 13px; border-radius: 13px; font-size: 12.5px; }

  .chat-composer { padding: 8px 9px max(8px, env(safe-area-inset-bottom)); }
  .chat-context-bar { align-items: stretch; gap: 8px; }
  .site-switch { width: 100%; }
  .context-picker { width: 100%; max-width: none; }
  .privacy-note { width: 100%; margin-inline-start: 0; }
  .composer-box {
    min-height: 54px;
    grid-template-columns: minmax(0, 1fr) 42px;
    padding: 6px 7px 6px 12px !important;
    border-radius: 17px !important;
  }
  .composer-box textarea {
    font-size: 16px; /* prevents iOS zoom */
    line-height: 1.7;
    max-height: min(140px, 25dvh);
    padding-block: 7px 5px;
  }
  .composer-send { width: 42px; height: 42px; }
  .composer-foot { font-size: 10px; margin-top: 5px; padding-inline: 3px; }
  .composer-foot > span:first-child { display: none !important; }

  .msg, .chat-typing { gap: 8px; }
  .msg .avatar, .chat-typing .avatar { width: 30px; height: 30px; border-radius: 10px; }
  .msg .bubble, .chat-typing .bubble { padding: 12px 13px; border-radius: 16px; }
  .msg.bot .bubble { max-width: calc(100% - 38px); }
  .msg.user .bubble { max-width: 88%; }
  .msg-body { font-size: 13.5px; line-height: 1.9; }
  .msg-body .md-h { font-size: 1em; }
  .msg .msg-actions { opacity: 1; }
  .tool-chips { align-items: flex-start; }
  .tool-chips-label { width: 100%; }
  .evidence-bar { align-items: flex-start; }
  .evidence-state { width: 100%; }
  .evidence-source { max-width: 100%; }
  .hooshi-metrics-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .answer-highlight { flex-direction: column; }
  .answer-highlight b { white-space: normal; }
  .code-head { padding: 8px 10px; }
  .code-block pre { padding: 13px 11px; font-size: 12px; }
  .edit-actions, #consent-msg .row-flex { flex-direction: column; align-items: stretch; }
  .edit-actions .btn, #consent-msg .btn { width: 100%; }

  .site-modal { padding: 10px; align-items: end; }
  .modal-card {
    width: 100%;
    max-height: min(90dvh, 760px);
    overflow-y: auto;
    border-radius: 22px 22px 0 0;
    padding: 24px 16px max(20px, env(safe-area-inset-bottom));
  }
  .modal-close { top: 11px; left: 11px; }

  #toast-wrap { bottom: max(12px, env(safe-area-inset-bottom)); width: calc(100% - 24px); }
  .toast { width: fit-content; max-width: 100%; text-align: center; }
}

@media (max-width: 390px) {
  .header-actions .icon-btn { display: none; }
  .header-actions .btn { font-size: 12px; padding-inline: 10px; }
  .chat-stream { padding-inline: 7px; }
  .msg .avatar, .chat-typing .avatar { display: none; }
  .msg.bot .bubble { max-width: 100%; }
  .msg.user .bubble { max-width: 92%; }
  .hooshi-metrics-grid { grid-template-columns: 1fr; }
  .composer-foot { justify-content: flex-end; }
}

/* Landscape phones and short screens. */
@media (max-height: 620px) and (orientation: landscape) {
  .page-chat .app-topbar { min-height: 50px; padding-block: 5px; }
  .chat-welcome { padding-top: 10px; }
  .chat-welcome .welcome-spark, .chat-welcome .welcome-kicker { display: none; }
  .chat-composer { padding-block: 5px max(5px, env(safe-area-inset-bottom)); }
  .composer-foot { display: none; }
  .composer-box textarea { max-height: 90px; }
}

/* Accessibility and lower-motion devices. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}
