/* ============================================================
   responsive.css — ValdiviaLux v2.9
   Capa responsiva: se carga DESPUÉS de main.css y v27.css.

   Criterios:
   · Alto real de pantalla en móviles (100dvh) y zonas seguras (notch,
     barra de gestos): nada queda escondido bajo la barra del navegador.
   · Objetivos táctiles de 44 px; inputs a 16 px (iOS no hace zoom).
   · Tablas: con poco ancho se vuelven TARJETAS (etiqueta + valor). Se
     decide por el ancho del contenedor (container queries), no de la
     pantalla: funciona igual en teléfono, tablet con menú o dentro de
     un modal. Las tablas que no son tarjetas se desplazan de lado
     con sombra indicadora, nunca se aplastan.
   · Barras de herramientas y acciones que se reparten en filas.
   ============================================================ */

/* ── Base robusta (todas las pantallas) ─────────────────────── */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
#app { height: 100vh; height: 100dvh; }
img, video { max-width: 100%; height: auto; }
.input, select.input, textarea.input { max-width: 100%; min-width: 0; }
.btn { max-width: 100%; touch-action: manipulation; }
#main-content { overscroll-behavior-y: contain; padding-bottom: env(safe-area-inset-bottom); }
.modal-body { overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.p-page { min-width: 0; }

/* Editor de elementos: 2 columnas con espacio, 1 en el teléfono */
.qie-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 14px;
  max-width: 760px;
}

/* Barras horizontales del panel (rankings) */
.hbar-chart {
  list-style: none;
  margin: 0;
  padding: 12px;
  display: grid;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
}
.hbar-row {
  display: grid;
  grid-template-columns: minmax(0, 11rem) minmax(0, 1fr) auto;
  align-items: center;
  gap: 4px 10px;
  font-size: 12.5px;
}
.hbar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.hbar-track { height: 12px; background: var(--bg-alt); border-radius: 3px; overflow: hidden; }
.hbar-fill { display: block; height: 100%; border-radius: 3px; }
.hbar-value { font-family: var(--font-mono); font-size: 12px; color: var(--text-muted); white-space: nowrap; }
@container (max-width: 480px) {
  .hbar-row { grid-template-columns: minmax(0, 1fr) auto; }
  .hbar-label { grid-column: 1 / -1; white-space: normal; overflow-wrap: anywhere; }
}
.hbar-wrap { container-type: inline-size; }

/* Sombra que avisa que una tabla sigue hacia el lado */
.table-wrap, .cq-grid-wrap {
  container-type: inline-size;
  background:
    linear-gradient(to right, var(--surface) 30%, transparent) left / 24px 100% no-repeat local,
    linear-gradient(to left,  var(--surface) 30%, transparent) right / 24px 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, rgba(0,0,0,.16), transparent) left / 10px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, rgba(0,0,0,.16), transparent) right / 10px 100% no-repeat scroll;
}

/* Con espacio, la columna de botones de una fila va en una sola línea */
table.rt td.rt-actions { width: 1%; white-space: nowrap; }
table.rt td.rt-actions > div { flex-wrap: nowrap !important; }

