:root {
  --bg: #0b0e14;
  --bg-elev: #131824;
  --bg-elev2: #1b2233;
  --border: #263045;
  --text: #e6ebf4;
  --muted: #8b95a7;
  --accent: #4c8dff;
  --accent-press: #3a74dd;
  --danger: #ff5c72;
  --ok: #35c281;
  --radius: 16px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.hidden { display: none !important; }

/* ---- Pantallas centradas (bienvenida, clave) ---------------------------- */
.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 28px;
  padding: 28px 22px calc(28px + env(safe-area-inset-bottom));
  text-align: center;
}

.card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 24px;
}

.welcome-text {
  font-size: 1.15rem;
  line-height: 1.6;
  color: var(--text);
  white-space: pre-wrap;
  margin: 0 0 8px;
}

.subtle { color: var(--muted); font-size: 0.95rem; line-height: 1.5; }

/* ---- Botones ------------------------------------------------------------ */
.btn {
  appearance: none;
  border: none;
  border-radius: 999px;
  padding: 16px 28px;
  font-size: 1.1rem;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  cursor: pointer;
  transition: transform 0.05s ease, background 0.15s ease;
  width: 100%;
  max-width: 420px;
  touch-action: manipulation;
}
.btn:active { transform: scale(0.98); background: var(--accent-press); }
.btn.secondary { background: var(--bg-elev2); color: var(--text); border: 1px solid var(--border); }
.btn.danger { background: var(--danger); }
.btn:disabled { opacity: 0.5; cursor: default; }

/* ---- Teclado numérico --------------------------------------------------- */
.pin-dots { display: flex; gap: 16px; justify-content: center; margin: 6px 0 4px; }
.pin-dot {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--border); background: transparent;
  transition: background 0.12s ease, border-color 0.12s ease;
}
.pin-dot.filled { background: var(--accent); border-color: var(--accent); }

.keypad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  width: 100%;
  max-width: 320px;
  margin: 0 auto;
}
.key {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-elev2);
  color: var(--text);
  font-size: 1.6rem;
  font-weight: 600;
  border-radius: 14px;
  padding: 18px 0;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.05s ease, background 0.12s ease;
}
.key:active { transform: scale(0.96); background: #222b40; }
.key.wide { grid-column: span 1; }
.key.ghost { background: transparent; border-color: transparent; }

.error-msg { color: var(--danger); min-height: 1.4em; font-size: 0.95rem; }

.shake { animation: shake 0.4s; }
@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-8px); }
  40%, 60% { transform: translateX(8px); }
}

/* ---- Vista de stream ---------------------------------------------------- */
#stream-view { position: fixed; inset: 0; background: #000; }
#stream-frame {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  border: 0;
  display: block;
}

/* Cortina: tapa el iframe y captura los toques del cliente. */
#curtain {
  position: fixed; inset: 0;
  background: #ffffff;
  color: #14181f;
  display: flex; align-items: center; justify-content: center;
  padding: 32px calc(32px + env(safe-area-inset-right)) 32px calc(32px + env(safe-area-inset-left));
  text-align: center;
  z-index: 50;
  touch-action: none;
}
#curtain .curtain-inner { max-width: 560px; }
#curtain .curtain-text {
  font-size: 1.35rem; line-height: 1.6; font-weight: 500; white-space: pre-wrap;
}
#curtain .spinner {
  width: 34px; height: 34px; margin: 0 auto 22px;
  border: 3px solid rgba(0,0,0,0.15); border-top-color: #14181f;
  border-radius: 50%; animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Overlay de estado (conectando / error) ----------------------------- */
.overlay {
  position: fixed; inset: 0; z-index: 40;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: 28px; text-align: center;
}
.overlay .spinner {
  width: 34px; height: 34px; margin: 0 auto 18px;
  border: 3px solid rgba(255,255,255,0.12); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.9s linear infinite;
}

/* ---- Panel admin -------------------------------------------------------- */
.admin-wrap {
  width: 100%; max-width: 480px; margin: 0 auto;
  padding: 18px 16px calc(24px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 18px;
}
.admin-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding-top: env(safe-area-inset-top);
}
.admin-header h1 { font-size: 1.15rem; margin: 0; }
.panel {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px 16px;
}
.panel h2 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.08em;
  color: var(--muted); margin: 0 0 14px; }
.field { margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }
.field label { display: block; font-size: 0.9rem; color: var(--muted); margin-bottom: 6px; }
.field input, .field textarea {
  width: 100%; background: var(--bg-elev2); border: 1px solid var(--border);
  color: var(--text); border-radius: 12px; padding: 12px 14px; font-size: 1rem;
  font-family: inherit; resize: vertical;
}
.field input:focus, .field textarea:focus { outline: none; border-color: var(--accent); }
.field .hint { font-size: 0.8rem; color: var(--muted); margin-top: 5px; }

.row { display: flex; gap: 10px; }
.row .btn { max-width: none; }

.rooms { display: flex; flex-direction: column; gap: 14px; }
.room-card {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden;
}
.room-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; gap: 10px;
}
.room-title { font-weight: 600; display: flex; align-items: center; gap: 10px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.dot.on { background: var(--ok); }
.dot.curtain { background: var(--danger); }
.badge { font-size: 0.75rem; color: var(--muted); }

.frame-box { position: relative; width: 100%; background: #000; aspect-ratio: 16 / 9; }
.frame-box iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }
.frame-box.collapsed { display: none; }

.room-actions { display: flex; gap: 10px; padding: 12px 14px; flex-wrap: wrap; }
.room-actions .btn { flex: 1; min-width: 120px; padding: 12px 16px; font-size: 1rem; }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--bg-elev2); border: 1px solid var(--border);
  color: var(--text); padding: 12px 18px; border-radius: 12px;
  box-shadow: var(--shadow); z-index: 100; font-size: 0.95rem;
  opacity: 0; transition: opacity 0.2s ease; pointer-events: none; max-width: 90vw;
}
.toast.show { opacity: 1; }
.toast.err { border-color: var(--danger); }
