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

:root {
  --bg: #ffffff;
  --surface: #f5f5f5;
  --border: #d0d0d0;
  --text: #1a1a1a;
  --muted: #666;
  --accent: #2d8cf0;
  --danger: #d9534f;
  --success: #2e7d32;
  --header-h: 52px;
  --tap-min: 44px;         /* Apple HIG / WCAG minimum touch target */
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ===== HEADER ===== */
header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  min-height: var(--header-h);
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  z-index: 100;
  position: relative;
}

header h1 { font-size: 1.1rem; font-weight: 700; white-space: nowrap; }

#header-setup {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex: 1;
  min-width: 0;
}

#header-setup input {
  height: var(--tap-min);
  font-size: 0.9rem;
  padding: 0 0.5rem;
  min-width: 0;
}

#passkey     { width: 120px; flex-shrink: 1; }
#room-label  { width: 110px; flex-shrink: 1; }

#btn-connect {
  background: var(--accent);
  color: #fff;
  height: var(--tap-min);
  padding: 0 1rem;
  font-size: 0.9rem;
  white-space: nowrap;
}
#btn-connect:hover { background: #2577d4; }

#btn-settings {
  background: transparent;
  border: 1px solid var(--border);
  height: var(--tap-min);
  width: var(--tap-min);
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  padding: 0;
  flex-shrink: 0;
}
#btn-settings:hover { border-color: var(--accent); color: var(--accent); }

.btn-info {
  background: transparent;
  border: 1px solid var(--border);
  height: var(--tap-min);
  width: var(--tap-min);
  font-size: 1.1rem;
  display: none;           /* Hidden on desktop — title is visible */
  align-items: center;
  justify-content: center;
  color: var(--muted);
  padding: 0;
  flex-shrink: 0;
}
.btn-info:hover { border-color: var(--accent); color: var(--accent); }

.connect-error {
  font-size: 0.85rem;
  color: var(--danger);
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 300px;
}

#status { font-weight: 500; font-size: 0.85rem; white-space: nowrap; margin-left: auto; }
.status-disconnected { color: var(--danger); }
.status-connecting { color: #e67e00; }
.status-connected { color: var(--success); }

/* ===== SETTINGS MODAL ===== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.overlay-hidden {
  display: none !important;
}

.modal {
  background: var(--bg);
  border-radius: 8px;
  padding: 1.5rem;
  width: 380px;
  max-width: 92vw;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0,0,0,0.15);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h2 { font-size: 1.1rem; }

.btn-icon {
  background: transparent;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--muted);
  line-height: 1;
  padding: 0.4rem;
  min-width: var(--tap-min);
  min-height: var(--tap-min);
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-icon:hover { color: var(--text); }

.btn-done {
  background: var(--accent);
  color: #fff;
  margin-top: 0.5rem;
  width: 100%;
  min-height: var(--tap-min);
}
.btn-done:hover { background: #2577d4; }

/* ===== FORM ELEMENTS ===== */
.form-group { display: flex; flex-direction: column; gap: 0.25rem; }
.form-group label { font-size: 0.9rem; color: var(--muted); }

input, select {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.5rem 0.75rem;
  border-radius: 4px;
  font-size: 1rem;
}

input:focus, select:focus { outline: none; border-color: var(--accent); }

button {
  padding: 0.6rem 1.25rem;
  border: none;
  border-radius: 4px;
  font-size: 0.9rem;
  cursor: pointer;
  font-weight: 500;
}

/* ===== SETUP PANEL (pre-connect landing) ===== */
#setup-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2rem 1rem 3rem;
  min-height: calc(100vh - var(--header-h));
  gap: 2rem;
}

.setup-hero {
  text-align: center;
}

.setup-hero h2 {
  font-size: 1.6rem;
  color: var(--text);
  margin-bottom: 0.4rem;
}

.setup-hero .subtitle {
  color: var(--muted);
  font-size: 0.9rem;
  letter-spacing: 0.02em;
}

