:root {
  color-scheme: light;
  --bg: #f4f7fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --surface-3: #eef3f8;
  --text: #172033;
  --muted: #667085;
  --line: #d8e0ea;
  --line-strong: #c7d1de;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #dbeafe;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --shadow: 0 20px 60px rgba(15, 23, 42, 0.08);
  --radius: 16px;
}

* { box-sizing: border-box; }
html { background: var(--bg); height: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  height: 100%;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at top left, rgba(37, 99, 235, 0.11), transparent 32rem),
    linear-gradient(180deg, #f8fbff 0%, var(--bg) 45%, #edf2f8 100%);
  color: var(--text);
}
button, input { font: inherit; }

.appShell {
  width: min(1440px, calc(100vw - 32px));
  height: 100vh;
  margin: 0 auto;
  padding: 24px 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
}

.topBar {
  height: 78px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
  flex-shrink: 0;
}
.kicker {
  margin: 0 0 5px;
  color: var(--primary);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .12em;
  text-transform: uppercase;
}
h1, h2 { margin: 0; line-height: 1.08; letter-spacing: -0.035em; }
h1 { font-size: clamp(32px, 4vw, 48px); }
h2 { font-size: 22px; }
.topActions { display: flex; align-items: center; gap: 12px; }
.status {
  min-width: 132px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255,255,255,.72);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  text-align: center;
}
.status.good { color: var(--success); border-color: rgba(22, 163, 74, .25); background: #f0fdf4; }
.status.bad { color: var(--danger); border-color: rgba(220, 38, 38, .25); background: #fef2f2; }

.workspace {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr) 280px;
  grid-template-areas: "setup chat users" "active chat users";
  gap: 18px;
  align-items: stretch;
  min-height: 0;
  overflow: hidden;
  transition: grid-template-columns .18s ease;
}
.workspace.outOfLobby {
  grid-template-columns: minmax(300px, 360px) minmax(320px, 430px) minmax(0, 1fr);
  grid-template-areas: "setup active chat";
}
.workspace.outOfLobby .usersPanel, .workspace.outOfLobby .inOnly { display: none; }
.workspace.outOfLobby .chatPanel { opacity: .82; }
.workspace.inLobby {
  grid-template-columns: 76px minmax(0, 1fr) minmax(220px, 280px);
  grid-template-areas: "active chat users";
}
.workspace.inLobby .setupPanel, .workspace.inLobby .outOnly { display: none; }
.workspace.inLobby { height: 100%; }
.workspace.inLobby .chatPanel,
.workspace.inLobby .activePanel,
.workspace.inLobby .usersPanel { min-height: 0; height: 100%; }

.panel {
  min-height: 0;
  background: rgba(255, 255, 255, 0.96);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.panel[aria-label] { position: relative; }
.setupPanel { grid-area: setup; padding: 18px; display: flex; flex-direction: column; gap: 14px; }
.activePanel { grid-area: active; display: flex; flex-direction: column; min-height: 320px; transition: width .18s ease, opacity .18s ease; }
.workspace.inLobby .activePanel { width: 76px; overflow: hidden; align-self: stretch; }
.workspace.inLobby .activePanel .panelHeader { height: 100%; align-items: flex-start; padding: 14px 10px; writing-mode: vertical-rl; text-orientation: mixed; justify-content: flex-start; }
.workspace.inLobby .activePanel .panelHeader .kicker { display: none; }
.workspace.inLobby .activePanel .panelHeader h2 { font-size: 14px; letter-spacing: .03em; text-transform: uppercase; white-space: nowrap; }
.workspace.inLobby .activePanel .iconBtn { width: 36px; height: 36px; margin-top: 12px; writing-mode: horizontal-tb; }
.workspace.inLobby .activePanel .panelNote,
.workspace.inLobby .activePanel .lobbyList { display: none; }
.chatPanel { grid-area: chat; min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr) auto auto; overflow: hidden; }
.usersPanel { grid-area: users; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.panelHeader, .chatHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 18px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, var(--surface), var(--surface-2));
}
.panelHeader.compact { padding: 16px 18px; }
.chatHeader { padding: 18px 22px; }
#lobbyCode {
  color: var(--primary);
  background: var(--primary-soft);
  border: 1px solid rgba(37,99,235,.18);
  border-radius: 999px;
  padding: 7px 11px;
  font-size: 13px;
  letter-spacing: .08em;
  white-space: nowrap;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 750;
}
input {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 12px;
  padding: 11px 12px;
  outline: none;
  font-size: 15px;
  transition: border-color .15s ease, box-shadow .15s ease;
}
input::placeholder { color: #98a2b3; }
input:focus { border-color: var(--primary); box-shadow: 0 0 0 4px rgba(37, 99, 235, .11); }
.hint { color: var(--primary); font-weight: 800; }

button {
  border: 1px solid transparent;
  border-radius: 12px;
  padding: 11px 14px;
  background: var(--text);
  color: #fff;
  cursor: pointer;
  font-weight: 800;
  transition: transform .14s ease, background .14s ease, border-color .14s ease, box-shadow .14s ease;
}
button:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 10px 22px rgba(15, 23, 42, .12); }
button:disabled { opacity: .45; cursor: not-allowed; }
.primary { background: var(--primary); }
.primary:hover:not(:disabled) { background: var(--primary-dark); }
.secondary { background: #fff; color: var(--text); border-color: var(--line-strong); }
.secondary:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }
.iconBtn {
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 12px;
  background: #fff;
  color: var(--primary);
  border-color: var(--line);
}
.hidden { display: none !important; }
.divider { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 12px; font-weight: 800; text-transform: uppercase; }
.divider:before, .divider:after { content: ""; flex: 1; height: 1px; background: var(--line); }
.panelNote { margin: 0; padding: 14px 18px; color: var(--muted); font-size: 13px; line-height: 1.45; border-bottom: 1px solid var(--line); background: #fbfdff; }

.lobbyList { overflow: auto; display: grid; align-content: start; }
.lobbyRow {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  text-align: left;
  padding: 14px 18px;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: #fff;
  color: var(--text);
  box-shadow: none;
}
.lobbyRow:hover { background: #f8fbff; color: var(--text); transform: none; box-shadow: none; }
.lobbyRow.locked .lobbyMain strong:after { content: "  🔒"; }
.lobbyMain { min-width: 0; display: grid; gap: 4px; }
.lobbyMain strong, .lobbyMain small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lobbyMain strong { font-size: 15px; }
.lobbyMain small { color: var(--muted); font-size: 12px; }
.lobbyMeta { display: grid; gap: 5px; justify-items: end; color: var(--muted); font-size: 12px; white-space: nowrap; }
.lockBadge { color: var(--warning); font-weight: 850; }
.emptyLobbies, .emptyUsers {
  color: var(--muted);
  padding: 18px;
  font-size: 14px;
  text-align: center;
}
.lobbyRow.joining { opacity: .6; pointer-events: none; }
.lobbyRow.joining .lobbyMain strong::after { content: " — joining"; color: var(--primary); font-weight: 500; }

.messages { min-height: 0; padding: 18px 22px; overflow: auto; display: flex; flex-direction: column; gap: 10px; background: #fbfdff; scroll-behavior: smooth; }
.messages.emptyState { align-items: center; justify-content: center; }
.emptyChat { display: grid; gap: 7px; text-align: center; color: var(--muted); }
.emptyChat strong { color: var(--text); font-size: 18px; }
.message {
  max-width: min(760px, 84%);
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fff;
  color: var(--text);
  line-height: 1.42;
  font-size: 15px;
  align-self: flex-start;
}
.message.mine {
  align-self: flex-end;
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.message.system {
  align-self: center;
  max-width: 92%;
  background: var(--surface-3);
  color: var(--muted);
  border-color: transparent;
  font-size: 13px;
  text-align: center;
}
.meta { color: var(--muted); font-size: 11px; font-weight: 800; margin-bottom: 4px; }
.message.mine .meta { color: rgba(255,255,255,.74); }
.typing { min-height: 28px; margin: 0; padding: 0 22px; color: var(--primary); font-size: 13px; font-weight: 700; background: #fbfdff; display: flex; align-items: center; }
.composer { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 12px; margin: 0; padding: 14px 22px 16px; border-top: 1px solid var(--line); background: #fff; flex-shrink: 0; }
.composer input { min-height: 46px; }
.composer button { min-height: 46px; min-width: 92px; }

.usersList { min-height: 0; overflow: auto; display: grid; align-content: start; }
.userRow {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 13px 18px;
  border-bottom: 1px solid var(--line);
}
.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 900;
}
.userText { min-width: 0; display: grid; gap: 2px; }
.userText strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.userText small { color: var(--muted); font-size: 12px; }
.onlineDot { width: 9px; height: 9px; border-radius: 50%; background: var(--success); margin-left: auto; }
.countPill { min-width: 30px; padding: 5px 9px; border-radius: 999px; background: var(--primary-soft); color: var(--primary); text-align: center; font-weight: 900; font-size: 13px; }

@media (max-width: 1060px) {
  .workspace, .workspace.outOfLobby, .workspace.inLobby {
    grid-template-columns: 1fr;
    grid-template-areas: "setup" "active" "chat" "users";
  }
  .workspace.inLobby { grid-template-areas: "chat" "users" "active"; }
  .workspace.inLobby .setupPanel { display: none; }
  .workspace.outOfLobby .usersPanel { display: none; }
  .workspace.inLobby .activePanel { width: auto; min-height: auto; opacity: .92; }
  .workspace.inLobby .activePanel .panelHeader { height: auto; writing-mode: horizontal-tb; text-orientation: initial; flex-direction: row; align-items: center; justify-content: space-between; padding: 12px 16px; }
  .workspace.inLobby .activePanel .panelHeader .kicker { display: block; }
  .workspace.inLobby .activePanel .panelHeader h2 { font-size: 16px; text-transform: none; letter-spacing: -0.02em; }
  .workspace.inLobby .activePanel .panelNote,
  .workspace.inLobby .activePanel .lobbyList { display: none; }
  .appShell { height: auto; min-height: 100vh; overflow: visible; }
  .workspace, .workspace.outOfLobby, .workspace.inLobby { overflow: visible; }
  .workspace.inLobby .chatPanel { min-height: min(680px, calc(100vh - 132px)); height: calc(100vh - 132px); }
  .usersPanel { min-height: 280px; }
}
@media (max-width: 620px) {
  .appShell { width: min(100vw - 20px, 1440px); padding-top: 12px; padding-bottom: 12px; }
  .topBar { height: auto; align-items: flex-start; flex-direction: column; }
  .topActions { width: 100%; justify-content: space-between; }
  .workspace.inLobby .chatPanel { height: calc(100vh - 138px); min-height: 480px; }
  .composer { grid-template-columns: 1fr; padding: 12px; }
  .message { max-width: 96%; }
}
