@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; height: auto; min-height: calc(100vh - var(--header-height) - 2rem); }
  .chat-panel { min-height: 70vh; }
  .side-panel { order: -1; }
  .phase-list { display: none; }
}
@media (max-width: 640px) {
  :root { --header-height: 64px; }
  .app-header { padding: .65rem .8rem; }
  .brand-logos img:first-child { max-width: 112px; }
  .brand-logos img:last-child { display: none; }
  .brand-copy { display: none; }
  .main-content { padding: .55rem; }
  .workspace { min-height: calc(100vh - var(--header-height) - 1.1rem); }
  .side-panel { display: none; }
  .panel { border-radius: 15px; }
  .suggestion-grid { grid-template-columns: 1fr; }
  .messages { padding: .75rem; }
  .message { width: 96%; }
  .message-row[data-role="user"] .message { max-width: 90%; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}

@media (max-width: 900px) {
  .conversation-select { width: min(260px, 36vw); }
}
@media (max-width: 640px) {
  .chat-toolbar { align-items: flex-start; flex-direction: column; }
  .conversation-toolbar { width: 100%; justify-content: stretch; }
  .conversation-select { width: auto; min-width: 0; flex: 1; }
  .alpha-badge { display: none; }
  .edit-bar { align-items: flex-start; flex-direction: column; }
  .message-footer { align-items: flex-start; flex-direction: column; }
  .document-list li { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  .message-actions { width: 100%; }
  .message-action { min-height: 34px; }
  .message-progress-header { align-items: flex-start; flex-direction: column; gap: .15rem; }
  .message-progress-header span:last-child { white-space: normal; }
  .message-metrics { text-align: left; }
}

@media (max-width: 640px) {
  .composer-support { align-items: flex-start; flex-direction: column; gap: 0; }
  .character-count { align-self: flex-end; margin-top: .15rem; }
  .scroll-latest { right: .75rem; bottom: 98px; }
}

/* v7.1.0-beta.18 */
@media (max-width: 1100px) {
  .category-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; height: auto; }
  .side-panel { order: 2; overflow: visible; }
  .chat-panel { min-height: min(780px, calc(100vh - var(--header-height) - 1rem)); }
}
@media (max-width: 700px) {
  .header-actions { gap: .35rem; }
  .display-actions { display: flex; gap: .22rem; }
  .display-actions .icon-button { display: inline-flex; width: 38px; min-width: 38px; height: 38px; padding: 0; font-size: .86rem; }
  .language-control select { width: 92px; padding-right: .4rem; }
  .category-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .source-list { grid-template-columns: 1fr; }
  .source-card-primary { grid-column: auto; }
}
@media (max-width: 480px) {
  .category-grid { grid-template-columns: 1fr 1fr; }
  .category-button { min-height: 72px; font-size: .78rem; }
  .source-actions { flex-direction: column; }
  .source-action { justify-content: center; }
}

/* v7.1.0-beta.18.2 · composición móvil fija y scroll interno estable */
@media (max-width: 700px) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }
  .app-shell {
    width: 100%;
    height: 100dvh;
    min-height: 100dvh;
    grid-template-rows: auto minmax(0, 1fr);
    overflow: hidden;
  }
  .app-header {
    position: relative;
    top: auto;
  }
  .main-content {
    min-height: 0;
    overflow: hidden;
    padding: .55rem;
  }
  .workspace {
    height: 100%;
    min-height: 0;
  }
  .chat-panel {
    height: 100%;
    min-height: 0;
    max-height: none;
    grid-template-rows: auto minmax(0, 1fr) auto;
  }
  .messages {
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    scroll-padding-block: .75rem;
    -webkit-overflow-scrolling: touch;
  }
  .composer {
    position: sticky;
    bottom: 0;
    z-index: 7;
    flex: none;
    padding-bottom: max(.9rem, env(safe-area-inset-bottom));
    box-shadow: 0 -10px 22px color-mix(in srgb, var(--text) 9%, transparent);
  }
  .side-panel {
    display: none;
  }
}


/* v7.1.0-hotfix.2 · controles tipográficos siempre visibles en móvil */
@media (max-width: 480px) {
  .app-header { gap: .35rem; }
  .header-actions { min-width: 0; flex-wrap: nowrap; }
  .language-control select { width: 78px; min-width: 78px; font-size: .78rem; }
  .display-actions { flex: none; }
  .display-actions .icon-button { width: 34px; min-width: 34px; height: 36px; font-size: .8rem; }
}
@media (max-width: 390px) {
  .brand-logos img:first-child { max-width: 96px; }
  .language-control select { width: 70px; min-width: 70px; }
  .display-actions .icon-button { width: 31px; min-width: 31px; }
}

