:root {
  --background: #1e1e1e;
  --sidebar-bg: #181818;
  --chat-bg: #222222;
  --input-bg: #2b2b2b;
  --hover-bg: #333333;
  --active-bg: #3a3a3a;

  --text-primary: #e0e0e0;
  --text-secondary: #aaaaaa;
  --text-link: #4da6ff;
  --text-accent: #ffffff;
  
  --border-color: #383838;
  --accent-color: #5865f2;
  --success-color: #43b581;
  --error-color: #f04747;
  
  --font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --border-radius: 6px;
  --transition: all 0.2s ease;
}

/* --- Base & Reset --- */
*, *::before, *::after { box-sizing: border-box; }
body {
  margin: 0;
  font-family: var(--font-family);
  background: var(--background);
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.5;
  overflow: hidden;
}
input, button { font-family: inherit; font-size: inherit; }
button { cursor: pointer; border: none; background: none; }
.hidden { display: none !important; }

/* --- Modal --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-box {
  background: var(--chat-bg);
  padding: 32px;
  border-radius: var(--border-radius);
  text-align: center;
  width: 90%;
  max-width: 400px;
  border: 1px solid var(--border-color);
}
.modal-logo { font-size: 48px; margin-bottom: 16px; }
.modal-box h2 { margin: 0 0 8px; color: var(--text-accent); }
.modal-box p { margin: 0 0 24px; color: var(--text-secondary); }
#username-form input {
  width: 100%;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 10px;
  border-radius: var(--border-radius);
  margin-bottom: 12px;
  text-align: center;
}
#username-form button {
  width: 100%;
  background: var(--accent-color);
  color: var(--text-accent);
  padding: 10px;
  border-radius: var(--border-radius);
  font-weight: 600;
  transition: var(--transition);
}
#username-form button:hover { opacity: 0.9; }
#username-error { color: var(--error-color); font-size: 13px; height: 16px; display: block; }

/* --- Main App Layout --- */
.app {
  display: flex;
  height: 100vh;
}

/* --- Sidebar --- */
.sidebar {
  width: 240px;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}
.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border-color);
  font-weight: 600;
  color: var(--text-accent);
}
.channel-list { padding: 8px; }
.channel-category {
  padding: 4px 8px;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-bottom: 4px;
}
.channel-item {
  padding: 8px;
  border-radius: var(--border-radius);
  color: var(--text-secondary);
  cursor: pointer;
  transition: var(--transition);
}
.channel-item:hover { background: var(--hover-bg); color: var(--text-primary); }
.channel-item.active { background: var(--active-bg); color: var(--text-accent); font-weight: 500; }
.sidebar-footer {
  margin-top: auto;
  padding: 16px;
  background: #111;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.user-info { display: flex; align-items: center; gap: 8px; overflow: hidden; }
.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent-color);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  color: #fff;
}
#sidebar-username {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-accent);
  font-weight: 500;
}
#logout-btn { color: var(--text-secondary); padding: 4px; border-radius: var(--border-radius); }
#logout-btn:hover { background: var(--hover-bg); color: var(--text-primary); }

/* --- Chat Area --- */
.chat-area {
  flex-grow: 1;
  background: var(--chat-bg);
  display: flex;
  flex-direction: column;
}
.chat-header {
  padding: 16px;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}
.chat-title { font-weight: 600; color: var(--text-accent); }
.chat-desc { font-size: 13px; color: var(--text-secondary); margin-left: 8px; }

/* --- Messages --- */
.messages {
  flex-grow: 1;
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column-reverse;
}
.message-group {
  display: flex;
  margin-bottom: 16px;
}
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent-color);
  margin-right: 16px;
  flex-shrink: 0;
  font-weight: 700;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}
.message-content .username {
  font-weight: 600;
  color: var(--text-accent);
  margin-right: 8px;
}
.message-content .timestamp {
  font-size: 12px;
  color: var(--text-secondary);
}
.message-body {
  white-space: pre-wrap;
  word-break: break-word;
}
.message-image {
  max-width: 400px;
  max-height: 300px;
  border-radius: var(--border-radius);
  margin-top: 8px;
  cursor: pointer;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
}
.system-msg {
  text-align: center;
  color: var(--text-secondary);
  font-style: italic;
  padding: 16px;
}

/* --- Input Area --- */
.input-area {
  padding: 16px;
  background: var(--chat-bg);
  flex-shrink: 0;
}
.chat-form {
  display: flex;
  align-items: center;
  background: var(--input-bg);
  border-radius: var(--border-radius);
  padding: 4px;
}
.attach-btn, .send-btn {
  padding: 8px;
  color: var(--text-secondary);
  transition: var(--transition);
}
.attach-btn:hover, .send-btn:hover { color: var(--text-primary); }
#message-input {
  flex-grow: 1;
  background: none;
  border: none;
  color: var(--text-primary);
  padding: 8px;
  outline: none;
}
#message-input::placeholder { color: var(--text-secondary); }

/* --- Image Preview & Upload --- */
.image-preview-container {
  background: var(--sidebar-bg);
  padding: 8px;
  border-radius: var(--border-radius) var(--border-radius) 0 0;
  margin-bottom: -16px;
}
.image-preview-inner {
  position: relative;
  width: fit-content;
  max-width: 120px;
}
#image-preview {
  max-width: 100%;
  max-height: 120px;
  border-radius: var(--border-radius);
}
#remove-image-btn {
  position: absolute;
  top: -8px;
  right: -8px;
  background: var(--background);
  color: var(--text-primary);
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  line-height: 1;
}

/* --- Lightbox --- */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}
.lightbox-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.8);
  cursor: pointer;
}
.lightbox img {
  position: relative;
  max-width: 90vw;
  max-height: 90vh;
  object-fit: contain;
  border-radius: var(--border-radius);
}
