:root {
  color-scheme: dark;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --app-header-h: 3.5rem;
}

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  height: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  overflow-x: hidden;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(46, 207, 148, 0.12), transparent 55%),
    radial-gradient(900px 500px at 90% 10%, rgba(255, 155, 106, 0.08), transparent 50%),
    #070b10;
}

.safe-top {
  padding-top: var(--safe-top);
}

.safe-bottom {
  padding-bottom: max(0.75rem, var(--safe-bottom));
}

#app {
  height: 100dvh;
  height: -webkit-fill-available;
  max-height: 100dvh;
}

.app-shell {
  max-width: 1400px;
}

.atmosphere {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(160deg, rgba(7, 11, 16, 0.2), rgba(7, 11, 16, 0.85)),
    radial-gradient(ellipse 80% 60% at 70% 40%, rgba(46, 207, 148, 0.16), transparent 60%),
    radial-gradient(ellipse 50% 40% at 20% 80%, rgba(255, 155, 106, 0.1), transparent 55%),
    #0c1219;
  animation: drift 18s ease-in-out infinite alternate;
}

.noise {
  position: absolute;
  inset: 0;
  opacity: 0.07;
  pointer-events: none;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

@keyframes drift {
  from {
    transform: scale(1) translate3d(0, 0, 0);
  }
  to {
    transform: scale(1.06) translate3d(-1.5%, 1%, 0);
  }
}

.gate-fade {
  opacity: 0;
  transform: translateY(18px);
  animation: rise-in 0.7s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.gate-delay-1 {
  animation-delay: 0.12s;
}
.gate-delay-2 {
  animation-delay: 0.24s;
}
.gate-delay-3 {
  animation-delay: 0.36s;
}

@keyframes rise-in {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.auth-mode {
  color: #94a3b8;
  background: transparent;
  border: none;
  transition: color 0.15s ease, background 0.15s ease;
  min-height: 2.75rem;
}

.auth-mode.active {
  color: #070b10;
  background: #5ee0b0;
}

.auth-mode:focus-visible {
  outline: 1px solid rgba(46, 207, 148, 0.55);
  outline-offset: 1px;
}

.pulse-dot {
  box-shadow: 0 0 0 0 rgba(46, 207, 148, 0.55);
  animation: pulse-ring 2s ease-out infinite;
}

@keyframes pulse-ring {
  0% {
    box-shadow: 0 0 0 0 rgba(46, 207, 148, 0.55);
  }
  70% {
    box-shadow: 0 0 0 8px rgba(46, 207, 148, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(46, 207, 148, 0);
  }
}

.user-row {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  width: 100%;
  text-align: left;
  padding: 0.75rem;
  border: 1px solid transparent;
  color: inherit;
  cursor: pointer;
  min-height: 3rem;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.2s ease;
}

.user-row:hover {
  background: rgba(255, 255, 255, 0.03);
}

.user-row:active {
  background: rgba(255, 255, 255, 0.06);
}

.user-row:focus-visible {
  outline: 1px solid rgba(46, 207, 148, 0.55);
  outline-offset: 1px;
}

.user-row.active {
  background: rgba(46, 207, 148, 0.08);
  border-color: rgba(46, 207, 148, 0.25);
}

.user-row.just-joined {
  animation: user-flash-in 0.55s ease-out;
}

.user-row.just-left {
  animation: user-flash-out 0.35s ease-in forwards;
  pointer-events: none;
}

.user-avatar {
  width: 2.25rem;
  height: 2.25rem;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  font-size: 0.75rem;
  font-weight: 700;
  background: linear-gradient(145deg, #1c2836, #0c1219);
  border: 1px solid rgba(255, 255, 255, 0.08);
  color: #9af0d0;
  position: relative;
}

.user-avatar::after {
  content: "";
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 0.45rem;
  height: 0.45rem;
  border-radius: 999px;
  background: #2ecf94;
  box-shadow: 0 0 0 2px #0c1219;
}

@keyframes user-flash-in {
  from {
    opacity: 0;
    transform: translateX(-8px);
    background: rgba(46, 207, 148, 0.18);
  }
  to {
    opacity: 1;
    transform: translateX(0);
    background: transparent;
  }
}

@keyframes user-flash-out {
  to {
    opacity: 0;
    transform: translateX(8px);
  }
}

.msg {
  max-width: min(34rem, 88%);
  padding: 0.65rem 0.9rem;
  font-size: 0.925rem;
  line-height: 1.45;
  animation: msg-in 0.28s ease-out;
  overflow-wrap: anywhere;
}

.msg.mine {
  margin-left: auto;
  background: rgba(46, 207, 148, 0.16);
  border: 1px solid rgba(46, 207, 148, 0.22);
}

.msg.theirs {
  margin-right: auto;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.msg.pending {
  opacity: 0.65;
}

.msg time {
  display: block;
  margin-top: 0.35rem;
  font-size: 0.7rem;
  color: #64748b;
}

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

/* Desktop: sidebar + chat side by side */
@media (min-width: 768px) {
  .chat-pane {
    display: flex !important;
    flex-direction: column;
  }
}

/* Mobile: one panel at a time (user list OR chat) */
@media (max-width: 767px) {
  #gate {
    min-height: 100dvh;
    min-height: -webkit-fill-available;
  }

  .gate-inner {
    padding-left: max(1rem, var(--safe-left));
    padding-right: max(1rem, var(--safe-right));
    padding-bottom: max(1.5rem, var(--safe-bottom));
  }

  #join-form input,
  #join-form button,
  #message-form input,
  #message-form button,
  #user-search {
    font-size: 16px;
  }

  .sidebar-panel {
    flex: 1;
    width: 100%;
    min-height: 0;
  }

  .chat-pane {
    display: none !important;
    flex: 1;
    width: 100%;
    min-height: 0;
  }

  #app.mobile-chat .sidebar-panel {
    display: none !important;
  }

  #app.mobile-chat .chat-pane {
    display: flex !important;
    flex-direction: column;
  }

  #chat-empty {
    padding: 1.5rem;
  }

  #message-list {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  .message-form {
    position: sticky;
    bottom: 0;
    background: rgba(12, 18, 25, 0.96);
    backdrop-filter: blur(8px);
    padding-bottom: max(0.75rem, var(--safe-bottom));
  }

  .send-btn {
    min-width: 4.5rem;
  }

  .back-btn,
  #pin-btn {
    min-height: 2.75rem;
    min-width: 2.75rem;
  }

  .toast {
    left: max(1rem, var(--safe-left));
    right: max(1rem, var(--safe-right));
    transform: translateY(12px);
    width: auto;
    max-width: none;
  }

  .toast.show {
    transform: translateY(0);
  }
}

.toast {
  position: fixed;
  bottom: max(1.25rem, var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(12px);
  background: #141c27;
  border: 1px solid rgba(255, 255, 255, 0.12);
  padding: 0.65rem 1rem;
  font-size: 0.85rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 50;
  max-width: calc(100vw - 2rem);
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-width: 767px) {
  .toast.show {
    transform: translateY(0);
  }
}
