:root {
  --bg-app: #1e1f22;
  --bg-panel: #2b2d31;
  --bg-tile: #313338;
  --bg-tile-2: #383a40;
  --bg-elev: #232428;
  --accent: #5865f2;
  --accent-hover: #4752c4;
  --accent-soft: rgba(88, 101, 242, 0.15);
  --good: #23a55a;
  --good-soft: rgba(35, 165, 90, 0.15);
  --bad: #f23f42;
  --warn: #f0b232;
  --text: #f2f3f5;
  --text-dim: #b5bac1;
  --text-muted: #80848e;
  --line: rgba(255, 255, 255, 0.06);
  --line-2: rgba(255, 255, 255, 0.1);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }

body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  color: var(--text);
  background:
    radial-gradient(ellipse 800px 400px at 50% -10%, rgba(88, 101, 242, 0.08), transparent 60%),
    var(--bg-app);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}

/* ===== Common form controls ===== */
input[type="text"], select {
  width: 100%;
  padding: 0.7rem 0.85rem;
  background: var(--bg-elev);
  border: 1px solid var(--line-2);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.92rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.18s, box-shadow 0.18s;
  appearance: none;
  -webkit-appearance: none;
}
select {
  background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23b5bac1' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'/%3e%3c/svg%3e");
  background-repeat: no-repeat;
  background-position: right 0.7rem center;
  background-size: 14px;
  padding-right: 2.2rem;
  cursor: pointer;
}
select option { background: var(--bg-panel); color: var(--text); }
input[type="text"]::placeholder { color: var(--text-muted); }
input[type="text"]:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  background: var(--bg-elev);
  border-radius: 999px;
  outline: none;
  cursor: pointer;
  padding: 0;
  margin: 0;
  border: none;
}
input[type="range"]::-webkit-slider-runnable-track { height: 6px; background: var(--bg-elev); border-radius: 999px; }
input[type="range"]::-moz-range-track { height: 6px; background: var(--bg-elev); border-radius: 999px; }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: #fff;
  margin-top: -4px;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
}
input[type="range"]::-moz-range-thumb {
  width: 14px; height: 14px;
  border-radius: 50%;
  background: #fff;
  border: none;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
}
input[type="range"]:disabled { opacity: 0.5; cursor: not-allowed; }

.field { display: flex; flex-direction: column; gap: 0.4rem; }
.field.compact { gap: 0.35rem; }
.field label {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}
.field label small { text-transform: none; letter-spacing: 0; color: var(--text-muted); font-weight: 400; }
.field label .value { color: var(--text); font-weight: 600; font-size: 0.78rem; text-transform: none; }
.field.disabled { opacity: 0.5; }

.row { display: flex; gap: 0.5rem; }
.row > :first-child { flex: 1; }
.icon-btn {
  width: 42px;
  border: 1px solid var(--line-2);
  background: var(--bg-elev);
  border-radius: 8px;
  color: var(--text);
  font-size: 1rem;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.18s, transform 0.1s;
}
.icon-btn:hover { background: var(--bg-tile-2); }
.icon-btn:active { transform: scale(0.94); }

button.primary {
  width: 100%;
  padding: 0.85rem 1rem;
  border: none;
  border-radius: 8px;
  color: #fff;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  transition: background 0.15s, transform 0.1s;
}
button.primary:hover { background: var(--accent-hover); }
button.primary:active { transform: scale(0.985); }
button.primary:disabled { opacity: 0.6; cursor: not-allowed; }