/* ── Tablas como tarjetas (contenedor angosto) ──────────────── */
@container (max-width: 600px) {
  table.rt td.rt-actions > div { flex-wrap: wrap !important; }
  table.rt td.rt-actions { width: auto !important; white-space: normal; }
  table.rt, table.rt tbody { display: block; width: 100%; min-width: 0 !important; }
  table.rt thead { display: none; }
  table.rt tr {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px 14px;
    padding: 12px 14px;
    margin: 0 0 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
  }
  table.rt tr:last-child { margin-bottom: 0; }
  table.rt td {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 1px;
    min-width: 0;
    padding: 0 !important;
    border: 0 !important;
    background: none !important;
    box-shadow: none !important;
    text-align: left !important;
    overflow-wrap: anywhere;
    font-size: 13.5px;
  }
  table.rt td::before {
    content: attr(data-label);
    font-family: var(--font-cond);
    font-size: 10.5px;
    font-weight: 700;
    letter-spacing: .8px;
    text-transform: uppercase;
    color: var(--text-muted);
  }
  table.rt td > .num { text-align: left; }
  table.rt td.num { font-family: var(--font-mono); }
  table.rt td[data-label=""]::before,
  table.rt td:not([data-label])::before { display: none; }
  table.rt td:empty { display: none; }
  /* Primera celda = título de la tarjeta */
  table.rt td:first-child {
    grid-column: 1 / -1;
    flex-direction: row;
    align-items: baseline;
    gap: 8px;
    font-size: 15px;
    font-weight: 700;
  }
  table.rt td.rt-wide, table.rt td.rt-full { grid-column: 1 / -1; }
  table.rt td.rt-actions {
    grid-column: 1 / -1;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 6px;
    padding-top: 10px !important;
    border-top: 1px dashed var(--border) !important;
  }
  table.rt td.rt-actions > div { width: 100%; }
  table.rt td.rt-actions .btn { flex: 1 1 auto; justify-content: center; min-height: 40px; }
  table.rt td.col-hide-mobile { display: none; }
  table.rt td.empty-state { grid-column: 1 / -1; }
  table.rt tbody tr:hover td { background: none !important; }
  /* Fila elegida (teclado / selección) */
  table.rt tr.kb-sel { outline: 2px solid var(--accent); outline-offset: -1px; }
  table.rt tr.cq-row.sel { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
  table.rt tr.cq-row.sel td::before { color: inherit; opacity: .8; }
  /* Grilla de elementos del presupuesto */
  .cq-grid.rt tr.cq-row { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px 10px; }
  .cq-grid.rt .cq-dwg { grid-row: 1; grid-column: 3; align-items: flex-end; }
  .cq-grid.rt .cq-dwg svg { width: 56px; }
  .cq-grid.rt td:first-child { grid-column: 1 / 3; }
  .cq-grid.rt .cq-glosa { grid-column: 1 / -1; min-width: 0; }
  .cq-grid.rt tr.cq-row { margin: 8px; }
}

/* ── Teléfono y tablet chica (< 768 px) ─────────────────────── */
@media (max-width: 767px) {
  /* Barra superior: una sola línea, sin cosas apretadas */
  #topbar {
    height: calc(var(--topbar-h) + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) max(10px, env(safe-area-inset-right)) 0 max(6px, env(safe-area-inset-left));
    gap: 6px;
  }
  #topbar-menu-btn {
    width: 44px; height: 44px;
    align-items: center; justify-content: center;
  }
  #topbar-breadcrumb {
    flex: 1 1 auto;
    min-width: 0;
    font-size: 15px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .topbar-spacer { display: none; }
  #conn-indicator { flex-shrink: 0; display: inline-flex; align-items: center; }
  #conn-indicator .conn-label { display: none; }
  #conn-indicator .conn-dot { margin-right: 0 !important; }
  #topbar-time { display: none; }
  #user-chip { flex-shrink: 0; }

  /* Menú lateral */
  #sidebar {
    width: min(300px, 86vw);
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    overscroll-behavior: contain;
  }
  .nav-item { min-height: 44px; }

  /* Página */
  .p-page { padding: 14px max(12px, env(safe-area-inset-right)) 24px max(12px, env(safe-area-inset-left)); }
  .page-title { font-size: 20px; }
  .card { padding: 14px 12px; border-radius: var(--radius-lg); }

  /* Acciones de página: cuadrícula pareja en vez de botones apretados */
  .page-actions {
    display: grid !important;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    width: 100%;
    gap: 8px;
  }
  .page-actions .btn,
  .page-actions .btn:only-child { min-width: 0; width: 100%; justify-content: center; }

  /* Botones e inputs táctiles; 16 px evita el zoom automático de iOS */
  .btn { min-height: 44px; white-space: normal; text-align: center; line-height: 1.2; }
  .btn-sm { min-height: 36px; }
  #main-content .btn, .modal-box .btn { min-height: 40px; }
  .input, select.input, textarea.input,
  .modal-body input, .modal-body select, .modal-body textarea { font-size: 16px; }
  .input { min-height: 44px; }
  textarea.input { min-height: 88px; }

  /* Barras de herramientas: buscadores y selectores a lo ancho */
  .toolbar { gap: 8px; }
  .toolbar > .input,
  .toolbar > select,
  .toolbar > input {
    flex: 1 1 100% !important;
    width: 100% !important;
    max-width: none !important;
  }
  .toolbar > .fn-chips {
    flex: 1 1 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    padding-bottom: 2px;
  }
  .toolbar > .fn-chips::-webkit-scrollbar { display: none; }
  .toolbar > .fn-chips > * { flex-shrink: 0; }

  /* Tablas que no son tarjetas: ancho legible + desplazamiento lateral */
  .table-wrap > table.data-table:not(.rt),
  .modal-body table.data-table:not(.rt) { min-width: 560px; }

  /* Modales: hoja inferior que respeta la barra del navegador y el notch */
  .modal-box {
    max-height: calc(100dvh - 12px - env(safe-area-inset-top));
  }
  .modal-header { padding: 10px 8px 10px 16px; }
  .modal-close { min-width: 44px; min-height: 44px; font-size: 20px; }
  .modal-body { padding: 14px; }
  .modal-footer { padding: 10px 14px calc(10px + env(safe-area-inset-bottom)); }

  /* Editor de elementos */
  .qie-line-row .btn { flex: 1 1 120px; justify-content: center; }
  /* Ancho × Alto lado a lado, cada uno con la mitad */
  .qie-row-dims { flex-direction: row !important; flex-wrap: nowrap !important; align-items: flex-end; }
  .qie-row-dims .field { flex: 1 1 0; min-width: 0; width: auto !important; }
  .qie-row-dims .input { width: 100% !important; }
  .qie-row-dims .qie-dims-x { display: block !important; padding: 0 0 12px !important; align-self: flex-end !important; }
  .qie-calc-col { max-height: none; }

  /* Avisos (toasts): a lo ancho y sobre la barra de gestos */
  .vl-toast {
    left: 12px !important;
    right: 12px !important;
    bottom: calc(12px + env(safe-area-inset-bottom)) !important;
    max-width: none !important;
  }

  /* Gráficos y SVG del panel no se salen de su tarjeta */
  .card svg { max-width: 100%; }
}

@media (max-width: 719px) {
  .qie-grid { grid-template-columns: minmax(0, 1fr); }
}

/* Teléfonos muy angostos */
@media (max-width: 360px) {
  .page-actions { grid-template-columns: 1fr; }
  .p-page { padding-left: 10px; padding-right: 10px; }
}

/* Teléfono horizontal: poca altura */
@media (max-height: 500px) and (orientation: landscape) {
  .modal-overlay { align-items: stretch; }
  .modal-box { max-height: 100dvh; border-radius: 0; }
}

/* Táctil: sin efectos hover que quedan "pegados" */
@media (hover: none) {
  .data-table tbody tr:hover td,
  .cq-grid tr.cq-row:hover td { background: inherit; }
  .stat-card:hover { transform: none; box-shadow: var(--shadow); }
  .btn:hover { opacity: 1; }
}

/* Accesibilidad: menos movimiento si el sistema lo pide */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}

@media print {
  table.rt thead { display: table-header-group !important; }
}