/* v7.1.0-hotfix.14 · conversación móvil más compacta y controles alineados */
@media (max-width: 700px) {
  :root { --header-height: 52px; }

  /* Recupera altura útil para el historial de mensajes. */
  .app-header {
    min-height: var(--header-height);
    gap: .35rem;
    padding: .35rem .45rem;
  }
  .brand { gap: .4rem; }
  .brand-logos { gap: .4rem; }
  .brand-logos img { max-height: 32px; }
  .brand-logos img:first-child { max-width: 102px; }
  .header-actions { gap: .3rem; }
  .language-control select {
    min-height: 36px;
    padding-top: .32rem;
    padding-bottom: .32rem;
  }

  /* En móvil no aporta información útil y ocupa una fila completa. */
  .service-pill { display: none; }

  /* Selector de conversación + nueva conversación siempre en una sola fila. */
  .chat-toolbar {
    display: block;
    padding: .42rem .5rem;
  }
  .conversation-toolbar {
    width: 100%;
    display: flex;
    align-items: center;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: stretch;
    gap: .35rem;
  }
  .conversation-select {
    width: auto;
    min-width: 0;
    max-width: none;
    min-height: 34px;
    flex: 1 1 auto;
    padding: .28rem .42rem;
    font-size: .7rem;
  }
  .conversation-toolbar .button-compact {
    min-height: 34px;
    flex: 0 0 auto;
    padding: .32rem .5rem;
    font-size: .7rem;
    line-height: 1.1;
    white-space: nowrap;
  }

  /* La ayuda de teclado y el aviso ya están disponibles en escritorio. */
  .composer-support { display: none; }

  .main-content { padding: .3rem; }
  .panel { border-radius: 12px; }
  .messages { padding: .55rem; gap: .65rem; }
  .composer {
    padding: .5rem;
    padding-bottom: max(.55rem, env(safe-area-inset-bottom));
  }
  .composer-box { gap: .4rem; padding: .3rem; border-radius: 13px; }
  .composer textarea { padding: .42rem .45rem; }

  /* El override móvil anterior cambiaba grid por inline-flex y desplazaba el glifo. */
  .display-actions .icon-button {
    display: grid;
    place-items: center;
    align-items: center;
    justify-content: center;
    padding: 0;
    line-height: 1;
    text-align: center;
  }

  .scroll-latest { bottom: 76px; }
}

@media (max-width: 480px) {
  .app-header { padding-inline: .35rem; }
  .language-control select { width: 74px; min-width: 74px; }
  .display-actions .icon-button {
    width: 32px;
    min-width: 32px;
    height: 34px;
  }
  .conversation-toolbar .button-compact { padding-inline: .42rem; }
}

@media (max-width: 360px) {
  .brand-logos img:first-child { max-width: 86px; }
  .language-control select { width: 66px; min-width: 66px; }
  .display-actions { gap: .14rem; }
  .display-actions .icon-button { width: 29px; min-width: 29px; }
  .conversation-select,
  .conversation-toolbar .button-compact { font-size: .66rem; }
  .conversation-toolbar .button-compact { padding-inline: .35rem; }
}

