:root {
    color-scheme: dark;
    --canvas: #0d0b12;
    --surface: #15111d;
    --surface-raised: #1d1728;
    --surface-hover: #261e33;
    --border: #342a43;
    --border-strong: #4b3d60;
    --text: #f8f5ff;
    --text-muted: #bcb2ca;
    --text-faint: #8f849f;
    --brand: #8b5cf6;
    --brand-strong: #7446df;
    --brand-soft: #c4a9ff;
    --danger: #ff6b75;
    --focus: #d7c5ff;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --shadow-raised: 0 18px 50px rgba(0, 0, 0, 0.38);
}

* { box-sizing: border-box; }
html, body { min-width: 320px; height: 100%; }
body {
    margin: 0;
    overflow: hidden;
    background: var(--canvas);
    color: var(--text);
    font-family: "Avenir Next", "Segoe UI Variable", "Segoe UI", ui-sans-serif, system-ui, sans-serif;
    -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; }
button { color: inherit; }
button, a, input { -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

#header {
    position: relative;
    z-index: 50;
    min-height: 72px;
    padding: 12px clamp(16px, 3vw, 32px);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 28px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--text); font-size: 20px; font-weight: 760; letter-spacing: -0.025em; text-decoration: none; flex: none; }
.brand img { width: 44px; height: 44px; object-fit: contain; }
.stream-search { position: relative; width: min(100%, 520px); }
.input-shell { display: flex; align-items: center; min-height: 46px; border: 1px solid var(--border-strong); border-radius: var(--radius-md); background: #100d16; overflow: hidden; transition: border-color 160ms ease, box-shadow 160ms ease; }
.input-shell:focus-within { border-color: var(--brand-soft); box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.18); }
.input-shell > i { margin-left: 15px; color: var(--text-faint); font-size: 14px; }
#streamInput { min-width: 0; flex: 1; height: 44px; padding: 0 12px; border: 0; outline: 0; background: transparent; color: var(--text); }
#streamInput::placeholder { color: var(--text-faint); opacity: 1; }
#loadStreamButton, .primary-button, .empty-state button { border: 0; background: var(--brand-strong); color: #fff; font-weight: 700; cursor: pointer; transition: background 160ms ease, transform 160ms ease; }
#loadStreamButton { align-self: stretch; display: inline-flex; align-items: center; gap: 8px; padding: 0 18px; border-left: 1px solid rgba(255,255,255,.13); }
#loadStreamButton:hover, .primary-button:hover, .empty-state button:hover { background: var(--brand); }
#loadStreamButton:active, .primary-button:active, .empty-state button:active { transform: translateY(1px); }
.input-hint { position: absolute; top: calc(100% + 7px); right: 0; margin: 0; color: var(--danger); font-size: 12px; font-weight: 650; }
.dropdown-content { display: none; position: absolute; top: calc(100% + 8px); left: 0; right: 0; z-index: 100; padding: 6px; border: 1px solid var(--border-strong); border-radius: var(--radius-md); background: var(--surface-raised); box-shadow: var(--shadow-raised); }
.dropdown-content div { padding: 11px 12px; border-radius: var(--radius-sm); color: var(--text-muted); cursor: pointer; }
.dropdown-content div:hover { background: var(--surface-hover); color: var(--text); }

#mainContent { display: flex; height: calc(100vh - 72px); min-height: 0; }
#streamsContainer { flex: 1; min-width: 0; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); grid-auto-rows: minmax(280px, 1fr); gap: 12px; padding: 12px; overflow: auto; background: var(--canvas); }
.empty-state { grid-column: 1 / -1; align-self: center; justify-self: center; width: min(100%, 520px); padding: 44px 24px; display: flex; flex-direction: column; align-items: center; text-align: center; }
.empty-state[hidden] { display: none; }
.empty-state img { width: 132px; height: 132px; margin-bottom: 22px; filter: drop-shadow(0 16px 24px rgba(79,43,160,.3)); }
.empty-state h1 { max-width: 12ch; margin: 0 0 12px; font-size: clamp(30px, 5vw, 44px); line-height: 1.03; letter-spacing: -0.035em; }
.empty-state p { max-width: 48ch; margin: 0; color: var(--text-muted); font-size: 16px; line-height: 1.6; }
.empty-state button { min-height: 46px; margin-top: 26px; padding: 0 18px; border-radius: var(--radius-sm); }
.empty-state button i { margin-right: 8px; }
.empty-state > span { margin-top: 16px; color: var(--text-faint); font-size: 13px; }