/* ===== Lobby ===== */
.lobby-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(1rem, 3vw, 2rem);
}
.lobby-card {
  width: 100%;
  max-width: 440px;
  background: var(--bg-panel);
  border: 1px solid var(--line-2);
  border-radius: 12px;
  padding: 1.75rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.brand {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  margin-bottom: 0.5rem;
}
.logo {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  color: #fff;
  background: var(--accent);
}
.logo svg { width: 22px; height: 22px; }
h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.tag { margin: 2px 0 0; font-size: 0.8rem; color: var(--text-dim); }
.hint {
  margin: 0;
  font-size: 0.78rem;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.5;
}

/* ===== Call view ===== */
.call-screen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.call-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.9rem clamp(1rem, 3vw, 1.5rem);
  background: var(--bg-panel);
  border-bottom: 1px solid var(--line-2);
  box-shadow: 0 1px 0 rgba(0,0,0,0.2);
  position: sticky;
  top: 0;
  z-index: 10;
}
.channel-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}
.channel-hash {
  font-size: 1.3rem;
  color: var(--text-muted);
  font-weight: 700;
  line-height: 1;
}
.room-name {
  font-weight: 700;
  font-size: 1.05rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 50vw;
}
.conn-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
}
.conn-dot.online { background: var(--good); box-shadow: 0 0 8px var(--good); }
.conn-dot.connecting { background: var(--warn); box-shadow: 0 0 8px var(--warn); }
.header-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}
.member-count {
  font-size: 0.82rem;
  color: var(--text-dim);
  padding: 0.3rem 0.6rem;
  background: var(--bg-elev);
  border-radius: 999px;
  font-variant-numeric: tabular-nums;
}
.header-btn {
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  border-radius: 8px;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.15s, color 0.15s;
}
.header-btn:hover { background: var(--bg-elev); color: var(--text); }
.header-btn.copied { color: var(--good); }

.call-main {
  flex: 1;
  padding: 1rem clamp(0.75rem, 3vw, 1.5rem);
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding-bottom: 7rem;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
}

/* ===== Member grid ===== */
.member-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 0.75rem;
}
body.has-screen .member-grid {
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
}

.member-tile {
  position: relative;
  aspect-ratio: 16 / 11;
  background: var(--bg-tile);
  border-radius: 10px;
  overflow: hidden;
  transition: background 0.18s, outline-color 0.18s;
  outline: 2px solid transparent;
  outline-offset: -2px;
}
.member-tile.speaking {
  background: var(--bg-tile-2);
  outline-color: var(--good);
}

.tile-avatar-wrap {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, calc(-50% - 0.5rem));
  width: clamp(56px, 32%, 92px);
  aspect-ratio: 1;
}
.avatar-circle {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  color: #fff;
  font-size: clamp(1.05rem, 4cqi, 1.8rem);
  background: var(--accent);
  position: relative;
  z-index: 2;
}
.avatar-ring {
  position: absolute;
  inset: -6px;
  border-radius: 50%;
  border: 3px solid var(--good);
  opacity: 0;
  pointer-events: none;
}
.member-tile.speaking .avatar-ring {
  animation: ring-pulse 1.4s ease-out infinite;
}
@keyframes ring-pulse {
  0% { transform: scale(0.95); opacity: 0.85; }
  70% { transform: scale(1.18); opacity: 0; }
  100% { transform: scale(1.18); opacity: 0; }
}
.member-tile.muted-peer .avatar-circle { opacity: 0.55; }

.tile-footer {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  padding: 0.4rem 0.6rem;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.5));
  font-size: 0.82rem;
  color: #fff;
}
.tile-footer .mic-status {
  display: inline-flex;
  width: 18px; height: 18px;
  flex-shrink: 0;
}
.tile-footer .mic-status svg { width: 100%; height: 100%; }
.tile-footer .mic-off { display: none; color: var(--bad); }
.member-tile.muted-peer .tile-footer .mic-on { display: none; }
.member-tile.muted-peer .tile-footer .mic-off { display: inline; }
.tile-name {
  font-weight: 600;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}
.share-badge {
  display: inline-flex;
  width: 16px; height: 16px;
  color: var(--accent);
  flex-shrink: 0;
}
.share-badge svg { width: 100%; height: 100%; }

.tile-vol {
  position: absolute;
  top: 0.5rem; left: 0.5rem; right: 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.55rem;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(8px);
  border-radius: 999px;
  opacity: 0;
  transition: opacity 0.18s;
  pointer-events: none;
}
.member-tile:hover .tile-vol,
.member-tile:focus-within .tile-vol { opacity: 1; pointer-events: auto; }
@media (hover: none) { .tile-vol { opacity: 1; pointer-events: auto; } }
.tile-vol .vol-icon { font-size: 0.8rem; flex-shrink: 0; }
.tile-vol input[type="range"] { flex: 1; height: 4px; background: rgba(255,255,255,0.2); }
.tile-vol input[type="range"]::-webkit-slider-runnable-track { background: transparent; }
.tile-vol input[type="range"]::-webkit-slider-thumb { width: 12px; height: 12px; margin-top: -4px; }
.tile-vol input[type="range"]::-moz-range-thumb { width: 12px; height: 12px; }
.tile-vol .vol-val {
  font-size: 0.7rem;
  min-width: 28px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
}

