:root,
[data-theme='dark'] {
  --bg: #0f1115;
  --panel: #181b22;
  --panel-2: #20242d;
  --border: #2a2f3a;
  --text: #e6e8ee;
  --muted: #9aa3b2;
  --accent: #5b8cff;
  --accent-hover: #4071ee;
  --error: #ff6b6b;
  --me: #5b8cff;
  --me-text: #ffffff;
  --other: #2a2f3a;
  --other-text: #e6e8ee;
  --hover-bg: #262c38;
  --secondary-hover: #2a3140;
  --danger-hover: #e85555;
  --chat-text-scale: 1;
}

[data-theme='light'] {
  --bg: #f7f8fb;
  --panel: #ffffff;
  --panel-2: #eef0f5;
  --border: #d6dae3;
  --text: #1a1d24;
  --muted: #6b7280;
  --accent: #4071ee;
  --accent-hover: #2f5fe0;
  --error: #d63030;
  --me: #4071ee;
  --me-text: #ffffff;
  --other: #e8ebf2;
  --other-text: #1a1d24;
  --hover-bg: #dde0e7;
  --secondary-hover: #d4d8e0;
  --danger-hover: #b82828;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-size: 15px;
  transition: background-color 0.2s ease, color 0.2s ease;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

#topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

#topbar h1 {
  margin: 0;
  font-size: 18px;
}

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

.topbar-controls {
  display: flex;
  gap: 4px;
  align-items: center;
}

.icon-btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 10px;
  font-size: 13px;
  border-radius: 6px;
  min-width: 38px;
  cursor: pointer;
  line-height: 1;
}

.icon-btn:hover:not(:disabled) {
  background: var(--hover-bg);
}

.icon-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#me-label {
  color: var(--muted);
  font-size: 14px;
}

main {
  flex: 1;
  display: flex;
  min-height: 0;
}

.view {
  flex: 1;
  display: flex;
  min-height: 0;
}

#auth-view {
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 420px;
}

.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  border-bottom: 1px solid var(--border);
}

.tab {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 10px;
  cursor: pointer;
  font-size: 14px;
  border-bottom: 2px solid transparent;
}

.tab.active {
  color: var(--text);
  border-bottom-color: var(--accent);
}

.auth-form {
  display: none;
  flex-direction: column;
  gap: 14px;
}

.auth-form.active {
  display: flex;
}

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

input[type='text'],
input[type='email'],
input[type='password'] {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  width: 100%;
}

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

button {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
}

button:hover {
  background: var(--accent-hover);
}

button[type='button'].secondary {
  background: var(--panel-2);
}

.form-error {
  color: var(--error);
  font-size: 13px;
  margin: 0;
  min-height: 1.2em;
}

#chat-view {
  display: flex;
}

#sidebar {
  width: 280px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 20px;
  overflow-y: auto;
}

.sidebar-section h2,
.sidebar-section h3 {
  margin: 0 0 10px 0;
  font-size: 14px;
  text-transform: uppercase;
  color: var(--muted);
  letter-spacing: 0.5px;
}

#room-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

#room-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  background: var(--panel-2);
}

#room-list li:hover {
  background: var(--hover-bg);
}

#room-list li.active {
  background: var(--accent);
  color: #fff;
}

#room-list li.active .room-meta {
  color: rgba(255, 255, 255, 0.85);
}

#room-list li.active .room-delete {
  color: rgba(255, 255, 255, 0.7);
}

.room-name {
  font-weight: 500;
}

.room-meta {
  font-size: 12px;
  color: var(--muted);
}

.room-delete {
  background: transparent;
  color: var(--muted);
  padding: 2px 6px;
  font-size: 12px;
}

.room-delete:hover {
  color: var(--error);
  background: transparent;
}

#create-room-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#chat-header {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--panel);
}

#chat-header h2 {
  margin: 0;
  font-size: 16px;
}

#chat-header p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

#online-users {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-width: 50%;
  justify-content: flex-end;
}

.online-pill {
  background: var(--panel-2);
  color: var(--muted);
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
}

#messages {
  list-style: none;
  margin: 0;
  padding: 16px 20px;
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.msg {
  max-width: 70%;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--other);
  color: var(--other-text);
  align-self: flex-start;
  word-wrap: break-word;
  white-space: pre-wrap;
  font-size: calc(15px * var(--chat-text-scale));
}

.msg.me {
  background: var(--me);
  color: var(--me-text);
  align-self: flex-end;
}

.msg .author {
  font-size: calc(12px * var(--chat-text-scale));
  color: var(--muted);
  margin-bottom: 2px;
  display: block;
}

.msg.me .author {
  color: rgba(255, 255, 255, 0.85);
}

.msg .time {
  font-size: 10px;
  opacity: 0.6;
  margin-left: 8px;
}

.msg-link {
  color: var(--accent);
  text-decoration: underline;
  word-break: break-all;
}

.msg-link:hover {
  opacity: 0.85;
}

.msg.me .msg-link {
  color: #fff;
  text-decoration: underline;
}

.system-msg {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  font-style: italic;
  padding: 4px 0;
}

.typing-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 20px 6px;
  color: var(--muted);
  font-size: 12px;
  font-style: italic;
  min-height: 22px;
}

.typing-dots {
  display: inline-flex;
  gap: 3px;
  align-items: flex-end;
  height: 10px;
}

.typing-dots > span {
  width: 5px;
  height: 5px;
  background: var(--muted);
  border-radius: 50%;
  display: inline-block;
  animation: typing-bounce 1.2s infinite ease-in-out both;
}

.typing-dots > span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-dots > span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes typing-bounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-5px);
    opacity: 1;
  }
}

#message-form {
  display: flex;
  gap: 8px;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}

#message-form input {
  flex: 1;
  font-size: calc(14px * var(--chat-text-scale));
}

.modal-root {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
  cursor: pointer;
}

.modal {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
  animation: modal-pop 120ms ease-out;
}

@keyframes modal-pop {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.modal-title {
  margin: 0 0 8px 0;
  font-size: 16px;
  color: var(--text);
}

.modal-message {
  margin: 0 0 20px 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.modal-btn {
  padding: 8px 16px;
  border-radius: 8px;
  border: none;
  font-size: 14px;
  cursor: pointer;
}

.modal-btn.primary {
  background: var(--accent);
  color: #fff;
}

.modal-btn.primary:hover {
  background: var(--accent-hover);
}

.modal-btn.secondary {
  background: var(--panel-2);
  color: var(--text);
}

.modal-btn.secondary:hover {
  background: var(--secondary-hover);
}

.modal-btn.danger {
  background: var(--error);
  color: #fff;
}

.modal-btn.danger:hover {
  background: var(--danger-hover);
}

@media (max-width: 700px) {
  #sidebar {
    width: 100%;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  #chat-view {
    flex-direction: column;
  }
}
