@import url('https://fonts.googleapis.com/css2?family=Manrope:wght@400;600&display=swap');

/* P. GYM v5: interfaz minimalista. Un acento (menta), rejilla de 8 px, un radio, dos pesos (400 / 600). */
:root {
  color-scheme: dark;
  --font: 'Manrope', 'Segoe UI', system-ui, sans-serif;
  --bg: #0a0b0d;
  --surface: rgba(255, 255, 255, .04);
  --surface-hover: rgba(255, 255, 255, .08);
  --line: rgba(255, 255, 255, .08);
  --line-strong: rgba(255, 255, 255, .16);
  --text: #eef0f2;
  --text-2: #b4bac4;
  --text-3: #8b929d;
  --accent: #b8f36b;
  --accent-ink: #0e1208;
  --r: 12px;
  --ease: cubic-bezier(.3, .6, .3, 1);
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

* { box-sizing: border-box; }
html, body { min-width: 320px; margin: 0; }
body { min-height: 100vh; background: var(--bg); font: 400 14px/1.5 var(--font); }
button, select, input { font: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
p { margin: 0; }

/* ---------- Estructura ---------- */
.app { min-height: 100vh; display: flex; flex-direction: column; width: min(1840px, 100%); margin: 0 auto; }
.topbar { height: 56px; display: flex; align-items: center; justify-content: space-between; padding: 0 24px; flex: none; }
.brand { margin: 0; font: 600 15px/1 var(--font); letter-spacing: .16em; }
.topbar-right { display: flex; align-items: center; gap: 24px; }
.status { display: inline-flex; align-items: center; gap: 8px; color: var(--text-2); font-size: 13px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #4b515b; transition: background .2s var(--ease); }
.dot.active { background: var(--accent); }

.workspace { flex: 1; display: grid; grid-template-columns: minmax(0, 1fr); gap: 24px; padding: 0 24px 24px; align-items: start; }
.stage-column { width: 100%; margin: 0 auto; display: flex; flex-direction: column; gap: 16px; min-width: 0; }

/* ---------- Escenario (cámara) ---------- */
.camera-stage {
  position: relative; overflow: hidden; isolation: isolate; container-type: inline-size;
  width: 100%; aspect-ratio: 16 / 9; border-radius: var(--r);
  background: #060708; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .06);
}
.camera-stage video, .camera-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.camera-stage video { transform: scaleX(-1); opacity: 0; transition: opacity .25s var(--ease), filter .25s var(--ease); }
.camera-stage canvas { transform: scaleX(-1); pointer-events: none; transition: opacity .2s var(--ease); }
.camera-stage.active video { opacity: 1; }
.camera-stage.active::before, .camera-stage.active::after { content: ''; position: absolute; left: 0; right: 0; z-index: 1; pointer-events: none; }
.camera-stage.active::before { top: 0; height: 26%; background: linear-gradient(to bottom, rgba(6, 7, 8, .5), transparent); }
.camera-stage.active::after { bottom: 0; height: 30%; background: linear-gradient(to top, rgba(6, 7, 8, .55), transparent); }

.camera-empty { position: absolute; inset: 0; display: grid; place-items: center; padding: 24px; color: var(--text-3); font-size: clamp(14px, 1.6cqw, 20px); text-align: center; }
.camera-empty.hidden, .camera-stage[data-phase='done'] .camera-empty { display: none; }

/* HUD: solo datos en vivo, sobre el escenario */
.hud, .hud-angle, .hud-phase, .tempo-ring { display: none; position: absolute; z-index: 3; pointer-events: none; }
.camera-stage.active[data-phase='set'] :is(.hud, .hud-angle, .tempo-ring):not([hidden]) { display: block; }
.camera-stage.active[data-phase='set'] .hud-phase { display: flex; }
.camera-stage.overlay-on :is(.hud, .hud-angle, .hud-phase, .tempo-ring) { display: none !important; }

.hud { top: 2.5cqw; right: 2.5cqw; text-align: right; text-shadow: 0 2px 20px rgba(0, 0, 0, .5); }
.hud-count { white-space: nowrap; line-height: 1; }
.hud-count span { font: 400 clamp(56px, 9cqw, 160px)/.9 var(--font); letter-spacing: -.04em; font-variant-numeric: tabular-nums; color: #fff; }
.hud-count small { margin-left: .4em; font: 400 clamp(16px, 2.4cqw, 40px)/1 var(--font); color: var(--text-2); }
.hud-set, .hud-sides { margin-top: 8px; color: rgba(255, 255, 255, .82); font: 600 clamp(12px, 1.3cqw, 20px)/1.2 var(--font); font-variant-numeric: tabular-nums; }
.hud-quality { position: absolute; top: 100%; right: 0; margin-top: 8px; color: var(--accent); font: 600 clamp(14px, 1.7cqw, 28px)/1 var(--font); opacity: 0; transition: opacity .2s var(--ease); }
.hud-quality.show { opacity: 1; }
.hud-angle { top: 2.5cqw; left: 2.5cqw; color: rgba(255, 255, 255, .78); font: 600 clamp(14px, 1.7cqw, 28px)/1 var(--font); font-variant-numeric: tabular-nums; text-shadow: 0 2px 16px rgba(0, 0, 0, .5); }
.camera-stage .hud-angle:empty { display: none; }

.hud-phase { left: 50%; bottom: 3.5cqw; max-width: 70%; flex-direction: column; align-items: center; gap: 4px; text-align: center; transform: translateX(-50%); text-shadow: 0 1px 14px rgba(0, 0, 0, .6); }
.hud-phase p:first-child { color: #fff; font: 600 clamp(16px, 2.2cqw, 36px)/1.2 var(--font); }
.hud-phase p:last-child { color: rgba(255, 255, 255, .75); font: 400 clamp(12px, 1.3cqw, 20px)/1.3 var(--font); }
.hud-phase p:empty { display: none; }

/* Anillo de ritmo: uno solo, fino */
.tempo-ring { --ring: var(--accent); left: 3cqw; top: 50%; width: clamp(96px, 13cqw, 220px); aspect-ratio: 1; transform: translateY(-50%); transition: opacity .2s var(--ease); }
.tempo-ring.subtle { opacity: .5; }
.tempo-ring svg, .rest-dial svg { display: block; width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-bg { fill: none; stroke: rgba(255, 255, 255, .12); stroke-width: 1.2; }
.ring-fg { fill: none; stroke: var(--ring, var(--accent)); stroke-width: 1.8; stroke-linecap: round; stroke-dasharray: 100; stroke-dashoffset: 0; }
.ring-text { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.ring-num { font: 400 clamp(32px, 5.2cqw, 90px)/1 var(--font); font-variant-numeric: tabular-nums; color: #fff; }
.ring-label { margin-top: 8px; color: var(--text-2); font: 600 clamp(10px, 1cqw, 15px)/1 var(--font); letter-spacing: .12em; text-transform: uppercase; }

/* Mensajes centrales: encuadre, cuenta atrás, calibración */
.stage-overlay { position: absolute; inset: 0; z-index: 4; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; padding: 0 8%; text-align: center; pointer-events: none; background: rgba(6, 7, 8, .3); }
.stage-overlay strong { color: #fff; font: 600 clamp(18px, 2.6cqw, 40px)/1.25 var(--font); text-shadow: 0 2px 20px rgba(0, 0, 0, .6); }
.stage-overlay span { color: var(--text-2); font: 400 clamp(13px, 1.5cqw, 22px)/1.4 var(--font); }
.stage-overlay span:empty { display: none; }
.stage-overlay.big strong { font-weight: 400; font-size: clamp(120px, 24cqw, 400px); line-height: 1; letter-spacing: -.04em; }
.stage-overlay[data-state='go'] strong { color: var(--accent); font-weight: 400; font-size: clamp(40px, 7cqw, 110px); }
.stage-overlay[data-state='ok'] strong { color: var(--accent); }
.stage-overlay.pop strong { animation: fade-in .25s var(--ease); }
@keyframes fade-in { from { opacity: 0; transform: scale(.96); } to { opacity: 1; transform: none; } }

/* Descanso y resúmenes: una sola cosa en el centro */
.camera-stage[data-phase='rest'] video, .camera-stage[data-phase='done'] video { filter: blur(16px) brightness(.35); }
.camera-stage[data-phase='rest'] canvas, .camera-stage[data-phase='done'] canvas { opacity: 0; }
.stage-rest { position: absolute; inset: 0; z-index: 5; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; padding: 24px; text-align: center; }
.rest-dial { position: relative; width: clamp(176px, 26cqw, 360px); aspect-ratio: 1; }
.stage-rest .ring-fg { stroke: var(--accent); transition: stroke-dashoffset .25s linear; }
.rest-number { position: absolute; inset: 0; display: grid; place-items: center; font: 400 clamp(44px, 7cqw, 120px)/1 var(--font); letter-spacing: -.03em; font-variant-numeric: tabular-nums; }
.rest-title { color: var(--text-2); font: 600 clamp(15px, 1.6cqw, 22px)/1.3 var(--font); }
.rest-summary { max-width: 46ch; color: var(--text-2); font-size: clamp(13px, 1.3cqw, 18px); }
.rest-hint { max-width: 46ch; color: var(--text-3); font-size: clamp(12px, 1.2cqw, 16px); }
.rest-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 8px; }

.stage-rest[data-mode='done'] {
  inset: 50% auto auto 50%; width: min(520px, calc(100% - 32px)); max-height: calc(100% - 32px); overflow-y: auto;
  transform: translate(-50%, -50%); padding: 40px 32px 32px; gap: 8px;
  background: rgba(16, 17, 20, .86); border: 1px solid var(--line); border-radius: var(--r);
}
.stage-rest[data-mode='done'] .rest-dial { width: auto; aspect-ratio: auto; }
.stage-rest[data-mode='done'] .rest-dial svg { display: none; }
.stage-rest[data-mode='done'] .rest-number { position: static; font-size: clamp(56px, 7cqw, 96px); }
.stage-rest[data-mode='done'] .rest-title { order: -1; color: var(--text); font-size: 18px; }
.stage-rest[data-mode='done'] .rest-actions { margin-top: 24px; }
.routine-summary { width: 100%; margin: 16px 0 0; padding: 0; list-style: none; text-align: left; }
.routine-summary li { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 0 16px; padding: 8px 0; border-top: 1px solid var(--line); color: var(--text-2); font-size: 13px; }
.routine-summary strong { color: var(--text); font-weight: 600; }

/* ---------- Acciones ---------- */
.actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.actions-end { margin-left: auto; }
.btn { height: 44px; display: inline-flex; align-items: center; justify-content: center; padding: 0 24px; border: 1px solid transparent; border-radius: var(--r); font: 600 14px/1 var(--font); cursor: pointer; transition: background .2s var(--ease), color .2s var(--ease), border-color .2s var(--ease), transform .15s var(--ease); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn-sm { height: 36px; padding: 0 16px; font-size: 13px; }
.btn-primary { color: var(--accent-ink); background: var(--accent); }
.btn-primary:hover:not(:disabled) { background: #c9f98a; }
.btn-secondary { color: var(--text); background: var(--surface-hover); }
.btn-secondary:hover:not(:disabled) { background: rgba(255, 255, 255, .13); }
.btn-ghost { color: var(--text-2); background: transparent; }
.btn-ghost:hover:not(:disabled) { color: var(--text); background: var(--surface); }
.link-button { padding: 0; border: 0; color: var(--text-2); background: none; font-size: 13px; text-decoration: underline; text-underline-offset: 3px; cursor: pointer; }
.link-button:hover { color: var(--text); }

/* ---------- Barra lateral: solo controles ---------- */
.sidebar { display: flex; flex-direction: column; gap: 24px; min-width: 0; }
.field { display: grid; gap: 8px; min-width: 0; }
.label { display: block; color: var(--text-3); font: 600 11px/16px var(--font); letter-spacing: .08em; text-transform: uppercase; }
.label strong { margin-left: 8px; color: var(--text-2); font-weight: 600; }
.select, .input {
  width: 100%; height: 44px; padding: 0 40px 0 16px; color: var(--text); background-color: var(--surface);
  border: 1px solid var(--line); border-radius: var(--r); font: 400 14px var(--font);
  appearance: none; color-scheme: dark; cursor: pointer; transition: border-color .2s var(--ease), background-color .2s var(--ease);
}
.select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='m1 1.5 5 5 5-5' fill='none' stroke='%238b929d' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 16px center;
}
.input { padding-right: 16px; cursor: text; }
.select:hover, .input:hover { border-color: var(--line-strong); background-color: var(--surface-hover); }
.select option, .select optgroup { color: var(--text); background: #16181c; }
.select optgroup { color: var(--text-3); font-weight: 600; }
.select:disabled, .input:disabled { opacity: .5; cursor: not-allowed; }
.note { color: var(--text-3); font-size: 13px; }

.calib-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: -8px; }
.calib-row .btn { margin-left: -16px; }
.calib-state { color: var(--text-2); font-size: 13px; }
.calib-state::after { content: ' ·'; color: var(--text-3); }

.settings { border-top: 1px solid var(--line); padding-top: 8px; }
.settings summary { height: 40px; display: flex; align-items: center; justify-content: space-between; color: var(--text-2); font-weight: 600; cursor: pointer; list-style: none; }
.settings summary::-webkit-details-marker { display: none; }
.settings summary::after { content: '+'; color: var(--text-3); font-size: 18px; font-weight: 400; }
.settings[open] summary::after { content: '−'; }
.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; padding: 8px 0; }
.settings-grid .select, .settings-grid .input { height: 40px; }
.settings-wide { grid-column: 1 / -1; }
.settings-grid > .btn { justify-self: start; margin-left: -16px; }
.settings input[type='range'] { width: 100%; accent-color: var(--accent); cursor: pointer; }
.toggle-group { display: flex; flex-wrap: wrap; gap: 8px 24px; }
.toggle { display: inline-flex; align-items: center; gap: 8px; color: var(--text-2); font-size: 13px; cursor: pointer; }
.toggle input { margin: 0; accent-color: var(--accent); }
.tip { color: var(--text-3); font-size: 13px; line-height: 1.5; }

/* ---------- Aviso ---------- */
.toast { position: fixed; z-index: 20; left: 50%; bottom: 24px; max-width: calc(100% - 32px); padding: 12px 24px; border: 1px solid var(--line); border-radius: var(--r); color: var(--text); background: rgba(24, 26, 30, .94); box-shadow: 0 8px 24px rgba(0, 0, 0, .3); font-size: 13px; opacity: 0; transform: translate(-50%, 8px); pointer-events: none; transition: opacity .2s var(--ease), transform .2s var(--ease); }
.toast.visible { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Historial ---------- */
html.modal-open { overflow: hidden; }
.modal { position: fixed; inset: 0; z-index: 10; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgba(6, 7, 9, .72); }
.modal-card { width: min(640px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; border: 1px solid var(--line); border-radius: var(--r); background: #111214; box-shadow: 0 24px 64px rgba(0, 0, 0, .45); }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 24px 24px 8px; }
.modal-head h2 { margin: 0; font: 600 20px/1.2 var(--font); }
.modal-body { overflow-y: auto; padding: 8px 24px 24px; }
.history-section { padding-top: 24px; }
.history-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.history-head .select { width: auto; max-width: 62%; height: 36px; font-size: 13px; }
.history-empty { margin-top: 8px; color: var(--text-3); font-size: 13px; }
.history-record { display: grid; grid-template-columns: 1fr auto auto; gap: 4px 16px; padding: 8px 0; border-bottom: 1px solid var(--line); color: var(--text-2); font-size: 13px; }
.history-record strong { color: var(--text); font-weight: 600; }
.history-svg { display: block; width: 100%; height: auto; margin-top: 8px; }
.chart-grid { stroke: var(--line); stroke-width: 1; }
.chart-line { fill: none; stroke: var(--accent); stroke-width: 1.5; stroke-linejoin: round; stroke-linecap: round; }
.chart-dot { fill: var(--accent); }
.chart-quality { fill: var(--text); stroke: #111214; stroke-width: 1; }
.chart-label { fill: var(--text-3); font: 400 8px var(--font); }
.chart-label-reps { fill: var(--accent); }
.chart-legend { display: flex; flex-wrap: wrap; gap: 4px 16px; margin-top: 8px; font-size: 12px; color: var(--text-3); }
.legend-reps { color: var(--accent); }
.legend-quality { color: var(--text); }
.history-day { margin: 16px 0 8px; color: var(--text-3); font: 600 11px/16px var(--font); letter-spacing: .08em; text-transform: uppercase; }
.history-entry { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.history-entry-main { flex: 1; min-width: 0; display: grid; gap: 4px; }
.history-entry-main strong { color: var(--text); font-size: 14px; font-weight: 600; overflow-wrap: anywhere; }
.history-meta { color: var(--text-3); font-size: 13px; }
.history-badge { color: var(--text-2); font-size: 12px; }
.history-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--line); }

/* ---------- Pantalla de PC (1366-1920): escenario protagonista, sin scroll ---------- */
@media (min-width: 1000px) {
  .workspace { grid-template-columns: minmax(0, 1fr) clamp(300px, 22vw, 340px); }
  .stage-column { width: min(100%, max(560px, calc((100vh - 56px - 24px - 16px - 44px) * 16 / 9))); }
  .sidebar { position: sticky; top: 0; max-height: calc(100vh - 56px - 24px); overflow-y: auto; padding-right: 4px; scrollbar-width: thin; scrollbar-color: var(--line-strong) transparent; }
}

@media (max-width: 560px) {
  .topbar { padding: 0 16px; }
  .topbar-right { gap: 16px; }
  .workspace { padding: 0 16px 24px; gap: 24px; }
  .btn { padding: 0 16px; }
  .modal { padding: 8px; }
  .modal-card { max-height: calc(100vh - 16px); }
  .modal-head, .modal-body { padding-left: 16px; padding-right: 16px; }
  .history-record { grid-template-columns: 1fr 1fr; }
  .history-record strong { grid-column: 1 / -1; }
  .stage-rest[data-mode='done'] { padding: 24px 16px 16px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}

/* v6: opciones del desplegable con mejor contraste */
.select option { background: #0f1114; color: #f2f4f7; padding: 10px 12px; }
.select option:checked, .select option:hover {
  background: var(--accent) linear-gradient(var(--accent), var(--accent)); color: var(--accent-ink); font-weight: 600;
}
