:root {
  --bg: #0f0f12;
  --surface: #1a1a21;
  --surface-2: #24242e;
  --border: #2e2e3a;
  --text: #f4f4f6;
  --text-muted: #9ca3af;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --danger: #ef4444;
  --success: #22c55e;
  --warning: #f59e0b;
  --radius: 12px;
  --radius-sm: 8px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --header-h: 52px;
  --toolbar-h: 36px;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
}

#app {
  height: 100%;
  height: 100dvh;
}

.screen {
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.screen[hidden] {
  display: none !important;
}

/* ── Lobby ── */

.screen-lobby {
  padding: max(16px, env(safe-area-inset-top)) 16px 24px;
  justify-content: center;
  gap: 24px;
  max-width: 420px;
  margin: 0 auto;
  width: 100%;
}

.lobby-header {
  text-align: center;
}

.logo {
  font-size: 48px;
  margin-bottom: 8px;
}

.lobby-header h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: 4px;
}

.lobby-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.field-label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.field-row {
  display: flex;
  gap: 8px;
}

input[type="text"] {
  flex: 1;
  min-width: 0;
  padding: 14px 16px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 16px;
}

input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
}

.hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 10px;
}

.media-error {
  margin-top: 12px;
  padding: 12px;
  background: rgba(239, 68, 68, 0.12);
  border: 1px solid rgba(239, 68, 68, 0.35);
  border-radius: var(--radius-sm);
  color: #fca5a5;
  font-size: 0.82rem;
  line-height: 1.45;
}

.media-error strong {
  display: block;
  color: #fecaca;
  margin-bottom: 4px;
}

.permissions-compact {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  justify-content: center;
}

.perm-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
}

.perm-badge {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.perm-badge.granted { color: var(--success); border-color: var(--success); }
.perm-badge.denied { color: var(--danger); border-color: var(--danger); }

.media-settings {
  width: 100%;
  max-width: 420px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field-select {
  width: 100%;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 16px;
}

.field-select:focus {
  outline: none;
  border-color: var(--accent);
}

.media-settings .field-label,
.settings-panel-body .field-label {
  margin-top: 8px;
  margin-bottom: 6px;
}

.media-settings .field-label:first-child,
.settings-panel-body .field-label:first-child {
  margin-top: 0;
}

/* ── Buttons ── */

.btn {
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  padding: 14px 20px;
  transition: background 0.15s, transform 0.1s;
  white-space: nowrap;
}

.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  color: white;
}

.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

.btn-ghost {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--border);
}

.btn-sm { padding: 10px 16px; font-size: 0.85rem; }

.btn-icon {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 4px 8px;
}

/* ── Call screen ── */

.screen-call {
  position: relative;
  overflow: hidden;
}

.call-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-height: var(--header-h);
  z-index: 10;
}

.room-badge {
  font-size: 0.75rem;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 28%;
  flex-shrink: 1;
}

.net-speeds {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 6px 12px;
  flex: 1;
  min-width: 0;
}

.net-speed {
  font-size: 0.72rem;
  white-space: nowrap;
}

.net-speed-label {
  color: var(--text-muted);
}

