:root {
  color-scheme: dark;
  --bg: #090b0f;
  --panel: #10141a;
  --panel-raised: #151a21;
  --line: rgba(255, 255, 255, 0.1);
  --muted: #7e8795;
  --text: #f4f6f8;
  --accent: #e5484d;
  --accent-soft: rgba(229, 72, 77, 0.15);
  --radius: 4px;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button,
a {
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
a:focus-visible,
input:focus-visible {
  outline: 2px solid #ff656a;
  outline-offset: 2px;
}

.app {
  position: relative;
  width: 100%;
  height: 100dvh;
  padding: 3px;
  background: var(--bg);
}

.control-dock {
  position: fixed;
  z-index: 20;
  top: 9px;
  right: 9px;
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 3px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  background: rgba(8, 10, 13, 0.72);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.32);
  backdrop-filter: blur(10px);
  opacity: 0.22;
  transition: opacity 150ms ease;
}

.control-dock:hover,
.control-dock:focus-within {
  opacity: 1;
}

.autoplay-gate {
  position: fixed;
  z-index: 30;
  inset: 0;
  display: grid;
  width: 100%;
  height: 100%;
  place-items: center;
  align-items: center;
  padding: 0;
  border: 0;
  color: white;
  background: rgba(3, 5, 8, 0.74);
  backdrop-filter: blur(8px);
  cursor: pointer;
}

.autoplay-gate-panel {
  display: flex;
  width: 60vw;
  min-width: min(320px, calc(100vw - 24px));
  max-width: calc(100vw - 24px);
  height: 60vh;
  min-height: min(280px, calc(100vh - 24px));
  max-height: calc(100vh - 24px);
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: clamp(14px, 2.5vh, 28px);
  padding: 30px;
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 18px;
  background: rgba(12, 15, 20, 0.94);
  box-shadow: 0 30px 100px rgba(0, 0, 0, 0.72);
  transition: border-color 150ms ease, background 150ms ease, transform 150ms ease;
}

.autoplay-gate-icon {
  display: grid;
  width: clamp(72px, 9vw, 128px);
  height: clamp(72px, 9vw, 128px);
  place-items: center;
  padding-left: 0.12em;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  font-size: clamp(28px, 4vw, 56px);
}

.autoplay-gate-panel strong {
  font-size: clamp(20px, 3vw, 44px);
  font-weight: 850;
  letter-spacing: 0.03em;
  text-align: center;
}

.autoplay-gate-panel small {
  max-width: 520px;
  color: #9ca5b2;
  font-size: clamp(11px, 1.2vw, 16px);
  line-height: 1.45;
  text-align: center;
}

.autoplay-gate:hover .autoplay-gate-panel {
  border-color: rgba(255, 255, 255, 0.38);
  background: rgba(20, 24, 31, 0.97);
  transform: scale(1.01);
}

.tool-button,
.dialog-close {
  border: 1px solid var(--line);
  color: #c7cdd5;
  background: rgba(255, 255, 255, 0.04);
  cursor: pointer;
}

.tool-button {
  display: grid;
  width: 30px;
  height: 30px;
  place-items: center;
  align-items: center;
  padding: 0;
  border-radius: 7px;
  font-size: 16px;
  font-weight: 700;
}

.tool-button:hover,
.dialog-close:hover {
  border-color: rgba(255, 255, 255, 0.23);
  background: rgba(255, 255, 255, 0.09);
  color: white;
}

.sound-button {
  width: auto;
  min-width: 38px;
  padding: 0 9px;
  border-color: rgba(229, 72, 77, 0.42);
  background: rgba(229, 72, 77, 0.16);
}

.button-icon {
  font-size: 16px;
  line-height: 1;
}

.streams {
  display: grid;
  min-width: 0;
  min-height: 0;
  width: 100%;
  height: 100%;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 3px;
}

.stream-card {
  position: relative;
  display: grid;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border: 0;
  border-radius: var(--radius);
  background: var(--panel);
}

.player-shell {
  position: relative;
  min-height: 0;
  overflow: hidden;
  background: #050607;
}

.player-shell iframe {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
}

.stream-ended-placeholder {
  position: absolute;
  z-index: 4;
  inset: 0;
  display: grid;
  place-items: center;
  color: rgba(255, 255, 255, 0.58);
  background: #0a0c0f;
  font-size: clamp(11px, 1vw, 15px);
  letter-spacing: 0.08em;
}

.stream-ended-placeholder[hidden] {
  display: none;
}

.stream-card.is-ended iframe,
.stream-card.is-ended .loading-shade,
.stream-card.is-ended .stream-hover-label,
.stream-card.is-ended .focus-hitbox {
  display: none;
}

.loading-shade {
  position: absolute;
  z-index: 1;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
  background: #080a0d;
  transition: opacity 400ms ease;
}

.is-loaded .loading-shade {
  opacity: 0;
}

.stream-hover-label {
  position: absolute;
  z-index: 3;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: hidden;
  padding: 46px 14px 12px;
  color: white;
  background: linear-gradient(to bottom, transparent, rgba(3, 5, 8, 0.92));
  font-size: clamp(11px, 1vw, 15px);
  font-weight: 750;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transform: translateY(4px);
  transition: opacity 140ms ease, transform 140ms ease;
}

.streams:not(.is-focused) .stream-card:hover .stream-hover-label {
  opacity: 1;
  transform: translateY(0);
}

.loading-shade span {
  width: 22px;
  height: 22px;
  border: 2px solid rgba(255, 255, 255, 0.12);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.focus-hitbox {
  position: absolute;
  z-index: 2;
  inset: 0;
  display: grid;
  place-items: center;
  border: 0;
  background: transparent;
  color: white;
  cursor: zoom-in;
}

.focus-hitbox::before {
  position: absolute;
  inset: 0;
  background: rgba(2, 4, 7, 0.42);
  content: "";
  opacity: 0;
  transition: opacity 150ms ease;
}

.focus-action {
  position: relative;
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 50%;
  background: rgba(8, 10, 14, 0.86);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
  font-size: 15px;
  font-weight: 800;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 150ms ease, transform 150ms ease;
}

.focus-hitbox:hover::before,
.focus-hitbox:focus-visible::before,
.focus-hitbox:hover .focus-action,
.focus-hitbox:focus-visible .focus-action {
  opacity: 1;
}

.focus-hitbox:hover .focus-action,
.focus-hitbox:focus-visible .focus-action {
  transform: translateY(0);
}

.streams.is-focused {
  position: relative;
  grid-template-columns: minmax(0, 1fr) clamp(180px, 20vw, 300px);
  grid-template-rows: repeat(3, auto) 1fr;
  gap: 0;
}

.streams.is-focused .stream-card {
  grid-column: 2;
  z-index: 4;
  width: calc(100% - 6px);
  aspect-ratio: 16 / 9;
  justify-self: end;
  margin: 6px 6px 0 0;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 5px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
}

.streams.is-focused .stream-card.is-active {
  position: absolute;
  z-index: 1;
  inset: 0;
  grid-column: 1 / -1;
  grid-row: 1 / -1 !important;
  width: auto;
  aspect-ratio: auto;
  justify-self: stretch;
  margin: 0;
  border: 0;
  border-radius: var(--radius);
  box-shadow: none;
}

.streams.is-focused .stream-card.is-active .focus-hitbox {
  display: none;
}

.streams.is-focused .stream-card:not(.is-active) .focus-action {
  width: 28px;
  height: 28px;
  font-size: 10px;
}

.viewer-fantasy {
  position: absolute;
  z-index: 8;
  top: 6px;
  right: 6px;
  width: min(620px, 40vw);
  height: min(216px, calc(100dvh - 12px));
  overflow: hidden;
  pointer-events: none;
}

.viewer-fantasy iframe {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  background: transparent;
}

.app.is-viewer .control-dock {
  top: auto;
  right: 9px;
  bottom: 54px;
  opacity: 0.78;
}

.app.is-viewer .streams,
.app.is-viewer .streams.is-focused {
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
  gap: 0;
}

.app.is-viewer .streams .stream-card,
.app.is-viewer .streams.is-focused .stream-card,
.app.is-viewer .streams.is-focused .stream-card.is-active {
  position: relative;
  inset: auto;
  grid-column: 1;
  grid-row: 1 !important;
  width: 100%;
  height: 100%;
  aspect-ratio: auto;
  justify-self: stretch;
  margin: 0;
  border: 0;
  border-radius: var(--radius);
  box-shadow: none;
}

.app.is-viewer .stream-hover-label,
.app.is-viewer .focus-hitbox {
  display: none;
}

.link-dialog {
  width: min(660px, calc(100vw - 28px));
  max-height: calc(100dvh - 28px);
  padding: 0;
  overflow: auto;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 14px;
  color: var(--text);
  background: #12161c;
  box-shadow: 0 30px 100px rgba(0, 0, 0, 0.65);
}

.link-dialog::backdrop {
  background: rgba(3, 5, 7, 0.74);
  backdrop-filter: blur(7px);
}

.link-dialog form {
  padding: 24px;
}

.dialog-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
}

.eyebrow {
  margin: 0 0 7px;
  color: #ef656a;
  font-size: 9px;
  font-weight: 850;
  letter-spacing: 0.18em;
}

.dialog-heading h1 {
  margin: 0;
  font-size: 21px;
  letter-spacing: -0.02em;
}

.dialog-close {
  width: 30px;
  height: 30px;
  border-radius: 7px;
  font-size: 20px;
  line-height: 1;
}

.dialog-intro {
  margin: 9px 0 20px;
  color: var(--muted);
  font-size: 12px;
}

.stream-fields {
  display: grid;
  gap: 10px;
}

.stream-field {
  display: grid;
  grid-template-columns: 70px 1fr;
  align-items: center;
  gap: 10px;
}

.stream-field label {
  color: #c9ced6;
  font-size: 11px;
  font-weight: 800;
}

.stream-field input {
  width: 100%;
  height: 38px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 7px;
  color: #f0f2f5;
  background: #0b0e12;
  font-size: 12px;
}

.stream-field input:hover {
  border-color: rgba(255, 255, 255, 0.2);
}

.form-error {
  min-height: 15px;
  margin: 11px 0 0 80px;
  color: #ff7479;
  font-size: 10px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 7px;
}

.primary-button,
.secondary-button {
  height: 36px;
  padding: 0 15px;
  border-radius: 7px;
  font-size: 11px;
  font-weight: 800;
  cursor: pointer;
}

.primary-button {
  border: 1px solid #ef5b60;
  color: white;
  background: #d64247;
}

.primary-button:hover {
  background: #e44a4f;
}

.secondary-button {
  margin-right: auto;
  border: 1px solid var(--line);
  color: #aeb5c0;
  background: transparent;
}

.secondary-button:hover {
  color: white;
  background: rgba(255, 255, 255, 0.06);
}

@media (max-width: 760px) {
  .control-dock { top: 6px; right: 6px; }
  .streams.is-focused {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-rows: minmax(0, 1fr) auto;
  }
  .streams.is-focused .stream-card:not(.is-active) {
    grid-column: auto !important;
    grid-row: 2 !important;
    width: calc(100% - 4px);
    margin: 0 2px 2px;
    align-self: end;
  }
  .link-dialog form { padding: 18px; }
  .stream-field { grid-template-columns: 1fr; gap: 5px; }
  .form-error { margin-left: 0; }

  .viewer-fantasy {
    top: auto;
    right: 4px;
    bottom: 44px;
    width: calc(100% - 8px);
    height: min(216px, 48dvh);
  }
}

@media (max-height: 560px) {
  .control-dock { top: 6px; right: 6px; }
}

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