:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --border: #2a2f3a;
  --text: #e6e8ee;
  --muted: #8b93a5;
  --accent: #4f8cff;
  --danger: #e5484d;
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
}
button {
  font: inherit; color: inherit; cursor: pointer;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; padding: 8px 14px;
}
button:hover { border-color: var(--accent); }
button.active { background: var(--accent); border-color: var(--accent); }
button.danger { background: var(--danger); border-color: var(--danger); }
button.ghost { background: transparent; }
button.small { padding: 4px 10px; font-size: 12px; }
input {
  font: inherit; color: inherit;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; width: 100%;
}
input:focus { outline: none; border-color: var(--accent); }
[hidden] { display: none !important; }

/* ---- 입장 ------------------------------------------------------------- */
.join { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.join form {
  width: min(360px, 100%); background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 28px; display: grid; gap: 14px;
}
.join h1 { margin: 0 0 6px; font-size: 22px; }
.join label { display: grid; gap: 6px; color: var(--muted); font-size: 13px; }
.join button { background: var(--accent); border-color: var(--accent); padding: 10px; }
.join-error { margin: 0; color: var(--danger); font-size: 13px; }
.hint { color: var(--muted); font-size: 12px; line-height: 1.4; }

/* ---- 방 레이아웃 -------------------------------------------------------- */
#room-view {
  height: 100%;
  display: grid;
  grid-template-columns: 1fr 300px;
  grid-template-rows: 48px 1fr 72px;
  grid-template-areas:
    "header chat"
    "main   chat"
    "footer chat";
}
#room-view header {
  grid-area: header; display: flex; align-items: center; gap: 10px; padding: 0 16px;
  border-bottom: 1px solid var(--border); background: var(--panel);
}
.room-label { color: var(--muted); }
.count { margin-left: auto; color: var(--muted); }

#room-view main {
  grid-area: main; min-height: 0; padding: 12px; display: grid; gap: 12px;
  grid-template-rows: 1fr;
}
#room-view.stage-active main { grid-template-rows: 1fr 140px; }

/* 스테이지: 화면공유 / 유튜브 */
#stage { min-height: 0; display: grid; gap: 12px; grid-auto-flow: column; }
#screens { display: grid; gap: 12px; min-height: 0; }
#screens:empty { display: none; }
.screen, #yt-panel {
  position: relative; background: #000; border-radius: var(--radius); overflow: hidden; min-height: 0;
}
.screen video { width: 100%; height: 100%; object-fit: contain; display: block; }
.screen .label, .tile .label {
  position: absolute; left: 8px; bottom: 8px; padding: 3px 8px; border-radius: 8px;
  background: rgba(0,0,0,.55); font-size: 12px; display: flex; gap: 8px;
}
#yt-panel { aspect-ratio: 16 / 9; max-height: 100%; }
#yt-player, #yt-player iframe { width: 100%; height: 100%; }

/* 타일 그리드: 인원 수에 따라 열 수 조정 */
#tiles { min-height: 0; display: grid; gap: 12px; grid-auto-rows: 1fr; }
#tiles[data-count="1"] { grid-template-columns: 1fr; }
#tiles[data-count="2"] { grid-template-columns: repeat(2, 1fr); }
#tiles[data-count="3"], #tiles[data-count="4"] { grid-template-columns: repeat(2, 1fr); }
#tiles[data-count="5"], #tiles[data-count="6"] { grid-template-columns: repeat(3, 1fr); }
#room-view.stage-active #tiles { grid-auto-flow: column; grid-template-columns: none; grid-auto-columns: 220px; grid-auto-rows: 1fr; overflow-x: auto; }

.tile {
  position: relative; background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; min-height: 0;
}
.tile video { width: 100%; height: 100%; object-fit: cover; display: none; }
.tile.local video { transform: scaleX(-1); }
.tile.has-video video { display: block; }
.tile .placeholder {
  position: absolute; inset: 0; display: grid; place-items: center;
  font-size: 40px; font-weight: 700; color: var(--muted);
}
.tile.has-video .placeholder { display: none; }

/* 채팅 */
#chat {
  grid-area: chat; display: grid; grid-template-rows: 1fr auto; min-height: 0;
  border-left: 1px solid var(--border); background: var(--panel);
}
.chat-list { list-style: none; margin: 0; padding: 12px; overflow-y: auto; display: grid; gap: 6px; align-content: start; }
.chat-list li { display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: baseline; }
.chat-list .who { color: var(--accent); font-weight: 600; white-space: nowrap; }
.chat-list li.mine .who { color: #7ee787; }
.chat-list .text { word-break: break-word; white-space: pre-wrap; }
.chat-list .time { color: var(--muted); font-size: 11px; }
.chat-list .system { color: var(--muted); font-size: 12px; text-align: center; display: block; }
#chat form { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--border); }

/* 툴바 */
#toolbar {
  grid-area: footer; display: flex; justify-content: center; align-items: center; gap: 10px;
  border-top: 1px solid var(--border); background: var(--panel);
}
#toolbar button {
  display: grid; justify-items: center; gap: 2px; min-width: 72px; height: 52px; padding: 4px 10px;
}
#toolbar .ico { font-size: 18px; line-height: 1; }
#toolbar .lbl { font-size: 11px; color: var(--muted); }
#toolbar button.active .lbl, #toolbar button.danger .lbl { color: #fff; }
#toolbar #btn-leave { margin-left: 16px; }
.overlay-btn, .overlay-bar {
  position: absolute; top: 8px; right: 8px;
}
.overlay-bar { display: flex; gap: 6px; }
.overlay-bar .overlay-btn { position: static; }
.overlay-btn {
  padding: 6px 12px; font-size: 12px;
  background: rgba(0,0,0,.65); border-color: rgba(255,255,255,.25);
}
.overlay-btn:hover:not(:disabled) { background: var(--danger); border-color: var(--danger); }
.overlay-btn.screen-audio.active { background: rgba(79,140,255,.75); border-color: transparent; }
.overlay-btn.screen-audio.active:hover { background: rgba(79,140,255,.95); border-color: transparent; }
.overlay-btn:disabled { opacity: .6; cursor: not-allowed; }
.overlay-label {
  padding: 6px 10px; font-size: 12px; border-radius: 10px; align-self: center;
  background: rgba(0,0,0,.65); color: var(--muted);
}
.overlay-label.me { color: #7ee787; }

#toast {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; padding: 8px 14px;
  z-index: 10; max-width: 90vw;
}

/* ---- 모바일 ------------------------------------------------------------ */
@media (max-width: 800px) {
  #room-view {
    grid-template-columns: 1fr;
    grid-template-rows: 48px 1fr 220px 72px;
    grid-template-areas: "header" "main" "chat" "footer";
  }
  #chat { border-left: none; border-top: 1px solid var(--border); }
  #room-view.stage-active main { grid-template-rows: 1fr 100px; }
  #room-view.stage-active #tiles { grid-auto-columns: 140px; }
}
