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

:root {
  --bg: #111;
  --surface: #1e1e1e;
  --border: #333;
  --accent: #1a73e8;
  --accent-hover: #1557b0;
  --danger: #ea4335;
  --danger-hover: #c5221f;
  --text: #fff;
  --text-muted: #aaa;
  --speaking: #34a853;
  --radius: 12px;
  --ctrl: 56px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

/* ── Home page ─────────────────────────────────────────────────────── */

.home-page {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  gap: 32px;
}

.logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  text-align: center;
}

.logo h1 {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.logo p {
  color: var(--text-muted);
  font-size: 1rem;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 16px 32px;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  -webkit-user-select: none;
  user-select: none;
  text-decoration: none;
  color: var(--text);
}

.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--accent);
  width: 100%;
  max-width: 320px;
  padding: 18px;
  font-size: 1.1rem;
}

.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }

.btn-secondary {
  background: var(--surface);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: #2a2a2a; }

/* Share panel */
.share-panel {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: 100%;
  max-width: 360px;
}
.share-panel.visible { display: flex; }

.share-label {
  color: var(--text-muted);
  font-size: 0.875rem;
}

.link-row {
  display: flex;
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.link-input {
  flex: 1;
  background: none;
  border: none;
  padding: 14px 16px;
  font-size: 0.875rem;
  color: var(--text);
  cursor: text;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.link-input:focus { outline: none; }

.copy-btn {
  flex-shrink: 0;
  background: var(--accent);
  border: none;
  padding: 0 20px;
  cursor: pointer;
  color: var(--text);
  font-weight: 600;
  font-size: 0.875rem;
  transition: background 0.15s;
}
.copy-btn:hover { background: var(--accent-hover); }
.copy-btn.copied { background: var(--speaking); }

.enter-btn { width: 100%; max-width: 360px; }

/* ── Status screens (loading / error) ─────────────────────────────── */

.status-screen {
  flex: 1;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px;
  gap: 20px;
  text-align: center;
}
.status-screen.visible { display: flex; }

.status-screen svg { opacity: 0.6; }
.status-screen h2 { font-size: 1.4rem; }
.status-screen p { color: var(--text-muted); font-size: 0.95rem; max-width: 300px; line-height: 1.5; }

/* Spinner */
@keyframes spin { to { transform: rotate(360deg); } }
.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* ── Call page ──────────────────────────────────────────────────────── */

.call-page {
  flex: 1;
  min-height: 0;
  display: none;
  flex-direction: column;
}
.call-page.active { display: flex; }

/* Video grid */
.video-grid {
  flex: 1;
  min-height: 0;
  display: grid;
  gap: 3px;
  padding: 3px;
  background: #000;
}

.video-grid[data-count="1"] {
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
}

/* Portrait: stack vertically; Landscape: side by side */
.video-grid[data-count="2"] {
  grid-template-columns: 1fr;
  grid-template-rows: 1fr 1fr;
}

.video-grid[data-count="3"],
.video-grid[data-count="4"] {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}

/* 3rd tile full-width in 2×2 grid */
.video-grid[data-count="3"] .video-tile:last-child {
  grid-column: 1 / -1;
}

@media (orientation: landscape) {
  .video-grid[data-count="2"] {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr;
  }
}

/* Video tile */
.video-tile {
  position: relative;
  background: #1a1a1a;
  border-radius: 6px;
  overflow: hidden;
  transition: box-shadow 0.1s;
}

.video-tile.speaking {
  box-shadow: 0 0 0 3px var(--speaking);
}

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

/* Mirror local camera */
.video-tile.local video { transform: scaleX(-1); }

/* Camera-off overlay */
.cam-off-overlay {
  display: none;
  position: absolute;
  inset: 0;
  background: #1a1a1a;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-muted);
  font-size: 0.8rem;
}
.cam-off-overlay svg { width: 44px; height: 44px; opacity: 0.45; }
.video-tile.cam-off .cam-off-overlay { display: flex; }

/* Connecting overlay */
.connecting-overlay {
  display: none;
  position: absolute;
  inset: 0;
  background: #1a1a1a;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-muted);
  font-size: 0.8rem;
}
.video-tile.connecting .connecting-overlay { display: flex; }

/* Failed overlay */
.failed-overlay {
  display: none;
  position: absolute;
  inset: 0;
  background: #1a1a1a;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--text-muted);
  font-size: 0.8rem;
  text-align: center;
  padding: 16px;
}
.failed-overlay svg { width: 36px; height: 36px; opacity: 0.4; }
.video-tile.failed .failed-overlay { display: flex; }

/* Mic-off badge */
.mic-badge {
  display: none;
  position: absolute;
  bottom: 8px;
  left: 8px;
  background: rgba(0, 0, 0, 0.65);
  border-radius: 50%;
  width: 28px;
  height: 28px;
  align-items: center;
  justify-content: center;
}
.mic-badge svg { width: 16px; height: 16px; fill: #fff; }
.video-tile.mic-off .mic-badge { display: flex; }

/* Controls bar */
.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 14px 16px;
  background: #111;
  flex-shrink: 0;
}

.ctrl-btn {
  width: var(--ctrl);
  height: var(--ctrl);
  border-radius: 50%;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  background: #2a2a2a;
}
.ctrl-btn:active { transform: scale(0.92); }
.ctrl-btn svg { width: 24px; height: 24px; fill: var(--text); }

.ctrl-btn.off {
  background: #3a3a3a;
}
.ctrl-btn.off svg { fill: #888; }

.ctrl-btn.danger {
  background: var(--danger);
  width: 64px;
  height: 64px;
}
.ctrl-btn.danger svg { width: 28px; height: 28px; }
.ctrl-btn.danger:hover { background: var(--danger-hover); }

/* Toast */
.toast {
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%) translateY(12px);
  background: rgba(40, 40, 40, 0.92);
  backdrop-filter: blur(8px);
  border-radius: 24px;
  padding: 10px 20px;
  font-size: 0.875rem;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
  white-space: nowrap;
  z-index: 100;
  max-width: 90vw;
  text-align: center;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Compact controls on landscape mobile */
@media (orientation: landscape) and (max-height: 500px) {
  .controls { padding: 6px 16px; gap: 12px; }
  .ctrl-btn { --ctrl: 44px; }
  .ctrl-btn.danger { width: 50px; height: 50px; }
}
