/* Spectate UI for Abyss of Alaria. Dark theme to match the game. */

/* ---- "X Players Viewing" badge (shown to the PLAYER when others watch) ---- */
/* Fixed top-right of the game window. z-index sits above the game board/HUD but
   below the full-screen modals (Start Game, inventory, etc. at ~9999) so it
   tucks behind those backdrops instead of floating over them. */
#viewerBadge {
    position: fixed;
    top: 10px;
    right: 12px;
    z-index: 9000;
    font: 13px system-ui, Segoe UI, sans-serif;
    user-select: none;
}
/* The viewer badge also appears in the spectate bar (to the right of Leave). It
   shares the look + list behavior with the host badge. position:relative so its
   dropdown anchors to it. */
#spectateViewers {
    position: relative;
    font: 13px system-ui, Segoe UI, sans-serif;
    user-select: none;
}

/* The pill button. The #spectateBar #... selector is intentionally more specific
   than the bar's generic `#spectateBar button` rule so the pill styling wins. */
#viewerBadgeBtn,
#spectateBar #spectateViewersBtn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 11px;
    border-radius: 999px;
    border: 1px solid rgba(0, 255, 0, 0.45);
    background: rgba(15, 15, 22, 0.92);
    color: #d8ffd8;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
}
#viewerBadgeBtn:hover,
#spectateBar #spectateViewersBtn:hover { background: rgba(28, 28, 40, 0.96); border-color: rgba(0, 255, 0, 0.75); }
.viewer-eye { font-size: 14px; }

/* The viewer list: hidden by default, revealed on hover or when the badge has
   the .open class (click/touch toggle). .viewer-* classes are unique to these
   badges, so the inner styling is shared by both via the class alone. */
.viewer-list {
    display: none;
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    min-width: 180px;
    max-width: 260px;
    max-height: 50vh;
    overflow-y: auto;
    padding: 8px 10px;
    border-radius: 8px;
    border: 1px solid #3a3a52;
    background: rgba(12, 12, 18, 0.98);
    color: #eee;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.55);
    text-align: left;
    font-weight: 400;
}
#viewerBadge:hover .viewer-list,
#viewerBadge.open .viewer-list,
#spectateViewers:hover .viewer-list,
#spectateViewers.open .viewer-list { display: block; }

.viewer-list-title {
    font-weight: 700;
    color: #ffd400;
    margin-bottom: 6px;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.viewer-list ul { list-style: none; margin: 0; padding: 0; }
.viewer-list li {
    padding: 4px 2px;
    border-top: 1px solid rgba(255, 255, 255, 0.07);
    word-break: break-word;
}
.viewer-list li:first-child { border-top: 0; }
.viewer-list .viewer-empty { color: #aaa; font-style: italic; }
.viewer-you { color: #9fdcff; font-style: italic; }

/* A spectator never sees the host badge (defensive — JS already gates this). */
body.spectating #viewerBadge { display: none !important; }

/* Host's "message viewers" composer, inside the viewer-badge popover. */
#viewerMsgRow {
    display: flex;
    gap: 6px;
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid rgba(255, 255, 255, 0.12);
}
#viewerMsg {
    flex: 1 1 auto;
    min-width: 0;
    padding: 6px 8px;
    border-radius: 6px;
    border: 1px solid #44445e;
    background: #1c1c28;
    color: #fff;
    font: 13px system-ui, Segoe UI, sans-serif;
}
#viewerMsgSend {
    padding: 6px 12px;
    border-radius: 6px;
    border: 1px solid #2f6b3a;
    background: #2f6b3a;
    color: #fff;
    cursor: pointer;
    flex: 0 0 auto;
}
#viewerMsgSend:hover { background: #3a8048; }


/* While watching, hide local controls that don't apply to a spectator. The
   game timer is hidden because RunTimer keeps rewriting it; the watched time is
   shown in the spectate bar instead. */
body.spectating #actionButtonsBar { display: none !important; }
/* Always show the watched time at full brightness, even if the player hid their
   own timer (we add .timer-hidden only to stop the game's RunTimer rewriting it). */
body.spectating #runTimer { color: #fff !important; filter: none !important; cursor: default !important; }

/* The watcher uses the game's real renderer, which wires tile click handlers;
   disable board interaction so a spectator can't drive the game. */
body.spectating #dungeon { pointer-events: none !important; }

/* Read-only mirror of the player's open modal (inventory, best runs, etc.). */
#spectateOverlayMirror { position: fixed; inset: 0; z-index: 90000; pointer-events: none; }
#spectateOverlayMirror * { pointer-events: none !important; }

