:root {
  --bg: #0b1220;
  --panel: #121b2e;
  --panel-border: #223154;
  --text: #e8edf7;
  --text-dim: #93a2c2;
  --accent: #3aa7ff;
  --accent-dim: #1c5c8a;
  --danger: #ff4d5e;
  --warn: #ffb020;
  --ok: #2ecc71;
  --radius: 14px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f3f6fc;
    --panel: #ffffff;
    --panel-border: #dde4f2;
    --text: #16213e;
    --text-dim: #5a6785;
  }
}

* { box-sizing: border-box; }

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

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 20px 16px 48px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.app-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 4px 8px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.lang-toggle {
  padding: 6px 10px;
  min-width: 0;
  flex: 0 0 auto;
  font-size: 0.75rem;
}

.title-block h1 {
  margin: 0;
  font-size: 1.5rem;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 4px 0 0;
  color: var(--text-dim);
  font-size: 0.85rem;
}

.badge {
  flex-shrink: 0;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
  border: 1px solid transparent;
}

.badge--idle { background: rgba(147,162,194,0.15); color: var(--text-dim); border-color: var(--panel-border); }
.badge--running { background: rgba(46,204,113,0.15); color: var(--ok); border-color: rgba(46,204,113,0.4); }
.badge--alert { background: rgba(255,77,94,0.18); color: var(--danger); border-color: rgba(255,77,94,0.5); animation: pulse 1s ease-in-out infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}

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

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.btn {
  flex: 1 1 auto;
  min-width: 140px;
  padding: 12px 16px;
  border-radius: 10px;
  border: 1px solid transparent;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.05s ease, opacity 0.15s ease;
}

.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-primary { background: var(--accent); color: #06121f; }
.btn-secondary { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-ghost { background: transparent; color: var(--text-dim); border-color: var(--panel-border); }

.metrics {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
}

.metric {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.metric-label {
  font-size: 0.75rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.metric-value {
  font-size: 1.6rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.metric-value small {
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text-dim);
}

#fall-count-wrap.has-fall { color: var(--danger); }

.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.chart-header h2, .log-panel h2, .about-panel h2 {
  margin: 0;
  font-size: 1rem;
}

.legend {
  display: flex;
  gap: 12px;
  font-size: 0.75rem;
  color: var(--text-dim);
}

.legend-item { display: flex; align-items: center; gap: 5px; }

.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot--signal { background: var(--accent); }
.dot--low { background: var(--warn); }
.dot--high { background: var(--danger); }

canvas#chart {
  width: 100%;
  height: 220px;
  display: block;
  border-radius: 10px;
  background: rgba(58,167,255,0.03);
}

.event-log {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 240px;
  overflow-y: auto;
}

.event-log li {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  background: rgba(147,162,194,0.08);
  font-size: 0.85rem;
}

.event-log li.event-fall {
  background: rgba(255,77,94,0.12);
  color: var(--danger);
  font-weight: 600;
}

.event-log li.event-empty {
  color: var(--text-dim);
  justify-content: center;
}

.event-log time {
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

.about-panel p {
  margin: 0;
  color: var(--text-dim);
  font-size: 0.88rem;
  line-height: 1.55;
}

.app-footer {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.75rem;
  padding-top: 8px;
}

/* -------------------- emparejamiento (QR + MQTT) -------------------- */

.pairing-hint {
  margin: 0 0 14px;
  color: var(--text-dim);
  font-size: 0.85rem;
  line-height: 1.5;
}

#role-prompt {
  padding: 4px 0 14px;
}

.pairing-role-group {
  padding: 14px 0;
}

.pairing-change-link {
  margin-top: 12px;
  padding: 6px 10px;
  font-size: 0.75rem;
  min-width: 0;
}

.pairing-role-group + .pairing-role-group {
  border-top: 1px solid var(--panel-border);
}

.pairing-role-label {
  margin: 0 0 10px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.pairing-join {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.pairing-code-input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: transparent;
  color: var(--text);
  font-size: 1rem;
  letter-spacing: 0.15em;
}

.qr-display {
  margin-top: 14px;
  width: 200px;
  height: 200px;
  max-width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  border-radius: 10px;
  padding: 8px;
}

.qr-display img, .qr-display svg, .qr-display table {
  width: 100%;
  height: 100%;
}

.pair-code-display {
  margin: 10px 0 0;
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.2em;
  font-variant-numeric: tabular-nums;
}

#qr-video {
  width: 100%;
  max-width: 320px;
  border-radius: 10px;
  margin-top: 10px;
}

.pairing-status {
  margin: 0 0 12px;
  font-size: 0.9rem;
  color: var(--text-dim);
}

#role-picker {
  margin-bottom: 12px;
}

/* -------------------- alerta a pantalla completa -------------------- */

#alert-overlay {
  position: fixed;
  inset: 0;
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--danger);
}

#alert-overlay[hidden] {
  display: none;
}

.alert-box {
  text-align: center;
  color: #1a0508;
  padding: 24px;
  animation: pulse 1s ease-in-out infinite;
}

.alert-box h2 {
  margin: 0 0 10px;
  font-size: 2rem;
}

.alert-box p {
  margin: 4px 0;
  font-size: 1rem;
}

.alert-box .btn {
  margin-top: 20px;
  min-width: 220px;
}
