:root {
  color-scheme: dark;
  --ink: #f6f3ec;
  --muted: #9898a7;
  --canvas: #090a0f;
  --surface: rgba(19, 20, 28, 0.88);
  --surface-2: #171821;
  --line: rgba(255, 255, 255, 0.1);
  --purple: #a970ff;
  --purple-2: #7c3aed;
  --lime: #c7f36b;
  --red: #ff4f64;
  --green: #5be49b;
  --shadow: 0 30px 80px rgba(0, 0, 0, 0.45);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

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

html, body { min-height: 100%; }

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 15% -10%, rgba(124, 58, 237, 0.16), transparent 32rem),
    radial-gradient(circle at 90% 20%, rgba(199, 243, 107, 0.07), transparent 28rem),
    var(--canvas);
  overflow-x: hidden;
}

button, input { font: inherit; }
button { color: inherit; }

.ambient {
  position: fixed;
  width: 24rem;
  height: 24rem;
  border-radius: 50%;
  filter: blur(100px);
  opacity: 0.14;
  pointer-events: none;
}

.ambient-one { background: var(--purple); top: 15%; left: -15rem; }
.ambient-two { background: var(--lime); right: -18rem; bottom: 5%; }

.lobby {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 2rem;
  position: relative;
  z-index: 1;
}

.lobby-card {
  width: min(26rem, 100%);
  display: flex;
  flex-direction: column;
  gap: 2.4rem;
  padding: 1rem;
}

.eyebrow { color: var(--lime); font-size: 0.68rem; font-weight: 800; letter-spacing: 0.17em; margin: 0 0 1rem; }

.entry-heading h1 {
  margin: 0;
  font-size: 2rem;
  line-height: 1.05;
  letter-spacing: -0.045em;
}

.entry-heading > p:last-child { margin: .7rem 0 0; color: var(--muted); font-size: .82rem; line-height: 1.55; }

.join-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

