@import url('https://fonts.googleapis.com/css2?family=Rajdhani:wght@400;600;700&family=Fira+Code:wght@400;500&display=swap');

:root {
  /* Neon Palette */
  --neon-cyan: #00f3ff;
  --neon-pink: #ff00ff;
  --deep-space: #050510;
  --grid-color: rgba(0, 243, 255, 0.15);
  --glass-panel: rgba(10, 10, 20, 0.7);
  --text-main: #e0e6ed;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Rajdhani', sans-serif; /* Tech/Sci-Fi font */
  background-color: var(--deep-space);
  color: var(--text-main);
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  position: relative;
}

/* =========================================
   🌐 THE LIVING BACKGROUND (Infinite Grid)
   ========================================= */

/* Layer 1: The Moving 3D Grid */
body::before {
  content: "";
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background-image: 
    linear-gradient(var(--grid-color) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-color) 1px, transparent 1px);
  background-size: 50px 50px;
  
  /* The 3D Perspective Trick */
  transform: perspective(500px) rotateX(60deg) scale(2);
  transform-origin: top center;
  
  /* Infinite Movement Animation */
  animation: flyGrid 2s linear infinite;
  z-index: -2;
}

/* Layer 2: The Pulsing Horizon Glow */
body::after {
  content: "";
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: radial-gradient(circle at 50% 0%, rgba(0, 243, 255, 0.2) 0%, transparent 60%);
  z-index: -1;
  animation: pulseGlow 4s ease-in-out infinite alternate;
}

@keyframes flyGrid {
  0% { background-position: 0 0; }
  100% { background-position: 0 50px; } /* Matches grid size for seamless loop */
}

@keyframes pulseGlow {
  0% { opacity: 0.5; }
  100% { opacity: 1; }
}

/* =========================================
   🖥️ GLASS HUD CONTAINER
   ========================================= */
#chatContainer {
  width: 90%;
  max-width: 1200px;
  height: 90vh;
  display: flex;
  flex-direction: column;
  
  /* Glassmorphism */
  background: var(--glass-panel);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  
  /* Glowing Border */
  border: 1px solid rgba(0, 243, 255, 0.3);
  box-shadow: 0 0 20px rgba(0, 243, 255, 0.1), inset 0 0 20px rgba(0,0,0,0.8);
  border-radius: 12px;
  position: relative;
  overflow: hidden;
}

/* Scanline Effect overlay on the whole chat */
#chatContainer::after {
  content: " ";
  display: block;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
  background: linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.25) 50%), linear-gradient(90deg, rgba(255, 0, 0, 0.06), rgba(0, 255, 0, 0.02), rgba(0, 0, 255, 0.06));
  z-index: 2;
  background-size: 100% 2px, 3px 100%;
  pointer-events: none; /* Let clicks pass through */
}

/* =========================================
   HEADER
   ========================================= */
#chatHeader {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 25px;
  background: rgba(0,0,0,0.6);
  border-bottom: 1px solid var(--neon-cyan);
  z-index: 10;
}

#chatHeader h1 {
  font-size: 1.6rem;
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 2px;
  color: var(--neon-cyan);
  text-shadow: 0 0 8px var(--neon-cyan);
}

#roomJoin { display: flex; gap: 10px; }

/* =========================================
   💬 CHAT AREA (Fixed Alignment)
   ========================================= */
#chatBox {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  
  /* ✅ FIX: Flex column allows us to use align-self */
  display: flex;
  flex-direction: column;
  gap: 15px; 
  
  scroll-behavior: smooth;
  z-index: 5; /* Above scanlines */
}

