:root {
  --bg: #F3F5F9;
  --surface: #FFFFFF;
  --ink: #0E1A33;
  --muted: #53607A;
  --line: #D9DFEA;
  --brand: #1740C9;
  --band: #1740C9;
  --fresh: #E4EBFF;
  --ok: #12805C;
  --late: #9A5B00;
  --late-fill: #F5A524;
  --alta: #B42318;
  --media: #9A5B00;
  --baja: #53607A;
  --f-display: 'Unbounded', 'Segoe UI', system-ui, sans-serif;
  --f-body: 'Onest', 'Segoe UI', system-ui, sans-serif;
  color-scheme: light dark;
  box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

html {
  scroll-padding-top: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0D1426;
    --surface: #152039;
    --ink: #E8EDF8;
    --muted: #9AA7C2;
    --line: #2A3553;
    --brand: #8FA6FF;
    --fresh: #23325E;
    --ok: #3DD598;
    --late: #F5B74A;
    --alta: #FF7A70;
    --media: #F5B74A;
    --baja: #9AA7C2;
  }
}

:root[data-theme="dark"] {
  --bg: #0D1426;
  --surface: #152039;
  --ink: #E8EDF8;
  --muted: #9AA7C2;
  --line: #2A3553;
  --brand: #8FA6FF;
  --fresh: #23325E;
  --ok: #3DD598;
  --late: #F5B74A;
  --alta: #FF7A70;
  --media: #F5B74A;
  --baja: #9AA7C2;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--f-body);
  line-height: 1.5;
}

.wrap {
  max-width: 1160px;
  margin: 0 auto;
  padding: 24px 16px 64px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

:focus-visible {
  outline: 3px solid var(--brand);
  outline-offset: 2px;
}

h1, h2 {
  margin: 0;
}

/* ==========================================================================
   Cabecera
   ========================================================================== */
.top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 20px;
  margin-bottom: 18px;
}

.brand h1 {
  font-family: var(--f-display);
  font-weight: 600;
  font-size: 1.35rem;
  letter-spacing: .02em;
}

.brand p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: .95rem;
  text-transform: capitalize;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: .9rem;
}

.chip .dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--muted);
}

.chip.ok .dot {
  background: var(--ok);
}

.chip.off {
  border-color: var(--alta);
  color: var(--alta);
}

.chip.off .dot {
  background: var(--alta);
}

.chip.demo .dot {
  background: var(--late-fill);
}

/* ==========================================================================
   Última lectura: el momento central del panel
   ========================================================================== */
.last {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 22px 24px;
  border-radius: 14px;
  background: var(--band);
  color: #fff;
}

.pulse {
  position: relative;
  flex: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
}

.pulse.go::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid #fff;
  animation: ring 1.1s ease-out 1;
}

@keyframes ring {
  from {
    transform: scale(1);
    opacity: .9;
  }
  to {
    transform: scale(3.2);
    opacity: 0;
  }
}

.last-name {
  font-family: var(--f-display);
  font-weight: 600;
  font-size: clamp(1.25rem, 3.6vw, 2.2rem);
  line-height: 1.15;
  overflow-wrap: anywhere;
}

.last-meta {
  margin: 6px 0 0;
  font-size: 1rem;
  opacity: .92;
}

/* ==========================================================================
   Indicadores (KPIs)
   ========================================================================== */
.kpis {
  display: flex;
  flex-wrap: wrap;
  margin: 22px 0;
}

.kpi {
  flex: 1 1 200px;
  padding: 4px 22px;
  border-left: 1px solid var(--line);
}

.kpi:first-child {
  border-left: 0;
  padding-left: 0;
}

.kpi .v {
  font-family: var(--f-display);
  font-weight: 600;
  font-size: 2rem;
  line-height: 1.2;
}

.kpi .l {
  font-weight: 500;
}

.kpi .s {
  color: var(--muted);
  font-size: .9rem;
}

/* ==========================================================================
   Paneles y Distribución
   ========================================================================== */
.cols {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 20px;
  align-items: start;
}

.stack {
  display: grid;
  gap: 20px;
  min-width: 0;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 18px;
}

.panel h2 {
  font-size: 1.05rem;
  font-weight: 600;
}

.panel-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.hint {
  margin: 2px 0 14px;
  color: var(--muted);
  font-size: .9rem;
}

/* ==========================================================================
   Gráfica
   ========================================================================== */
.chart {
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  gap: 3px;
  align-items: end;
  height: 130px;
}

.bar {
  background: var(--brand);
  border-radius: 3px 3px 0 0;
  min-height: 3px;
  transition: height .35s ease;
}

.bar.late {
  background: var(--late-fill);
}

.bar.zero {
  background: var(--line);
}

.axis {
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  gap: 3px;
  margin-top: 6px;
  font-size: .78rem;
  color: var(--muted);
}

.axis span {
  white-space: nowrap;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 18px;
  margin-top: 12px;
  font-size: .88rem;
  color: var(--muted);
}

.legend i {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 3px;
  margin-right: 6px;
  vertical-align: -1px;
}

/* ==========================================================================
   Lecturas en vivo
   ========================================================================== */
.search {
  font: inherit;
  min-height: 38px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: inherit;
  min-width: 0;
  flex: 0 1 240px;
}

