:root {
  color-scheme: dark;
  --twitch-purple: #9146ff;
  --bg-dark: #0e0e10;
  --bg-box: #18181b;
  --border: #2f2f35;
  --text: #efeff1;
  --text-dim: #adadb8;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
}

body {
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg-dark);
  color: var(--text);
  overflow: hidden;
}

.hidden {
  display: none !important;
}

/* ---------- Connect screen ---------- */

.connect-screen {
  height: 100vh;
  height: 100dvh;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.connect-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
  width: 420px;
  max-width: 90vw;
}

.twitch-logo {
  width: 144px;
  height: 144px;
  margin-bottom: -36px;
}

.app-version {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-dim);
  letter-spacing: 0.05em;
  position: absolute;
  bottom: 16px;
  left: 0;
  right: 0;
  text-align: center;
}

.channel-field {
  position: relative;
  display: flex;
  align-items: stretch;
  width: 240px;
  max-width: 100%;
  margin-bottom: 8px;
}

#channel-input {
  flex: 1;
  min-width: 0;
  padding: 12px 16px 12px 40px;
  border-radius: 8px 0 0 8px;
  border: 1px solid var(--border);
  border-right: none;
  background: #0e0e10;
  color: var(--text);
  font-size: 16px;
  outline: none;
}

.clear-channel-btn {
  position: absolute;
  left: 6px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 1;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-dim);
  font-size: 18px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.clear-channel-btn:hover {
  background: rgba(255, 255, 255, 0.08);
}

.clear-channel-btn:active {
  color: var(--text);
}

#add-channel-btn {
  flex-shrink: 0;
  width: 46px;
  padding: 0;
  border: 1px solid var(--border);
  border-left: none;
  border-radius: 0 8px 8px 0;
  background: #0e0e10;
  color: var(--twitch-purple);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  user-select: none;
}

#add-channel-btn:hover {
  background: #18181b;
}

#add-channel-btn.saved {
  color: #38d996;
}

.channel-field:focus-within #channel-input,
.channel-field:focus-within #add-channel-btn {
  border-color: var(--twitch-purple);
}

#channel-suggestions {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 50;
  max-height: 40vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  list-style: none;
  padding: 4px 0;
  background: var(--bg-box);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.channel-option {
  display: flex;
  align-items: center;
  min-height: 40px;
  padding: 2px 8px;
  cursor: pointer;
}

.channel-option.highlighted {
  background: #26262b;
}

.channel-option-name {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
  font-size: 16px;
}

.remove-channel {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  margin-right: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: #ff5c5c;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}

.remove-channel:hover {
  background: rgba(255, 92, 92, 0.12);
}

.remove-channel:active {
  background: rgba(255, 92, 92, 0.2);
}

#channel-input:focus {
  border-color: var(--twitch-purple);
}

#connect-btn {
  width: 240px;
  max-width: 100%;
  padding: 12px 16px;
  border: none;
  border-radius: 8px;
  background: var(--twitch-purple);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

#connect-btn:hover {
  background: #772ce8;
}

#connect-btn:disabled {
  opacity: 0.6;
  cursor: default;
}

.error-msg {
  color: #ff5c5c;
  font-size: 14px;
  text-align: center;
  margin-top: 12px;
}

/* ---------- Instalar como Web App ---------- */

.install-btn {
  width: 240px;
  max-width: 100%;
  padding: 12px 16px;
  border: 1px solid var(--twitch-purple);
  border-radius: 8px;
  background: transparent;
  color: var(--twitch-purple);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 12px;
}

.install-btn:hover {
  background: var(--twitch-purple);
  color: #fff;
}

.install-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(2px);
}

.install-modal-box {
  display: flex;
  flex-direction: column;
  gap: 16px;
  background: var(--bg-box);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px 28px;
  width: 380px;
  max-width: 100%;
}

.install-modal-box h2 {
  font-size: 18px;
  font-weight: 600;
  text-align: center;
}

.install-modal-box ol {
  margin: 0;
  padding-left: 20px;
  font-size: 14px;
  color: var(--text-dim);
  line-height: 1.7;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.install-modal-box ol strong {
  color: var(--text);
}

.install-modal-note {
  margin: 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12.5px;
  color: var(--text-dim);
  line-height: 1.5;
}

.ios-share-icon {
  display: inline-block;
  width: 14px;
  height: 14px;
  margin: 0 3px;
  border: 1.5px solid currentColor;
  border-radius: 3px;
  position: relative;
  vertical-align: -2px;
}

.ios-share-icon::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 3px;
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-bottom: 6px solid currentColor;
  transform: translateX(-50%);
}

.modal-close-btn {
  width: 100%;
  padding: 12px 16px;
  border: none;
  border-radius: 8px;
  background: var(--twitch-purple);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

.modal-close-btn:hover {
  background: #772ce8;
}

/* ---------- Actualización disponible ---------- */

.update-toast {
  position: fixed;
  top: calc(12px + env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-box);
  color: var(--text);
  font-size: 14px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
}

#update-reload {
  padding: 6px 12px;
  border: none;
  border-radius: 6px;
  background: var(--twitch-purple);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

#update-reload:hover {
  background: #772ce8;
}