/* Centered cell markers for the watcher board (the watcher has no sprites). */
#dungeon .spectate-mark {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    line-height: 1;
    color: #fff;
    pointer-events: none;
}
#dungeon .spectate-player { color: #ff2fbf; text-shadow: 0 0 4px rgba(255, 47, 191, 0.9); }
#dungeon .spectate-item { color: #ffd400; }
#dungeon .spectate-vendor { color: #29d0d0; }

/* Lock the viewer down: nothing in the game UI is clickable (only the spectate
   bar's message box / Send / Leave). The log stays scrollable. The player's own
   modals still mirror because they're shown via #spectateOverlayMirror. */
body.spectating #topArea,
body.spectating #headerBar { pointer-events: none !important; }
body.spectating #log { pointer-events: auto !important; }   /* allow scrolling */
body.spectating #logLinks { display: none !important; }     /* hide Key Mappings / Best Runs */

/* Share-your-game dialog (host presses "^"). */
#spectateShareOverlay {
    position: fixed;
    inset: 0;
    z-index: 100003;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.6);
}
#spectateShareModal {
    position: relative;
    width: min(480px, 92vw);
    background: #14141c;
    color: #eee;
    border: 1px solid #3a3a52;
    border-radius: 10px;
    padding: 18px 20px;
    font: 14px system-ui, Segoe UI, sans-serif;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
}
#spectateShareModal h3 { margin: 0 0 10px; color: #fff; }
#spectateShareModal p { margin: 0 0 12px; color: #cdd; }
#spectateShareClose {
    position: absolute;
    top: 8px;
    right: 10px;
    background: transparent;
    border: none;
    color: #aaa;
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
}
#spectateShareClose:hover { color: #fff; }
.spectate-share-row { display: flex; gap: 8px; }
#spectateShareUrl {
    flex: 1 1 auto;
    padding: 8px;
    border-radius: 6px;
    border: 1px solid #44445e;
    background: #1c1c28;
    color: #fff;
}
#spectateShareCopy {
    padding: 8px 14px;
    border-radius: 6px;
    border: 1px solid #2f6b3a;
    background: #2f6b3a;
    color: #fff;
    cursor: pointer;
}
#spectateShareCopy:hover { background: #3a8048; }
.spectate-share-buttons { margin-top: 16px; text-align: right; }
.spectate-share-buttons button {
    padding: 8px 14px;
    border-radius: 6px;
    border: 1px solid #44445e;
    background: #2a2a3c;
    color: #fff;
    cursor: pointer;
}
.spectate-share-buttons button:hover { background: #3a3a52; }

/* Connection-status banner (top of screen). Hidden until SFG.Connection shows
   it via inline display; amber while reconnecting, red once it gives up. */
#sfgConnBanner {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100010;
    text-align: center;
    padding: 9px 14px;
    font: 14px system-ui, Segoe UI, sans-serif;
    color: #fff;
    background: #b8860b;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}
#sfgConnBanner.conn-givenup { background: #8a2a2a; }
/* While spectating, sit the banner just below the spectate bar (and beneath its
   z-index) so the Leave button is never covered and stays clickable. */
body.spectating #sfgConnBanner { top: 44px; z-index: 99999; }
#sfgConnBanner .sfg-conn-retry {
    margin-left: 8px;
    padding: 3px 12px;
    border-radius: 5px;
    border: 1px solid rgba(255, 255, 255, 0.5);
    background: rgba(255, 255, 255, 0.18);
    color: #fff;
    cursor: pointer;
}
#sfgConnBanner .sfg-conn-retry:hover { background: rgba(255, 255, 255, 0.32); }

/* Host-gone overlay: grey the screen (still visible) below the spectate bar. */
#spectateReconnect,
#spectateStalled {
    position: fixed;
    inset: 0;
    z-index: 95000;
    pointer-events: none;            /* keep the Leave button (in the bar above) clickable */
    background: rgba(10, 10, 14, 0.62);
    display: flex;
    align-items: center;
    justify-content: center;
}
.spectate-reconnect-msg {
    max-width: 460px;
    text-align: center;
    color: #fff;
    font: 16px system-ui, Segoe UI, sans-serif;
    line-height: 1.5;
    text-shadow: 0 1px 4px #000;
}

/* ---- Top spectate bar ---- */
#spectateBar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100000;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: rgba(15, 15, 22, 0.96);
    color: #eee;
    border-bottom: 1px solid #3a3a52;
    font: 14px system-ui, Segoe UI, sans-serif;
}

#spectateBar .spectate-eye { font-size: 16px; }
#spectateBar .spectate-host { color: #ffd400; }
#spectateBar .spectate-spacer { flex: 1 1 auto; }