.feed {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 460px;
  overflow-y: auto;
  border-top: 1px solid var(--line);
}

.row {
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 10px 4px;
  border-bottom: 1px solid var(--line);
}

.row.fresh {
  animation: fresh 2.2s ease-out 1;
}

@keyframes fresh {
  from {
    background: var(--fresh);
  }
  to {
    background: transparent;
  }
}

.row time {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.row .who {
  overflow-wrap: anywhere;
}

.row .who small {
  display: block;
  color: var(--muted);
  font-size: .85rem;
}

.badge {
  font-size: .85rem;
  font-weight: 600;
  padding: 2px 10px;
  border-radius: 999px;
  border: 1px solid currentColor;
  white-space: nowrap;
}

.badge.ok {
  color: var(--ok);
}

.badge.late {
  color: var(--late);
}

.badge.rep {
  color: var(--muted);
}

.badge.out {
  color: var(--brand);
}

.empty {
  padding: 24px 4px;
  color: var(--muted);
}

/* ==========================================================================
   Alertas de la IA
   ========================================================================== */
.alerts {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 14px;
}

.alert {
  padding: 2px 0 2px 14px;
  border-left: 4px solid var(--c);
}

.alert.alta {
  --c: var(--alta);
}

.alert.media {
  --c: var(--media);
}

.alert.baja {
  --c: var(--baja);
}

.alert .sev {
  font-weight: 600;
  color: var(--c);
  font-size: .88rem;
}

.alert h3 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.alert .who {
  margin: 0;
  color: var(--muted);
  font-size: .9rem;
}

.alert p.desc {
  margin: 6px 0;
  font-size: .94rem;
}

.alert .foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  color: var(--muted);
  font-size: .86rem;
}

button {
  font: inherit;
  cursor: pointer;
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
}

button:hover {
  border-color: var(--muted);
}

.msg {
  min-height: 1.4em;
  margin: 10px 0 0;
  color: var(--alta);
  font-size: .9rem;
}

/* ==========================================================================
   Responsive & Accesibilidad
   ========================================================================== */
@media (max-width: 900px) {
  .cols {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (max-width: 560px) {
  .kpi {
    flex-basis: 45%;
    border-left: 0;
    padding: 8px 0;
  }
  .last {
    padding: 18px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}

/* ==========================================================================
   Pantalla de Login & Sesión
   ========================================================================== */
.login-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: radial-gradient(circle at 50% 20%, rgba(23, 64, 201, 0.08), transparent 70%), var(--bg);
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 36px 32px;
  box-shadow: 0 12px 40px rgba(14, 26, 51, 0.08);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.login-header {
  text-align: center;
  margin-bottom: 28px;
}

.login-logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 52px;
  height: 52px;
  border-radius: 14px;
  background: var(--band);
  color: #fff;
  font-size: 24px;
  margin-bottom: 14px;
  box-shadow: 0 4px 14px rgba(23, 64, 201, 0.35);
}

.login-header h1 {
  font-family: var(--f-display);
  font-size: 1.6rem;
  font-weight: 600;
  letter-spacing: .02em;
  margin: 0;
  color: var(--ink);
}

.login-header p {
  color: var(--muted);
  font-size: 0.92rem;
  margin: 6px 0 0;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group label {
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--ink);
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.form-input {
  width: 100%;
  height: 44px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--ink);
  font-family: inherit;
  font-size: 0.95rem;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.form-input:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(23, 64, 201, 0.15);
}

.form-input::placeholder {
  color: var(--muted);
  opacity: 0.7;
}

.toggle-password {
  position: absolute;
  right: 8px;
  background: none;
  border: none;
  min-height: auto;
  padding: 6px;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.toggle-password:hover {
  color: var(--ink);
  border: none;
}

.btn-primary {
  width: 100%;
  height: 46px;
  margin-top: 6px;
  border: none;
  border-radius: 10px;
  background: var(--brand);
  color: #fff;
  font-weight: 600;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: opacity 0.15s ease, transform 0.1s ease;
  box-shadow: 0 4px 14px rgba(23, 64, 201, 0.25);
}

.btn-primary:hover:not(:disabled) {
  opacity: 0.92;
  transform: translateY(-1px);
}

.btn-primary:active:not(:disabled) {
  transform: translateY(0);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.login-error {
  display: none;
  padding: 10px 14px;
  border-radius: 10px;
  background: rgba(180, 35, 24, 0.08);
  border: 1px solid rgba(180, 35, 24, 0.2);
  color: var(--alta);
  font-size: 0.88rem;
  line-height: 1.4;
  align-items: center;
  gap: 8px;
}

.login-error.show {
  display: flex;
}

.login-footer {
  text-align: center;
  margin-top: 24px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.8rem;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.user-chip {
  font-weight: 500;
  color: var(--ink);
  background: var(--fresh);
  border-color: rgba(23, 64, 201, 0.2);
}

.btn-logout {
  min-height: 32px;
  padding: 0 12px;
  font-size: 0.85rem;
  color: var(--alta);
  border-color: rgba(180, 35, 24, 0.25);
  background: transparent;
  transition: all 0.15s ease;
}

.btn-logout:hover {
  background: rgba(180, 35, 24, 0.08);
  border-color: var(--alta);
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}

.shake {
  animation: shake 0.4s ease;
}

