/* Adaptación a notebooks, tablets y celulares. */

/*
  Notebooks. Antes había un solo corte en 1550px que mandaba todo lo más chico
  (o sea, casi todas las notebooks: 1280, 1366, 1440 y 1536 de ancho) al diseño
  de tablet: gráficos de a uno por fila, estirados a 1000px, el selector de año
  ocupando un renglón entero y los KPI en dos filas. En una pantalla de 768px de
  alto, la primera vista mostraba el encabezado y media fila de tarjetas.

  Ahora el diseño se achica por partes, según lo que de verdad no entra:
  - 901–1599px: sidebar más angosta, márgenes y tarjetas más compactos, botones
    de los gráficos solo con ícono, KPI en 3×2. (Probado: con los seis en una
    fila a 1440px los rótulos y los chips se partían en dos renglones.)
  - desde 1200px: gráficos de a dos por fila.
  - hasta 1199px: una columna, como antes en tablet.
  Desde 1600px van los seis KPI en una fila; el tamaño del número lo ajusta
  fitKpiValues() (js/ui/kpi-values.js) para que ninguno se salga de su tarjeta.
*/
@media (max-width: 1599px) {
  .territorial-map-panel .panel-title-row { align-items: flex-start; }
  .kpi-grid { grid-template-columns: repeat(3, 1fr); }

  /* Con 3 columnas, la grilla de KPIs pasa a 2 filas: reacomodar qué
     card lleva cada esquina redondeada. */
  .kpi-grid .card:first-child { border-bottom-left-radius: 0; }
  .kpi-grid .card:last-child { border-top-right-radius: 0; }
  .kpi-grid .card:nth-child(3) { border-top-right-radius: var(--radius); }
  .kpi-grid .card:nth-child(4) { border-bottom-left-radius: var(--radius); }
}

@media (max-width: 1399px) {
  .territorial-command,
  .territorial-analytics-grid { grid-template-columns: 1fr; }
}

@media (max-width: 1199px) {
  .hero-inner { grid-template-columns: 1fr; }
  .hero-controls { min-width: 0; }

  .chart-grid, .analysis-grid { grid-template-columns: 1fr; }
  .territorial-top { grid-template-columns: 1fr; }
  .territorial-kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }

  .territorial-kpis .card:first-child { border-bottom-left-radius: 0; }
  .territorial-kpis .card:last-child { border-top-right-radius: 0; }
  .territorial-kpis .card:nth-child(3) { border-top-right-radius: var(--radius); }
  .territorial-kpis .card:nth-child(4) { border-bottom-left-radius: var(--radius); }
}

@media (min-width: 901px) and (max-width: 1599px) {
  .sidebar { width: 232px; flex: 0 0 232px; padding: 22px 0; }
  .sidebar-brand { padding: 0 18px 18px; gap: 11px; }
  .sidebar .brand-logo { width: 40px; height: 40px; }
  .view-tabs { padding: 14px 10px; }
  .view-tab { padding: 11px 12px; font-size: 13.5px; gap: 10px; white-space: nowrap; }

  .page { padding: 22px 28px 48px; }

  /* Título y bajada arriba; los chips y el botón de imprimir comparten renglón. */
  .hero-inner > div:first-child { display: flex; flex-wrap: wrap; align-items: center; column-gap: 8px; }
  .hero-inner > div:first-child > h1,
  .hero-inner > div:first-child > p { flex: 0 0 100%; }
  .hero h1 { font-size: 24px; }
  .hero-meta,
  .hero .actions { margin-top: 12px; }
  .hero .actions button { padding: 7px 12px; }
  .hero-controls { min-width: 170px; padding: 0; }

  .kpi-grid { margin-top: 16px; }
  .kpi-grid .card,
  .territorial-kpis .card { padding: 14px 16px 13px; }
  .kpi-grid .value,
  .territorial-kpis .value { margin-top: 8px; }
  .kpi-grid .sub,
  .territorial-kpis .sub { margin-top: 8px; font-size: 12px; }

  .chart-grid, .analysis-grid { gap: 16px; margin-top: 16px; }
  .panel { padding: 18px; }
  .panel h2 { font-size: 17px; }
  .section-head { margin-top: 20px; }
  .chart-canvas-box,
  .analysis-grid .chart-canvas-box,
  .territorial-analytics-grid .chart-canvas-box { height: 280px; }

  /* Con dos gráficos por fila, "Imprimir" y "Ampliar" le comían medio renglón al
     título. Quedan solo con ícono (el botón ya tiene aria-label y title). */
  .chart-expand-btn,
  .chart-print-btn { width: 32px; min-width: 32px; padding: 0; justify-content: center; }
  .chart-expand-btn .chart-expand-text,
  .chart-print-btn .chart-print-text { display: none; }
}

