* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --primary: #C41E24;
  --primary-dark: #8B1419;
  --primary-light: #E85D5D;
  --bg: #1A0A0B;
  --surface: #2D1214;
  --surface-light: #3D1E21;
  --text: #FFF5F5;
  --text-dim: #B89A9C;
  --accent: #FF6B6B;
  --green: #4ADE80;
  --yellow: #FACC15;
  --red: #EF4444;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  background: radial-gradient(ellipse at 50% 30%, #2D1214 0%, #1A0A0B 70%);
  color: var(--text);
  height: 100%;
  overflow: hidden;
  position: fixed;
  width: 100%;
  touch-action: manipulation;
}

.view { display: flex; }
.hidden { display: none !important; }

/* ===== LANDING PAGE ===== */
#landing {
  align-items: center;
  justify-content: center;
  min-height: 100%;
  padding: 20px;
  padding-top: calc(20px + var(--safe-top));
}

.landing-card {
  background: var(--surface);
  border: 1px solid rgba(196, 30, 36, 0.3);
  border-radius: 24px;
  padding: 40px 32px;
  text-align: center;
  max-width: 380px;
  width: 100%;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5), 0 0 80px rgba(196, 30, 36, 0.1);
}

.logo {
  margin-bottom: 12px;
}

.lobster-svg {
  width: 90px;
  height: 90px;
  filter: drop-shadow(0 4px 12px rgba(196, 30, 36, 0.4));
}

.landing-card h1 {
  font-size: 26px;
  font-weight: 700;
  margin-bottom: 4px;
  background: linear-gradient(135deg, var(--accent), var(--primary-light));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: var(--text-dim);
  font-size: 14px;
  margin-bottom: 28px;
}

#join-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#name-input {
  background: var(--bg);
  border: 2px solid rgba(196, 30, 36, 0.3);
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 16px;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
  /* Prevent iOS zoom on focus */
  font-size: 16px;
}

#name-input:focus {
  border-color: var(--primary);
}

#name-input::placeholder {
  color: var(--text-dim);
}

#join-btn {
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 12px;
  padding: 16px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  /* Better touch target */
  min-height: 50px;
}

#join-btn:hover {
  background: var(--primary-light);
}

#join-btn:active {
  transform: scale(0.97);
  background: var(--primary-dark);
}

#join-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.error-msg {
  color: var(--red);
  font-size: 13px;
  margin-top: 8px;
  min-height: 20px;
}

/* ===== ROOM VIEW ===== */
#room {
  flex-direction: column;
  height: 100%;
  width: 100%;
}

.room-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  padding-top: calc(10px + var(--safe-top));
  background: var(--surface);
  border-bottom: 1px solid rgba(196, 30, 36, 0.2);
  flex-shrink: 0;
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo-small {
  width: 28px;
  height: 28px;
}

.header-title {
  font-weight: 600;
  font-size: 15px;
  color: var(--accent);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.network-status {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--text-dim);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}

.status-dot.good { background: var(--green); box-shadow: 0 0 6px var(--green); }
.status-dot.degraded { background: var(--yellow); box-shadow: 0 0 6px var(--yellow); }
.status-dot.disconnected { background: var(--red); box-shadow: 0 0 6px var(--red); }

.user-count {
  font-size: 12px;
  color: var(--text-dim);
  background: var(--bg);
  padding: 3px 8px;
  border-radius: 20px;
  white-space: nowrap;
}

/* ===== USERS GRID ===== */
.users-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  align-content: center;
  gap: 16px;
  padding: 20px 16px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.user-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 16px 12px;
  border-radius: 16px;
  background: var(--surface);
  border: 2px solid rgba(196, 30, 36, 0.15);
  transition: border-color 0.3s, box-shadow 0.3s;
}

.user-card.is-you {
  border-color: rgba(196, 30, 36, 0.4);
}

.user-card.speaking {
  border-color: var(--green);
  box-shadow: 0 0 20px rgba(74, 222, 128, 0.2), 0 0 40px rgba(74, 222, 128, 0.1);
}

.user-avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  font-weight: 700;
  color: white;
  position: relative;
  transition: box-shadow 0.3s;
  flex-shrink: 0;
}