/* ---------- Stream layout ---------- */

.stream-layout {
  display: grid;
  grid-template-areas: "video chat";
  grid-template-columns: 3fr 1fr;
  grid-template-rows: minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}

.video-container {
  grid-area: video;
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  background: #000;
}

.chat-container {
  grid-area: chat;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-box);
  border-left: 1px solid var(--border);
}

#chat {
  flex: 1;
  width: 100%;
  border: none;
}

#player {
  flex: 1;
  width: 100%;
  height: 100%;
  min-height: 0;
  max-height: 100%;
  object-fit: contain;
  background: #000;
}

/* ---------- Quality bar ---------- */

.controls-float {
  position: absolute;
  top: calc(8px + env(safe-area-inset-top, 0px));
  left: calc(8px + env(safe-area-inset-left, 0px));
  right: calc(8px + env(safe-area-inset-right, 0px));
  z-index: 10;
  pointer-events: none;
  display: flex;
  align-items: stretch;
  gap: 4px;
}

.controls-float > * {
  pointer-events: auto;
}

.quality-bar {
  width: max-content;
  max-width: 100%;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 4px 6px;
  background: rgba(14, 14, 16, 0.7);
  border: 1px solid rgba(47, 47, 53, 0.7);
  border-radius: 8px;
  backdrop-filter: blur(4px);
  font-size: 13px;
  color: var(--text-dim);
}

.controls-toggle-btn {
  display: inline-flex;
  flex: 0 0 auto;
  align-items: center;
  justify-content: center;
  padding: 4px;
  border: 1px solid rgba(47, 47, 53, 0.7);
  background: rgba(14, 14, 16, 0.7);
  backdrop-filter: blur(4px);
  color: var(--text);
  border-radius: 6px;
  cursor: pointer;
}

.controls-toggle-btn:hover {
  color: var(--twitch-purple);
}

.controls-toggle-btn .eye-closed {
  display: none;
}

.controls-toggle-btn.controls-collapsed .eye-open {
  display: none;
}

.controls-toggle-btn.controls-collapsed .eye-closed {
  display: block;
}

.controls-toggle-btn.controls-collapsed ~ .quality-bar {
  display: none;
}

#quality-select {
  field-sizing: content;
  min-inline-size: 5ch;
  max-inline-size: 100%;
  background: #0e0e10;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 14px;
  outline: none;
}

