/* MERLIN Mobile — Universal responsive for Dashboard, Wizard, Admin Portal, Client Portal */

/* ═══ TABLET (max 900px) ═══ */
@media (max-width: 900px) {
  /* Chat como app: cabecera (sticky) y compositor quietos, SOLO los mensajes se desplazan.
     .ai-container medía 100vh-90px (cabecera de escritorio) y sobresalía: scrolleaba el body entero. */
  /* Fijo al viewport (no calculado contra paddings de .content, que cambian por bloque): bajo la
     cabecera y hasta abajo. Nada del body queda por desplazar, ni arrastrando desde tokens o compositor. */
  .view-ai .ai-container { position: fixed; top: var(--m-topbar, 64px); left: 0; right: 0; bottom: var(--tabbar-h, 0px); height: auto; max-height: none; border-radius: 0; z-index: 5; background: var(--m-bg); }
  .ai-messages { overscroll-behavior: contain; }
  /* Dashboard/Wizard */
  .app { grid-template-columns: 1fr; }
  /* Con el teclado abierto la WebView encoge pero 100vh no: el body sobraba y WebKit lo desplazaba
     al enfocar el campo (la cabecera se colaba bajo el reloj). dvh sigue al viewport real. */
  body, .app { min-height: 100dvh; }
  .sidebar { position: fixed; left: -260px; width: 260px; transition: left 0.25s; z-index: 100; box-shadow: 0 0 32px rgba(0,0,0,0.5); }
  .sidebar.open { left: 0; }
  .mobile-menu-btn { display: flex; }
  .topbar { padding: 16px 16px 16px 64px; }
  .content { padding: 20px 16px 32px; }
  .hero-cards, .energy-summary, .status-grid, .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .user-dropdown { right: 16px; top: 64px; }
  .modal-content, .modal-card { width: 95vw; max-width: 95vw; max-height: 90vh; margin: 5vh auto; }
  .climate-list, .lighting-list, .security-list, .media-list { grid-template-columns: 1fr; }
  .history-search-wrap { width: 100%; }

  /* Client Portal */
  .main-content { padding: 16px; }
  .stats-row { grid-template-columns: repeat(2, 1fr); }
  .plans-grid { grid-template-columns: 1fr; gap: 12px; }
  .nav-tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; flex-wrap: nowrap; }

  /* Admin */
  .main { margin-left: 0 !important; padding: 16px; }
}

