/*
  TOTAAL – estilos base (mobile-first)
  - Paleta neutra y tipografía simple (sin "color palette" agresiva).
  - Componentes: header, drawer, cards, modal y controles del historial.
*/

:root{
  --bg: #f6f8f4;
  --card: #f8fbf7;
  --text: #1d1f1b;
  --muted: rgba(0,0,0,.55);
  --border: rgba(0,0,0,.10);
  --shadow: 0 8px 18px rgba(0,0,0,.06);
  --accent: #2AA84A;
  --accent-rgb: 42, 168, 74;

  /* Barra de nivel (verde -> amarillo -> naranja -> rojo) */
  --g1:#2AA84A;
  --g2:#E9D436;
  --g3:#EB8C2E;
  --g4:#D9534F;
}

*{ box-sizing: border-box; }

/* Elimina el delay de 300ms y el zoom por doble-tap en todos los botones */
button{ touch-action: manipulation; }

body{
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app{
  max-width: 520px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Header — safe area para notch/Dynamic Island */
header{
  height: calc(88px + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  display: flex;
  align-items: center;
  justify-content: center;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 10;
}

.header-inner{
  width: 100%;
  padding: 10px 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo{
  flex: 1;
  min-width: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 56px;
}

.logo img{
  height: 56px;
  width: auto;
  max-width: 100%;
  display: block;
}

/* Contenedor de botones derechos (tema + idioma) */
.header-actions{
  display: flex;
  gap: 6px;
  align-items: center;
  flex-shrink: 0;
}

/* Botón idioma */
.lang-btn{
  display: inline-flex;
  gap: 8px;
  align-items: center;
  padding: 10px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,.65);
  cursor: pointer;
  user-select: none;
  font-weight: 600;
  color: rgba(0,0,0,.75);
}
.lang-btn:hover{ background: rgba(255,255,255,.9); }
.lang-btn:active{ opacity: .65; }
.lang-btn svg{ width: 18px; height: 18px; opacity: .85; }

/* Botón tema (luna/sol) */
.theme-btn{
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,.65);
  cursor: pointer;
  user-select: none;
}
.theme-btn:hover{ background: rgba(255,255,255,.9); }
.theme-btn:active{ opacity: .65; }
.theme-btn svg{ width: 18px; height: 18px; opacity: .85; }

/* Toggle de iconos luna/sol según la clase html.dark */
.icon-sun{ display: none; }
html.dark .icon-moon{ display: none; }
html.dark .icon-sun{ display: block; }

/* Botón menú */
.menu-btn{
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,.65);
  cursor: pointer;
  user-select: none;
}
.menu-btn:hover{ background: rgba(255,255,255,.9); }
.menu-btn:active{ opacity: .65; }
.menu-btn svg{ width: 20px; height: 20px; opacity: .85; }

/* Drawer */
.drawer-overlay{
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.25);
  display: none;
  z-index: 200; /* debajo del modal */
}
.drawer-overlay.show{ display: block; }

.drawer{
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  width: min(320px, 86vw);
  background: #ffffff;
  border-right: 1px solid var(--border);
  box-shadow: 14px 0 40px rgba(0,0,0,.12);
  transform: translateX(-105%);
  transition: transform 180ms ease;
  z-index: 201;
  display: flex;
  flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
}
.drawer.open{ transform: translateX(0); }