label { display: flex; flex-direction: column; gap: 0.45rem; }
label > span { color: #c7c7d0; font-size: 0.75rem; font-weight: 680; }

input[type="text"], input[type="url"], input[type="password"], input:not([type]) {
  width: 100%;
  padding: 0.82rem 0.9rem;
  border: 1px solid var(--line);
  border-radius: 0.7rem;
  outline: 0;
  color: var(--ink);
  background: rgba(4, 5, 8, 0.55);
  transition: border-color .2s, box-shadow .2s;
}

input:focus { border-color: var(--purple); box-shadow: 0 0 0 3px rgba(169, 112, 255, .14); }
.host-fields { display: flex; flex-direction: column; gap: 0.85rem; }
.form-error { min-height: 1.1rem; margin: 0; color: #ff8493; font-size: 0.75rem; }

.primary-button, .secondary-button {
  border: 0;
  border-radius: 0.72rem;
  cursor: pointer;
  font-weight: 750;
  transition: transform .16s, background .16s;
}

.primary-button { display: flex; justify-content: space-between; padding: 0.9rem 1rem; color: #111318; background: var(--lime); }
.primary-button:hover { transform: translateY(-1px); background: #d5ff7d; }
.secondary-button { padding: 0.66rem 0.85rem; border: 1px solid var(--line); background: rgba(255,255,255,.045); }
.secondary-button:hover { background: rgba(255,255,255,.09); }
.fine-print { margin: 0; color: #6f707d; font-size: .7rem; line-height: 1.45; }

.app { position: relative; z-index: 1; min-height: 100vh; padding: 1.1rem; }
.topbar { min-height: 3.8rem; display: flex; align-items: center; justify-content: flex-end; gap: 1rem; margin-bottom: 1rem; padding: 0 .4rem; }
.session-meta { display: flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: .65rem; }

.status-pill, .room-pill, .viewer-count {
  display: inline-flex;
  align-items: center;
  gap: .45rem;
  min-height: 2rem;
  padding: .42rem .65rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: rgba(13, 14, 20, .72);
  font-size: .72rem;
}

.status-pill i, .sync-label i, #host-auth i { width: .42rem; height: .42rem; border-radius: 50%; background: #e7b44e; box-shadow: 0 0 10px currentColor; }
.status-pill.online i, .sync-label.synced i, #host-auth.ready i { background: var(--green); }
.status-pill.offline i { background: var(--red); }
.room-pill { cursor: pointer; letter-spacing: .05em; }
.room-pill strong { color: var(--ink); }
.viewer-count { border-color: transparent; background: transparent; }

.watch-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: .85rem;
  align-items: stretch;
}

.chat-panel { grid-column: 1 / -1; }

.panel {
  min-width: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 1rem;
  background: var(--surface);
  box-shadow: 0 18px 60px rgba(0,0,0,.2);
}

.panel-heading { min-height: 3.4rem; display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .75rem .9rem; border-bottom: 1px solid var(--line); }
.panel-heading > div { display: flex; align-items: center; min-width: 0; gap: .65rem; }
.panel-heading strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .83rem; }
.panel-heading a { color: var(--muted); font-size: .72rem; text-decoration: none; }
.subtle { overflow: hidden; text-overflow: ellipsis; color: var(--muted); font-size: .72rem; }
.live-badge, .source-badge, .chat-badge { flex: 0 0 auto; display: inline-flex; align-items: center; gap: .3rem; padding: .25rem .4rem; border-radius: .35rem; font-size: .59rem; font-weight: 850; letter-spacing: .08em; }
.live-badge { color: white; background: var(--red); }
.live-badge i { width: .3rem; height: .3rem; border-radius: 50%; background: white; }
.source-badge { color: #12140f; background: var(--lime); }
.chat-badge { color: white; background: var(--purple-2); }
.sync-label { display: flex; align-items: center; gap: .4rem; color: var(--muted); font-size: .68rem; }

.player-frame, .video-shell, .chat-frame { position: relative; background: #030406; }
.player-frame, .video-shell { aspect-ratio: 16 / 9; }
.player-frame iframe, .chat-frame iframe { display: block; width: 100%; height: 100%; border: 0; }
.chat-frame { height: calc(100% - 3.4rem); min-height: 28rem; }
.empty-state { display: grid; place-items: center; }
.empty-copy { display: grid; place-items: center; gap: .8rem; color: var(--muted); font-size: .78rem; }
.loader { width: 1.5rem; height: 1.5rem; border: 2px solid rgba(255,255,255,.12); border-top-color: var(--purple); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

#source-video { display: block; width: 100%; height: 100%; object-fit: contain; background: #020304; }
.video-placeholder { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: .8rem; color: var(--muted); background: radial-gradient(circle, #12141c, #050609 68%); }
.orbit-icon { width: 3rem; height: 3rem; display: grid; place-items: center; border: 1px solid rgba(169,112,255,.42); border-radius: 50%; box-shadow: 0 0 45px rgba(169,112,255,.2); }
.orbit-icon i { width: .65rem; height: .65rem; border-radius: 50%; background: var(--purple); box-shadow: 0 0 12px var(--purple); }
.video-placeholder p { margin: 0; font-size: .78rem; }
.audio-gate { position: absolute; left: 50%; bottom: 1rem; transform: translateX(-50%); padding: .62rem .9rem; border: 1px solid rgba(255,255,255,.2); border-radius: 999px; color: white; background: rgba(9,10,15,.78); backdrop-filter: blur(10px); cursor: pointer; }

.sync-bar { min-height: 4rem; display: grid; grid-template-columns: auto minmax(7rem, 1fr) minmax(7rem, .8fr) auto; align-items: center; gap: 1rem; padding: .7rem .9rem; border-top: 1px solid var(--line); }
.sync-stat span, .offset-control > span, .volume-control label > span { display: block; color: var(--muted); font-size: .64rem; }
.sync-stat strong { display: block; margin-top: .12rem; font-size: .8rem; }
.offset-control { gap: .36rem; }
.offset-control > span, .volume-control label > span { display: flex; justify-content: space-between; }
.offset-control output, .volume-control output { color: #d4d4dd; }
input[type="range"] { width: 100%; accent-color: var(--purple); }
.volume-control { display: grid; grid-template-columns: 1.8rem minmax(5rem, 1fr); align-items: center; gap: .5rem; }
.volume-control label { min-width: 0; gap: .36rem; }
.volume-control button {
  width: 1.8rem;
  height: 1.8rem;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  color: #d4d4dd;
  background: rgba(255,255,255,.045);
  cursor: pointer;
}
.volume-control button:hover { background: rgba(255,255,255,.1); }
.source-panel { container-name: source-panel; container-type: inline-size; }

#host-auth { display: flex; align-items: center; gap: .4rem; white-space: nowrap; }

.icon-button { width: 2rem; height: 2rem; padding: 0; border: 1px solid var(--line); border-radius: 50%; color: var(--muted); background: transparent; cursor: pointer; font-size: 1.2rem; }
.icon-button:hover { color: var(--ink); background: rgba(255,255,255,.06); }
.host-view .sync-bar { display: none; }

.sync-label.playing i, .sync-label.ready i { background: var(--green); }
.sync-label.paused i { background: #a970ff; }
.sync-label.error i { background: #ff4f64; }
.topbar-leave { padding: .45rem .65rem; font-size: .72rem; }
.layout-menu { position: relative; }
.layout-menu > summary {
  min-height: 2rem;
  display: inline-flex;
  align-items: center;
  padding: .42rem .7rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: rgba(13, 14, 20, .72);
  cursor: pointer;
  font-size: .72rem;
  font-weight: 700;
  list-style: none;
}
.layout-menu > summary::-webkit-details-marker { display: none; }
.layout-menu[open] > summary, .layout-menu > summary:hover { color: var(--ink); background: rgba(255,255,255,.08); }
.layout-popover {
  position: absolute;
  z-index: 100;
  top: calc(100% + .5rem);
  right: 0;
  width: min(20rem, calc(100vw - 1.5rem));
  padding: .85rem;
  border: 1px solid var(--line);
  border-radius: .85rem;
  background: #171821;
  box-shadow: var(--shadow);
}
.layout-popover p { margin: 0 0 .75rem; color: var(--muted); font-size: .7rem; line-height: 1.45; }
.layout-actions { display: grid; grid-template-columns: 1fr 1fr; gap: .45rem; }
.layout-actions button {
  padding: .58rem .65rem;
  border: 1px solid var(--line);
  border-radius: .58rem;
  color: #d9d9e1;
  background: rgba(255,255,255,.045);
  cursor: pointer;
  font-size: .7rem;
  font-weight: 700;
}
.layout-actions button:hover { background: rgba(255,255,255,.1); }
#save-layout { grid-column: 1 / -1; color: #111318; background: var(--lime); }
#save-layout:hover { background: #d5ff7d; }
#reset-layout { grid-column: 1 / -1; color: var(--muted); }
footer { display: flex; justify-content: space-between; align-items: center; min-height: 3rem; padding: .5rem; color: #676875; font-size: .68rem; }
footer button { border: 0; color: #8b8c98; background: transparent; cursor: pointer; }
.toast { position: fixed; z-index: 10; left: 50%; bottom: 1.4rem; transform: translate(-50%, 1rem); padding: .7rem 1rem; border: 1px solid var(--line); border-radius: .7rem; color: white; background: #22232d; box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: .2s; font-size: .76rem; }
.toast.show { transform: translate(-50%, 0); opacity: 1; }

@media (max-width: 760px) {
  .lobby { padding: .8rem; }
  .lobby-card { gap: 2rem; padding: 1rem .4rem; }
  .topbar { align-items: flex-start; }
  .session-meta { gap: .35rem; }
  .status-pill, .viewer-count { display: none; }
  .layout-menu { display: none; }
  .watch-grid { grid-template-columns: 1fr; }
  .host-view .watch-grid { grid-template-columns: 1fr; }
  .chat-panel { grid-column: auto; }
  .player-frame, .video-shell { min-height: 13rem; }
  .sync-bar { grid-template-columns: 1fr auto; }
  .offset-control, .volume-control { grid-column: 1 / -1; }
  footer > span { max-width: 65%; }
}

@media (min-width: 761px) {
  body.room-active { height: 100dvh; overflow: hidden; }

  .app {
    height: 100dvh;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: .55rem;
    overflow: hidden;
    padding: .65rem;
  }

  .topbar {
    position: relative;
    z-index: 2000;
    flex: 0 0 auto;
    min-height: 2.5rem;
    margin: 0;
    padding: 0 .25rem;
  }

  .watch-grid {
    position: relative;
    display: block;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
  }

  .panel {
    position: absolute;
    min-height: 0;
    transition: border-color .15s, box-shadow .15s;
  }
  .panel:hover, .panel:focus-within { border-color: rgba(255,255,255,.18); }
  .twitch-panel, .source-panel, .chat-panel { display: grid; }
  .twitch-panel { grid-template-rows: auto minmax(0, 1fr); }
  .source-panel { grid-template-rows: auto minmax(0, 1fr) auto; }
  .chat-panel { grid-template-rows: auto minmax(0, 1fr); }

  .player-frame, .video-shell {
    height: 100%;
    min-height: 0;
    aspect-ratio: auto;
  }

  .chat-frame {
    height: auto;
    min-height: 0;
  }

  .host-view .sync-bar { grid-template-columns: auto minmax(8rem, 1fr) auto; }

  .panel-heading {
    position: relative;
    z-index: 4;
    cursor: grab;
    touch-action: none;
    user-select: none;
  }
  .panel-heading:active { cursor: grabbing; }
  .panel-heading:focus-visible { outline: 2px solid var(--purple); outline-offset: -3px; }
  .panel-heading::before {
    flex: 0 0 auto;
    margin-right: -.42rem;
    color: #666875;
    content: "⠿";
    font-size: .8rem;
    line-height: 1;
  }

  .resize-handle { position: absolute; z-index: 8; touch-action: none; }
  .resize-handle[data-direction="n"] { top: 0; right: .8rem; left: .8rem; height: .5rem; cursor: ns-resize; }
  .resize-handle[data-direction="s"] { right: .8rem; bottom: 0; left: .8rem; height: .65rem; cursor: ns-resize; }
  .resize-handle[data-direction="e"] { top: .8rem; right: 0; bottom: .8rem; width: .65rem; cursor: ew-resize; }
  .resize-handle[data-direction="w"] { top: .8rem; bottom: .8rem; left: 0; width: .65rem; cursor: ew-resize; }
  .resize-handle[data-direction="ne"],
  .resize-handle[data-direction="nw"],
  .resize-handle[data-direction="se"],
  .resize-handle[data-direction="sw"] { width: 1rem; height: 1rem; }
  .resize-handle[data-direction="ne"] { top: 0; right: 0; cursor: nesw-resize; }
  .resize-handle[data-direction="nw"] { top: 0; left: 0; cursor: nwse-resize; }
  .resize-handle[data-direction="se"] { right: 0; bottom: 0; cursor: nwse-resize; }
  .resize-handle[data-direction="sw"] { bottom: 0; left: 0; cursor: nesw-resize; }
  .resize-handle[data-direction="se"]::after {
    position: absolute;
    right: .28rem;
    bottom: .28rem;
    width: .42rem;
    height: .42rem;
    border-right: 2px solid rgba(255,255,255,.34);
    border-bottom: 2px solid rgba(255,255,255,.34);
    content: "";
  }
  body.layout-interacting { cursor: grabbing; user-select: none; }
  body.layout-interacting iframe { pointer-events: none; }

  footer {
    flex: 0 0 auto;
    min-height: 1.8rem;
    padding: 0 .35rem;
  }
}

@container source-panel (max-width: 34rem) {
  .sync-bar { grid-template-columns: 1fr auto; gap: .65rem; }
  .offset-control, .volume-control { grid-column: 1 / -1; }
}

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