#spectateBar #spectateMsg {
    flex: 0 1 320px;
    min-width: 140px;
    padding: 6px 8px;
    border-radius: 6px;
    border: 1px solid #44445e;
    background: #1c1c28;
    color: #fff;
}

#spectateBar button {
    padding: 6px 12px;
    border-radius: 6px;
    border: 1px solid #44445e;
    background: #2a2a3c;
    color: #fff;
    cursor: pointer;
}
#spectateBar button:hover { background: #3a3a52; }
#spectateBar #spectateLeave { background: #5a2330; border-color: #7a3040; }
#spectateBar #spectateLeave:hover { background: #7a3040; }

/* Push the game header down so the fixed bar doesn't cover it. */
body.spectating #headerBar { margin-top: 44px; }

/* ---- Lobby modal ---- */
#spectateLobbyOverlay {
    position: fixed;
    inset: 0;
    z-index: 100001;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.7);
}

#spectateLobbyModal {
    width: min(520px, 92vw);
    max-height: 80vh;
    overflow-y: auto;
    background: #14141c;
    color: #eee;
    border: 1px solid #3a3a52;
    border-radius: 10px;
    padding: 18px 20px;
    font: 14px system-ui, Segoe UI, sans-serif;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
}

#spectateLobbyModal h3 { margin: 0 0 12px; color: #fff; }

.spectate-nick-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 14px;
}
.spectate-nick-row label { color: #9aa; }
.spectate-nick-row input {
    flex: 1 1 auto;
    padding: 6px 8px;
    border-radius: 6px;
    border: 1px solid #44445e;
    background: #1c1c28;
    color: #fff;
}

.spectate-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 0;
    border-bottom: 1px solid #2a2a3c;
}
.spectate-row:last-child { border-bottom: 0; }
.spectate-row-main { flex: 1 1 auto; display: flex; flex-direction: column; gap: 2px; }
.spectate-row-main strong { color: #fff; }
.spectate-row-meta { color: #9aa; font-size: 12px; }

.spectate-watch-btn {
    padding: 6px 14px;
    border-radius: 6px;
    border: 1px solid #2f6b3a;
    background: #2f6b3a;
    color: #fff;
    cursor: pointer;
}
.spectate-watch-btn:hover { background: #3a8048; }

.spectate-empty { color: #9aa; padding: 16px 0; text-align: center; }

#spectateLobbyButtons { margin-top: 16px; text-align: right; }
#spectateLobbyButtons button {
    padding: 6px 14px;
    border-radius: 6px;
    border: 1px solid #44445e;
    background: #2a2a3c;
    color: #fff;
    cursor: pointer;
}

/* ---- Chat toasts (shown to everyone in the channel; newest on top, reflow) ---- */
#spectateMsgStack {
    position: fixed;
    right: 16px;
    bottom: 70px;               /* bottom-anchored so the stack grows upward */
    z-index: 100002;
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-width: 340px;
    pointer-events: none;
}
.spectate-msg-toast {
    background: rgba(20, 20, 30, 0.97);
    color: #fff;
    border: 1px solid #ffd400;
    border-radius: 8px;
    padding: 8px 12px;
    font: 14px system-ui, Segoe UI, sans-serif;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
    overflow: hidden;
    max-height: 200px;
    opacity: 0;
    transform: translateY(6px);
    /* max-height/margin/padding transitions make the stack reflow smoothly when
       a toast leaves (others above shift down). */
    transition: opacity .25s ease, transform .25s ease,
                max-height .32s ease, margin .32s ease, padding .32s ease;
}
.spectate-msg-toast.show { opacity: 1; transform: translateY(0); }
.spectate-msg-toast.leaving {
    opacity: 0;
    max-height: 0;
    margin: 0;
    padding-top: 0;
    padding-bottom: 0;
    transform: translateY(-4px);
}
.spectate-msg-from { color: #ffd400; font-size: 12px; margin-bottom: 2px; }
.spectate-msg-text { word-break: break-word; }

/* ---- Transient note (for the watcher) ---- */
.spectate-note {
    position: fixed;
    left: 50%;
    bottom: 24px;
    transform: translateX(-50%) translateY(8px);
    z-index: 100002;
    background: rgba(20, 20, 30, 0.97);
    color: #fff;
    border: 1px solid #44445e;
    border-radius: 6px;
    padding: 8px 14px;
    font: 13px system-ui, Segoe UI, sans-serif;
    opacity: 0;
    transition: opacity .3s ease, transform .3s ease;
}
.spectate-note.show { opacity: 1; transform: translateX(-50%) translateY(0); }