.latency-display {
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.latency-display::before {
  content: "Delay ";
}

.go-live-btn {
  padding: 6px 12px;
  border: 1px solid var(--twitch-purple);
  background: transparent;
  color: var(--twitch-purple);
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}

.go-live-btn:hover {
  background: var(--twitch-purple);
  color: #fff;
}

/* ---------- Chat toggle ---------- */

.chat-toggle-btn {
  padding: 6px 12px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}

.chat-toggle-btn:hover {
  border-color: var(--twitch-purple);
  color: var(--text);
}

.chat-toggle-btn.active {
  border-color: var(--twitch-purple);
  color: var(--twitch-purple);
}

/* ---------- Chat IRC (mobile landscape) ---------- */

.chat-container {
  position: relative;
}

.chat-irc {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px 10px;
  font-size: 14px;
  line-height: 1.45;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}

.chat-irc-msg {
  display: block;
  padding: 2px 0;
  word-break: break-word;
}

.chat-irc-name {
  display: inline-block;
  vertical-align: top;
  font-weight: 600;
  max-width: 45%;
  margin-right: 6px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-irc-text {
  display: inline;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

.chat-irc-emote {
  height: 1.5em;
  vertical-align: -0.35em;
}

.chat-irc-badge {
  width: 18px;
  height: 18px;
  margin-right: 2px;
  vertical-align: -4px;
  flex: none;
}

.chat-irc-reply {
  display: block;
  padding: 0 0 1px;
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.chat-irc-bits {
  display: inline;
  margin-left: 4px;
  color: var(--twitch-purple);
  font-weight: 600;
}

.chat-irc-gif {
  max-height: 2em;
  vertical-align: -0.35em;
}

.chat-irc-sys {
  padding: 4px 2px;
  font-size: 12px;
  color: var(--text-dim);
  text-align: center;
}

.chat-irc-sys--sub {
  color: #a970ff;
}

.chat-irc-sys--raid {
  color: #00f593;
}

.chat-irc-sys--announcement {
  border-top: 1px solid var(--notice-color, var(--twitch-purple));
  border-bottom: 1px solid var(--notice-color, var(--twitch-purple));
  color: var(--notice-color, var(--twitch-purple));
  font-weight: 600;
}

.chat-irc-sys--ritual {
  color: var(--text-dim);
}

.chat-irc-live-btn {
  position: absolute;
  left: 50%;
  bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 5;
  padding: 6px 12px;
  border: 1px solid var(--twitch-purple);
  background: rgba(14, 14, 16, 0.9);
  color: var(--twitch-purple);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
}

.chat-irc-live-btn:hover {
  background: var(--twitch-purple);
  color: #fff;
}

/* ---------- Toggle chat nativo ⇄ IRC (mobile portrait) ---------- */

.chat-type-btn {
  position: absolute;
  top: 3px;
  left: calc(4px + env(safe-area-inset-left, 0px));
  z-index: 5;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  min-height: 32px;
  padding: 7px;
  border: 1px solid var(--border);
  background: rgba(14, 14, 16, 0.85);
  backdrop-filter: blur(4px);
  color: var(--text-dim);
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
}

.chat-type-btn:hover {
  border-color: var(--twitch-purple);
  color: var(--text);
}

.chat-type-btn.active {
  border-color: var(--twitch-purple);
  color: var(--twitch-purple);
}

.chat-type-btn .chat-type-native {
  display: none;
}

.chat-type-btn.active .chat-type-irc {
  display: none;
}

.chat-type-btn.active .chat-type-native {
  display: block;
}

/* ---------- Fullscreen (solo landscape, Android instalado) ---------- */

.fullscreen-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}

.fullscreen-btn:hover {
  border-color: var(--twitch-purple);
  color: var(--text);
}

.fullscreen-btn.active {
  border-color: var(--twitch-purple);
  color: var(--twitch-purple);
}

@media not ((max-width: 767px) and (orientation: portrait)) {
  .stream-layout.chat-left {
    grid-template-areas: "chat video";
    grid-template-columns: 1fr 3fr;
  }

  .stream-layout.chat-collapsed {
    grid-template-areas: "video";
    grid-template-columns: 1fr;
  }

  .stream-layout.chat-collapsed .chat-container {
    display: none;
  }
}

@media (max-width: 900px) {
  .stream-layout {
    --chat-col-max: 200px;
    grid-template-columns: minmax(0, 1fr) minmax(0, var(--chat-col-max));
  }

  .stream-layout.chat-left {
    grid-template-columns: minmax(0, var(--chat-col-max)) minmax(0, 1fr);
  }
}

/* ---------- Táctil (móvil/tablet): labels solo en escritorio ---------- */

@media (pointer: coarse) {
  .latency-display::before {
    content: "";
  }

  .quality-bar label[for="quality-select"] {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }
}

/* ---------- Móvil vertical (formato teléfono en portrait) ---------- */

@media (max-width: 767px) and (orientation: portrait) {
  .stream-layout {
    grid-template-areas: "video" "chat";
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 3fr) minmax(0, 7fr);
  }

  .chat-container {
    border-left: none;
    border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  .controls-float {
    top: calc(4px + env(safe-area-inset-top, 0px));
    left: calc(4px + env(safe-area-inset-left, 0px));
    right: calc(4px + env(safe-area-inset-right, 0px));
  }

  .quality-bar {
    gap: 6px;
    padding: 3px 5px;
    font-size: 12px;
    max-width: 100%;
  }

  #quality-select {
    font-size: 12px;
    padding: 4px 6px;
  }

  .latency-display {
    font-size: 12px;
  }

  .go-live-btn,
  .chat-toggle-btn {
    padding: 4px 8px;
    font-size: 12px;
  }

  .controls-toggle-btn {
    padding: 4px;
    font-size: 12px;
  }

  .chat-toggle-btn {
    display: none;
  }
}

/* ---------- Diagnóstico (diag) ---------- */

.diag-badge {
  position: fixed;
  right: calc(12px + env(safe-area-inset-right, 0px));
  bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  z-index: 300;
  padding: 8px 14px;
  border: 1px solid var(--twitch-purple);
  border-radius: 999px;
  background: rgba(14, 14, 16, 0.92);
  color: var(--twitch-purple);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.06em;
  cursor: pointer;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
}

.diag-badge:hover {
  background: var(--twitch-purple);
  color: #fff;
}

.diag-panel {
  position: fixed;
  right: calc(12px + env(safe-area-inset-right, 0px));
  bottom: calc(56px + env(safe-area-inset-bottom, 0px));
  z-index: 300;
  display: flex;
  flex-direction: column;
  width: min(460px, calc(100vw - 24px));
  max-height: 70vh;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-box);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
  overflow: hidden;
}

.diag-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.diag-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.diag-actions button {
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--text-dim);
  font-size: 11px;
  cursor: pointer;
}

.diag-actions button:hover {
  border-color: var(--twitch-purple);
  color: var(--text);
}

#diag-output {
  flex: 1;
  min-height: 120px;
  padding: 8px;
  border: none;
  outline: none;
  background: #0e0e10;
  color: #9cdcfe;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 11px;
  line-height: 1.45;
  white-space: pre;
  overflow: auto;
  resize: none;
}
