:root {
  --bg:      var(--tg-theme-bg-color, #17212b);
  --card:    var(--tg-theme-secondary-bg-color, #232e3c);
  --text:    var(--tg-theme-text-color, #f5f5f5);
  --muted:   var(--tg-theme-hint-color, #8a97a5);
  --accent:  var(--tg-theme-button-color, #3390ec);
  --on-accent: var(--tg-theme-button-text-color, #fff);
  --danger:  #e5484d;
  --border:  rgba(255,255,255,.10);
  --radius: 14px;
  /* Márgenes seguros (notch / barra de gestos) — el meta usa viewport-fit=cover. */
  --pad-t: max(16px, env(safe-area-inset-top));
  --pad-b: max(16px, env(safe-area-inset-bottom));
  --pad-x: max(14px, env(safe-area-inset-left), env(safe-area-inset-right));
}

* { box-sizing: border-box; margin: 0; }
/* El atributo hidden debe ganarle a cualquier display (gate, app, subs…). */
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;                 /* nunca scroll horizontal */
  overscroll-behavior-y: contain;     /* sin "rebote" que tape la UI */
}

/* Gate (carga / error) */
.gate {
  min-height: 100dvh; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px;
  padding: var(--pad-t) calc(var(--pad-x) + 10px) var(--pad-b);
  text-align: center; color: var(--muted);
}
#gate-msg { white-space: pre-line; line-height: 1.5; max-width: 34ch; overflow-wrap: anywhere; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--border); border-top-color: var(--accent); animation: spin 1s linear infinite; }
.spin-sm { width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--border); border-top-color: var(--muted); display: inline-block; animation: spin 1s linear infinite; vertical-align: -2px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* App: columna que llena la pantalla; los bloques largos scrollean por dentro. */
#app {
  min-height: 100dvh; display: flex; flex-direction: column;
  padding: var(--pad-t) var(--pad-x) var(--pad-b);
  gap: 12px; max-width: 560px; margin: 0 auto;
}

.hd { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.hd > div { min-width: 0; flex: 1; }         /* deja que el título se recorte, no que empuje la ✕ */
/* Un nombre de sala largo se recorta a 2 líneas: no se come la pantalla. */
.hd h1 {
  font-size: 19px; font-weight: 600; line-height: 1.25; overflow-wrap: anywhere;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.live { font-size: 12px; color: var(--danger); display: flex; align-items: center; gap: 6px; margin-top: 3px; }
.live .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--danger); display: inline-block; flex: 0 0 auto; animation: pulse 1.6s ease-in-out infinite; }
/* En fantasma el badge deja de ser rojo/pulsante: los datos ya no son en vivo. */
.live.frozen { color: var(--muted); }
.live.frozen .dot { background: var(--muted); animation: none; }
.freetime { color: var(--muted); }
.freetime.low { color: var(--danger); font-weight: 600; }
@keyframes pulse { 50% { opacity: .35; } }

.label { font-size: 11px; letter-spacing: .04em; color: var(--muted); margin-bottom: 10px; }

.ghost { background: color-mix(in srgb, #e5a04d 18%, transparent); color: #e5a04d; border-radius: 10px; padding: 10px 12px; font-size: 13px; line-height: 1.4; }

.stage { background: var(--card); border-radius: var(--radius); padding: 13px 14px; }
.speakers { display: flex; gap: 14px; flex-wrap: wrap; min-height: 44px; }
.who { text-align: center; width: 56px; }
.av { width: 46px; height: 46px; margin: 0 auto; border-radius: 50%; background: color-mix(in srgb, var(--accent) 22%, transparent); color: var(--accent); display: flex; align-items: center; justify-content: center; font-weight: 600; font-size: 15px; }
.av.on { outline: 2px solid var(--accent); outline-offset: 2px; }
.who .nm { font-size: 11px; margin-top: 5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { font-size: 13px; color: var(--muted); align-self: center; }

.msgs { flex: 1 1 auto; min-height: 0; background: var(--card); border-radius: var(--radius); padding: 13px 14px; display: flex; flex-direction: column; }
.messages { flex: 1; min-height: 88px; max-height: 38dvh; overflow-y: auto; -webkit-overflow-scrolling: touch; display: flex; flex-direction: column; gap: 7px; }
.msg { font-size: 14px; line-height: 1.45; overflow-wrap: anywhere; }
.msg b { color: var(--accent); font-weight: 600; margin-right: 4px; }
.msg .reply { font-size: 12px; opacity: 0.6; border-left: 2px solid var(--accent); padding-left: 6px; margin-bottom: 2px; }

.subs { background: var(--card); border-radius: var(--radius); padding: 12px 14px; }
.subs-off { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.subs-off input { flex: 1 1 100%; min-width: 0; }
.subs-off .sel {
  flex: 1 1 8rem; min-width: 0;
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 34px 12px 14px; font-size: 16px; outline: none;
  appearance: none; -webkit-appearance: none; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8' fill='none' stroke='%238a97a5' stroke-width='2'%3E%3Cpath d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
}
.subs-off .sel:focus { border-color: var(--accent); }
.subs-off .btn { flex: 1 1 9rem; }           /* comparte la línea con el selector, sin desbordar */
.dg-status { font-size: 12px; color: var(--muted); margin-top: 8px; line-height: 1.4; overflow-wrap: anywhere; }
.captions { max-height: 26dvh; overflow-y: auto; -webkit-overflow-scrolling: touch; display: flex; flex-direction: column; gap: 6px; }
.cap { font-size: 14px; line-height: 1.4; overflow-wrap: anywhere; }
.cap b { color: var(--accent); }

/* font-size 16px: por debajo de eso iOS hace zoom automático al enfocar. */
input {
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 14px; font-size: 16px; width: 100%; outline: none;
}
input:focus { border-color: var(--accent); }

.btn { border: none; border-radius: 12px; padding: 13px; font-size: 15px; font-weight: 600; cursor: pointer; width: 100%; min-height: 44px; }
.btn.sm { width: auto; padding: 11px 14px; font-size: 14px; }
.btn.accent { background: var(--accent); color: var(--on-accent); }
.btn.subtle { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn:active { transform: scale(.98); }

/* 44px = objetivo táctil mínimo cómodo. */
.icon-btn { border: none; background: transparent; color: var(--muted); font-size: 20px; width: 44px; height: 44px; flex: 0 0 auto; border-radius: 50%; cursor: pointer; }
.icon-btn.danger { color: var(--danger); }

.ft { display: flex; flex-direction: column; gap: 10px; margin-top: auto; }
.renew { font-size: 12px; color: var(--muted); text-align: center; display: flex; align-items: center; justify-content: center; gap: 7px; flex-wrap: wrap; }

/* ── Teléfonos angostos ──────────────────────────────────────────────────── */
@media (max-width: 400px) {
  #app { gap: 10px; }
  .hd h1 { font-size: 17px; }
  .stage, .msgs, .subs { padding: 11px 12px; }
  .speakers { gap: 10px; }
  .who { width: 50px; }
  .av { width: 42px; height: 42px; font-size: 14px; }
  .messages { max-height: 32dvh; }
}

/* Muy angosto (≤360px): selector y botón, cada uno en su línea. */
@media (max-width: 360px) {
  .subs-off .sel, .subs-off .btn { flex: 1 1 100%; }
}

/* ── Apaisado / poca altura: que no se coma la pantalla ──────────────────── */
@media (max-height: 520px) {
  .messages { max-height: 26dvh; min-height: 60px; }
  .captions { max-height: 22dvh; }
}

@media (prefers-reduced-motion: reduce) {
  .spinner, .spin-sm, .live .dot { animation: none; }
  .btn:active { transform: none; }
}
