:root {
  --bg: #070b08;
  --surface: #0f1611;
  --surface-2: #162019;
  --line: #243228;
  --text: #e8f2ea;
  --muted: #93a99a;
  --accent: #3ee05a;
  --accent-strong: #2bc548;
  --accent-ink: #03140a;
  --accent-soft: rgba(62, 224, 90, 0.14);
  --danger: #ff6b6b;
  --danger-soft: rgba(255, 107, 107, 0.14);
  --warn: #f5b841;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3, p { margin: 0; }
h2 { font-size: 1.15rem; font-weight: 650; }
.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
.row { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }

svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ------------------------------------------------------------ botones y campos */

.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 12px 18px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  color: var(--text);
  background: var(--surface-2);
  min-height: 46px;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: var(--accent-strong); }
.btn.primary:disabled { opacity: 0.6; cursor: progress; }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { padding: 6px 12px; min-height: 36px; font-size: 0.9rem; }
.btn:focus-visible, .icon-btn:focus-visible, .tb:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.icon-btn {
  appearance: none;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  cursor: pointer;
}
.icon-btn.spinning svg { animation: spin 0.8s linear infinite; }

.field { display: grid; gap: 6px; font-size: 0.9rem; color: var(--muted); }
.field input, .search, dialog input {
  width: 100%;
  font: inherit;
  font-size: 1rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  min-height: 46px;
}
.field input:focus, .search:focus, dialog input:focus { border-color: var(--accent); outline: none; }

.check { display: flex; gap: 10px; align-items: flex-start; font-size: 0.92rem; cursor: pointer; }
.check input { width: 20px; height: 20px; margin: 2px 0 0; accent-color: var(--accent); flex: none; }
.check small { display: block; color: var(--muted); font-size: 0.8rem; margin-top: 2px; }

.error {
  color: var(--danger);
  background: var(--danger-soft);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.92rem;
}

/* ------------------------------------------------------------ inicio */

.home {
  width: min(560px, 100%);
  margin: 0 auto;
  padding: calc(24px + var(--safe-top)) calc(16px + var(--safe-right)) calc(24px + var(--safe-bottom)) calc(16px + var(--safe-left));
  display: grid;
  gap: 18px;
}

.brand { display: flex; align-items: center; gap: 14px; }
.brand img { border-radius: 12px; }
.brand h1 { font-size: 1.5rem; letter-spacing: -0.01em; }
.brand p { color: var(--muted); font-size: 0.9rem; }

.tabs { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 4px; }
.tabs button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  padding: 10px;
  border-radius: 9px;
  cursor: pointer;
  min-height: 42px;
}
.tabs button[aria-selected="true"] { background: var(--surface-2); color: var(--text); box-shadow: inset 0 -2px 0 var(--accent); }

.panel { display: grid; gap: 16px; }
.card { display: grid; gap: 14px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; }