/* Info cards grid */
.info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1rem;
  max-width: 680px;
  width: 100%;
}

.info-card {
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
}

.info-card-icon {
  font-size: 1.5rem;
  line-height: 1;
  flex-shrink: 0;
  margin-top: 1px;
}

.info-card-body strong {
  display: block;
  font-size: 0.9rem;
  margin-bottom: 0.3rem;
  color: var(--text);
}

.info-card-body p {
  font-size: 0.85rem;
  color: var(--muted);
  line-height: 1.5;
}

.hint-small {
  font-size: 0.8rem;
  color: #999;
}

.hint-small code {
  background: var(--border);
  padding: 1px 4px;
  border-radius: 3px;
  font-family: monospace;
  font-size: 0.8rem;
  color: var(--text);
}

.setup-footer-hint {
  font-size: 0.85rem;
  color: var(--muted);
  text-align: center;
}

/* ===== CALL PANEL (full-screen video) ===== */
#call-panel {
  position: fixed;
  inset: 0;
  top: var(--header-h);
  background: #111;
  display: flex;
  flex-direction: column;
}

#video-container {
  flex: 1;
  position: relative;
  overflow: hidden;
}

/* Remote video = full screen */
#remote-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #222;
}

/* Local video = PiP */
#local-video {
  position: absolute;
  bottom: 80px;
  right: 16px;
  width: 200px;
  border-radius: 8px;
  background: #333;
  aspect-ratio: 16/9;
  object-fit: cover;
  transform: scaleX(-1);
  box-shadow: 0 2px 12px rgba(0,0,0,0.4);
  z-index: 10;
  cursor: grab;
  touch-action: none;
  border: 2px solid rgba(255,255,255,0.15);
}

/* ===== CALL STATUS TOAST ===== */
.call-toast {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(8px);
  color: #fff;
  font-size: 0.85rem;
  padding: 0.5rem 1rem;
  border-radius: 20px;
  z-index: 25;
  white-space: nowrap;
  transition: opacity 0.3s ease;
  pointer-events: none;
  max-width: 90vw;
  overflow: hidden;
  text-overflow: ellipsis;
}

.call-toast-hidden {
  opacity: 0;
}

.call-toast-error {
  background: rgba(180, 40, 40, 0.85);
}

/* ===== TRAFFIC STATS (always-on, inconspicuous) ===== */
.stats-overlay {
  position: absolute;
  bottom: 68px;
  left: 12px;
  background: rgba(0, 0, 0, 0.45);
  color: rgba(255, 255, 255, 0.6);
  font-size: 0.65rem;
  font-family: monospace;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  z-index: 12;
  line-height: 1.5;
  pointer-events: none;
  white-space: nowrap;
}

/* ===== WAITING FOR PEER OVERLAY ===== */
.waiting-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(17, 17, 17, 0.9);
  z-index: 5;
}

.waiting-overlay[hidden] {
  display: none;
}

.waiting-content {
  text-align: center;
  color: #ccc;
  padding: 1rem;
}

.waiting-content p {
  font-size: 1rem;
  margin-top: 1rem;
}

.waiting-hint {
  font-size: 0.85rem !important;
  color: #888 !important;
  margin-top: 0.3rem !important;
}

.waiting-spinner {
  width: 40px;
  height: 40px;
  margin: 0 auto;
  border: 3px solid rgba(255,255,255,0.15);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== FLOATING CONTROL BAR ===== */
#controls-bar {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 0.5rem;
  background: rgba(30, 30, 30, 0.85);
  backdrop-filter: blur(12px);
  padding: 0.5rem 0.75rem;
  border-radius: 12px;
  z-index: 20;
}

.ctrl-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: rgba(255,255,255,0.1);
  border: none;
  color: #fff;
  border-radius: 8px;
  padding: 0.5rem 0.9rem;
  cursor: pointer;
  transition: background 0.15s;
  min-width: 56px;
  min-height: var(--tap-min);
}

