:root {
  --bg: #0b0d14;
  --panel: #12151f;
  --line: #232838;
  --text: #d7dce8;
  --dim: #6b7284;
  --flux: #7c6cff;
  --flux-soft: #7c6cff33;
  --owner: #ffb454;
  --admin: #7c6cff;
  --user: #4fd6c4;
  --danger: #ff5f7e;
  --online: #4fd6c4;
  --bubble-mine: #7c6cff;
  --bubble-mine-text: #0b0d14;
  --bubble-other: #12151f;
}

html[data-theme="light"] {
  --bg: #f4f3fb;
  --panel: #ffffff;
  --line: #e2e1f0;
  --text: #1c1d2b;
  --dim: #7a7c92;
  --flux: #5f4de0;
  --flux-soft: #5f4de022;
  --owner: #c47a12;
  --admin: #5f4de0;
  --user: #0e8f77;
  --danger: #d43c5f;
  --online: #0e8f77;
  --bubble-mine: #5f4de0;
  --bubble-mine-text: #ffffff;
  --bubble-other: #ffffff;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "JetBrains Mono", "Fira Code", ui-monospace, Menlo, Consolas, monospace;
  overflow-x: hidden;
  width: 100%;
}

body {
  display: flex;
  flex-direction: column;
}

img, svg, video, canvas { max-width: 100%; }

/* ---------- Shared page shell (dipakai semua halaman non-chat) ---------- */
.doc-page {
  max-width: 640px;
  margin: 0 auto;
  width: 100%;
  padding: clamp(20px, 6vw, 32px) clamp(16px, 4vw, 20px) 80px;
  font-size: 13.5px;
  line-height: 1.7;
  color: var(--text);
}
.doc-page.wide { max-width: 720px; }
.doc-page h1 { font-size: clamp(18px, 5vw, 22px); word-break: break-word; }

.moon-glow {
  position: fixed;
  top: -120px; right: -80px;
  width: 320px; height: 320px;
  border-radius: 50%;
  background: radial-gradient(circle, var(--flux-soft), transparent 70%);
  pointer-events: none;
}

/* ---------- Auth ---------- */
.auth-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 28px 24px;
}

.brand {
  font-size: 13px;
  letter-spacing: .12em;
  color: var(--flux);
  margin: 0 0 4px;
}

.brand::before { content: "moonflux/"; color: var(--dim); }

h1 { font-size: 20px; margin: 0 0 20px; }
h2 { font-size: 15px; margin: 0 0 14px; }

label {
  display: block;
  font-size: 12px;
  color: var(--dim);
  margin: 14px 0 6px;
}

input, textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 12px;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
}

textarea { resize: vertical; min-height: 60px; }

input:focus, textarea:focus {
  outline: none;
  border-color: var(--flux);
}

button {
  font-family: inherit;
  cursor: pointer;
}

.btn-primary {
  width: 100%;
  margin-top: 20px;
  padding: 11px;
  background: var(--flux);
  border: none;
  border-radius: 6px;
  color: var(--bubble-mine-text);
  font-weight: 700;
  font-size: 14px;
}

.btn-primary:hover { filter: brightness(1.1); }

.btn-secondary {
  width: 100%;
  margin-top: 10px;
  padding: 10px;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--dim);
  font-size: 13px;
}
.btn-secondary:hover { color: var(--text); border-color: var(--flux); }

.switch-mode {
  margin-top: 16px;
  font-size: 12px;
  color: var(--dim);
  text-align: center;
}
.switch-mode a { color: var(--flux); cursor: pointer; text-decoration: none; }

.error-msg {
  margin-top: 12px;
  font-size: 12px;
  color: var(--danger);
  min-height: 14px;
}

/* ---------- Avatar ---------- */
.avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700;
  color: var(--bubble-mine-text);
  background: var(--flux);
  flex-shrink: 0;
  overflow: hidden;
  position: relative;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.sm { width: 24px; height: 24px; font-size: 10px; }
.avatar.lg { width: 64px; height: 64px; font-size: 20px; }

.avatar .dot {
  position: absolute;
  bottom: -1px; right: -1px;
  width: 9px; height: 9px;
  border-radius: 50%;
  background: var(--dim);
  border: 2px solid var(--panel);
}
.avatar .dot.online { background: var(--online); }

