:root {
  --bg: #3d363e;          /* fondo Bicifree */
  --bg-deep: #2f2930;
  --panel: #463f48;
  --panel-2: #524954;
  --line: rgba(255, 255, 255, 0.09);
  --text: #ffffff;
  --muted: #bdb4bf;
  --brand: #ffc20e;       /* amarillo Bicifree */
  --brand-dark: #e0a800;
  --ink: #2b252c;         /* texto sobre amarillo */
  --ok: #2fbf71;
  --danger: #f0453a;
  --radius: 14px;
  --z1: #8b8592; --z2: #3d9be9; --z3: #2fbf71; --z4: #ffc20e;
  --z5: #ff8c1a; --z6: #f0453a; --z7: #a855f7;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Poppins", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.82rem; }

/* ---------- Barra superior ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 18px;
  padding: 10px 20px;
  background: var(--bg-deep);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { height: 46px; width: auto; border-radius: 8px; }
.brand-text { display: flex; align-items: baseline; font-weight: 800; font-size: 1.45rem; letter-spacing: -0.5px; line-height: 1; }
.brand-text .y { color: var(--brand); }
.brand-text small { margin-left: 8px; font-size: 0.62rem; font-weight: 600; letter-spacing: 2.5px; color: var(--muted); }

.tabs { display: flex; gap: 4px; background: var(--panel); padding: 4px; border-radius: 999px; }
.tabs button {
  border: 0; background: transparent; padding: 7px 18px; border-radius: 999px;
  font-weight: 600; font-size: 0.92rem; cursor: pointer; color: var(--muted);
}
.tabs button.active { background: var(--brand); color: var(--ink); }

.devices { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.dev {
  display: flex; align-items: center; gap: 7px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 999px; padding: 6px 14px 6px 10px; cursor: pointer;
  font-size: 0.86rem; font-weight: 500;
}
.dev svg { width: 18px; height: 18px; }
.dev .dot { width: 9px; height: 9px; border-radius: 50%; background: #776e79; }
.dev.connecting .dot { background: var(--brand); animation: pulse 1s infinite; }
.dev.connected { border-color: rgba(255, 194, 14, 0.55); }
.dev.connected .dot { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.dev.lost .dot { background: var(--danger); }
.dev .val { color: var(--brand); font-weight: 700; }
@keyframes pulse { 50% { opacity: 0.3; } }

.icon-btn { background: var(--panel); border: 1px solid var(--line); border-radius: 50%; width: 38px; height: 38px; display: grid; place-items: center; cursor: pointer; }

.banner { background: var(--brand); color: var(--ink); padding: 10px 20px; font-size: 0.9rem; }

main { padding: 20px; max-width: 1500px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }

/* ---------- Botones ---------- */
.btn {
  border: 0; border-radius: 10px; padding: 10px 18px; font-weight: 600; cursor: pointer;
  background: var(--panel-2); transition: filter 0.15s, transform 0.05s;
}
.btn:hover { filter: brightness(1.12); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--brand); color: var(--ink); }
.btn.ghost { background: transparent; border: 1px solid var(--line); }
.btn.danger { background: transparent; border: 1px solid var(--danger); color: #ff8a80; }
.btn.sm { padding: 6px 12px; font-size: 0.85rem; }

/* ---------- Biblioteca ---------- */
.lib-hero { display: flex; gap: 16px; margin-bottom: 18px; }
.dropzone {
  flex: 1; display: flex; align-items: center; gap: 16px;
  border: 2px dashed rgba(255, 194, 14, 0.5); border-radius: var(--radius);
  padding: 22px 26px; cursor: pointer; background: rgba(255, 194, 14, 0.04);
  transition: background 0.2s, border-color 0.2s;
}
.dropzone:hover, .dropzone.over { background: rgba(255, 194, 14, 0.12); border-color: var(--brand); }
.dropzone svg { width: 38px; height: 38px; color: var(--brand); flex: none; }
.dropzone b { display: block; font-size: 1.1rem; }
.dropzone span { color: var(--muted); font-size: 0.9rem; }
.lib-side { display: flex; flex-direction: column; gap: 8px; justify-content: center; min-width: 180px; }
.ftp-chip { background: var(--panel); border-radius: 10px; padding: 10px 14px; text-align: center; color: var(--muted); font-weight: 500; }
.ftp-chip b { color: var(--brand); font-size: 1.3rem; }

.lib-toolbar { display: flex; gap: 10px; margin-bottom: 14px; }
.lib-toolbar input, .lib-toolbar select, .form-grid input, .form-grid select {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; outline: none;
}
.lib-toolbar input { flex: 1; max-width: 420px; }
.lib-toolbar input:focus, .form-grid input:focus { border-color: var(--brand); }
select option { background: var(--panel); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 14px; }
.card {
  background: var(--panel); border-radius: var(--radius); padding: 14px; border: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 10px; position: relative; transition: border-color 0.15s, transform 0.15s;
}
.card:hover { border-color: rgba(255, 194, 14, 0.5); transform: translateY(-2px); }
.card h4 { margin: 0; font-size: 1rem; line-height: 1.3; padding-right: 26px; }
.card .author { font-size: 0.78rem; color: var(--muted); }
.card canvas { width: 100%; height: 70px; background: var(--bg-deep); border-radius: 8px; }
.card .stats { display: flex; gap: 14px; font-size: 0.82rem; color: var(--muted); }
.card .stats b { color: var(--text); font-weight: 600; }
.card .desc { font-size: 0.8rem; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; margin: 0; }
.card .actions { display: flex; gap: 8px; margin-top: auto; }
.card .actions .btn { flex: 1; }
.card .del { position: absolute; top: 10px; right: 10px; background: none; border: 0; color: var(--muted); cursor: pointer; padding: 4px; border-radius: 6px; }
.card .del:hover { color: var(--danger); background: rgba(240, 69, 58, 0.12); }
.card .del svg { width: 16px; height: 16px; }
.tag { display: inline-block; font-size: 0.7rem; background: var(--panel-2); border-radius: 6px; padding: 2px 7px; margin-right: 4px; color: var(--muted); }

.empty { color: var(--muted); text-align: center; padding: 40px 10px; }
.empty.big { padding: 90px 10px; font-size: 1.1rem; }

/* ---------- Entrenar ---------- */
.ride-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; gap: 10px; }
.ride-head h2 { margin: 0; font-size: 1.35rem; }
.pill { background: var(--panel); border-radius: 999px; padding: 6px 14px; font-weight: 600; font-size: 0.85rem; white-space: nowrap; }
.pill.run { background: var(--ok); color: #0d2a19; }
.pill.pause { background: var(--brand); color: var(--ink); }

.metrics { display: grid; grid-template-columns: 1.35fr 1.35fr 1fr 1fr 1fr 1fr; gap: 12px; }
.tile {
  background: var(--panel); border-radius: var(--radius); padding: 12px 16px 14px;
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 6px; min-width: 0;
  border: 1px solid var(--line);
}
.t-lbl { width: 100%; font-size: 0.85rem; letter-spacing: 0.3px; color: var(--muted); font-weight: 600; }
.t-val { font-size: 2.6rem; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.t-unit { color: var(--muted); font-weight: 500; }
.t-sub { width: 100%; font-size: 0.8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile.power .t-val { font-size: 3.6rem; color: var(--brand); }
.tile.target { border-left: 6px solid var(--z4); }
.tile.target .t-val { font-size: 3.6rem; }
.tile.hr .t-val { color: #ff8a80; }
.tile.power.low .t-val { color: #ffd97a; }
.tile.power.high .t-val { color: #ff8a80; }

.interval-bar { margin: 12px 0; }
.ib-progress { height: 8px; background: var(--panel); border-radius: 99px; overflow: hidden; }
#ibFill { height: 100%; width: 0; background: var(--brand); transition: width 0.25s linear; }
.ib-text { display: flex; justify-content: space-between; font-size: 0.88rem; margin-top: 6px; gap: 10px; }

.chart-wrap { position: relative; background: var(--bg-deep); border-radius: var(--radius); border: 1px solid var(--line); height: clamp(220px, 34vh, 420px); }
.chart-wrap + .chart-wrap { margin-top: 12px; }
.chart-wrap canvas { width: 100%; height: 100%; display: block; }
.toast {
  position: absolute; top: 16px; left: 50%; transform: translateX(-50%);
  background: var(--brand); color: var(--ink); font-weight: 600; padding: 10px 20px; border-radius: 12px;
  max-width: 80%; text-align: center; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}
.start-hint {
  position: absolute; inset: 0; display: grid; place-items: center; font-size: 1.6rem; font-weight: 700;
  color: var(--brand); background: rgba(47, 41, 48, 0.55); border-radius: var(--radius); pointer-events: none;
}

.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 14px; margin-top: 14px; }
.group { display: flex; align-items: center; gap: 8px; }
.group.right { margin-left: auto; }
.ctl {
  width: 48px; height: 48px; border-radius: 50%; border: 1px solid var(--line); background: var(--panel);
  display: grid; place-items: center; cursor: pointer; font-size: 1.4rem; font-weight: 600;
}
.ctl:hover { border-color: var(--brand); }
.ctl.play { width: 62px; height: 62px; background: var(--brand); color: var(--ink); border: 0; }
.ctl.play svg { fill: var(--ink); stroke: none; width: 26px; height: 26px; }
.ctl.sm { width: 40px; height: 40px; }
.toggle {
  border: 2px solid var(--line); background: var(--panel); border-radius: 12px; padding: 10px 18px;
  font-weight: 800; letter-spacing: 1px; cursor: pointer; color: var(--muted);
}
.toggle.on { border-color: var(--brand); color: var(--brand); background: rgba(255, 194, 14, 0.1); }
/* Selector de estilo de música: píldora unida al botón de música, distinta del botón ERG */
/* El sonido ocupa el hueco de la derecha, separado de pantalla completa y Finalizar */
.audio-btns { margin-left: auto; padding-right: 18px; margin-right: 4px; border-right: 1px solid var(--line); }
.audio-btns + .group.right { margin-left: 0; }
.music-seg {
  display: inline-flex; align-items: center; gap: 2px; padding: 3px;
  border: 1px solid var(--line); border-radius: 999px; background: var(--panel);
}
.music-seg button {
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  font: inherit; font-size: 0.78rem; font-weight: 600; padding: 7px 13px; border-radius: 999px;
}
.music-seg button:hover { color: var(--text); }
.music-seg button.on { background: var(--brand); color: var(--ink); }
.stepper .st-val { min-width: 92px; text-align: center; line-height: 1.1; }
.stepper .st-val b { font-size: 1.5rem; }
.stepper .st-val small { display: block; font-size: 0.7rem; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }

/* ---------- Historial ---------- */
.history { display: flex; flex-direction: column; gap: 10px; }
.h-row {
  display: grid; grid-template-columns: 1fr repeat(5, auto) auto; gap: 18px; align-items: center;
  background: var(--panel); border-radius: var(--radius); padding: 12px 16px; border: 1px solid var(--line);
}
.h-row h4 { margin: 0; font-size: 0.98rem; }
.h-row .k { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.8px; }
.h-row .v { font-weight: 700; }
.h-row .btns { display: flex; gap: 6px; }

/* ---------- Diálogos ---------- */
.dlg {
  background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 18px;
  padding: 24px; width: min(480px, 92vw); box-shadow: 0 30px 80px rgba(0, 0, 0, 0.5);
}
.dlg.wide { width: min(760px, 94vw); }
.dlg::backdrop { background: rgba(20, 16, 21, 0.7); backdrop-filter: blur(3px); }
.dlg h3 { margin: 0 0 16px; font-size: 1.25rem; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 14px; }
.form-grid label { display: flex; flex-direction: column; gap: 6px; font-size: 0.85rem; color: var(--muted); }
.check { display: flex; gap: 10px; align-items: center; margin: 8px 0; font-size: 0.92rem; }
.check input { accent-color: var(--brand); width: 18px; height: 18px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
.sum-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.sum-grid div { background: var(--panel); border-radius: 12px; padding: 10px 12px; }
.sum-grid span { display: block; font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.8px; }
.sum-grid b { font-size: 1.35rem; }
.sum-grid b.y { color: var(--brand); }
.sum-chart { width: 100%; height: 170px; margin-top: 14px; background: var(--bg-deep); border-radius: 12px; }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .metrics { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 820px) {
  .topbar { flex-wrap: wrap; padding: 10px 16px; gap: 10px; }
  .devices { margin-left: 0; width: 100%; overflow-x: auto; }
  .brand-text small { display: none; }
  main { padding: 16px; }
  .lib-hero { flex-direction: column; }
  .lib-side { flex-direction: row; flex-wrap: wrap; }
  .metrics { grid-template-columns: repeat(2, 1fr); }
  .t-val { font-size: 2rem; }
  .tile.power .t-val, .tile.target .t-val { font-size: 2.6rem; }
  .group.right { margin-left: 0; }
  .audio-btns { margin-left: 0; padding-right: 0; margin-right: 0; border-right: 0; }
  .h-row { grid-template-columns: 1fr 1fr 1fr; }
  .sum-grid { grid-template-columns: repeat(2, 1fr); }
  .form-grid { grid-template-columns: 1fr; }
}

/* Pantalla completa: más espacio para el gráfico */
:fullscreen main { max-width: none; }
:fullscreen .chart-wrap { height: 32vh; }

/* Aviso flotante */
.notify {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 140%);
  background: var(--panel-2); color: var(--text); border-left: 4px solid var(--brand);
  padding: 12px 18px; border-radius: 12px; box-shadow: 0 12px 30px rgba(0, 0, 0, 0.4);
  transition: transform 0.25s ease; z-index: 50; max-width: 90vw; font-size: 0.92rem;
}
.notify.show { transform: translate(-50%, 0); }

/* Música y voz */
.audio-info { display: block; font-size: 0.8rem; color: var(--brand); min-height: 1em; }
.ctl.audio { color: var(--muted); }
.ctl.audio.on { color: var(--brand); border-color: var(--brand); background: rgba(255, 194, 14, 0.1); }
.ctl.audio.speaking { box-shadow: 0 0 0 4px rgba(255, 194, 14, 0.25); }
.dlg { max-height: 92vh; overflow-y: auto; }
.dlg-sub { margin: 18px 0 6px; font-size: 0.95rem; color: var(--brand); border-top: 1px solid var(--line); padding-top: 14px; }
.form-grid input[type=range] { accent-color: var(--brand); padding: 0; border: 0; background: none; }
.voice-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 6px 0; }
code { background: var(--panel); padding: 1px 5px; border-radius: 5px; font-size: 0.8em; }

/* Medias bajo la potencia instantánea */
.t-avgs { width: 100%; display: flex; flex-wrap: wrap; gap: 2px 14px; font-size: 0.78rem; color: var(--muted); margin-top: 2px; }
.t-avgs b { color: var(--text); font-weight: 700; font-variant-numeric: tabular-nums; }

/* Duración total junto al tiempo ("2:58 / 3:00") */
.t-of { font-size: 1.3rem; font-weight: 600; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Botón de vista de la gráfica */
.chart-view {
  position: absolute; top: 6px; left: 50px; z-index: 2;
  background: rgba(47, 41, 48, 0.85); border: 1px solid var(--line); border-radius: 8px;
  padding: 3px 10px; font-size: 0.75rem; font-weight: 600; color: var(--muted); cursor: pointer;
}
.chart-view:hover { color: var(--brand); border-color: var(--brand); }

/* Panel de conexiones */
.light { display: inline-block; width: 14px; height: 14px; border-radius: 50%; background: #776e79; flex: none; }
.light.on { background: var(--ok); box-shadow: 0 0 10px var(--ok); }
.light.idle, .light.wait { background: var(--brand); box-shadow: 0 0 8px rgba(255, 194, 14, 0.6); }
.light.wait { animation: pulse 1s infinite; }
.light.lost { background: var(--danger); box-shadow: 0 0 8px var(--danger); }
.legend-lights { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 0.78rem; color: var(--muted); margin-bottom: 14px; }
.legend-lights span { display: inline-flex; align-items: center; gap: 6px; }
.legend-lights .light { width: 10px; height: 10px; }
.dev-rows { display: flex; flex-direction: column; gap: 10px; margin-bottom: 10px; }
.dev-row { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; }
.dev-main { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.dev-text { display: flex; flex-direction: column; flex: 1; min-width: 180px; }
.dev-text b { font-size: 1rem; }
.dev-name { font-size: 0.85rem; }
.dev-value { font-weight: 700; color: var(--brand); font-variant-numeric: tabular-nums; min-width: 60px; text-align: right; }
.dev-actions { display: flex; gap: 6px; }
.dev-known { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 8px; }
.dev-known:empty { display: none; }
.dev-chip { display: inline-flex; align-items: center; background: var(--panel-2); border-radius: 999px; overflow: hidden; }
.dev-chip button { border: 0; background: none; cursor: pointer; font-size: 0.8rem; padding: 4px 10px; }
.chip-go:hover { color: var(--brand); }
.chip-x { color: var(--muted); padding-left: 2px !important; }
.chip-x:hover { color: var(--danger); }

/* Barra superior en pantallas medianas: los equipos pasan a una segunda línea */
@media (max-width: 1080px) {
  .topbar { flex-wrap: wrap; row-gap: 10px; }
  .devices { margin-left: 0; width: 100%; overflow-x: auto; }
}

.bt-off { background: rgba(240, 69, 58, 0.15); border: 1px solid var(--danger); border-radius: 12px; padding: 10px 14px; margin-bottom: 12px; font-size: 0.88rem; }

/* Título de cada gráfica */
.chart-title {
  position: absolute; top: 6px; left: 50px; z-index: 2; pointer-events: none;
  background: rgba(47, 41, 48, 0.85); border: 1px solid var(--line); border-radius: 8px;
  padding: 2px 10px; font-size: 0.75rem; font-weight: 600; color: var(--muted);
}

/* Cadencia fuera del objetivo (±5 rpm) */
.t-val.bad { color: var(--danger); }