.stream { position: relative; min-width: 0; min-height: 280px; display: flex; flex-direction: column; overflow: hidden; border-radius: var(--radius-md); background: #08070a; box-shadow: inset 0 0 0 1px var(--border); cursor: grab; }
.stream:active { cursor: grabbing; }
.stream.full { grid-column: 1 / -1; }
.stream iframe { width: 100%; height: 100%; min-height: 0; flex: 1; border: 0; }
.controls { order: -1; min-height: 48px; padding: 0 8px 0 14px; display: flex; align-items: center; justify-content: flex-end; gap: 4px; background: var(--surface-raised); border-bottom: 1px solid var(--border); z-index: 10; }
.stream-name { position: absolute; top: 0; left: 0; z-index: 15; max-width: calc(100% - 140px); height: 48px; padding: 0 14px; display: flex; align-items: center; overflow: hidden; color: var(--text); font-size: 14px; font-weight: 720; text-overflow: ellipsis; white-space: nowrap; pointer-events: none; }
.closeButton, .chatButton, .settingsButton, .icon-button { width: 36px; height: 36px; display: inline-grid; place-items: center; padding: 0; border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--text-muted); cursor: pointer; transition: color 140ms ease, background 140ms ease; }
.closeButton:hover, .chatButton:hover, .settingsButton:hover, .icon-button:hover { background: var(--surface-hover); color: var(--text); }
.closeButton:hover { color: var(--danger); }
.chatButton[aria-pressed="true"] { background: rgba(139,92,246,.18); color: var(--brand-soft); }
#chatSidebar { width: min(390px, 34vw); flex: none; min-width: 300px; padding: 12px 12px 12px 0; background: var(--canvas); }
#chatSidebar[hidden] { display: none; }
#chatSidebar iframe { width: 100%; height: 100%; border: 0; border-radius: var(--radius-md); background: var(--surface); }

.modal { position: fixed; inset: 0; z-index: 1000; display: grid; place-items: center; padding: 20px; background: rgba(5,3,8,.78); }
.modal[hidden] { display: none; }
.modal-content { width: min(100%, 440px); padding: 24px; border-radius: var(--radius-lg); background: var(--surface-raised); box-shadow: var(--shadow-raised); animation: modal-enter 220ms cubic-bezier(.16,1,.3,1); }
.modal-heading { margin-bottom: 24px; display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; }
.modal-heading h2 { margin: 0 0 6px; font-size: 22px; letter-spacing: -0.025em; }
.modal-heading p, .field-help { margin: 0; color: var(--text-muted); font-size: 14px; line-height: 1.5; }
.modal-content > label { display: block; margin-bottom: 8px; font-size: 14px; font-weight: 700; }
.field-with-suffix { height: 46px; display: flex; align-items: center; overflow: hidden; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); background: #100d16; }
.field-with-suffix:focus-within { border-color: var(--brand-soft); box-shadow: 0 0 0 3px rgba(139,92,246,.18); }
.field-with-suffix input { min-width: 0; height: 100%; flex: 1; padding: 0 12px; border: 0; outline: 0; background: transparent; color: var(--text); }
.field-with-suffix span { padding: 0 12px; color: var(--text-muted); font-size: 14px; }
.field-help { margin-top: 8px; }
.modal-actions { margin-top: 26px; display: flex; justify-content: flex-end; gap: 10px; }
.primary-button, .secondary-button { min-height: 42px; padding: 0 16px; border-radius: var(--radius-sm); }
.secondary-button { border: 1px solid var(--border-strong); background: transparent; color: var(--text-muted); font-weight: 700; cursor: pointer; }
.secondary-button:hover { background: var(--surface-hover); color: var(--text); }

@keyframes modal-enter { from { opacity: 0; transform: translateY(12px) scale(.98); } }
@keyframes refresh-pulse { 50% { box-shadow: inset 0 0 0 2px var(--brand-soft), 0 0 0 4px rgba(139,92,246,.15); } }
.stream.refreshing { animation: refresh-pulse 900ms ease-out; }

@media (max-width: 760px) {
    body { overflow: auto; }
    #header { min-height: auto; padding: 10px 12px 12px; flex-wrap: wrap; gap: 10px; }
    .brand img { width: 36px; height: 36px; }
    .brand { font-size: 18px; }
    .stream-search { width: 100%; }
    #mainContent { height: auto; min-height: calc(100vh - 104px); flex-direction: column; }
    #streamsContainer { min-height: calc(100vh - 104px); grid-template-columns: 1fr; grid-auto-rows: minmax(280px, 52vh); padding: 8px; gap: 8px; overflow: visible; }
    .stream.full { grid-column: auto; }
    #chatSidebar { width: 100%; min-width: 0; height: 70vh; padding: 0 8px 8px; }
}
@media (max-width: 460px) {
    #loadStreamButton { width: 46px; padding: 0; justify-content: center; }
    #loadStreamButton span { display: none; }
    .empty-state { padding-inline: 12px; }
    .empty-state img { width: 104px; height: 104px; }
}
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
