:root {
  --bg: #0d0d0d;
  --bg-soft: #141414;
  --sidebar: #161616;
  --panel: #1a1a1a;
  --input: #1f1f1f;
  --border: #2a2a2a;
  --border-soft: #222;
  --text: #e8e8e8;
  --text-muted: #9a9a9a;
  --text-dim: #6a6a6a;
  --accent: #6366f1;
  --accent-hover: #7c7ff5;
  --danger: #e0556b;
  --ok: #4caf7d;
  --radius-sm: 3px;
  --radius: 4px;
  --radius-lg: 5px;
  font-size: 15px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: clamp(13px, 0.95vw, 15px);
  line-height: 1.55;
  overflow: hidden;
}

#app { height: 100vh; display: flex; flex-direction: column; }
#toast {
  position: fixed; top: 18px; left: 50%; transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--border); color: var(--text);
  padding: 9px 18px; border-radius: var(--radius); font-size: 0.9em;
  z-index: 9999; opacity: 0; pointer-events: none; transition: opacity .2s, top .2s;
  box-shadow: 0 6px 24px rgba(0,0,0,.5); max-width: 90vw;
}
#toast.show { opacity: 1; top: 24px; }
#toast.error { border-color: var(--danger); color: #ffb3bd; }
#toast.ok { border-color: var(--ok); color: #a8e6c4; }

