/* Paleta basada en el sistema de referencia validado (light + dark). */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --series-1: #2a78d6; /* coste total */
  --series-2: #008300; /* coste marginal */
  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #d03b3b;
  --success-text: #006300;
  --accent: #2a78d6;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --series-1: #3987e5;
    --series-2: #008300;
    --success-text: #0ca30c;
    --accent: #3987e5;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--text-primary);
  line-height: 1.5;
}

#app { max-width: 960px; margin: 0 auto; padding: 24px 16px 64px; }

h1 { font-size: 1.6rem; margin: 0 0 4px; }
h2 { font-size: 1.2rem; margin: 0 0 12px; }
p { margin: 0 0 12px; }

a { color: var(--accent); }

.muted { color: var(--text-secondary); }
.small { font-size: 0.85rem; color: var(--text-muted); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 16px;
}

/* ---------- Auth ---------- */

.auth-wrap { max-width: 440px; margin: 6vh auto 0; }
.auth-hero { text-align: center; margin-bottom: 20px; }
.auth-hero h1 { font-size: 1.7rem; }

.tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.tabs button {
  flex: 1;
  padding: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-secondary);
  border-radius: 8px;
  font-size: 0.95rem;
  cursor: pointer;
}
.tabs button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

form label { display: block; font-size: 0.9rem; margin: 12px 0 4px; color: var(--text-secondary); }
form input, form select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--baseline);
  border-radius: 8px;
  background: var(--page);
  color: var(--text-primary);
  font-size: 1rem;
}
form input:focus, form select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn {
  display: inline-block;
  padding: 11px 18px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  margin-top: 16px;
}
.btn:disabled { opacity: 0.6; cursor: wait; }
.btn-secondary {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
}
.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  font-weight: 400;
}

.form-error {
  color: var(--status-critical);
  font-size: 0.9rem;
  margin-top: 10px;
  min-height: 1.2em;
}

/* ---------- Topbar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.topbar .btn { margin-top: 0; }

/* ---------- Dashboard ---------- */

.video-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.video-card { cursor: pointer; transition: border-color 0.15s; }
.video-card:hover { border-color: var(--accent); }
.video-card h3 { margin: 0 0 6px; font-size: 1.05rem; }
/* Los enlaces largos no deben desbordar la tarjeta ni la cabecera. */
.video-link, .topbar h2 { overflow-wrap: anywhere; }
.video-card .metrics { display: flex; gap: 16px; margin-top: 8px; }
.metric-label { font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.metric-value { font-size: 1.15rem; font-weight: 600; }

/* ---------- Detalle vídeo ---------- */

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; }
.tile .metric-value { font-size: 1.5rem; }
.tile .delta { font-size: 0.85rem; margin-top: 2px; }
.delta.up-bad { color: var(--status-critical); }
.delta.down-good { color: var(--success-text); }

.chart-wrap { position: relative; height: 300px; }

/* ---------- Diagnóstico ---------- */

.diagnosis { border-left: 5px solid var(--baseline); }
.diagnosis.excelente, .diagnosis.bien { border-left-color: var(--status-good); }
.diagnosis.atencion { border-left-color: var(--status-warning); }
.diagnosis.critico { border-left-color: var(--status-critical); }

.level-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  margin-bottom: 10px;
}
.level-badge .dot { width: 9px; height: 9px; border-radius: 50%; }
.excelente .dot, .bien .dot { background: var(--status-good); }
.atencion .dot { background: var(--status-warning); }
.critico .dot { background: var(--status-critical); }
.neutro .dot { background: var(--text-muted); }

.cta {
  margin-top: 16px;
  padding: 16px;
  border-radius: 10px;
  background: var(--page);
  border: 1px dashed var(--baseline);
}

/* ---------- Historial ---------- */

table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th, td { text-align: right; padding: 8px 10px; border-bottom: 1px solid var(--grid); font-variant-numeric: tabular-nums; }
th:first-child, td:first-child { text-align: left; }
th { color: var(--text-muted); font-weight: 600; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; }
.table-scroll { overflow-x: auto; }

td .del-entry {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.9rem;
}
td .del-entry:hover { color: var(--status-critical); }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0 16px; }

.back-link { display: inline-block; margin-bottom: 12px; font-size: 0.9rem; text-decoration: none; }

/* ---------- Consentimiento y páginas legales ---------- */

.consent {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  margin: 4px 0 12px;
  font-size: 0.85rem;
  line-height: 1.45;
  color: var(--text-secondary);
  font-weight: 400;
  cursor: pointer;
}
.consent input[type='checkbox'] {
  flex: 0 0 auto;
  width: 18px;
  height: 18px;
  margin: 1px 0 0;
  accent-color: var(--accent);
  cursor: pointer;
}

.legal-links { margin-top: 18px; text-align: center; }
.legal-links a { color: var(--text-muted); }

.legal {
  max-width: 760px;
  margin: 0 auto;
  padding: 32px 20px 64px;
}
.legal h1 { font-size: 1.7rem; margin-bottom: 4px; }
.legal h2 { font-size: 1.1rem; margin: 28px 0 8px; }
.legal p, .legal li { color: var(--text-secondary); line-height: 1.6; }
.legal ul { padding-left: 20px; }
.legal li { margin-bottom: 6px; }
.legal .updated { color: var(--text-muted); font-size: 0.85rem; }

/* Botón de contacto por WhatsApp en el diagnóstico. */
.btn-whatsapp {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: #25d366;
  /* Verde de WhatsApp con texto oscuro: el blanco no da contraste suficiente. */
  color: #06331b;
  text-decoration: none;
}
.btn-whatsapp:hover { background: #1eb959; }
.btn-whatsapp svg { flex: 0 0 auto; }

/* ---------- Identidad: foto de Instagram + usuario ---------- */

.identidad { display: flex; align-items: center; gap: 12px; min-width: 0; }

/* La imagen se superpone a las iniciales: si no carga se elimina del DOM y las
   iniciales quedan visibles sin ningún hueco intermedio. */
.avatar {
  position: relative;
  flex: 0 0 auto;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  overflow: hidden;
  background: var(--grid);
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}
.avatar-inicial {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text-secondary);
  letter-spacing: 0.02em;
}
.avatar img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Enlace del vídeo bajo su título, cuando el usuario le puso uno. */
.video-url-sub { color: var(--text-muted); margin-top: -6px; overflow-wrap: anywhere; }

.rename-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.9rem;
  padding: 0 4px;
  vertical-align: middle;
}
.rename-btn:hover { color: var(--accent); }