/* ---------- Chat shell ---------- */
.chat-shell {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 720px;
  width: 100%;
  min-width: 0;
  margin: 0 auto;
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  gap: 8px;
  flex-wrap: wrap;
  row-gap: 6px;
}

.who {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
  min-width: 0;
  flex-shrink: 1;
}

.who-name { display: flex; flex-direction: column; line-height: 1.3; min-width: 0; }
.who-name #me-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 38vw; }

.role-tag {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: .04em;
  font-weight: 700;
  width: fit-content;
}
.role-owner { background: #3a2c1233; color: var(--owner); }
.role-admin { background: #251f4733; color: var(--admin); }
.role-user  { background: #10312d33; color: var(--user); }

.header-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.header-actions button, .icon-btn {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--dim);
  border-radius: 6px;
  padding: 6px 9px;
  font-size: 11.5px;
  white-space: nowrap;
}
.header-actions button:hover, .icon-btn:hover { color: var(--text); border-color: var(--flux); }

/* ---------- Messages ---------- */
#load-more-wrap { text-align: center; padding: 6px 0; }
#load-more-btn {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--dim);
  border-radius: 6px;
  padding: 6px 14px;
  font-size: 11.5px;
}
#load-more-btn:hover { color: var(--flux); border-color: var(--flux); }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.msg-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  max-width: 82%;
}
.msg-row.grouped { margin-top: -6px; }
.msg-row.mine { align-self: flex-end; flex-direction: row-reverse; }

.msg-row .avatar-slot { width: 28px; flex-shrink: 0; }
.msg-row.grouped .avatar-slot .avatar { visibility: hidden; }

.msg-col { display: flex; flex-direction: column; }
.msg-row.mine .msg-col { align-items: flex-end; }

.msg-meta {
  display: flex; gap: 6px; align-items: baseline;
  font-size: 10.5px; margin: 0 4px 2px;
}
.msg-row.grouped .msg-meta { display: none; }
.msg-meta .u { font-weight: 700; }
.msg-row.role-owner .u { color: var(--owner); }
.msg-row.role-admin .u { color: var(--admin); }
.msg-row.role-user .u { color: var(--user); }
.msg-meta .t { color: var(--dim); }

.bubble {
  background: var(--bubble-other);
  border: 1px solid var(--line);
  border-radius: 12px 12px 12px 3px;
  padding: 8px 12px;
  font-size: 13.5px;
  line-height: 1.5;
  color: var(--text);
  word-break: break-word;
  position: relative;
}
.msg-row.mine .bubble {
  background: var(--bubble-mine);
  color: var(--bubble-mine-text);
  border: none;
  border-radius: 12px 12px 3px 12px;
}

.bubble .edited-tag {
  font-size: 9.5px;
  opacity: .65;
  margin-left: 6px;
}

.bubble-foot {
  display: flex; align-items: center; gap: 6px;
  font-size: 9.5px; color: var(--dim); margin: 2px 4px 0;
}
.msg-row.mine .bubble-foot { justify-content: flex-end; }

.bubble-actions {
  display: none;
  gap: 4px;
}
.msg-row:hover .bubble-actions { display: flex; }
.bubble-actions button {
  background: none; border: none; color: var(--dim);
  font-size: 10px; cursor: pointer; padding: 0 2px;
}
.bubble-actions button:hover { color: var(--flux); }
.bubble-actions button.danger:hover { color: var(--danger); }

.msg-row.deleted .bubble { font-style: italic; color: var(--dim); background: transparent; border-style: dashed; }

.bubble-edit-input {
  width: 100%;
  background: transparent;
  border: none;
  color: inherit;
  font-family: inherit;
  font-size: 13.5px;
  outline: none;
  resize: none;
}

.composer {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  background: var(--panel);
}

.composer input { flex: 1; }

.composer button {
  background: var(--flux);
  border: none;
  color: var(--bubble-mine-text);
  font-weight: 700;
  padding: 0 18px;
  border-radius: 6px;
  font-size: 13px;
}

.system-note {
  text-align: center;
  color: var(--dim);
  font-size: 12px;
  padding: 8px;
}