/* ---------- Auth ---------- */
.auth-wrap {
  flex: 1; display: flex; align-items: center; justify-content: center;
  padding: 20px; background: radial-gradient(1200px 600px at 50% -10%, #1a1b2e 0%, var(--bg) 60%);
}
.auth-card {
  width: 100%; max-width: 400px; background: var(--panel);
  border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 30px 28px; box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.auth-logo {
  width: 44px; height: 44px; border-radius: var(--radius);
  background: linear-gradient(135deg, var(--accent), #a855f7);
  display: flex; align-items: center; justify-content: center;
  font-size: 22px; font-weight: 700; color: #fff; margin: 0 auto 14px;
  letter-spacing: -1px;
}
.auth-title { text-align: center; font-size: 1.35em; font-weight: 600; margin-bottom: 4px; }
.auth-sub { text-align: center; color: var(--text-muted); font-size: 0.88em; margin-bottom: 20px; }

.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tab {
  flex: 1; padding: 8px 0; text-align: center; cursor: pointer;
  color: var(--text-muted); font-size: 0.92em; border-bottom: 2px solid transparent;
  user-select: none; transition: color .15s, border-color .15s;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 500; }

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 0.82em; color: var(--text-muted); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 9px 11px; background: var(--input);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  color: var(--text); font-size: 0.95em; font-family: inherit; outline: none;
  transition: border-color .15s;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); }
.field input::placeholder { color: var(--text-dim); }
.field .row { display: flex; gap: 8px; }
.field .row input { flex: 1; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px; border: 1px solid var(--accent); background: var(--accent);
  color: #fff; border-radius: var(--radius-sm); font-size: 0.93em; cursor: pointer;
  font-family: inherit; transition: background .15s, opacity .15s; width: 100%;
}
.btn:hover { background: var(--accent-hover); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.ghost:hover { background: var(--input); border-color: var(--text-dim); }
.btn.sm { padding: 5px 11px; font-size: 0.82em; width: auto; }
.btn.danger { background: var(--danger); border-color: var(--danger); }

.link { color: var(--accent); cursor: pointer; text-decoration: none; font-size: 0.88em; }
.link:hover { text-decoration: underline; }
.muted { color: var(--text-muted); font-size: 0.85em; }
.center { text-align: center; }
.mt12 { margin-top: 12px; }
.mt8 { margin-top: 8px; }

.turnstile-wrap { display: flex; justify-content: center; margin: 8px 0 12px; min-height: 0; }
.turnstile-wrap:empty { display: none; }
.code-hint { font-size: 0.78em; color: var(--text-dim); margin-top: 3px; }

.checkbox-row { display: flex; align-items: flex-start; gap: 7px; font-size: 0.85em; color: var(--text-muted); margin: 6px 0 12px; }
.checkbox-row input { margin-top: 3px; accent-color: var(--accent); }

/* ---------- App shell ---------- */
.shell { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 260px; background: var(--sidebar); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0; flex-shrink: 0;
  transition: transform .2s;
}
.sidebar-head {
  padding: 12px 14px; border-bottom: 1px solid var(--border-soft);
  display: flex; align-items: center; gap: 10px;
}
.sidebar-logo {
  width: 30px; height: 30px; border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--accent), #a855f7);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: #fff; font-size: 16px;
}
.sidebar-name { font-weight: 600; font-size: 0.98em; }
.new-btn {
  margin: 12px; padding: 9px; border: 1px solid var(--border); background: var(--panel);
  color: var(--text); border-radius: var(--radius-sm); cursor: pointer; font-size: 0.9em;
  display: flex; align-items: center; justify-content: center; gap: 6px; transition: background .15s;
}
.new-btn:hover { background: var(--input); }
.nav { flex: 1; overflow-y: auto; padding: 4px 8px; }
.nav-section-label { font-size: 0.72em; color: var(--text-dim); text-transform: uppercase; letter-spacing: .5px; padding: 10px 10px 4px; }
.nav-item {
  display: flex; align-items: center; gap: 9px; padding: 8px 10px; border-radius: var(--radius-sm);
  cursor: pointer; color: var(--text-muted); font-size: 0.9em; transition: background .12s, color .12s;
  overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
.nav-item:hover { background: var(--input); color: var(--text); }
.nav-item.active { background: var(--input); color: var(--text); }
.nav-item .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); flex-shrink: 0; }
.nav-item .badge { margin-left: auto; background: var(--danger); color: #fff; font-size: 0.7em; padding: 1px 6px; border-radius: 10px; }
.nav-item .del { margin-left: auto; color: var(--text-dim); opacity: 0; font-size: 1.1em; line-height: 1; }
.nav-item:hover .del { opacity: 1; }
.nav-item .del:hover { color: var(--danger); }
.sidebar-foot { border-top: 1px solid var(--border-soft); padding: 8px; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg-soft); }
.topbar {
  height: 50px; border-bottom: 1px solid var(--border); display: flex; align-items: center;
  padding: 0 16px; gap: 12px; background: var(--bg-soft);
}
.topbar .menu-btn { display: none; background: none; border: none; color: var(--text); font-size: 1.4em; cursor: pointer; line-height: 1; }
.topbar-title { font-size: 0.98em; font-weight: 500; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.content { flex: 1; overflow-y: auto; display: flex; flex-direction: column; }

/* welcome / empty chat */
.welcome {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 24px; text-align: center;
}
.welcome h1 { font-size: clamp(1.5em, 3vw, 2.2em); font-weight: 600; margin-bottom: 6px; }
.welcome p { color: var(--text-muted); font-size: 1em; margin-bottom: 28px; }

/* messages */
.messages { flex: 1; overflow-y: auto; padding: 20px 0; }
.msg-row { max-width: 820px; margin: 0 auto; padding: 14px 20px; display: flex; gap: 12px; }
.msg-row.user { flex-direction: row-reverse; }
.msg-avatar {
  width: 30px; height: 30px; border-radius: var(--radius-sm); flex-shrink: 0;
  background: var(--input); display: flex; align-items: center; justify-content: center;
  font-size: 0.82em; color: var(--text-muted); font-weight: 600;
}
.msg-row.user .msg-avatar { background: var(--accent); color: #fff; }
.msg-bubble {
  background: var(--panel); border: 1px solid var(--border-soft); border-radius: var(--radius);
  padding: 10px 14px; max-width: 78%; word-break: break-word;
}
.msg-row.user .msg-bubble { background: var(--accent); border-color: var(--accent); color: #fff; }
.msg-row.user .msg-bubble a { color: #fff; }
.msg-bubble .markdown-body { color: inherit; }
.msg-bubble pre { background: #0a0a0a; border: 1px solid var(--border); }
.cursor { display: inline-block; width: 7px; height: 1em; background: var(--text); vertical-align: -2px; margin-left: 2px; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* composer */
.composer { padding: 12px 16px 16px; border-top: 1px solid var(--border); background: var(--bg-soft); }
.composer-inner { max-width: 820px; margin: 0 auto; }
.composer textarea {
  width: 100%; min-height: 44px; max-height: 200px; resize: none; padding: 10px 12px;
  background: var(--input); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); font-family: inherit; font-size: 0.95em; outline: none; line-height: 1.5;
}
.composer textarea:focus { border-color: var(--accent); }
.composer-actions { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; }
.composer .hint { font-size: 0.76em; color: var(--text-dim); }

/* panels (friends/account/admin/setup) */
.panel-page { flex: 1; overflow-y: auto; padding: 24px; }
.panel-container { max-width: 760px; margin: 0 auto; }
.panel-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 20px; margin-bottom: 16px;
}
.panel-card h2 { font-size: 1.15em; margin-bottom: 12px; font-weight: 600; }
.panel-card h3 { font-size: 1em; margin: 14px 0 8px; color: var(--text); }
.kv { display: flex; justify-content: space-between; padding: 7px 0; border-bottom: 1px dashed var(--border-soft); font-size: 0.9em; }
.kv:last-child { border-bottom: none; }
.kv .k { color: var(--text-muted); }
.kv .v { color: var(--text); word-break: break-all; text-align: right; margin-left: 12px; }
.tag { display: inline-block; font-size: 0.72em; padding: 2px 7px; border-radius: 2px; margin-left: 6px; }
.tag.admin { background: rgba(99,102,241,.18); color: var(--accent); }
.tag.banned { background: rgba(224,85,107,.18); color: var(--danger); }
.tag.ok { background: rgba(76,175,125,.18); color: var(--ok); }

.list-row {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  border: 1px solid var(--border-soft); border-radius: var(--radius-sm); margin-bottom: 8px;
  background: var(--bg-soft);
}
.list-row .info { flex: 1; min-width: 0; }
.list-row .info .name { font-weight: 500; font-size: 0.95em; }
.list-row .info .meta { font-size: 0.78em; color: var(--text-muted); margin-top: 2px; word-break: break-all; }
.list-row .actions { display: flex; gap: 6px; flex-shrink: 0; flex-wrap: wrap; }

.danger-box { background: rgba(224,85,107,.08); border: 1px solid rgba(224,85,107,.3); border-radius: var(--radius-sm); padding: 12px; font-size: 0.85em; color: #ffb3bd; margin-top: 12px; }

/* modal */
.modal-mask {
  position: fixed; inset: 0; background: rgba(0,0,0,.6); backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center; z-index: 800; padding: 16px;
}
.modal {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius-lg);
  max-width: 560px; width: 100%; max-height: 85vh; display: flex; flex-direction: column;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h3 { font-size: 1.05em; }
.modal-body { padding: 18px; overflow-y: auto; font-size: 0.9em; line-height: 1.7; color: var(--text); }
.modal-body h4 { margin: 12px 0 6px; color: var(--text); }
.modal-body p { margin-bottom: 8px; color: var(--text-muted); }
.modal-close { background: none; border: none; color: var(--text-muted); font-size: 1.4em; cursor: pointer; line-height: 1; }
.modal-close:hover { color: var(--text); }

.spinner { display: inline-block; width: 12px; height: 12px; border: 2px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: spin .7s linear infinite; vertical-align: -2px; margin-right: 5px; }
.btn.ghost .spinner { border-color: rgba(255,255,255,.2); border-top-color: var(--text); }
@keyframes spin { to { transform: rotate(360deg); } }

.back { display: inline-flex; align-items: center; gap: 5px; color: var(--text-muted); cursor: pointer; font-size: 0.88em; }
.back:hover { color: var(--text); }

/* responsive */
.sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 100; }
@media (max-width: 760px) {
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 200;
    transform: translateX(-100%); width: 80%; max-width: 300px;
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-overlay.show { display: block; }
  .topbar .menu-btn { display: block; }
  .msg-row { padding: 10px 12px; }
  .msg-bubble { max-width: 85%; }
  .panel-page { padding: 14px; }
  .auth-card { padding: 22px 18px; }
}
@media (max-width: 400px) {
  .auth-card { max-width: 100%; }
}
@media (min-width: 1400px) {
  :root { font-size: 16px; }
}