.net-speed-value {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.net-speed-total .net-speed-value {
  color: var(--accent);
}

.call-meta-secondary {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.75rem;
  color: var(--text-muted);
  flex-shrink: 0;
}

.call-timer {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  min-width: 2.5rem;
}

/* ── Video stage & grid ── */

.video-stage {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}

.pinned-slot {
  flex-shrink: 0;
  padding: 8px 8px 0;
  max-height: 45vh;
}

.pinned-slot:not([hidden]) + .video-grid {
  padding-top: 4px;
}

.video-grid {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px;
  padding-bottom: calc(var(--toolbar-h) + var(--safe-bottom) + 8px);
  display: grid;
  gap: 8px;
  align-content: start;
  grid-template-columns: 1fr;
}

.video-grid[data-count="2"] { grid-template-columns: repeat(2, 1fr); }
.video-grid[data-count="3"],
.video-grid[data-count="4"] { grid-template-columns: repeat(2, 1fr); }
.video-grid[data-count="5"],
.video-grid[data-count="6"] { grid-template-columns: repeat(2, 1fr); }

@media (min-width: 640px) {
  .video-grid[data-count="3"] { grid-template-columns: repeat(3, 1fr); }
  .video-grid[data-count="4"] { grid-template-columns: repeat(2, 1fr); }
  .video-grid[data-count="5"],
  .video-grid[data-count="6"] { grid-template-columns: repeat(3, 1fr); }
}

@media (min-width: 1024px) {
  .video-grid[data-count="4"] { grid-template-columns: repeat(4, 1fr); }
  .video-grid[data-count="5"],
  .video-grid[data-count="6"] { grid-template-columns: repeat(3, 1fr); }
  .video-grid[data-count="7"],
  .video-grid[data-count="8"],
  .video-grid[data-count="9"] { grid-template-columns: repeat(3, 1fr); }
}

/* Duo (2 pessoas): tela cheia + PiP embaixo */

.screen-call.layout-duo .video-stage {
  overflow: hidden;
}

.screen-call.layout-duo .video-grid {
  position: absolute;
  inset: 0;
  display: block;
  padding: 0;
  padding-bottom: calc(var(--toolbar-h) + var(--safe-bottom));
  overflow: hidden;
}

.screen-call.layout-duo .video-tile.duo-main {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  aspect-ratio: unset;
  min-height: 0;
  border-radius: 0;
  z-index: 1;
}

.screen-call.layout-duo .video-tile.duo-main video {
  object-fit: cover;
}

.screen-call.layout-duo .video-tile.duo-pip {
  position: absolute;
  width: 32%;
  max-width: 168px;
  min-width: 108px;
  aspect-ratio: 3 / 4;
  z-index: 5;
  border-radius: var(--radius-sm);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.55);
  cursor: grab;
  touch-action: none;
  user-select: none;
  outline: 2px solid rgba(255, 255, 255, 0.35);
}

.screen-call.layout-duo .video-tile.duo-pip.duo-pip-dragging {
  cursor: grabbing;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.65);
}

.screen-call.layout-duo .video-tile.duo-pip .tile-label {
  font-size: 0.65rem;
  bottom: 32px;
}

.screen-call.layout-duo .video-tile.duo-pip .tile-controls {
  transform: scale(0.85);
  transform-origin: bottom right;
}

/* Tile */

.video-tile {
  position: relative;
  background: #000;
  border-radius: var(--radius-sm);
  overflow: hidden;
  aspect-ratio: 4 / 3;
  min-height: 120px;
}

.video-tile.is-local { outline: 2px solid var(--accent); outline-offset: -2px; }
.video-tile.cam-hidden { display: none !important; }
.video-tile.is-pinned { outline: 2px solid var(--warning); outline-offset: -2px; }
.video-tile.is-connecting { opacity: 0.7; }

.video-tile video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  background: #111;
}

.video-tile.fullscreen-active {
  position: fixed;
  inset: 0;
  z-index: 1000;
  aspect-ratio: unset;
  border-radius: 0;
  min-height: 100%;
}

.video-tile.fullscreen-active video {
  object-fit: contain;
}

.tile-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(to top, rgba(0,0,0,0.75) 0%, transparent 40%);
}

.tile-label {
  position: absolute;
  bottom: 36px;
  left: 8px;
  right: 8px;
  font-size: 0.75rem;
  font-weight: 600;
  color: white;
  text-shadow: 0 1px 3px rgba(0,0,0,0.8);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tile-status {
  position: absolute;
  top: 8px;
  left: 8px;
  font-size: 0.65rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(0,0,0,0.6);
  color: var(--text-muted);
}

.tile-status.live { color: var(--success); }
.tile-status.muted-label { color: var(--warning); }

.tile-controls {
  position: absolute;
  bottom: 6px;
  right: 6px;
  display: flex;
  gap: 4px;
  pointer-events: auto;
}

.tile-btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 8px;
  background: rgba(0,0,0,0.65);
  color: white;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.tile-btn:active { background: rgba(255,255,255,0.2); }
.tile-btn.active { background: var(--accent); }
.tile-btn.muted { background: var(--danger); }

.pinned-slot .video-tile {
  aspect-ratio: 16 / 9;
  max-height: 45vh;
  width: 100%;
}

/* ── Toolbar ── */

.call-toolbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  padding-bottom: calc(4px + var(--safe-bottom));
  background: linear-gradient(to top, var(--bg) 60%, transparent);
  z-index: 20;
}