/* v7.1.0-hotfix.31 · viewport móvil adaptativo (320px+, barras del navegador, teclado y safe areas) */
@media (max-width: 700px) {
  html,
  body,
  #app {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    overflow-x: hidden;
  }

  html {
    height: 100%;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }

  body {
    min-height: 100%;
    height: 100%;
  }

  .app-shell {
    width: 100%;
    max-width: 100%;
    height: var(--app-viewport-height, 100dvh);
    min-height: 0;
    max-height: var(--app-viewport-height, 100dvh);
    overflow: hidden;
  }

  .app-header {
    width: 100%;
    min-width: 0;
    /* En navegador móvil el viewport ya empieza debajo de la barra del sistema.
       No sumamos safe-area-top para evitar el hueco superior (~20-30 px en Samsung). */
    padding-top: .35rem;
    padding-left: max(.35rem, env(safe-area-inset-left));
    padding-right: max(.35rem, env(safe-area-inset-right));
  }

  /* En una PWA instalada/fullscreen sí puede ser necesario respetar el notch/status bar. */
  @media (display-mode: standalone), (display-mode: fullscreen) {
    .app-header {
      padding-top: max(.35rem, env(safe-area-inset-top));
    }
  }

  .brand,
  .brand-logos,
  .header-actions,
  .display-actions,
  .main-content,
  .workspace,
  .chat-panel,
  .chat-toolbar,
  .conversation-toolbar,
  .messages,
  .composer,
  .composer-box,
  .message-row,
  .message,
  .message-body,
  .sources-panel,
  .source-card,
  .viewer-recommendations,
  .viewer-recommendation-card {
    min-width: 0;
    max-width: 100%;
  }

  .brand {
    flex: 1 1 auto;
    overflow: hidden;
  }

  .brand-logos {
    flex: 1 1 auto;
    overflow: hidden;
  }

  .brand-logos img:first-child {
    width: auto;
    max-width: min(102px, 28vw);
  }

  .header-actions {
    flex: 0 1 auto;
    min-width: 0;
  }

  .language-control {
    min-width: 0;
    flex: 0 1 auto;
  }

  .language-control select {
    max-width: 23vw;
  }

  .display-actions {
    flex: 0 0 auto;
  }

  .main-content {
    width: 100%;
    padding-left: max(.3rem, env(safe-area-inset-left));
    padding-right: max(.3rem, env(safe-area-inset-right));
    padding-bottom: 0;
  }

  .workspace,
  .chat-panel {
    width: 100%;
    max-width: 100%;
    height: 100%;
  }

  .chat-panel {
    grid-template-rows: auto minmax(0, 1fr) auto;
  }

  .chat-toolbar {
    min-height: 0;
  }

  .conversation-select {
    min-width: 0;
    width: 0;
    max-width: 100%;
    flex: 1 1 0;
    text-overflow: ellipsis;
  }

  .conversation-toolbar .button-compact {
    max-width: 46%;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .messages {
    width: 100%;
    min-height: 0;
    max-width: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    scrollbar-gutter: stable;
  }

  .message {
    width: 100%;
    max-width: 100%;
    overflow-wrap: anywhere;
    word-break: normal;
  }

  .message-row[data-role="user"] .message {
    width: auto;
    max-width: 90%;
  }

  .message-body,
  .message-body p,
  .message-body li,
  .message-body a,
  .source-title,
  .source-snippet {
    overflow-wrap: anywhere;
  }

  .message-body img,
  .message-body video,
  .message-body iframe,
  .message-body table,
  .source-card img {
    max-width: 100%;
  }

  .message-body table {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .message-body pre,
  .message-body code {
    max-width: 100%;
  }

  .composer {
    width: 100%;
    max-width: 100%;
    padding-left: max(.5rem, env(safe-area-inset-left));
    padding-right: max(.5rem, env(safe-area-inset-right));
    padding-bottom: max(.55rem, env(safe-area-inset-bottom));
  }

  .composer-box {
    width: 100%;
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .composer textarea {
    min-width: 0;
    max-width: 100%;
    max-height: min(160px, 28vh);
  }

  .composer .button-primary {
    max-width: 38vw;
    white-space: nowrap;
  }

  .composer-actions {
    gap: .3rem;
  }

  .voice-dictation-button {
    width: 38px;
    min-width: 38px;
    height: 38px;
    border-radius: 10px;
  }

  .scroll-latest {
    right: max(.75rem, env(safe-area-inset-right));
  }
}

/* Teléfonos especialmente estrechos: no permitimos que cabecera o compositor ensanchen el viewport. */
@media (max-width: 380px) {
  .app-header { gap: .2rem; }
  .brand-logos img:first-child { max-width: 78px; }
  .language-control select {
    width: 60px;
    min-width: 60px;
    max-width: 60px;
    padding-left: .35rem;
    padding-right: .2rem;
    font-size: .72rem;
  }
  .display-actions { gap: .08rem; }
  .display-actions .icon-button {
    width: 27px;
    min-width: 27px;
    height: 32px;
    font-size: .74rem;
  }
  .conversation-toolbar { gap: .25rem; }
  .conversation-toolbar .button-compact {
    max-width: 44%;
    padding-inline: .3rem;
    font-size: .64rem;
  }
  .conversation-select { font-size: .64rem; }
  .messages { padding-inline: .42rem; }
  .composer { padding-inline: .4rem; }
  .composer .button-primary {
    padding-inline: .62rem;
    font-size: .78rem;
  }
}

@media (max-width: 340px) {
  .brand-logos img:first-child { max-width: 64px; }
  .language-control select { width: 54px; min-width: 54px; max-width: 54px; }
  .display-actions .icon-button { width: 25px; min-width: 25px; }
  .conversation-toolbar .button-compact { max-width: 42%; }
  .composer .button-primary { padding-inline: .48rem; }
}

/* Fallback para navegadores móviles sin unidades dvh. */
@supports not (height: 100dvh) {
  @media (max-width: 700px) {
    .app-shell {
      height: var(--app-viewport-height, 100vh);
      max-height: var(--app-viewport-height, 100vh);
    }
  }
}


/* v7.1.0-hotfix.31 · móvil: fuentes complementarias compactas, apiladas y expandibles */
@media (max-width: 720px) {
  .source-list {
    grid-template-columns: minmax(0, 1fr);
    gap: .45rem;
  }
  .source-card-primary {
    grid-column: auto;
  }
  .source-card-complementary {
    width: 100%;
    border-radius: 10px;
  }
  .source-card-complementary:not([open]) {
    background: var(--surface-soft);
  }
  .source-card-summary {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas:
      "role arrow"
      "title arrow";
    align-items: start;
    column-gap: .5rem;
    row-gap: .28rem;
    min-height: 44px;
    padding: .58rem .65rem;
  }
  .source-card-summary .source-role {
    grid-area: role;
    white-space: normal;
    max-width: 100%;
  }
  .source-card-summary::after {
    grid-area: arrow;
    align-self: center;
    justify-self: end;
  }
  .source-summary-title {
    grid-area: title;
    font-size: .78rem;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    overflow: hidden;
  }
  .source-card-complementary[open] .source-summary-title {
    display: block;
    overflow: visible;
  }
  .source-card-body {
    padding: .58rem .65rem .7rem;
  }
}