.ctrl-btn:hover { background: rgba(255,255,255,0.2); }

.ctrl-btn.muted-state {
  background: rgba(255,80,80,0.25);
  color: #ff8888;
}

.ctrl-btn.muted-state:hover {
  background: rgba(255,80,80,0.35);
}

.ctrl-btn-danger {
  background: var(--danger) !important;
}
.ctrl-btn-danger:hover { background: #c9302c !important; }

.ctrl-icon { font-size: 1.3rem; line-height: 1; }
.ctrl-label { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.03em; }

/* ===== CHAT PANEL (slide-out) ===== */
#chat-panel {
  position: absolute;
  top: 0;
  right: 0;
  width: 320px;
  height: 100%;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  z-index: 15;
  box-shadow: -4px 0 16px rgba(0,0,0,0.2);
  transition: transform 0.25s ease;
}

#chat-panel.chat-hidden {
  transform: translateX(100%);
}

#chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.9rem;
  min-height: var(--tap-min);
}

#chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
  font-size: 0.9rem;
}

#chat-log .msg { margin-bottom: 0.25rem; }
#chat-log .msg-self { color: var(--accent); }
#chat-log .msg-peer { color: var(--success); }

#chat-input-row {
  display: flex;
  border-top: 1px solid var(--border);
}

#chat-input {
  flex: 1;
  border: none;
  border-radius: 0;
  min-height: var(--tap-min);
}

#btn-send {
  background: var(--accent);
  color: #fff;
  flex-shrink: 0;
  border-radius: 0;
  min-height: var(--tap-min);
  min-width: var(--tap-min);
}

/* ===== RESPONSIVE: MOBILE ===== */
@media (max-width: 600px) {
  /* Header: hide title, show info icon, setup form wraps below */
  header {
    gap: 0.4rem 0.5rem;
    padding: 0.4rem 0.75rem;
    height: auto;
    min-height: var(--header-h);
  }

  header h1 { display: none; }
  .btn-info { display: flex; }

  #header-setup {
    order: 1;                /* Push setup form to second row */
    flex-basis: 100%;        /* Force it onto its own line */
    gap: 0.3rem;
  }

  #header-setup input { flex: 1; width: auto; min-width: 0; }
  #passkey     { flex: 1; width: auto; }
  #room-label  { flex: 1; width: auto; }

  #btn-connect {
    flex-shrink: 0;
    padding: 0 0.75rem;
  }

  .connect-error {
    order: 2;
    flex-basis: 100%;
    max-width: none;
  }

  /* During a call: header is hidden on mobile (via JS), full-screen video. */
  #call-panel {
    top: 0;
  }

  /* Local PiP: smaller, above control bar */
  #local-video {
    width: 110px;
    bottom: 76px;
    right: 8px;
  }

  /* Chat: full screen overlay on mobile */
  #chat-panel { width: 100%; }

  /* Info grid: single column */
  .info-grid { grid-template-columns: 1fr; }

  /* Setup panel: tighter padding */
  #setup-panel {
    padding: 1.5rem 0.75rem 2rem;
    gap: 1.5rem;
  }

  .setup-hero h2 { font-size: 1.3rem; }

  /* Control bar: safe area for phones with gesture bars */
  #controls-bar {
    bottom: max(12px, env(safe-area-inset-bottom, 0px));
    padding: 0.4rem 0.5rem;
    gap: 0.35rem;
  }

  .ctrl-btn {
    min-width: 50px;
    padding: 0.4rem 0.6rem;
  }

  /* Toast: allow wrapping on narrow screens */
  .call-toast {
    white-space: normal;
    text-align: center;
    max-width: 85vw;
  }
}

/* ===== RESPONSIVE: VERY SMALL (≤360px) ===== */
@media (max-width: 360px) {
  header h1 { font-size: 1rem; }

  .ctrl-btn {
    min-width: 44px;
    padding: 0.35rem 0.4rem;
  }

  .ctrl-label { font-size: 0.6rem; }
}