/* ═══ PHONE (max 600px) ═══ */
@media (max-width: 600px) {
  body { font-size: 14px; }

  /* ── Dashboard/Wizard topbar ── */
  .topbar { padding: 12px 12px 12px 12px; min-height: 56px; display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: nowrap; }
  .topbar-greeting { min-width: 0; flex: 1; overflow: hidden; margin-left: 48px; }
  .topbar-greeting h1, .topbar h1 { font-size: 17px; line-height: 1.3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .topbar-date { font-size: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .topbar-actions { gap: 4px; flex-shrink: 0; }
  .icon-btn { width: 40px; height: 40px; font-size: 17px; }
  .user-badge { width: 40px; height: 40px; font-size: 14px; }
  .mobile-menu-btn { top: calc(12px + env(safe-area-inset-top)); left: 12px; width: 40px; height: 40px; z-index: 50; position: fixed; }
  .content { padding: 16px 12px 80px; }
  .view-header h2 { font-size: 20px; }
  .view-subtitle { font-size: 12px; }
  /* History search: keep icon inside, no overlap */
  .history-search-wrap { position: relative !important; }
  .history-search-wrap i.ti-search { position: absolute !important; left: 14px !important; top: 50% !important; transform: translateY(-50%) !important; pointer-events: none !important; opacity: 0.5 !important; z-index: 2; font-size: 16px; }
  .history-search-wrap input { padding-left: 40px !important; }

  /* ── Programar ── */
  .modo-tiles { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .modo-tile { min-height: 104px; padding: 12px; }
  .modo-fila .prog-hora { width: 96px; }
  .hor-fila i { height: 16px; border-radius: 2px; }
  .hor-fila, .hor-horas { grid-template-columns: 22px repeat(48, 1fr); }
  .hor-horas { grid-template-columns: 22px repeat(24, 1fr); }
  .prog-tabs { width: 100%; }
  .prog-frase .prog-select { max-width: 100%; }
  .prog-params { grid-template-columns: 1fr; }

  /* ── Franja de Inicio: una fila que se desplaza, como la cabecera de la app de Loxone ── */
  .home-strip { flex-wrap: nowrap; overflow-x: auto; margin: 0 -12px 16px; padding: 0 12px 4px; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .home-strip::-webkit-scrollbar { display: none; }
  .home-chip, .home-strip .quick-card { flex-shrink: 0; }
  .now-playing { padding: 10px 12px; gap: 10px; margin-bottom: 16px; }
  .np-art { width: 52px; height: 52px; }
  .np-vol { display: none; }
  .room-ctl span { max-width: 90px; }

  /* ── Hero cards ── */
  .hero-cards { grid-template-columns: 1fr; gap: 8px; }
  .hero-card { padding: 14px 16px; }
  .hero-card-value { font-size: 28px; }
  .hero-card-label { font-size: 10px; }

  /* ── Rooms ── */
  .rooms-grid, .cards-grid { grid-template-columns: 1fr; gap: 8px; }
  .rooms-grid { gap: 18px; }
  .hk-tiles { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .hk-tile { min-height: 96px; padding: 10px; }
  .room-card { padding: 14px; }
  .room-name { font-size: 14px; }
  .room-pills { gap: 4px; }
  .room-pill { font-size: 11px; }
  .section-header { flex-direction: column; align-items: flex-start; gap: 10px; }
  .tab-switch { width: 100%; }
  .tab-btn { flex: 1; font-size: 12px; padding: 6px 8px; }

  /* ── Energy ── */
  .energy-summary { grid-template-columns: 1fr; gap: 8px; }
  .metric-card, .stat-card { padding: 14px; }
  .metric-value, .stat-value { font-size: 24px; }
  .metric-label, .stat-label { font-size: 10px; }
  .energy-period-tabs { gap: 0; }
  .energy-tab { font-size: 12px; padding: 8px 10px; min-height: 40px; }

  /* ── Climate ── */
  .climate-card { padding: 14px; }
  .climate-card-title { font-size: 14px; }
  .climate-now { font-size: 48px; }
  .climate-now small { font-size: 20px; }
  .climate-target-value { font-size: 22px; }
  .climate-target-btn { width: 44px; height: 44px; font-size: 22px; }
  .climate-mode-btn { padding: 9px 6px; font-size: 12px; }
  .climate-mode-btn i { display: none; }

  /* ── Lighting ── */
  .light-card { padding: 14px; }
  .light-card-title { font-size: 14px; }
  .brightness-value { font-size: 12px; min-width: 36px; }

  /* ── Security ── */
  .security-row { padding: 10px 12px; }
  .security-row-name { font-size: 13px; }
  .security-row-icon { width: 32px; height: 32px; font-size: 15px; }
  .alarm-card { padding: 14px; }
  .alarm-modes { flex-direction: column; gap: 6px; }

  /* ── AI Chat ── */
  .ai-welcome-title { font-size: 18px; }
  .ai-welcome-sub { font-size: 12px; padding: 0 8px; }
  .ai-suggestions { flex-wrap: wrap; gap: 6px; justify-content: center; }
  .ai-suggestion { font-size: 12px; padding: 6px 12px; }
  /* Compositor del chat: UNA fila [campo][micro][enviar], como los chats de IA. Lo demás va en
     .ai-toolbar arriba. Los !important son porque styles.css:2345 fuerza padding/gap con !important. */
  /* Sin franja gris: la píldora del campo sobre el fondo, mismo margen arriba y abajo; el safe-area
     queda fuera de la barra (margen), no dentro (que la hacía desnivelada). */
  .ai-input-bar { gap: 8px !important; padding: 8px 12px !important; margin-bottom: env(safe-area-inset-bottom); background: var(--m-bg) !important; border-top: none !important; }
  .ai-input-bar #ai-input { border-radius: 20px; padding: 10px 14px !important; font-size: 15px; }
  .ai-input-bar .ai-input-btn { width: 38px; height: 38px; min-width: 38px; border: none; background: transparent; border-radius: 50%; font-size: 19px; }
  .ai-input-bar #ai-send { background: var(--m-brand); color: #fff; }
  .ai-msg-bubble { font-size: 13px; max-width: 85%; }

  /* ── Status ── */
  .status-grid { grid-template-columns: 1fr; gap: 8px; }
  .info-row { padding: 10px 0; }
  .info-label, .info-value { font-size: 12px; }

  /* ── Account ── */
  .plan-card { padding: 16px; }
  .plan-card-name { font-size: 22px; }
  .plan-card-actions { flex-direction: column; gap: 8px; }
  .plan-card-actions .btn-primary, .plan-card-actions .btn-secondary { width: 100%; text-align: center; justify-content: center; }
  .benefits-list li { font-size: 13px; }

  /* ── Settings ── */
  .settings-row { flex-direction: column; gap: 10px; align-items: flex-start; }

  /* ── History ── */
  .history-entity-list { max-height: 280px; }
  .hist-valor { font-size: 32px; }
  .history-entity-btn { font-size: 12px; padding: 6px 10px; }

  /* ── Modals — bottom sheet style ── */
  .modal-content, .modal-card { width: 100vw; max-width: 100vw; max-height: 100vh; border-radius: 16px 16px 0 0; margin: auto 0 0; position: fixed; bottom: 0; left: 0; right: 0; }
  .modal-body { max-height: 60vh; overflow-y: auto; -webkit-overflow-scrolling: touch; }
  .modal-small { width: 100vw; max-width: 100vw; }
  .icon-grid { grid-template-columns: repeat(5, 1fr); gap: 6px; }
  .icon-pick { width: 44px; height: 44px; font-size: 18px; }

  /* ── Device rows ── */
  .device-row { padding: 10px 12px; gap: 8px; }
  .device-icon { width: 32px; height: 32px; font-size: 14px; }
  .device-name { font-size: 13px; }
  .device-state { font-size: 11px; }
  .cover-controls { gap: 4px; }
  .cover-btn { width: 40px; height: 40px; font-size: 16px; }
  .tab-btn { min-height: 36px; }
  .toggle { min-width: 48px; min-height: 28px; }
  /* Fijo al viewport: con absolute se abría arriba del documento y, con scroll, quedaba fuera de pantalla. */
  .user-dropdown { position: fixed; top: var(--m-topbar, 64px); left: 12px; right: 12px; width: auto; max-width: none; z-index: 70; }
  .status-bar { font-size: 11px; padding: 8px 12px; }
  .card { padding: 14px; border-radius: 12px; }
  .card-title { font-size: 14px; }
  .card-subtitle { font-size: 11px; }

  /* ── Wizard specific ── */
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table-wrap table { min-width: 600px; }
  .filter-bar { width: 100%; }
  .filter-bar select { width: 100%; }
  .view-header { flex-direction: column; align-items: flex-start; gap: 10px; }
  .view-header .btn-primary { width: 100%; justify-content: center; }
  .config-card { margin-bottom: 12px; }
  .config-card-header { flex-direction: column; gap: 8px; }
  .config-card-body { padding: 14px; }
  .form-row { flex-direction: column; gap: 8px; }
  .form-group input, .form-group select { font-size: 14px; }

  /* Wizard automations */
  .preset-grid { grid-template-columns: 1fr; }
  .preset-card { padding: 14px; }
  .auto-builder { padding: 14px; }
  .preset-tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; flex-wrap: nowrap; }
  .preset-tab { white-space: nowrap; font-size: 12px; }
  .block-card { padding: 10px; }

  /* Wizard integrations */
  .integration-card { flex-direction: column; gap: 10px; }
  .int-actions { justify-content: flex-start; }

  /* Wizard logs */
  .logs-filters { flex-direction: column; gap: 8px; }
  .logs-filters select { width: 100%; }
  .log-entry { gap: 8px; }
  .log-time { font-size: 10px; }
  .system-log-wrap { font-size: 11px; }

  .topbar-left h2 { font-size: 14px; }
  .topbar-right { gap: 8px; }
  .topbar-user { font-size: 11px; display: none; }
  .btn-logout { font-size: 11px; padding: 5px 10px; }
  .main-content { padding: 12px; }
  .nav-tabs { gap: 0; overflow-x: auto; }
  .nav-tab { font-size: 12px; padding: 10px 12px; white-space: nowrap; }
  .stats-row { grid-template-columns: 1fr; gap: 8px; }
  .stat-card { padding: 14px; }
  .stat-value { font-size: 24px; }
  .device-card { padding: 16px; }
  .device-info { grid-template-columns: 1fr; gap: 8px; }
  .device-info-item { padding: 6px 0; }
  .plans-grid { grid-template-columns: 1fr; gap: 12px; }
  .plan-card { padding: 20px; }
  .plan-price { font-size: 24px; }
  .plan-features { font-size: 12px; }
  .profile-form { gap: 10px; }
  .profile-form input { font-size: 14px; }
  .profile-actions { flex-direction: column; gap: 8px; }
  .usage-bar-header { font-size: 11px; }
  .activate-input-row { flex-direction: column; gap: 8px; }
  .activate-input-row input { width: 100%; }
  .activate-input-row button { width: 100%; }

  /* Client Portal auth */
  .auth-container { padding: 12px; }
  .auth-box { padding: 20px; max-width: 100%; }
  .auth-logo img { width: 48px; height: 48px; }
  .auth-logo h1 { font-size: 1.3rem; }
  .auth-tabs { gap: 4px; }

  /* ── Admin Portal ── */
  .sidebar { width: 220px; left: -230px; }
  .sidebar.open { left: 0; }
  .main { margin-left: 0 !important; padding: 16px 12px; }
  .section-title { font-size: 18px; }
  .stats { grid-template-columns: 1fr; gap: 8px; }
  .stat-card .label { font-size: 10px; }
  table { font-size: 12px; }
  table th, table td { padding: 8px 6px; }
  .modal-overlay .modal-card { width: 95vw; }
  .modal-card { padding: 16px; }

  /* ── SCADA móvil (v2) ── */
  .scada-wrap { padding: 12px; }
  .scada-grid { grid-template-columns: 1fr; }
  .scada-kpis { grid-template-columns: repeat(3, 1fr) !important; gap: 8px; }
  .scada-kpis .hero-card { padding: 12px; }
  .scada-kpis .hero-card-value { font-size: 20px; }
  .scada-head .input { width: 100%; }
  .scada-editor { flex-direction: column; }
  .scada-props { width: 100%; max-height: none; }
  .scada-hint-inline { display: none; }
}

/* ═══ VERY SMALL PHONES (max 380px) ═══ */
@media (max-width: 380px) {
  .content { padding: 12px 8px 80px; }
  .hero-card-value { font-size: 24px; }
  .room-card { padding: 12px; }
  .topbar-greeting h1 { font-size: 15px; }
  .climate-target-value { font-size: 24px; }
  .ai-suggestions { gap: 4px; }
  .ai-suggestion { font-size: 11px; padding: 5px 10px; }
  .icon-grid { grid-template-columns: repeat(4, 1fr); }
  .auth-box { padding: 16px; }
  .plan-card { padding: 14px; }
}

/* ═══ LANDSCAPE PHONE ═══ */
@media (max-height: 500px) and (orientation: landscape) {
  .content { padding: 12px 16px 24px; }
  .hero-cards { grid-template-columns: repeat(3, 1fr); }
  .rooms-grid { grid-template-columns: repeat(2, 1fr); }
  .modal-content, .modal-card { max-height: 95vh; }
  .modal-body { max-height: 50vh; }
}

/* ═══ TOUCH DEVICES ═══ */
@media (hover: none) and (pointer: coarse) {
  .nav-item { padding: 12px 14px; min-height: 44px; }
  .toggle { min-width: 51px; min-height: 31px; }
  .climate-target-btn { min-width: 44px; min-height: 44px; }
  .cover-btn { min-width: 40px; min-height: 40px; }
  .icon-pick { min-width: 44px; min-height: 44px; }
  .room-card:hover, .nav-item:hover, .card:hover { transform: none; }
  .brightness-slider::-webkit-slider-thumb { width: 28px; height: 28px; }
  .modal-body, .history-entity-list, .ai-messages { -webkit-overflow-scrolling: touch; scroll-behavior: smooth; }
  /* Client portal */
  .nav-tab { min-height: 44px; }
  .btn-primary, .btn-secondary, .btn-save { min-height: 44px; }
  .plan-btn { min-height: 44px; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   MÓVIL — lo añadido el 28-jul (accesos de Inicio, tarjetas de area, permisos)
   Ninguna de estas rejillas estaba contemplada aqui. La de accesos pedia 190px
   por columna: en una pantalla de 390 no caben dos, asi que desbordaba, y al
   desbordar ensanchaba el contenedor y ESTIRABA todo lo demas — por eso parecia
   que ni las hero-cards ni las areas hacian caso a su media query.
   ═══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 600px) {
  /* Red de seguridad: que nada pueda empujar la pagina a lo ancho. */
  /* clip, no hidden: overflow-x hidden convierte al body en contenedor de scroll y mata el sticky de la cabecera */
  html, body { max-width: 100%; overflow-x: clip; }

  .quick-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .quick-card { padding: 12px; font-size: 13px; }
  .quick-card i { font-size: 18px; }
  .quick-count { font-size: 11px; padding: 1px 6px; }

  .rooms-grid { grid-template-columns: 1fr; }
  .room-card, .room-card.quiet { min-height: 0; }
  .room-pills { min-height: 0; }

  /* La barra y el lateral vuelven a su alto compacto: los 84px de escritorio
     comen media pantalla en un movil. */
  .topbar { min-height: 56px; padding: 12px; }
  .sidebar-header { height: auto; margin: 0; padding: 16px 4px; }

  /* Permisos del wizard: una columna, o las etiquetas se parten. */
  .perm-grid { grid-template-columns: 1fr; }
  .perm-head { flex-wrap: wrap; gap: 6px; }
  .modal-card:has(.perm-block) { width: auto; max-width: 94vw; }

  /* Avisos del panel del instalador. */
  .wz-aviso { padding: 12px; gap: 10px; }
  .wz-aviso-txt strong { font-size: 13px; }
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 380px) {
  .quick-grid { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: 1fr; }
}

/* ── La hoja inferior, rematada (patrón iOS) ──
   En movil el modal ya subia desde abajo, pero le faltaba el acabado: no
   respetaba la zona segura del iPhone (el contenido quedaba bajo la barra de
   inicio), no tenia el asa que indica que se puede arrastrar, y el alto era fijo.
   Ver merlinautosys.com: superficie, radio grande arriba y linea de 1px. */
@media (max-width: 600px) {
  .modal-card, .modal-overlay .modal-card {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 92vh;
    border-radius: 20px 20px 0 0 !important;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .modal-overlay { align-items: flex-end !important; }
  /* Los botones de accion, al alcance del pulgar y con area suficiente. */
  .modal-footer .btn-primary, .modal-footer .btn-secondary { min-height: 44px; }
  .toggle { width: 51px !important; height: 31px !important; }
}

/* ═══ AREA SEGURA (PWA, app nativa y navegador) — AL FINAL, para que ningun bloque de movil lo pise ═══
   Sin media query: en un navegador normal env() vale 0 y no cambia nada; en la PWA y en la
   app nativa (WKWebView no es display-mode: standalone) es lo que aparta la cabecera del notch. */
/* el body NO lleva el inset: la cabecera (sticky) ya lo lleva y se sumaban los dos */
.sidebar { padding-top: env(safe-area-inset-top); }
.status-bar { padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
.ai-input-bar { padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
/* Android (app nativa, WebView >= 140): el contenido pasa por DEBAJO de la barra de navegacion del
   sistema (48 px con 3 botones) y las hojas inferiores quedaban cortadas por ella. Capacitor 8 deja
   ademas --safe-area-inset-* en :root; se toma el mayor de los dos por si env() llega a 0. */
:root { --m-sab: max(env(safe-area-inset-bottom, 0px), var(--safe-area-inset-bottom, 0px)); }
.modal-content, .modal-card { padding-bottom: var(--m-sab); }
.main { padding-bottom: var(--m-sab); }

/* Cabecera movil: el boton de menu vive DENTRO de la cabecera y se mueve con ella (antes era fixed
   y bailaba con el scroll). Barra compacta: 6px + inset del notch, sin el margen de 16px que el
   .main de admin colaba al dashboard. #main-content = solo el dashboard. */
@media (max-width: 900px) {
  /* padding 0 por ID pisaba el hueco de la barra de pestañas (.main, abajo): el final de cada vista quedaba bajo ella.
     Aquí, con la misma especificidad: en teléfono el alto de la barra; en tableta solo la zona segura. */
  #main-content { padding: 0 0 var(--tabbar-h, var(--m-sab)); }
  /* max(): sin notch (Android, navegador) 10px; con notch, 6px por debajo del inset */
  .topbar { margin: 0; min-height: 0; padding: max(10px, calc(6px + env(safe-area-inset-top))) 12px 10px; }
  .mobile-menu-btn { position: static; flex: none; width: 36px; height: 36px; }
  .topbar-greeting { margin-left: 0; }
}

/* ═══ BARRA DE PESTAÑAS DEL MOVIL (9-oct-2026, fase 3) ═══════════════════════
   Como las apps de Loxone, Homey y Apple Home: cuatro pestañas y «Mas», un toque. Solo en telefono; en
   tableta y escritorio sigue el lateral. La hamburguesa deja de salir en telefono: «Mas» la sustituye. */
@media (max-width: 600px) {
  :root { --tabbar-h: calc(56px + var(--m-sab)); }
  .main { padding-bottom: var(--tabbar-h); }
  .mobile-menu-btn { display: none; }
  .modal-content, .modal-card { padding-bottom: calc(var(--m-sab) + 8px); }
}
/* Estancia como página (10-oct): cabecera compacta en el móvil */
@media (max-width: 640px) { .sala-cab-ic { width: 44px; height: 44px; } .sala-cab-ic i { font-size: 22px; } .sala-cab h2 { font-size: 21px; } }