/* Drawer header — misma altura que el app header, incluyendo safe area */
.drawer-header{
  height: calc(88px + env(safe-area-inset-top));
  padding-top: calc(12px + env(safe-area-inset-top));
  display: flex;
  align-items: center;
  padding-left: 14px;
  padding-right: 14px;
  padding-bottom: 12px;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.drawer-logo{
  height: 46px;
  width: auto;
  display: block;
}

.drawer-logo-fallback{
  font-weight: 900;
  letter-spacing: 2px;
  color: rgba(0,0,0,.70);
  font-size: 18px;
  display: none;
}

.drawer-nav{
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.nav-item{
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 12px;
  border-radius: 12px;
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  text-align: left;
  font-weight: 800;
  color: rgba(0,0,0,.75);
}
.nav-item:hover{
  background: rgba(0,0,0,.04);
  border-color: var(--border);
}
.nav-item:active:not(:disabled):not([aria-disabled="true"]){ opacity: .65; }
.nav-item.active{
  background: rgba(var(--accent-rgb), .10);
  border-color: rgba(var(--accent-rgb), .25);
  color: rgba(0,0,0,.82);
}

/* Estado deshabilitado: "feature en desarrollo" */
.nav-item:disabled,
.nav-item[aria-disabled="true"]{
  opacity: .45;
  cursor: not-allowed;
  background: transparent;
  border-color: transparent;
}
.nav-item:disabled:hover,
.nav-item[aria-disabled="true"]:hover{
  background: transparent;
  border-color: transparent;
}

.nav-item svg{ width: 18px; height: 18px; opacity: .85; }

.drawer-divider{
  height: 1px;
  background: var(--border);
  margin: 6px 12px;
}

.drawer-section-label{
  padding: 8px 14px 4px;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
}

.drawer-stations{
  padding: 0 10px 4px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.station-badge{
  margin-left: auto;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  background: rgba(0,0,0,.06);
  padding: 2px 8px;
  border-radius: 999px;
  white-space: nowrap;
}

.drawer-footer{
  margin-top: auto;
  padding: 12px 12px 16px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
}

/* Main — safe area para barra de inicio de iOS */
main{
  padding: 14px 16px calc(28px + env(safe-area-inset-bottom));
  flex: 1;
}

.last-update{
  font-size: 20px;
  font-weight: 800;
  letter-spacing: .2px;
  margin: 6px 0 14px;
}
.last-update span{
  font-weight: 500;
  color: rgba(0,0,0,.72);
  margin-left: 6px;
}

/* Cards */
.card{
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 16px;
  margin: 12px 0;
}
.card-top{
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 12px;
}
.card-title{
  font-size: 20px;
  font-weight: 700;
  line-height: 1.2;
}

.icon{
  width: 22px;
  height: 22px;
  opacity: .9;
  flex: 0 0 auto;
}

.bar{
  height: 14px;
  border-radius: 10px;
  overflow: hidden;
  position: relative;
  border: 1px solid var(--border);
  background: linear-gradient(90deg, var(--g1), var(--g2), var(--g3), var(--g4));
}
.bar .mask{
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: 50%;
  background: rgba(255,255,255,.82);
}

.level-row{
  margin-top: 10px;
  font-size: 16px;
  display: flex;
  align-items: baseline;
  gap: 6px;
}
.level-row .label{
  color: var(--muted);
  font-weight: 600;
}
.level-row .value{
  font-weight: 700;
  color: rgba(0,0,0,.80);
}

/* Recomendaciones */
.section-title{
  margin: 18px 0 8px;
  font-size: 20px;
  font-weight: 800;
}
.section-text{
  color: rgba(0,0,0,.70);
  line-height: 1.45;
  font-size: 16px;
  margin: 0 0 10px;
}

.link-btn{
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid transparent;
  background: transparent;
  color: rgba(0,0,0,.70);
  font-weight: 700;
  cursor: pointer;
}
.link-btn:hover{
  background: rgba(0,0,0,.03);
  border-color: var(--border);
}
.link-btn:active{ opacity: .65; }

/* Error box */
.error-box{
  margin-top: 14px;
  padding: 16px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,.55);
  display: flex;
  gap: 12px;
  align-items: flex-start;
}
.error-title{
  font-weight: 900;
  margin-bottom: 6px;
}
.error-msg{
  color: rgba(0,0,0,.70);
}
.primary-btn{
  margin-top: 10px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(var(--accent-rgb), .12);
  color: rgba(0,0,0,.75);
  font-weight: 800;
  cursor: pointer;
}
.primary-btn:hover{ background: rgba(var(--accent-rgb), .18); }
.primary-btn:active{ opacity: .65; }

/* Loading */
.spinner{
  width: 34px; height: 34px;
  border-radius: 50%;
  border: 3px solid rgba(0,0,0,.12);
  border-top-color: rgba(0,0,0,.45);
  animation: spin 1s linear infinite;
  margin: 24px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Modal */
.overlay{
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.35);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;
  z-index: 999;
}
.overlay.show{ display: flex; }

.modal{
  width: min(420px, 100%);
  background: #eef2ea;
  border-radius: 18px;
  border: 1px solid rgba(0,0,0,.12);
  box-shadow: 0 20px 50px rgba(0,0,0,.18);
  padding: 18px 18px 14px;
}
.modal-title{
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 22px;
  font-weight: 900;
  margin: 2px 0 10px;
}
.modal-title svg{ width: 22px; height: 22px; }

.modal-text{
  color: rgba(0,0,0,.75);
  line-height: 1.45;
  font-size: 16px;
  margin: 0 0 14px;
  text-align: justify;
}

.modal-actions{
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
.modal-ok{
  border: none;
  background: transparent;
  color: rgba(var(--accent-rgb), .95);
  font-weight: 900;
  cursor: pointer;
  padding: 10px 12px;
  border-radius: 10px;
}
.modal-ok:hover{
  background: rgba(var(--accent-rgb), .10);
}
.modal-ok:active{ opacity: .65; }

/* Historial – controles de intervalo (columna) */
.history-controls{
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: stretch;
  margin: 10px 0 14px;
}
.hc-row{
  display: flex;
  gap: 10px;
  align-items: center;
}
.history-controls label{
  font-weight: 800;
  color: rgba(0,0,0,.72);
  font-size: 14px;
  white-space: nowrap;
  width: 70px;
  flex: 0 0 auto;
}
.history-controls input[type="date"]{
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border-radius: 14px;
  border: 1px solid rgba(0,0,0,.10);
  background: #fff;
  /* 16px mínimo para evitar zoom automático en iOS Safari */
  font-size: 16px;
}
.history-controls button{
  width: 100%;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(0,0,0,.02);
  cursor: pointer;
  font-weight: 800;
  color: rgba(0,0,0,.72);
  white-space: nowrap;
}
.history-controls button:hover{
  background: rgba(0,0,0,.05);
}
.history-controls button:active{ opacity: .65; }

.empty-note{
  color: rgba(0,0,0,.70);
  line-height: 1.45;
  font-size: 16px;
  margin: 0;
}

/* Espaciador */
.spacer{ height: 18px; }

/* Fallback si el logo no carga */
.logo-fallback{
  font-weight: 900;
  letter-spacing: 2px;
  font-size: 28px;
  color: rgba(0,0,0,.65);
  display: none;
}


/* Historial: paginación */
.history-pager{
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 14px 0 4px;
}

.history-pager button{
  border: 1px solid rgba(0,0,0,.14);
  background: #fff;
  color: rgba(0,0,0,.86);
  padding: 10px 12px;
  border-radius: 12px;
  font-weight: 800;
  cursor: pointer;
}

.history-pager button:hover{
  background: rgba(0,0,0,.03);
}

.history-pager button:active:not(:disabled){ opacity: .65; }

.history-pager button:disabled{
  opacity: .45;
  cursor: not-allowed;
}

.history-page-indicator{
  font-weight: 800;
  color: rgba(0,0,0,.65);
  font-size: 13px;
  text-align: center;
  flex: 1;
}

/* Historial: filas de métricas */
.history-date{
  font-weight: 900;
  font-size: 18px;
  margin-bottom: 10px;
}

.history-metrics{
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-metric-header{
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}

.history-metric-name{
  font-weight: 800;
}

/* =====================
   Modo oscuro manual
   Se activa con la clase html.dark (botón en el header).
   El script inline en <head> la aplica antes del primer render
   para evitar flash de color incorrecto.
   ===================== */
html.dark{
  --bg: #161a14;
  --card: #1e2219;
  --text: #e8ede5;
  --muted: rgba(255,255,255,.50);
  --border: rgba(255,255,255,.12);
  --shadow: 0 8px 18px rgba(0,0,0,.35);
}

html.dark .drawer{ background: #1e2219; }

html.dark .lang-btn,
html.dark .menu-btn,
html.dark .theme-btn{
  background: rgba(255,255,255,.08);
  color: rgba(255,255,255,.85);
}
html.dark .lang-btn:hover,
html.dark .menu-btn:hover,
html.dark .theme-btn:hover{ background: rgba(255,255,255,.13); }

html.dark .nav-item{ color: rgba(255,255,255,.80); }
html.dark .nav-item:hover{ background: rgba(255,255,255,.06); }

html.dark .logo-fallback,
html.dark .drawer-logo-fallback{ color: rgba(255,255,255,.75); }

/* La máscara blanca de la barra de nivel pasa a oscura */
html.dark .bar .mask{ background: rgba(0,0,0,.72); }

html.dark .last-update span{ color: rgba(255,255,255,.65); }
html.dark .level-row .value{ color: rgba(255,255,255,.85); }

html.dark .section-text,
html.dark .empty-note{ color: rgba(255,255,255,.65); }

html.dark .link-btn{ color: rgba(255,255,255,.65); }
html.dark .link-btn:hover{ background: rgba(255,255,255,.05); }

html.dark .error-box{ background: rgba(255,255,255,.05); }

html.dark .modal{ background: #1e2a1f; }
html.dark .modal-text{ color: rgba(255,255,255,.75); }

html.dark .history-controls input[type="date"]{
  background: #2a2f26;
  color: var(--text);
  border-color: var(--border);
  color-scheme: dark;
}
html.dark .history-controls label{ color: rgba(255,255,255,.70); }
html.dark .history-controls button{
  background: rgba(255,255,255,.04);
  color: rgba(255,255,255,.72);
}
html.dark .history-controls button:hover{ background: rgba(255,255,255,.08); }

html.dark .history-pager button{
  background: #2a2f26;
  color: rgba(255,255,255,.85);
  border-color: rgba(255,255,255,.14);
}
html.dark .history-pager button:hover{ background: rgba(255,255,255,.06); }

html.dark .history-page-indicator{ color: rgba(255,255,255,.60); }

html.dark .spinner{
  border-color: rgba(255,255,255,.12);
  border-top-color: rgba(255,255,255,.50);
}

html.dark .station-badge{ background: rgba(255,255,255,.10); }

html.dark .error-msg{ color: rgba(255,255,255,.65); }
html.dark .primary-btn{ color: rgba(255,255,255,.80); }