.devices-head { display: flex; align-items: center; gap: 8px; }
.devices-title { flex: 1; min-width: 0; }
.devices-title p { font-size: 0.85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#devices-view { display: grid; gap: 12px; }

.devices { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.device {
  appearance: none;
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
  cursor: pointer;
  min-height: 64px;
}
.device:hover:not(:disabled) { border-color: var(--accent); }
.device:disabled { cursor: default; opacity: 0.55; }
.device .dot { width: 10px; height: 10px; border-radius: 50%; background: #5b6b60; flex: none; }
.device .dot.online { background: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.device .info { flex: 1; min-width: 0; display: grid; }
.device .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device .meta { color: var(--muted); font-size: 0.84rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-variant-numeric: tabular-nums; }
.device .go { color: var(--muted); }
.device .badge { font-size: 0.72rem; color: var(--warn); border: 1px solid currentColor; border-radius: 99px; padding: 1px 7px; margin-left: 6px; font-weight: 500; }

.empty { color: var(--muted); text-align: center; padding: 28px 12px; border: 1px dashed var(--line); border-radius: 12px; }
.loading { display: grid; place-items: center; padding: 32px; }

.recent h3 { font-size: 0.85rem; color: var(--muted); font-weight: 600; margin-bottom: 8px; }
.recent ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.recent button {
  appearance: none;
  font: inherit;
  font-size: 0.9rem;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 99px;
  padding: 8px 14px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.recent button small { color: var(--muted); margin-left: 6px; }

.home-foot { display: grid; gap: 8px; justify-items: center; text-align: center; padding-top: 6px; }

/* ------------------------------------------------------------ superposiciones */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 16px;
  background: rgba(7, 11, 8, 0.88);
  text-align: center;
  padding: 24px;
}

.spinner {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

dialog {
  width: min(400px, calc(100% - 32px));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 20px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
dialog form { display: grid; gap: 14px; }

/* ------------------------------------------------------------ sesión */

body.in-session { overflow: hidden; overscroll-behavior: none; }

.session {
  position: fixed;
  left: 0;
  right: 0;
  top: var(--vv-top, 0px);
  height: var(--vv-height, 100dvh);
  background: #000;
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  display: flex;
  flex-direction: column;
}

.viewport {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  touch-action: none;
  cursor: default;
}
.stage { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
.stage canvas { display: block; }

.cursor { position: absolute; left: 0; top: 0; pointer-events: none; will-change: transform; }
.cursor svg { width: 18px; height: 25px; fill: #fff; stroke: #000; stroke-width: 1.4; filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.6)); }

.ripple {
  position: absolute;
  width: 34px;
  height: 34px;
  margin: -17px 0 0 -17px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  pointer-events: none;
  opacity: 0;
}
.ripple.show { animation: ripple 0.35s ease-out; }
.ripple.right { border-color: var(--warn); }
@keyframes ripple { from { opacity: 0.9; transform: scale(0.4); } to { opacity: 0; transform: scale(1.3); } }

.session-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 12px;
  padding: 24px;
  text-align: center;
  background: rgba(0, 0, 0, 0.72);
  z-index: 5;
}
.session-overlay #session-overlay-title { font-size: 1.1rem; font-weight: 600; }
.session-overlay .row { justify-content: center; margin-top: 8px; }

.toolbar {
  position: absolute;
  top: calc(8px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 2px;
  max-width: calc(100% - 16px - var(--safe-left) - var(--safe-right));
  overflow-x: auto;
  scrollbar-width: none;
  padding: 4px;
  background: rgba(15, 22, 17, 0.92);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.toolbar::-webkit-scrollbar { display: none; }

.tb {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex: none;
  height: 40px;
  min-width: 40px;
  padding: 0 10px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.88rem;
  cursor: pointer;
  white-space: nowrap;
}
.tb:hover { background: var(--surface-2); }
.tb[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.tb.danger { color: var(--danger); }
.tb span { display: none; }
.tb.select { padding-right: 4px; }
.tb select {
  appearance: none;
  font: inherit;
  font-size: 0.88rem;
  color: var(--text);
  background: transparent;
  border: 0;
  padding: 8px 4px;
  max-width: 130px;
  cursor: pointer;
}
.tb select option { background: var(--surface); }
.stats { color: var(--muted); font-size: 0.78rem; padding: 0 8px; white-space: nowrap; font-variant-numeric: tabular-nums; min-width: 76px; text-align: center; }

@media (min-width: 760px) {
  .tb span { display: inline; }
}

.tb-extra { display: contents; }
.sheet-only { display: none; }

/* Menú "Más" (pantallas estrechas o táctiles): zoom, monitor, calidad, pantalla completa y estadísticas. */
.tb-sheet {
  position: absolute;
  top: calc(62px + var(--safe-top));
  right: calc(8px + var(--safe-right));
  z-index: 11;
  width: min(280px, calc(100% - 16px));
  padding: 6px;
  background: rgba(15, 22, 17, 0.97);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}
.tb-sheet .tb-extra { display: grid; gap: 2px; }
.tb-sheet .tb { width: 100%; height: 46px; justify-content: flex-start; gap: 10px; }
.tb-sheet .tb span { display: inline; }
.tb-sheet .sheet-only { display: inline; font-style: normal; color: var(--muted); }
.tb-sheet .tb select { flex: 1; max-width: none; text-align: right; }
.tb-sheet .stats { text-align: left; padding: 10px 12px 6px; }

.toolbar-handle {
  position: absolute;
  top: calc(6px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  appearance: none;
  width: 52px;
  height: 30px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 99px;
  background: rgba(15, 22, 17, 0.85);
  color: var(--text);
  cursor: pointer;
}

.keys {
  flex: none;
  display: grid;
  gap: 6px;
  padding: 8px calc(8px + var(--safe-right)) calc(8px + var(--safe-bottom)) calc(8px + var(--safe-left));
  background: var(--surface);
  border-top: 1px solid var(--line);
  z-index: 10;
}
.keys-row { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; overscroll-behavior-x: contain; }
.keys-row::-webkit-scrollbar { display: none; }
.keys button {
  appearance: none;
  flex: none;
  min-width: 44px;
  height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 0.92rem;
  cursor: pointer;
  touch-action: manipulation;
}
.keys button:active, .keys button.active { background: var(--line); }
.keys button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.keys button.accent { color: var(--accent); border-color: rgba(62, 224, 90, 0.4); }

/* Campo invisible que recibe el teclado del móvil. Debe ser enfocable (no display:none). */
.kbd-input {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  border: 0;
  padding: 0;
  resize: none;
  font-size: 16px; /* iOS no hace zoom al enfocar con 16px o más */
  color: transparent;
  background: transparent;
  caret-color: transparent;
}

.toast {
  position: absolute;
  left: 50%;
  bottom: calc(16px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 20;
  max-width: min(480px, calc(100% - 32px));
  background: rgba(22, 32, 25, 0.96);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 0.9rem;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  text-align: center;
}
.keys:not([hidden]) ~ .toast { bottom: 120px; }

@media (prefers-reduced-motion: reduce) {
  .spinner, .icon-btn.spinning svg { animation-duration: 2s; }
  .ripple.show { animation: none; }
}