/* ===== Screen share area ===== */
.screen-area { display: flex; flex-direction: column; gap: 0.5rem; }
.screen-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.5rem;
}
.screen-tile {
  position: relative;
  background: #000;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--line-2);
  aspect-ratio: 16 / 9;
}
.screen-tile video {
  width: 100%; height: 100%;
  object-fit: contain;
  display: block;
  background: #000;
  cursor: zoom-in;
}
.screen-label {
  position: absolute;
  left: 0.6rem; bottom: 0.6rem;
  font-size: 0.78rem;
  padding: 0.3rem 0.65rem;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(8px);
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}
.screen-label .live-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--bad);
  box-shadow: 0 0 8px var(--bad);
}
.screen-tile .expand-btn {
  position: absolute;
  top: 0.5rem; right: 0.5rem;
  width: 34px; height: 34px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  opacity: 0;
  transition: opacity 0.2s, background 0.18s, transform 0.1s;
}
.screen-tile .expand-btn svg { width: 16px; height: 16px; }
.screen-tile:hover .expand-btn,
.screen-tile:focus-within .expand-btn { opacity: 1; }
.screen-tile .expand-btn:hover { background: rgba(0, 0, 0, 0.78); }
.screen-tile .expand-btn:active { transform: scale(0.92); }

.expand-btn .ic-compress { display: none; }
.screen-tile.expanded {
  position: fixed;
  inset: 0;
  z-index: 9999;
  aspect-ratio: auto;
  border-radius: 0;
  border: none;
  background: #000;
  margin: 0;
  width: 100%;
  height: 100%;
  contain: layout paint;
}
.screen-tile.expanded video { object-fit: contain; width: 100%; height: 100%; cursor: zoom-out; }
.screen-tile.expanded .expand-btn { opacity: 1; top: max(1rem, env(safe-area-inset-top)); right: max(1rem, env(safe-area-inset-right)); }
.screen-tile.expanded .expand-btn .ic-expand { display: none; }
.screen-tile.expanded .expand-btn .ic-compress { display: block; }
.screen-tile.expanded .screen-label {
  left: max(1rem, env(safe-area-inset-left));
  bottom: max(1rem, env(safe-area-inset-bottom));
  font-size: 0.9rem;
}

body.has-expanded { overflow: hidden; }
body.has-expanded > :not(.screen-tile.expanded) { visibility: hidden; }

/* ===== Control bar (floating, Discord-style) ===== */
.control-bar-wrap {
  position: fixed;
  bottom: max(1rem, env(safe-area-inset-bottom));
  left: 0; right: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.45rem;
  pointer-events: none;
  z-index: 50;
}
.control-bar {
  display: flex;
  gap: 0.45rem;
  padding: 0.55rem;
  background: var(--bg-panel);
  border: 1px solid var(--line-2);
  border-radius: 999px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.5);
  pointer-events: auto;
}
button.ctrl {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: none;
  background: var(--bg-elev);
  color: var(--text);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.15s, color 0.15s, transform 0.1s;
}
button.ctrl svg { width: 22px; height: 22px; }
button.ctrl:hover { background: var(--bg-tile-2); }
button.ctrl:active { transform: scale(0.92); }

button.ctrl .ic-off { display: none; }
button#muteBtn[aria-pressed="true"] {
  background: rgba(242, 63, 66, 0.15);
  color: var(--bad);
}
button#muteBtn[aria-pressed="true"] .ic-on { display: none; }
button#muteBtn[aria-pressed="true"] .ic-off { display: block; }

button#shareBtn[aria-pressed="true"] {
  background: var(--accent-soft);
  color: var(--accent);
}

button.ctrl.leave {
  background: var(--bad);
  color: #fff;
}
button.ctrl.leave:hover { background: #d63339; }

.status {
  font-size: 0.78rem;
  color: var(--text-dim);
  text-align: center;
  pointer-events: none;
  min-height: 1em;
  padding: 0 1rem;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
}

/* ===== Settings drawer ===== */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 998;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.drawer-backdrop.show { opacity: 1; pointer-events: auto; }

.settings-drawer {
  position: fixed;
  top: 0; right: 0;
  height: 100dvh;
  height: 100vh;
  width: min(400px, 100vw);
  background: var(--bg-panel);
  border-left: 1px solid var(--line-2);
  box-shadow: -10px 0 30px rgba(0, 0, 0, 0.4);
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
  z-index: 999;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.settings-drawer.open { transform: translateX(0); }
.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--line-2);
  position: sticky;
  top: 0;
  background: var(--bg-panel);
  z-index: 1;
}
.drawer-header h3 { margin: 0; font-size: 1.05rem; font-weight: 700; }
.drawer-close {
  width: 36px; height: 36px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  border-radius: 8px;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.15s, color 0.15s;
}
.drawer-close:hover { background: var(--bg-elev); color: var(--text); }

