:root {
  --bg: #070b14;
  --surface: #121820;
  --surface2: #161d28;
  --border: rgba(255, 255, 255, 0.08);
  --text: #eef3ff;
  --muted: #94a3b8;
  --blue: #3a8dff;
  --purple: #6366f1;
  --green: #34b97a;
  --red: #f87171;
  --grad: linear-gradient(135deg, #1a3a6e 0%, #3a8dff 45%, #6366f1 100%);
  --radius: 14px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
}

.hidden { display: none !important; }

.view { display: none; min-height: 100vh; min-height: 100dvh; }
.view.active { display: flex; }

/* Login */
#view-login {
  align-items: center;
  justify-content: center;
  padding: 24px;
  position: relative;
}

.login-bg {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse 80% 60% at 20% -10%, rgba(58, 141, 255, 0.35), transparent),
    radial-gradient(ellipse 60% 50% at 90% 20%, rgba(99, 102, 241, 0.25), transparent);
  z-index: 0;
}

.login-card {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 400px;
  padding: 36px 32px;
  border-radius: 24px;
  background: rgba(18, 24, 32, 0.92);
  border: 1px solid var(--border);
  backdrop-filter: blur(16px);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.45);
}

.login-logo {
  width: 56px;
  height: 56px;
  border-radius: 16px;
  background: var(--grad);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.75rem;
  margin: 0 auto 16px;
}

.login-card h1 { text-align: center; font-size: 1.5rem; margin-bottom: 6px; }
.login-sub { text-align: center; color: var(--muted); font-size: 0.875rem; margin-bottom: 24px; }

.tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 20px;
  background: var(--surface2);
  padding: 4px;
  border-radius: 12px;
}

.tab {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 10px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
}

.tab.active { background: var(--blue); color: #fff; }

.form-panel { display: none; flex-direction: column; gap: 14px; }
.form-panel.active { display: flex; }

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--muted);
}

label.checkbox {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
}

input[type="text"],
input[type="password"],
input[type="search"] {
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
}

input:focus { border-color: var(--blue); }

.btn-primary {
  padding: 13px;
  border: none;
  border-radius: 12px;
  background: var(--grad);
  color: #fff;
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  margin-top: 4px;
}

.btn-primary:hover { filter: brightness(1.08); }

.error-msg { color: var(--red); font-size: 0.85rem; margin-top: 12px; text-align: center; }
.back-link { display: block; text-align: center; margin-top: 20px; color: var(--muted); font-size: 0.85rem; text-decoration: none; }
.back-link:hover { color: var(--blue); }

/* Main layout */
#view-main { flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.topbar-left { display: flex; align-items: center; gap: 12px; }
.topbar-logo { font-size: 1.5rem; }
.topbar-title { font-weight: 700; font-size: 1rem; }
.topbar-sub { font-size: 0.75rem; color: var(--muted); }
.topbar-right { display: flex; align-items: center; gap: 10px; font-size: 0.8rem; color: var(--muted); }

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ffb74d;
}

.status-dot.online { background: var(--green); }

.btn-ghost {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.8rem;
}

.main-layout {
  display: flex;
  flex: 1;
  min-height: 0;
  height: calc(100vh - 56px);
  height: calc(100dvh - 56px);
}

.sidebar {
  width: 280px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--surface);
}

.sidebar-section { padding: 12px; border-bottom: 1px solid var(--border); }
.sidebar-section.grow { flex: 1; overflow: hidden; display: flex; flex-direction: column; }

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
  font-size: 0.85rem;
  margin-bottom: 10px;
}

.muted { color: var(--muted); font-size: 0.8rem; }

.icon-btn {
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 8px;
  background: rgba(58, 141, 255, 0.2);
  color: var(--blue);
  cursor: pointer;
  font-size: 1.1rem;
  line-height: 1;
}

.search-box { display: flex; gap: 6px; margin-bottom: 8px; }
.search-box input { flex: 1; padding: 8px 10px; font-size: 0.85rem; }
.search-box button {
  border: 1px solid var(--border);
  background: var(--surface2);
  border-radius: 10px;
  padding: 0 10px;
  cursor: pointer;
}

.conv-list {
  list-style: none;
  overflow-y: auto;
  max-height: 220px;
  flex: 1;
}

.sidebar-section.grow .conv-list { max-height: none; }

.conv-item {
  padding: 10px 12px;
  border-radius: 12px;
  cursor: pointer;
  margin-bottom: 4px;
  transition: background 0.15s;
}

