:root {
    --bg: #0f1529;          /* gui c1 (dark background) */
    --surface: #262f49;     /* gui widgetBackground[1] */
    --surface-2: #1c2337;   /* gui c2 */
    --accent: #00e9ff;      /* gui c7 dark (brand cyan) */
    --text: #ffffff;        /* gui c13 dark */
    --muted: #8a93ab;
    --danger: #ff4442;      /* gui c22 */
    --ok: #35c46a;
    --active: #2dab00;      /* gui c25 — active control (cam / screen on) */
    --icon: #e6ebf5;        /* neutral control icon */
    --radius: 14px;
    color-scheme: dark;
}

* { box-sizing: border-box; }

/* The [hidden] attribute must win over the explicit `display` on .join/.call below
   (a class with `display` otherwise defeats the UA [hidden]{display:none} rule). */
[hidden] { display: none !important; }

html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
}

/* ---------- Join gate ---------- */
.join {
    min-height: 100%;
    display: grid;
    place-items: center;
    padding: 24px;
}
.join-card {
    width: min(420px, 100%);
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: var(--radius);
    padding: 28px 26px 24px;
    box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
}
.brand {
    font-size: 26px;
    font-weight: 700;
    letter-spacing: 0.5px;
    color: var(--accent);
}
.sub {
    color: var(--muted);
    margin: 4px 0 22px;
    font-size: 14px;
}
.fld { display: block; margin-bottom: 16px; }
.fld > span {
    display: block;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    color: var(--muted);
    margin-bottom: 6px;
}
.fld input, .fld textarea {
    width: 100%;
    background: var(--bg);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    color: var(--text);
    padding: 11px 13px;
    font-size: 15px;
    font-family: inherit;
    resize: none;   /* single-line inputs; Firefox honours `resize` on <input> too, hence no handles */
}
.fld textarea { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; line-height: 1.4; }
.fld input:focus, .fld textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(0, 233, 255, 0.15);
}
/* A field locked by a token / URL param: value is authoritative, not editable. */
.fld input:disabled, .fld textarea:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    border-style: dashed;
}
.btn {
    width: 100%;
    border: none;
    border-radius: 10px;
    padding: 12px 16px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
}
.btn-primary { background: var(--accent); color: #062330; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-primary:disabled { opacity: 0.5; cursor: default; }

/* "Tap to enable sound" — surfaced when the browser blocks audio autoplay (room.canPlaybackAudio === false).
   Clicking it calls room.startAudio() from within the gesture, unblocking every call audio element at once.
   Hidden via the [hidden] attribute (the global [hidden]{display:none!important} wins over display below). */
.audio-blocked {
    position: fixed; top: 60px; left: 50%; transform: translateX(-50%); z-index: 60;
    display: inline-flex; align-items: center; gap: 8px;
    padding: 9px 18px; border: 0; border-radius: 999px;
    background: var(--accent); color: #062330;
    font: inherit; font-size: 14px; font-weight: 600; cursor: pointer;
    box-shadow: 0 6px 20px rgba(0, 0, 0, .35);
}
.audio-blocked::before { content: "🔊"; font-size: 15px; }
.audio-blocked:hover { filter: brightness(1.08); }
.err {
    margin-top: 14px;
    color: var(--danger);
    font-size: 13px;
    line-height: 1.4;
    word-break: break-word;
}

/* ---------- Joining stage (centered progress card: radial loader + staged label / error) ---------- */
.joining {
    position: fixed;
    inset: 0;
    z-index: 60;
    display: grid;
    place-items: center;
    padding: 24px;
    background: radial-gradient(900px 640px at 50% -10%, #16223c, var(--bg) 62%);
}
.joining-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 18px;
    width: min(380px, 100%);
    padding: 34px 34px 28px;
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: var(--radius);
    box-shadow: 0 22px 56px rgba(0, 0, 0, 0.5);
    text-align: center;
}
.j-ring { position: relative; width: 132px; height: 132px; }
.j-loader { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.j-initials {                               /* the guest's initials sit in the ring's centre hole */
    position: absolute; inset: 0; display: grid; place-items: center;
    font-size: 30px; font-weight: 600; letter-spacing: 0.5px; color: rgba(233, 240, 252, 0.94);
    pointer-events: none;
}
.j-stage { font-size: 15px; color: var(--text); letter-spacing: 0.2px; min-height: 20px; }
.j-error { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.j-error-title { font-size: 17px; font-weight: 650; color: var(--danger); }
.j-error-msg {
    font-size: 13.5px;
    color: var(--muted);
    line-height: 1.5;
    max-width: 34ch;
    word-break: break-word;
}
.j-error-actions { display: flex; gap: 10px; margin-top: 8px; }
.btn-inline { width: auto; padding: 10px 20px; }
.btn-ghost { background: transparent; border: 1px solid rgba(255, 255, 255, 0.18); color: var(--text); }
.btn-ghost:hover { background: rgba(255, 255, 255, 0.06); }

/* ---------- Call stage ---------- */
.call {
    height: 100%;
    display: flex;
    flex-direction: column;
}
.bar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    padding: 10px 18px;
    background: var(--surface-2);
}
.bar.top { justify-content: space-between; gap: 12px; }
.room-name { font-weight: 600; }
.status { color: var(--muted); font-size: 13px; }
.status.ok { color: var(--ok); }
.status.err { color: var(--danger); }
.bar-actions { display: flex; align-items: center; gap: 12px; }
.bar-btn {                                  /* layout-mode toggle (top-right) — a view preference */
    background: transparent; border: 1px solid rgba(255, 255, 255, 0.12); border-radius: 8px;
    padding: 4px 7px; color: var(--icon); cursor: pointer; display: grid; place-items: center;
}
.bar-btn:hover { background: rgba(255, 255, 255, 0.06); }
.bar-btn svg { width: 18px; height: 18px; display: block; }

/* The stage positions tiles absolutely (JS-computed layout — see layout.js); #stage-content grows to the
   content height so the stage scrolls only once tiles hit their minimum size. */
.stage { flex: 1 1 auto; position: relative; overflow: auto; }
.stage-content { position: relative; width: 100%; min-height: 100%; }
.tile {
    position: absolute;
    width: 300px; height: 169px;            /* fallback size before the first JS layout (inline styles win) */
    background: #000;
    border-radius: var(--radius);
    overflow: hidden;
    border: 1px solid rgba(255, 255, 255, 0.06);
    transition: left .22s cubic-bezier(.4, 0, .2, 1), top .22s cubic-bezier(.4, 0, .2, 1),
                width .22s cubic-bezier(.4, 0, .2, 1), height .22s cubic-bezier(.4, 0, .2, 1);
}
.tile.speaking { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset; }
.tile video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    background: #000;
    cursor: pointer;   /* tap the feed → Picture-in-Picture */
}
.tile.local video { transform: scaleX(-1); }        /* mirror own camera (selfie) */
.tile.local video.in-pip { transform: none; }       /* while popped out — keep the PiP placeholder text upright */
.tile.screen { border-color: rgba(0, 233, 255, 0.28); }   /* screen/video/image share = larger (2x2 in mode B) */
.tile.screen video { object-fit: contain; }    /* fit the shared screen, don't crop */
/* audio-only share: an equalizer canvas fills the card (no video), on a dark-blue ground. */
.tile.audio { border-color: rgba(0, 233, 255, 0.28); background: radial-gradient(125% 120% at 50% 30%, #1b2540, #0d1424); }
.tile .eq { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.tile.audio.seekable .eq { cursor: pointer; }   /* streamer's own recording: click the canvas to seek */
/* viewer: "Connecting…" over the audio tile until real media arrives (covers the idle flat equalizer). */
.tile.audio .eq-connecting {
    position: absolute;
    inset: 0;
    z-index: 1;
    display: grid;
    place-items: center;
    color: var(--muted);
    font-size: 14px;
    letter-spacing: 0.3px;
    background: radial-gradient(125% 120% at 50% 30%, #1b2540, #0d1424);
    animation: eq-pulse 1.4s ease-in-out infinite;
}
@keyframes eq-pulse { 0%, 100% { opacity: 0.65; } 50% { opacity: 1; } }

/* A peer's join state under the avatar (seen by others): "Подключается…" while pending, then — if it drops
   before its media comes up — a brief red "Не удалось подключиться" so the vanish is explained. */
.tile .tile-status {
    position: absolute;
    left: 0;
    right: 0;
    top: 12px;                    /* top-center — clears the centered avatar + ring */
    z-index: 2;                   /* above the ring; alongside name/quality */
    text-align: center;
    padding: 0 10px;
    font-size: 13px;
    letter-spacing: 0.3px;
    pointer-events: none;
}
.tile .tile-status.pending { color: var(--muted); animation: eq-pulse 1.4s ease-in-out infinite; }
.tile .tile-status.failed  { color: var(--danger); font-weight: 600; }

/* Avatar voice/status ring — canvas over the placeholder, bars circle the initials; hidden under video. */
.tile .ring { position: absolute; inset: 0; width: 100%; height: 100%; display: block; z-index: 1; pointer-events: none; }
/* With a ring, give the avatar the same dark radial ground as the join card (instead of the flat surface). */
.tile.ring-on .placeholder { background: radial-gradient(125% 120% at 50% 32%, #1b2540, #0d1424); }
/* Muted-mic glyph (bottom-right) — pairs with the dim "off" ring. */
.tile .mic-off {
    position: absolute; right: 8px; bottom: 8px; width: 24px; height: 24px; border-radius: 50%;
    background: rgba(0, 0, 0, 0.55); color: var(--danger); display: grid; place-items: center;
    z-index: 2; pointer-events: none;
}
.tile .mic-off svg { width: 14px; height: 14px; fill: currentColor; display: block; }
.tile .placeholder {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    font-size: 40px;
    font-weight: 700;
    color: var(--muted);
    background: var(--surface);
}
.tile .name {
    position: absolute;
    z-index: 2;             /* stay above the audio "Connecting…" cover */
    left: 8px;
    bottom: 8px;
    padding: 3px 9px;
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.55);
    font-size: 13px;
    max-width: calc(100% - 16px);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.tile .muted-mic {
    position: absolute;
    right: 8px;
    top: 8px;
    font-size: 14px;
    opacity: 0.85;
}

.bar.bottom { justify-content: center; gap: 14px; }
.btn-ctl {
    width: 52px;
    height: 52px;
    border-radius: 50%;
    border: 1px solid rgba(255, 255, 255, 0.12);
    background: var(--surface);
    color: var(--icon);
    cursor: pointer;
    display: grid;
    place-items: center;
    transition: background 0.12s, color 0.12s;
}
.btn-ctl svg { width: 22px; height: 22px; fill: currentColor; display: block; }
.btn-ctl:hover { background: #33405f; }
.btn-ctl.active { color: var(--active); }   /* cam / screen on */
.btn-ctl.danger { color: var(--danger); }   /* mic off */
.btn-leave { background: var(--danger); border-color: transparent; color: #fff; }
.btn-leave:hover { filter: brightness(1.1); background: var(--danger); }

/* Share control = main button + a small "+" to add another surface (bottom-right corner). */
.ctl-wrap { position: relative; display: inline-grid; place-items: center; }
.btn-add {
    position: absolute;
    right: -3px;
    bottom: -3px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 2px solid var(--surface-2);   /* ring separates it from the main button */
    background: #3a86ff;                   /* blue — distinct from the green "sharing" state */
    color: #fff;
    padding: 0;
    cursor: pointer;
    display: grid;
    place-items: center;
}
.btn-add svg { width: 12px; height: 12px; fill: currentColor; display: block; }
.btn-add:hover { filter: brightness(1.12); }

/* "✕" to stop one of your own screen shares (own screen tiles only). */
.tile .tile-x {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    border: none;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    font-size: 13px;
    cursor: pointer;
    display: grid;
    place-items: center;
    z-index: 2;
}
.tile .tile-x svg { width: 14px; height: 14px; fill: currentColor; display: block; }
.tile .tile-x:hover { background: var(--danger); }

/* Streamer's play/pause for their own audio recording (bottom-right of the audio tile). */
.tile .tile-play {
    position: absolute;
    right: 8px;
    bottom: 8px;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    border: none;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    cursor: pointer;
    display: grid;
    place-items: center;
    z-index: 2;
}
.tile .tile-play svg { width: 15px; height: 15px; fill: currentColor; display: block; }
.tile .tile-play:hover { background: rgba(0, 0, 0, 0.75); }

/* Streamer's seek scrubber on a shared video (bottom edge, full width) — revealed on hover, click to seek. */
.tile .tile-seek {
    position: absolute; left: 0; right: 0; bottom: 0; height: 6px;
    background: rgba(255, 255, 255, 0.14); cursor: pointer; z-index: 2;
    opacity: 0; transition: opacity 0.15s, height 0.15s;
}
.tile:hover .tile-seek { opacity: 1; height: 9px; }
.tile .tile-seek-fill {
    height: 100%; width: 0;
    background: linear-gradient(90deg, rgba(179, 77, 255, 0.95), rgba(255, 60, 120, 0.95));
}

/* Per-tile stream toggles (incoming audio / video), top-left corner. Remote tiles only. */
.tile-ctl { position: absolute; left: 8px; top: 8px; display: flex; gap: 6px; z-index: 2; }
.tile-btn {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    border: none;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    cursor: pointer;
    display: grid;
    place-items: center;
}
.tile-btn svg { width: 15px; height: 15px; fill: currentColor; display: block; }
.tile-btn:hover { background: rgba(0, 0, 0, 0.75); }
.tile-btn.off { color: var(--danger); }   /* muted audio / stopped video */

/* Local volume: the mute glyph (in .tile-ctl) + a vertical slider that drops under it on hover. */
.tc-vol { position: relative; }
.tc-vol-slider {
    position: absolute; top: calc(100% + 5px); left: 50%; transform: translateX(-50%);
    width: 26px; height: 82px; padding: 8px 0; box-sizing: border-box;
    background: rgba(0, 0, 0, 0.6); border-radius: 13px; z-index: 3;
    opacity: 0; pointer-events: none; transition: opacity 0.14s;
    cursor: pointer; touch-action: none;
}
.tile:hover .tc-vol .tc-vol-slider { opacity: 1; pointer-events: auto; }
.tc-vol-track { position: relative; width: 6px; height: 100%; margin: 0 auto; background: rgba(255, 255, 255, 0.22); border-radius: 3px; }
.tc-vol-fill { position: absolute; left: 0; right: 0; bottom: 0; border-radius: 3px; background: linear-gradient(180deg, var(--accent), #3a86ff); }

/* LK connection-quality glyph, top-right of the main tile (informational — never intercepts a tile tap). */
.tile .quality {
    position: absolute;
    right: 8px;
    top: 8px;
    width: 22px;
    height: 22px;
    display: grid;
    place-items: center;
    z-index: 2;
    pointer-events: auto;   /* hoverable — reveals the live stats tooltip */
    cursor: help;
    filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.65));   /* legible over any video */
}
.tile .quality[hidden] { display: none; }
.tile .quality svg { width: 22px; height: 22px; fill: currentColor; display: block; }

/* Live connection-quality tooltip — dark popover pinned to <body> (the tile has overflow:hidden and clips). */
.qtip {
    position: fixed;
    z-index: 50;
    display: none;
    min-width: 200px;
    max-width: 320px;
    padding: 10px 12px;
    background: rgba(16, 20, 34, 0.97);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
    color: var(--text);
    font-size: 12.5px;
    line-height: 1.5;
}
.qtip-h { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.qtip-dot { width: 8px; height: 8px; border-radius: 50%; flex: 0 0 auto; }
.qtip-c { margin-top: 6px; padding-top: 6px; border-top: 1px solid rgba(255, 255, 255, 0.08); color: var(--muted); }
.qtip-c .qm { white-space: nowrap; }
.qtip-c .qm-k { cursor: help; border-bottom: 1px dotted rgba(255, 255, 255, 0.35); }   /* affordance: hover for the explanation */
.qtip-s { margin-top: 6px; padding-top: 6px; border-top: 1px solid rgba(255, 255, 255, 0.08); display: grid; gap: 3px; }
.qs-row { display: flex; align-items: center; gap: 7px; }
.qs-row.off { opacity: 0.5; }
.qs-i { width: 16px; flex: 0 0 auto; display: grid; place-items: center; color: var(--muted); }
.qs-i svg { width: 15px; height: 15px; fill: currentColor; display: block; }
.qs-n { flex: 0 0 74px; color: var(--muted); }
.qs-d { flex: 1 1 auto; font-variant-numeric: tabular-nums; }
.qs-badge { flex: 0 0 auto; font-size: 10px; padding: 1px 6px; border-radius: 6px; background: rgba(255, 255, 255, 0.1); color: var(--muted); }