.user-card.speaking .user-avatar {
  box-shadow: 0 0 0 3px var(--green), 0 0 16px rgba(74, 222, 128, 0.4);
  animation: pulse-ring 1.5s ease-in-out infinite;
}

@keyframes pulse-ring {
  0%, 100% { box-shadow: 0 0 0 3px var(--green), 0 0 16px rgba(74, 222, 128, 0.4); }
  50% { box-shadow: 0 0 0 6px rgba(74, 222, 128, 0.3), 0 0 24px rgba(74, 222, 128, 0.3); }
}

.muted-icon {
  position: absolute;
  bottom: -2px;
  right: -2px;
  width: 22px;
  height: 22px;
  background: var(--red);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--surface);
}

.muted-icon svg {
  width: 11px;
  height: 11px;
  stroke: white;
}

.user-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  max-width: 110px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
}

.user-tag {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: -4px;
}

/* ===== ROOM CONTROLS ===== */
.room-controls {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 14px 16px;
  padding-bottom: calc(14px + var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid rgba(196, 30, 36, 0.2);
  flex-shrink: 0;
  z-index: 10;
}

.control-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--surface-light);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 14px;
  padding: 14px 28px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s, transform 0.1s;
  /* Good touch target */
  min-height: 50px;
  user-select: none;
  -webkit-user-select: none;
}

.control-btn svg {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.control-btn:active {
  transform: scale(0.95);
}

.control-btn.muted {
  background: var(--red);
  border-color: var(--red);
}

.leave-btn {
  background: rgba(239, 68, 68, 0.15);
  border-color: rgba(239, 68, 68, 0.3);
  color: var(--red);
}

.leave-btn:active {
  background: rgba(239, 68, 68, 0.3);
}

/* ===== TOAST ===== */
#toast-container {
  position: fixed;
  top: calc(12px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 90%;
  max-width: 320px;
  pointer-events: none;
}

.toast {
  background: var(--surface);
  border: 1px solid rgba(196, 30, 36, 0.3);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  text-align: center;
  animation: toast-in 0.3s ease, toast-out 0.3s ease 2.7s forwards;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes toast-out {
  from { opacity: 1; }
  to { opacity: 0; }
}

/* ===== PHONE PORTRAIT (small screens) ===== */
@media (max-width: 480px) {
  .landing-card {
    padding: 32px 24px;
    border-radius: 20px;
  }

  .lobster-svg { width: 72px; height: 72px; }
  .landing-card h1 { font-size: 22px; }
  .subtitle { font-size: 13px; margin-bottom: 24px; }

  .users-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    padding: 16px 12px;
  }

  .user-card { padding: 14px 10px; border-radius: 14px; }
  .user-avatar { width: 52px; height: 52px; font-size: 22px; }
  .user-name { font-size: 12px; max-width: 90px; }

  .room-controls { gap: 10px; padding: 12px; padding-bottom: calc(12px + var(--safe-bottom)); }

  .control-btn {
    padding: 12px 20px;
    font-size: 13px;
    min-height: 46px;
    border-radius: 12px;
    flex: 1;
    max-width: 160px;
  }
}

/* ===== PHONE LANDSCAPE ===== */
@media (max-height: 500px) and (orientation: landscape) {
  .room-header { padding: 6px 16px; }
  .users-grid {
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
    gap: 10px;
    padding: 10px 12px;
  }
  .user-card { padding: 10px 8px; gap: 4px; }
  .user-avatar { width: 40px; height: 40px; font-size: 18px; }
  .user-name { font-size: 11px; }
  .user-tag { font-size: 10px; }
  .room-controls { padding: 8px 12px; padding-bottom: calc(8px + var(--safe-bottom)); }
  .control-btn { min-height: 40px; padding: 8px 16px; }
}

/* ===== TABLET ===== */
@media (min-width: 481px) and (max-width: 768px) {
  .users-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
    padding: 24px 20px;
  }
}

/* ===== DESKTOP ===== */
@media (min-width: 769px) {
  .users-grid {
    grid-template-columns: repeat(auto-fit, minmax(150px, 180px));
    gap: 20px;
    padding: 32px 24px;
  }

  .control-btn:hover {
    background: rgba(255,255,255,0.1);
  }

  .leave-btn:hover {
    background: rgba(239, 68, 68, 0.25);
  }
}

#audio-container { display: none; }
