/**
 * Optimización Universal Responsive para Todos los Dispositivos
 * Smartphones pequeños (320px+), medianos, tablets, plegables, laptops, monitores 4K.
 */

/* 1. Soporte para Áreas Seguras (Notch, Dynamic Island, Barra de inicio) */
:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* 2. Altura de ventana dinámica para evitar saltos de la barra del navegador */
@supports (height: 100dvh) {
  body {
    min-height: 100dvh;
  }
  #chat {
    height: calc(100dvh - var(--app-header, 64px)) !important;
  }
  .study-overlay {
    height: 100dvh;
  }
}

/* 3. Prevención de zoom automático en iOS en campos de texto */
@media screen and (max-width: 768px) {
  input, select, textarea {
    font-size: 16px !important;
  }
}

/* 4. Tablas y bloques de código con desplazamiento táctil suave */
.tbl-wrap,
pre,
.code-wrap {
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}

.m-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 480px;
}

.m-table th,
.m-table td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--rule);
  text-align: left;
}

.m-table th {
  background: var(--soft);
  font-weight: 700;
  color: var(--ink);
}

/* 5. Optimización para Pantallas Muy Pequeñas (< 380px) */
@media (max-width: 380px) {
  body.site-shell > header > nav > .nav svg {
    width: 18px;
    height: 18px;
  }
  
  .composer {
    padding: 6px 8px 6px 12px;
  }

  .send {
    width: 34px;
    height: 34px;
  }

  .home-primary,
  .home-secondary {
    width: 100%;
  }

  .tk button {
    padding: 8px 16px;
    font-size: 13px;
  }
}

/* 6. Optimización para Móviles (<= 720px) en MiyoIA */
@media (max-width: 720px) {
  body[data-view="chat"] #chat {
    padding-left: 0 !important;
    padding-bottom: calc(60px + var(--safe-bottom)) !important;
  }

  body[data-view="chat"] .rail {
    top: auto !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    width: 100% !important;
    height: calc(56px + var(--safe-bottom)) !important;
    min-height: 0;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
    padding: 4px 12px var(--safe-bottom) !important;
    border-right: 0 !important;
    border-top: 1px solid var(--rule) !important;
    background: var(--bg) !important;
    z-index: 100;
  }

  body[data-view="chat"][data-side="open"] .rail {
    top: var(--app-header) !important;
    bottom: 0 !important;
    width: min(300px, 86vw) !important;
    height: auto !important;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    padding: 12px 14px !important;
    box-shadow: var(--shadow-lg);
  }

  body[data-view="chat"] .rail button {
    width: 44px;
    height: 44px;
    padding: 0;
    justify-content: center;
  }

  body[data-view="chat"] .rail .t {
    display: none !important;
  }

  body[data-view="chat"][data-side="open"] .rail .t {
    display: inline !important;
  }

  body[data-view="chat"] .rail .rbot {
    display: none;
  }

  body[data-view="chat"][data-side="open"] .rail .rbot {
    display: flex;
  }

  body[data-side="open"] .scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 99;
    background: rgba(0, 0, 0, 0.35);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
  }

  .chat-main {
    padding: 10px 14px calc(14px + var(--safe-bottom));
  }

  .u-msg-text {
    max-width: 90%;
  }

  .composer {
    border-radius: 24px;
  }
}

/* 7. Optimización para Tablets (721px - 1024px) */
@media (min-width: 721px) and (max-width: 1024px) {
  #chat {
    padding-left: var(--rail, 76px);
  }
  .chat-main {
    padding: 16px 24px;
  }
}

/* 8. Optimización para Pantallas Ultra-Anchas (> 1440px) */
@media (min-width: 1440px) {
  .home-main {
    max-width: 1320px;
  }
  .chat-main {
    max-width: 920px;
  }
}

/* 9. Respeto de Movimiento Reducido */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .tk {
    animation: none !important;
  }
}