.typing-note {
  font-size: 11px;
  color: var(--dim);
  padding: 0 16px 6px;
  min-height: 14px;
  font-style: italic;
}

/* ---------- Search ---------- */
#search-bar input {
  font-size: 13px;
}
.search-hit {
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  color: var(--text);
}
.search-hit:hover { background: var(--flux-soft); }
.search-hit .su { font-weight: 700; color: var(--flux); margin-right: 6px; }
.search-hit .sc { color: var(--dim); }

/* ---------- Pinned bar ---------- */
#pinned-bar {
  border-bottom: 1px solid var(--line);
  background: var(--flux-soft);
  padding: 8px 16px;
  font-size: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.pinned-item {
  display: flex;
  gap: 6px;
  align-items: baseline;
}
.pinned-item .pu { font-weight: 700; color: var(--flux); }
.pinned-item .pc { color: var(--text); flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pinned-item .unpin-btn { color: var(--dim); background: none; border: none; font-size: 10px; cursor: pointer; }
.pinned-item .unpin-btn:hover { color: var(--danger); }

/* ---------- Reply ---------- */
.reply-quote {
  border-left: 2px solid var(--flux);
  padding: 3px 8px;
  margin-bottom: 4px;
  font-size: 11.5px;
  color: var(--dim);
  border-radius: 3px;
  background: var(--flux-soft);
}
.reply-quote .rq-user { color: var(--flux); font-weight: 700; }

#reply-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 16px;
  border-top: 1px solid var(--line);
  background: var(--panel);
  font-size: 12px;
}
#reply-preview .rp-text { flex: 1; color: var(--dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#reply-preview .rp-text b { color: var(--flux); }
#reply-preview button { background: none; border: none; color: var(--dim); cursor: pointer; font-size: 14px; }
#reply-preview button:hover { color: var(--danger); }

/* ---------- Admin panel ---------- */
.panel-overlay {
  position: fixed;
  inset: 0;
  background: #00000090;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  padding: 16px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  width: 100%;
  max-width: 420px;
  max-height: 80vh;
  overflow-y: auto;
  padding: 20px;
}

.user-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  font-size: 12.5px;
}

.user-row .name { flex: 1; }

.user-row select, .user-row button {
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 5px;
  font-size: 11px;
  padding: 4px 6px;
}

.panel-close {
  width: 100%;
  margin-top: 14px;
  padding: 9px;
  background: transparent;
  border: 1px solid var(--line);
  color: var(--dim);
  border-radius: 6px;
}

/* ---------- Profile page ---------- */
.profile-shell {
  flex: 1;
  display: flex;
  justify-content: center;
  padding: 24px 16px;
}

.profile-card {
  width: 100%;
  max-width: 400px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 24px;
}

.profile-head {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 20px;
}

.back-link {
  display: inline-block;
  color: var(--dim);
  text-decoration: none;
  font-size: 12px;
  margin-bottom: 16px;
}
.back-link:hover { color: var(--flux); }

.theme-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 14px;
}

.theme-switch {
  display: flex;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
}
.theme-switch button {
  background: transparent;
  border: none;
  color: var(--dim);
  padding: 6px 12px;
  font-size: 11px;
}
.theme-switch button.active { background: var(--flux); color: var(--bubble-mine-text); }

.save-note {
  font-size: 11.5px;
  color: var(--online);
  margin-top: 10px;
  min-height: 14px;
}

/* ---------- Small screens (HP sempit, <420px) ---------- */
@media (max-width: 420px) {
  .auth-card, .profile-card { padding: 20px 16px; }
  .chat-header { padding: 8px 10px; }
  .composer { padding: 10px 12px; }
  #messages { padding: 12px; }
  .msg-row { max-width: 90%; }
  .header-actions button, .icon-btn { padding: 5px 7px; font-size: 11px; }
  .who-name #me-name { max-width: 30vw; }
  .stat-grid { grid-template-columns: 1fr 1fr; }
}

/* ---------- Very narrow (<340px) ---------- */
@media (max-width: 340px) {
  .header-actions { gap: 4px; }
  .who-name #me-name { max-width: 22vw; }
}