.conv-item:hover { background: rgba(255, 255, 255, 0.05); }
.conv-item.active { background: rgba(58, 141, 255, 0.18); border: 1px solid rgba(58, 141, 255, 0.35); }

.conv-item .name { font-weight: 600; font-size: 0.9rem; }
.conv-item .preview { font-size: 0.75rem; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chat-panel { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }

.chat-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  gap: 12px;
}

.empty-icon { font-size: 3rem; opacity: 0.5; }

.chat-active { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.chat-head {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
}

.chat-head h2 { font-size: 1rem; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 0.9rem;
  line-height: 1.5;
  word-break: break-word;
}

.msg.in {
  align-self: flex-start;
  background: var(--surface2);
  border-bottom-left-radius: 4px;
}

.msg.out {
  align-self: flex-end;
  background: linear-gradient(135deg, #2563eb, #6366f1);
  border-bottom-right-radius: 4px;
}

.msg .meta { font-size: 0.7rem; color: var(--muted); margin-bottom: 4px; }
.msg.out .meta { color: rgba(255, 255, 255, 0.7); }
.msg .time-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}

.msg .time { font-size: 0.65rem; color: var(--muted); }
.msg.out .time { color: rgba(255, 255, 255, 0.55); }

.msg-recall-link {
  border: none;
  background: transparent;
  color: rgba(248, 113, 113, 0.9);
  font-size: 0.65rem;
  cursor: pointer;
  padding: 0 2px;
}

.msg.out .msg-recall-link { color: rgba(255, 200, 200, 0.95); }

.msg-recall-link:hover { text-decoration: underline; }

.msg.recalled { opacity: 0.55; font-style: italic; }

.msg.can-recall { cursor: context-menu; }

.msg-context-menu {
  position: fixed;
  z-index: 1000;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}

.msg-recall-btn {
  display: block;
  width: 100%;
  border: none;
  background: transparent;
  color: #f87171;
  padding: 10px 18px;
  font-size: 0.85rem;
  cursor: pointer;
  text-align: left;
}

.msg-recall-btn:hover { background: rgba(248, 113, 113, 0.12); }

.send-bar {
  display: flex;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--surface2);
}

.send-bar input { flex: 1; }

.hidden-input { position: absolute; width: 0; height: 0; opacity: 0; pointer-events: none; }

.btn-attach {
  width: 42px;
  height: 42px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  cursor: pointer;
  font-size: 1.1rem;
  flex-shrink: 0;
}

.btn-attach:hover { border-color: var(--blue); }

.upload-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 20px;
  background: rgba(58, 141, 255, 0.08);
  border-top: 1px solid var(--border);
}

.upload-bar-track {
  flex: 1;
  height: 6px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.08);
  overflow: hidden;
}

.upload-bar-fill {
  height: 100%;
  width: 0%;
  background: var(--blue);
  border-radius: 3px;
  transition: width 0.15s;
}

.upload-label { font-size: 0.75rem; color: var(--muted); white-space: nowrap; }

.msg.file-msg-wrap { max-width: 85%; }

.file-msg {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.file-icon { font-size: 1.5rem; line-height: 1; }

.file-name { font-weight: 600; font-size: 0.85rem; word-break: break-all; }
.file-size { font-size: 0.72rem; color: var(--muted); margin: 2px 0 6px; }

.file-dl {
  border: none;
  border-radius: 8px;
  padding: 5px 12px;
  background: rgba(58, 141, 255, 0.25);
  color: var(--blue);
  cursor: pointer;
  font-size: 0.78rem;
  font-weight: 600;
}

.msg.out .file-dl {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
}

.file-preview {
  display: block;
  max-width: 220px;
  max-height: 180px;
  margin-top: 8px;
  border-radius: 10px;
  cursor: pointer;
}

.btn-send {
  padding: 0 20px;
  border: none;
  border-radius: 12px;
  background: var(--blue);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

dialog {
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px;
  background: var(--surface2);
  color: var(--text);
  max-width: 360px;
  width: 90%;
}

dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
dialog h3 { margin-bottom: 8px; }
dialog input { width: 100%; margin: 12px 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 20px;
  border-radius: 12px;
  background: var(--surface2);
  border: 1px solid var(--border);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  z-index: 999;
  font-size: 0.875rem;
}

@media (max-width: 768px) {
  .main-layout { flex-direction: column; height: auto; }
  .sidebar { width: 100%; max-height: 42vh; border-right: none; border-bottom: 1px solid var(--border); }
  .chat-panel { min-height: 50vh; }
}