.drawer-body {
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}
.drawer-section {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}
.drawer-section h4 {
  margin: 0 0 0.25rem;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.drawer-note {
  margin: 0;
  font-size: 0.74rem;
  color: var(--text-muted);
  line-height: 1.5;
}

/* Toggle switch */
.toggle-row {
  display: flex;
  align-items: flex-start;
  gap: 0.7rem;
  cursor: pointer;
  user-select: none;
  padding: 0.2rem 0;
}
.toggle-row input { position: absolute; opacity: 0; pointer-events: none; }
.toggle-track {
  width: 38px;
  height: 22px;
  background: var(--bg-elev);
  border-radius: 999px;
  position: relative;
  flex-shrink: 0;
  transition: background 0.2s;
  margin-top: 2px;
}
.toggle-thumb {
  position: absolute;
  width: 16px; height: 16px;
  background: #fff;
  border-radius: 50%;
  top: 3px; left: 3px;
  transition: transform 0.2s;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
}
.toggle-row input:checked + .toggle-track { background: var(--accent); }
.toggle-row input:checked + .toggle-track .toggle-thumb { transform: translateX(16px); }
.toggle-text { font-size: 0.86rem; flex: 1; line-height: 1.4; }
.toggle-sub { font-size: 0.74rem; color: var(--text-muted); margin-top: 2px; }

.screen-stats {
  font-size: 0.78rem;
  color: var(--text);
  padding: 0.55rem 0.75rem;
  background: var(--accent-soft);
  border: 1px solid rgba(88, 101, 242, 0.25);
  border-radius: 8px;
  font-variant-numeric: tabular-nums;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.screen-stats::before {
  content: '';
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--good);
  box-shadow: 0 0 8px var(--good);
}

/* ===== Responsive ===== */
@media (max-width: 600px) {
  .call-header { padding: 0.7rem 1rem; }
  .room-name { font-size: 0.98rem; }
  .channel-hash { font-size: 1.15rem; }
  .member-count { font-size: 0.75rem; padding: 0.25rem 0.5rem; }
  .header-btn { width: 32px; height: 32px; }
  .call-main { padding: 0.75rem; padding-bottom: 6.5rem; gap: 0.75rem; }
  .member-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 0.5rem; }
  body.has-screen .member-grid { grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); }
  .member-tile { aspect-ratio: 1 / 1; border-radius: 8px; }
  .tile-footer { padding: 0.35rem 0.5rem; font-size: 0.75rem; }
  .tile-footer .mic-status { width: 14px; height: 14px; }
  button.ctrl { width: 44px; height: 44px; }
  button.ctrl svg { width: 20px; height: 20px; }
  .control-bar { padding: 0.45rem; gap: 0.35rem; }
  .lobby-card { padding: 1.3rem; }
  h1 { font-size: 1.2rem; }
  .drawer-body { padding: 1rem; }
}

@media (max-width: 380px) {
  .call-header { padding: 0.6rem 0.75rem; }
  .room-name { max-width: 40vw; }
  .member-count { display: none; }
  .member-grid { grid-template-columns: repeat(2, 1fr); }
  body.has-screen .member-grid { grid-template-columns: repeat(3, 1fr); }
  button.ctrl { width: 42px; height: 42px; }
  .control-bar { padding: 0.4rem; gap: 0.3rem; }
}

/* Landscape mobile: shorter header, tighter spacing */
@media (max-height: 500px) and (orientation: landscape) {
  .call-header { padding: 0.5rem 1rem; }
  .call-main { padding: 0.5rem 1rem; padding-bottom: 5.5rem; }
  button.ctrl { width: 40px; height: 40px; }
  .control-bar { padding: 0.35rem; }
  .lobby-screen { align-items: flex-start; padding-top: 1rem; }
}

@media (min-width: 1024px) {
  .member-grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
  body.has-screen .member-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
}