@media (min-width: 1200px) {
  .chart-grid, .analysis-grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }

  /* Dos gráficos lado a lado con bajadas de distinto largo: el gráfico va pegado
     abajo para que los dos ejes queden a la misma altura. */
  .chart-grid > .panel,
  .analysis-grid > .panel { display: flex; flex-direction: column; }
  .chart-grid > .panel > .panel-title-row,
  .analysis-grid > .panel > .panel-title-row { margin-bottom: 16px; align-items: flex-start; }
  .chart-grid > .panel > .chart-canvas-box,
  .analysis-grid > .panel > .chart-canvas-box { margin-top: auto; }
}

/* Pantallas bajas (notebooks de 720-800px de alto, menos la barra del navegador). */
@media (min-width: 901px) and (max-height: 820px) {
  .chart-canvas-box,
  .analysis-grid .chart-canvas-box,
  .territorial-analytics-grid .chart-canvas-box { height: 250px; }
}

@media (min-width: 741px) and (max-height: 820px) {
  .chart-modal-overlay { padding: 14px; }
  .chart-modal { max-height: calc(100vh - 28px); padding: 18px; }
  .chart-modal-head { margin-bottom: 8px; }
  .chart-modal h2 { font-size: 19px; }
  .chart-modal-canvas { height: calc(100vh - 170px); min-height: 260px; }
  .chart-modal-details { max-height: calc(100vh - 170px); }
}