/* Base Message Style */
.message {
  padding: 12px 18px;
  border-radius: 8px;
  max-width: 75%; /* Don't span full width */
  width: fit-content;
  line-height: 1.5;
  position: relative;
  animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes popIn {
  from { opacity: 0; transform: scale(0.9); }
  to { opacity: 1; transform: scale(1); }
}

/* --- ALIGNMENT LOGIC --- */

/* 1. YOU (User) -> RIGHT Side */
.user-msg {
  align-self: flex-end; /* Pushes to right */
  background: rgba(0, 243, 255, 0.1);
  border: 1px solid var(--neon-cyan);
  border-right: 4px solid var(--neon-cyan); /* Accent on right */
  color: #fff;
  text-align: left; /* Text inside remains readable */
}
.user-msg strong { color: var(--neon-cyan); }

/* 2. AI & OTHERS -> LEFT Side */
.ai-msg, .other-msg {
  align-self: flex-start; /* Pushes to left */
}

/* AI Specific Look */
.ai-msg {
  background: rgba(255, 0, 255, 0.08);
  border: 1px solid var(--neon-pink);
  border-left: 4px solid var(--neon-pink); /* Accent on left */
}
.ai-msg strong { color: var(--neon-pink); }

/* Other Users Look */
.other-msg {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid #fff;
}

/* System Messages (Center) */
.system-msg {
  align-self: center;
  background: transparent;
  color: rgba(255,255,255,0.5);
  font-size: 0.9rem;
  font-style: italic;
  border: none;
}

.error-msg {
  align-self: center;
  color: #ff4444;
  border: 1px solid #ff4444;
  background: rgba(255, 0, 0, 0.1);
}

/* =========================================
   INPUT AREA
   ========================================= */
#chatControls {
  padding: 20px;
  background: rgba(0,0,0,0.8);
  border-top: 1px solid var(--neon-cyan);
  display: flex;
  gap: 15px;
  z-index: 10;
}

input[type="text"] {
  flex: 1;
  background: rgba(0, 243, 255, 0.05);
  border: 1px solid rgba(0, 243, 255, 0.3);
  color: var(--neon-cyan);
  padding: 12px;
  font-family: 'Fira Code', monospace;
  font-size: 1rem;
  border-radius: 4px;
  outline: none;
  transition: 0.3s;
}

input[type="text"]:focus {
  background: rgba(0, 243, 255, 0.1);
  box-shadow: 0 0 15px rgba(0, 243, 255, 0.2);
}

button {
  background: transparent;
  color: var(--neon-cyan);
  border: 1px solid var(--neon-cyan);
  padding: 0 25px;
  font-family: 'Rajdhani', sans-serif;
  font-weight: 700;
  font-size: 1.1rem;
  text-transform: uppercase;
  cursor: pointer;
  transition: 0.3s;
  border-radius: 4px;
}

button:hover {
  background: var(--neon-cyan);
  color: #000;
  box-shadow: 0 0 20px var(--neon-cyan);
}

#joinBtn {
  font-size: 0.9rem;
  padding: 8px 15px;
}

/* =========================================
   MARKDOWN BEAUTIFICATION (Tech Style)
   ========================================= */
/* Code Blocks */
#chatBox pre {
  background: #000;
  border: 1px solid #333;
  padding: 15px;
  border-radius: 4px;
  overflow-x: auto;
  font-family: 'Fira Code', monospace;
  font-size: 0.9rem;
  margin-top: 10px;
  box-shadow: inset 0 0 10px rgba(0,0,0,0.8);
}

/* Inline Code */
#chatBox code {
  font-family: 'Fira Code', monospace;
  color: var(--neon-pink);
  background: rgba(255, 0, 255, 0.1);
  padding: 2px 5px;
  border-radius: 3px;
}

#chatBox pre code {
  color: #a5b3ce; /* Standard code color inside blocks */
  background: transparent;
  padding: 0;
}

/* Lists and Headings */
#chatBox ul, #chatBox ol { padding-left: 20px; }
#chatBox h3 {
  border-bottom: 1px dashed var(--neon-pink);
  padding-bottom: 5px;
  margin-top: 10px;
  color: var(--neon-pink);
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: #000; }
::-webkit-scrollbar-thumb { background: var(--neon-cyan); }