.toolbar-btn {
  position: relative;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: none;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, transform 0.1s;
  flex-shrink: 0;
}

.toolbar-btn::before {
  content: '';
  position: absolute;
  inset: -8px;
}

.toolbar-btn .icon {
  position: relative;
  z-index: 1;
  font-size: 0.5rem;
  line-height: 1;
}

.toolbar-btn:active { transform: scale(0.93); }
.toolbar-btn.off { background: var(--danger); }
.toolbar-btn-danger {
  width: 23px;
  height: 23px;
  background: var(--danger);
}

.toolbar-btn-danger::before {
  inset: -6px;
}

.toolbar-btn-danger .icon {
  font-size: 0.65rem;
}

.toolbar-btn.active-panel { background: var(--accent); }

/* ── Chat panel ── */

.chat-panel {
  position: fixed;
  bottom: calc(var(--toolbar-h) + var(--safe-bottom));
  left: 0;
  right: 0;
  max-height: 50vh;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 15;
  border-radius: var(--radius) var(--radius) 0 0;
}

.chat-panel[hidden] { display: none !important; }

.settings-panel {
  position: fixed;
  bottom: calc(var(--toolbar-h) + var(--safe-bottom));
  left: 0;
  right: 0;
  max-height: 55vh;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 15;
  border-radius: var(--radius) var(--radius) 0 0;
}

.settings-panel[hidden] { display: none !important; }

.settings-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.9rem;
}

.settings-panel-body {
  padding: 16px;
  overflow-y: auto;
}

.chat-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.9rem;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  min-height: 120px;
  max-height: 30vh;
}

.chat-message {
  margin-bottom: 10px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  line-height: 1.4;
}

.chat-message.own {
  background: var(--accent);
  color: white;
  margin-left: 15%;
  text-align: right;
}

.chat-message.other {
  background: var(--surface-2);
  margin-right: 15%;
}

.chat-message.system {
  background: transparent;
  color: var(--text-muted);
  text-align: center;
  font-size: 0.8rem;
  font-style: italic;
}

.message-sender {
  display: block;
  font-size: 0.7rem;
  font-weight: 600;
  opacity: 0.85;
  margin-bottom: 2px;
}

.message-time {
  display: block;
  font-size: 0.65rem;
  opacity: 0.6;
  margin-bottom: 4px;
}

.chat-input-row {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.chat-input-row input {
  flex: 1;
  min-width: 0;
}

/* ── Log ── */

.log-drawer {
  position: fixed;
  top: var(--header-h);
  right: 8px;
  z-index: 5;
  max-width: 280px;
}

.log-drawer summary {
  font-size: 0.7rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 8px;
  background: rgba(0,0,0,0.4);
  border-radius: var(--radius-sm);
  list-style: none;
}

.log-drawer summary::-webkit-details-marker { display: none; }

.log {
  background: rgba(0,0,0,0.85);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px;
  max-height: 160px;
  overflow-y: auto;
  font-family: ui-monospace, monospace;
  font-size: 0.65rem;
  line-height: 1.5;
  margin-top: 4px;
}

.log-entry { margin-bottom: 4px; color: var(--text-muted); }
.log-entry.success { color: var(--success); }
.log-entry.error { color: var(--danger); }
.log-entry.info { color: #93c5fd; }

@media (min-width: 768px) {
  .screen-call {
    max-width: 1200px;
    margin: 0 auto;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }

  .chat-panel {
    left: auto;
    right: 16px;
    bottom: calc(var(--toolbar-h) + var(--safe-bottom) + 8px);
    width: 340px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    max-height: 420px;
  }

  .chat-messages { max-height: 280px; }
}

@media (max-width: 380px) {
  .video-grid[data-count="2"],
  .video-grid[data-count="3"],
  .video-grid[data-count="4"] {
    grid-template-columns: 1fr;
  }
}