@media (max-width: 740px) {
  .page {
    width: 100%;
    padding: 10px 10px 48px;
  }
  .hero {
    padding: 14px;
    border-radius: 12px;
  }
  .hero-inner { gap: 11px; }
  .hero h1 { font-size: 25px; line-height: 1.08; }
  .hero p {
    max-width: 100%;
    font-size: 12.5px;
    line-height: 1.35;
  }
  .hero-meta { grid-template-columns: 1fr 1fr; gap: 7px; }
  .hero-meta-item { padding: 7px 8px; }
  .hero-meta-item span { font-size: 9.5px; }
  .hero-meta-item strong {
    font-size: 11.5px;
    overflow-wrap: anywhere;
  }
  .hero-meta-item:nth-child(2) { order: 3; grid-column: 1 / -1; }
  .hero-meta-item:nth-child(3) { order: 2; }
  .hero .actions { gap: 7px; margin-top: 10px; }
  .hero .actions button,
  .hero .actions .button {
    width: 100%;
    flex: 1 1 100%;
    justify-content: center;
    min-height: 37px;
    padding: 8px 9px;
    font-size: 12px;
  }
  .territorial-map-panel .panel-title-row {
    flex-direction: column;
    align-items: stretch;
  }
  .map-panel-actions {
    justify-items: stretch;
    min-width: 0;
  }
  .map-search {
    width: 100%;
  }
  .map-legend {
    justify-content: flex-start;
  }
  .map-mode-toggle {
    width: 100%;
  }
  .map-mode-toggle button {
    flex: 1 1 0;
  }
  .hero-controls {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
    border-radius: 12px;
    padding: 9px;
  }
  .hero-year-control {
    display: grid;
    grid-template-columns: auto 1fr;
    align-items: center;
    gap: 10px;
  }
  .hero-controls label { margin: 0; font-size: 10.5px; }
  .hero-controls select { padding: 9px 10px; }
  .view-tabs {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 4px;
    padding: 4px;
  }
  .view-tab {
    min-width: 0;
    justify-content: center;
    padding: 9px 6px;
    font-size: 11.5px;
    line-height: 1.15;
    white-space: normal;
  }
  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin-top: 10px;
  }
  .territorial-kpis {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .territorial-form,
  .territorial-preview-grid,
  .automation-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }
  .territorial-kpis { margin-top: 10px; }
  .automation-grid .wide { grid-column: auto; }
  .map-legend { justify-content: flex-start; }
  .territorial-command-side .actions button { width: 100%; }
  .card {
    min-height: 0;
    padding: 12px;
    border-radius: 9px;
  }
  .label {
    font-size: 10px;
    line-height: 1.18;
  }
  .info {
    width: 18px;
    height: 18px;
    font-size: 11px;
  }
  .value {
    margin-top: 10px;
    font-size: clamp(22px, 7vw, 27px);
    white-space: normal;
  }
  .trend {
    white-space: normal;
    align-items: flex-start;
    line-height: 1.15;
    padding: 5px 7px;
  }
  .sub {
    font-size: 11.5px;
    line-height: 1.34;
  }
  .insight {
    grid-template-columns: 1fr;
    margin-top: 12px;
    padding: 14px;
  }
  .insight-badges { align-items: stretch; }
  .badge { text-align: center; }
  .form-grid, .entry-grid, .trace-grid, .mini-grid.two, .mini-grid.three { grid-template-columns: 1fr; }
  .details-body { padding: 14px; }
  .details-body > .actions button { flex: 1 1 100%; }
  .panel-title-row { align-items: start; }
  .panel-tools { gap: 6px; }
  .chart-expand-btn, .chart-print-btn { width: 32px; min-width: 32px; padding: 0; }
  .chart-expand-btn .chart-expand-text, .chart-print-btn .chart-print-text { display: none; }
  .chart-modal-overlay { padding: 10px; align-items: stretch; }
  .chart-modal { max-height: none; height: calc(100vh - 20px); padding: 16px; border-radius: 14px; }
  .chart-modal-head { gap: 12px; }
  .chart-modal h2 { font-size: 18px; }
  .chart-modal-body {
    flex: 1 1 auto;
    grid-template-columns: 1fr;
    grid-template-rows: minmax(300px, 52vh) auto;
    gap: 12px;
    overflow: auto;
  }
  .chart-modal-canvas { min-height: 300px; height: 100%; }
  .chart-canvas-empty { font-size: 12px; }
  .chart-modal-details { max-height: none; overflow: visible; }
  .chart-detail-grid { grid-template-columns: 1fr; }
  .confirm-dialog { padding: 18px; }
  .confirm-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .confirm-actions button { min-width: 0; }
  .section-head {
    display: block;
    margin-top: 18px;
  }
  .section-head h2 { font-size: 19px; }
  .section-head p { font-size: 12px; }
  .panel {
    padding: 15px;
    border-radius: 10px;
  }
  .panel h2 { font-size: 16px; }
  .note { font-size: 12px; }
  .chart-grid,
  .analysis-grid {
    gap: 12px;
    margin-top: 12px;
  }
  canvas { height: 250px; }
  .chart-canvas-box { height: 250px; }
  .data-table-wrap { max-height: 440px; }
  th:first-child, td:first-child,
  th:nth-child(2), td:nth-child(2) {
    position: sticky;
    background: #fff;
    z-index: 2;
  }
  th:first-child, td:first-child { left: 0; min-width: 54px; }
  th:nth-child(2), td:nth-child(2) { left: 54px; min-width: 58px; box-shadow: 8px 0 12px rgba(15,23,42,.06); }
  th:first-child, th:nth-child(2) { background: var(--soft); z-index: 3; }
}

@media (max-width: 360px) {
  .kpi-grid,
  .territorial-kpis,
  .automation-grid {
    grid-template-columns: 1fr;
  }

  /* Una sola columna: solo la primera card redondea arriba y solo la
     última redondea abajo. */
  .kpi-grid .card,
  .territorial-kpis .card {
    border-radius: 0;
  }
  .kpi-grid .card:first-child,
  .territorial-kpis .card:first-child {
    border-top-left-radius: var(--radius);
    border-top-right-radius: var(--radius);
  }
  .kpi-grid .card:last-child,
  .territorial-kpis .card:last-child {
    border-bottom-left-radius: var(--radius);
    border-bottom-right-radius: var(--radius);
  }
  /* Misma especificidad que las reglas nth-child(3)/(4) del breakpoint
     de notebook: sin este reset, esas esquinas quedaban redondeadas
     también acá aunque ya no correspondan a un borde de la grilla. */
  .kpi-grid .card:nth-child(3),
  .territorial-kpis .card:nth-child(3),
  .kpi-grid .card:nth-child(4),
  .territorial-kpis .card:nth-child(4) {
    border-radius: 0;
  }
}
