/* ChatApp — hand-rolled styles. Dark (Discord-ish) default, light via prefers-color-scheme. */
:root {
  --rail-bg: #1e1f22;
  --sidebar-bg: #2b2d31;
  --chat-bg: #313338;
  --members-bg: #2b2d31;
  --input-bg: #383a40;
  --input-bg-2: #1e1f22;
  --hover: rgba(255, 255, 255, 0.04);
  --hover-strong: rgba(255, 255, 255, 0.08);
  --text: #dbdee1;
  --text-strong: #f2f3f5;
  --text-muted: #949ba4;
  --text-faint: #80848e;
  --accent: #5865f2;
  --accent-hover: #4752c4;
  --danger: #da373c;
  --danger-hover: #a12828;
  --online: #23a55a;
  --offline: #80848e;
  --divider: #3f4147;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  --radius: 8px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  background: var(--chat-bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button { font-family: inherit; cursor: pointer; }
textarea, input { font-family: inherit; }

#app.loading { display: grid; place-items: center; height: 100dvh; }
.boot { color: var(--text-muted); }

/* ---------- buttons / inputs ---------- */
.btn {
  border: none; border-radius: 4px; padding: 9px 16px; font-size: 14px; font-weight: 500;
  background: transparent; color: var(--text); transition: background .15s, opacity .15s;
}
.btn:hover { text-decoration: underline; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); text-decoration: none; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); text-decoration: none; }
.btn-block { width: 100%; display: block; }
.btn-small { padding: 6px 12px; font-size: 13px; background: var(--accent); color: #fff; margin-top: 8px; }
.btn-small:hover { background: var(--accent-hover); text-decoration: none; }
.btn:disabled { opacity: .6; cursor: default; }
.icon-btn {
  background: transparent; border: none; color: var(--text-muted); width: 34px; height: 34px;
  border-radius: 6px; display: inline-flex; align-items: center; justify-content: center; padding: 0;
}
.icon-btn:hover { background: var(--hover-strong); color: var(--text-strong); }
.link-btn { background: none; border: none; color: #00a8fc; font-size: 13px; padding: 4px 0; text-align: left; }
.link-btn:hover { text-decoration: underline; }

.input {
  width: 100%; background: var(--input-bg-2); border: 1px solid transparent; border-radius: 4px;
  color: var(--text); padding: 10px 11px; font-size: 15px; margin-bottom: 4px; outline: none;
}
.input:focus { border-color: var(--accent); }
.form-label { display: block; font-size: 12px; font-weight: 700; letter-spacing: .02em;
  text-transform: uppercase; color: var(--text-muted); margin: 12px 0 6px; }
.form-error { color: #fca5a5; font-size: 13px; min-height: 16px; margin: 4px 0; }
.form-status { color: var(--text-muted); font-size: 13px; min-height: 16px; margin-top: 8px; }

/* ---------- auth ---------- */
.auth-screen { height: 100dvh; display: grid; place-items: center;
  background: radial-gradient(1200px 600px at 50% -10%, #3a3c98 0%, #232428 55%); padding: 16px; }
.auth-card { background: var(--sidebar-bg); border-radius: 10px; box-shadow: var(--shadow);
  padding: 28px; width: 100%; max-width: 420px; }
.auth-title { margin: 0; text-align: center; font-size: 26px; color: var(--text-strong); }
.auth-sub { margin: 6px 0 18px; text-align: center; color: var(--text-muted); font-size: 14px; }
.auth-form { display: flex; flex-direction: column; }
.auth-form .btn-block { margin-top: 16px; }
.auth-error { text-align: center; }

/* ---------- app shell ---------- */
.app { display: grid; grid-template-columns: 72px 240px minmax(0, 1fr) auto; height: 100dvh; }
.guild-rail { background: var(--rail-bg); padding: 12px 0; overflow-y: auto; display: flex;
  flex-direction: column; align-items: center; gap: 2px; }
.guild-rail::-webkit-scrollbar { display: none; }
.sidebar { background: var(--sidebar-bg); display: flex; flex-direction: column; min-height: 0; }
.chat { background: var(--chat-bg); display: flex; flex-direction: column; min-width: 0; position: relative; }
.members { background: var(--members-bg); width: 240px; overflow-y: auto; padding: 12px 8px; }
.members.hidden { display: none; }
.nav-scrim { display: none; }

/* ---------- guild rail ---------- */
.rail-slot { position: relative; width: 100%; display: flex; justify-content: center; padding: 2px 0; }
.rail-pill { position: absolute; left: 0; top: 50%; transform: translateY(-50%); width: 4px; height: 8px;
  background: var(--text-strong); border-radius: 0 4px 4px 0; opacity: 0; transition: height .15s, opacity .15s; }
.rail-pill.unread { opacity: 1; height: 8px; }
.rail-pill.active { opacity: 1; height: 36px; }
.rail-item { position: relative; width: 48px; height: 48px; border: none; border-radius: 24px;
  background: var(--sidebar-bg); color: var(--text); display: flex; align-items: center; justify-content: center;
  transition: border-radius .15s, background .15s, color .15s; overflow: visible; padding: 0; font-size: 20px; }
.rail-item:hover { border-radius: 16px; }
.rail-item.active { border-radius: 16px; }
.rail-home:hover, .rail-home.active { background: var(--accent); color: #fff; }
.rail-add { color: var(--online); font-size: 26px; }
.rail-add:hover { background: var(--online); color: #fff; }
.guild-item.active .guild-avatar, .guild-item:hover .guild-avatar { border-radius: 16px; }
.rail-divider { width: 32px; height: 2px; background: var(--divider); border-radius: 1px; margin: 4px 0; }
.guild-avatar { border-radius: 24px; display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 600; transition: border-radius .15s; }

/* ---------- guild icons & notification mutes ---------- */
.guild-avatar-img { object-fit: cover; }
.guild-item.muted .guild-avatar { opacity: .6; }         /* dim muted guilds in the rail */
.guild-header-left { display: flex; align-items: center; gap: 8px; min-width: 0; overflow: hidden; }
.guild-header-left .guild-avatar { flex: none; }
.guild-header-left .guild-header-name { min-width: 0; }

/* ---------- avatars / presence ---------- */
.avatar { border-radius: 50%; display: inline-flex; align-items: center; justify-content: center;
  object-fit: cover; flex: none; }
.avatar-initials { color: #fff; font-weight: 600; }
.avatar-wrap { position: relative; display: inline-flex; flex: none; }
.presence-dot { position: absolute; right: -2px; bottom: -2px; width: 12px; height: 12px; border-radius: 50%;
  border: 3px solid var(--sidebar-bg); }
.presence-dot.on { background: var(--online); }
.presence-dot.off { background: var(--offline); }
.member-row .presence-dot, .members .presence-dot { border-color: var(--members-bg); }

/* ---------- sidebar ---------- */
.sidebar-body { flex: 1; display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.guild-header { height: 48px; flex: none; display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; background: transparent; border: none; border-bottom: 1px solid rgba(0,0,0,.2);
  color: var(--text-strong); font-weight: 600; font-size: 15px; box-shadow: 0 1px 0 rgba(0,0,0,.2); }
.guild-header:not(.static):hover { background: var(--hover); }
.guild-header-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chev { color: var(--text-muted); font-size: 12px; }
.channel-list { flex: 1; overflow-y: auto; padding: 8px 8px 12px; }
.channel-cat { display: flex; align-items: center; justify-content: space-between; padding: 16px 8px 4px;
  color: var(--text-muted); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .02em; }
.cat-add { background: none; border: none; color: var(--text-muted); font-size: 18px; line-height: 1; padding: 0 4px; }
.cat-add:hover { color: var(--text-strong); }
.channel-row, .dm-row { display: flex; align-items: center; gap: 6px; padding: 7px 8px; margin: 1px 0;
  border-radius: 4px; color: var(--text-muted); cursor: pointer; }
.channel-row:hover, .dm-row:hover { background: var(--hover); color: var(--text); }
.channel-row.active, .dm-row.active { background: var(--hover-strong); color: var(--text-strong); }
.channel-row.has-unread, .dm-row.has-unread { color: var(--text-strong); }
.hash { color: var(--text-faint); font-size: 18px; width: 18px; text-align: center; }
.channel-name, .dm-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dm-row { padding: 6px 8px; }
.row-x { margin-left: auto; background: none; border: none; color: var(--text-muted); opacity: 0;
  font-size: 12px; padding: 2px 4px; border-radius: 4px; }
.channel-row:hover .row-x, .dm-row:hover .row-x, .member-row:hover .member-x { opacity: 1; }
.row-x:hover { color: #fca5a5; background: var(--hover-strong); }
.empty-hint { color: var(--text-muted); font-size: 13px; padding: 12px 10px; }

.unread-badge { background: var(--danger); color: #fff; border-radius: 10px; font-size: 12px; font-weight: 700;
  min-width: 18px; height: 18px; padding: 0 5px; display: inline-flex; align-items: center; justify-content: center; }
.rail-item .unread-badge { position: absolute; right: -3px; bottom: -3px; border: 3px solid var(--rail-bg); box-sizing: content-box; }
.unread-badge.sm { min-width: 16px; height: 16px; font-size: 11px; }

.user-panel { flex: none; height: 52px; display: flex; align-items: center; gap: 8px; padding: 0 8px;
  background: var(--input-bg-2); }
.user-panel-meta { flex: 1; min-width: 0; }
.user-panel-name { color: var(--text-strong); font-size: 14px; font-weight: 600; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; }
.user-panel-handle { color: var(--text-muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- menus / popovers ---------- */
.menu { background: #111214; border-radius: 6px; padding: 6px; min-width: 190px; box-shadow: var(--shadow); }
.menu-item { display: block; width: 100%; text-align: left; background: none; border: none; color: var(--text);
  padding: 8px 10px; border-radius: 4px; font-size: 14px; }
.menu-item:hover { background: var(--accent); color: #fff; }
.menu-item.danger { color: #f77; }
.menu-item.danger:hover { background: var(--danger); color: #fff; }
.popover { position: fixed; z-index: 200; }

/* ---------- members ---------- */
.member-section { margin-bottom: 14px; }
.member-cat { color: var(--text-muted); font-size: 12px; font-weight: 700; text-transform: uppercase;
  padding: 6px 8px; letter-spacing: .02em; }
.member-row { display: flex; align-items: center; gap: 10px; padding: 5px 8px; border-radius: 4px;
  cursor: pointer; color: var(--text); }
.member-row:hover { background: var(--hover); }
.member-row.dim { color: var(--text-muted); }
.member-row.dim .avatar, .member-row.dim .avatar-wrap { opacity: .5; }
.member-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.crown { font-size: 12px; }
.member-x { opacity: 0; }
/* Touch screens have no hover — keep the row action buttons faintly visible. */
@media (hover: none) {
  .row-x, .member-x { opacity: .55; }
}

/* Members modal (guild menu → Members) */
.members-dialog .member-row { cursor: default; }
.members-dialog .kick-btn {
  margin-left: auto; background: none; border: 1px solid var(--danger); color: var(--danger);
  border-radius: 4px; padding: 3px 10px; font-size: 12px; cursor: pointer;
}
.members-dialog .kick-btn:hover { background: var(--danger); color: #fff; }

/* ---------- chat header ---------- */
.chat-header { height: 48px; flex: none; display: flex; align-items: center; gap: 8px; padding: 0 16px;
  border-bottom: 1px solid rgba(0,0,0,.2); box-shadow: 0 1px 0 rgba(0,0,0,.2); z-index: 2; }
.mobile-nav-btn { display: none; background: none; border: none; color: var(--text-muted); font-size: 20px; }
.chat-header-icon { color: var(--text-faint); font-size: 20px; }
.chat-header-title { color: var(--text-strong); font-weight: 600; font-size: 16px; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; }
.conn-pill { margin-left: auto; font-size: 12px; color: #f7c948; background: rgba(247,201,72,.12);
  padding: 3px 9px; border-radius: 10px; }

/* ---------- messages ---------- */
.messages-scroll { flex: 1; overflow-y: auto; overflow-x: hidden; display: flex; flex-direction: column; }
/* margin-top:auto keeps short conversations pinned to the bottom without the
   flex-end top-clipping bug when content overflows. */
.messages-content { margin-top: auto; padding-bottom: 20px; }
.no-channel, .channel-intro { color: var(--text-muted); padding: 24px; text-align: center; }
.load-spinner, .history-top { color: var(--text-muted); text-align: center; font-size: 13px; padding: 12px; }
.history-top.start { color: var(--text-faint); }

.day-divider { display: flex; align-items: center; margin: 12px 16px; color: var(--text-muted);
  font-size: 12px; font-weight: 600; }
.day-divider::before, .day-divider::after { content: ""; flex: 1; height: 1px; background: var(--divider); }
.day-divider span { padding: 0 8px; }

.msg-group { display: grid; grid-template-columns: 56px 1fr; padding: 4px 16px 2px; }
.msg-group:hover { background: rgba(0,0,0,.04); }
.msg-avatar { padding-top: 2px; }
.msg-main { min-width: 0; }
.msg-head { display: flex; align-items: baseline; gap: 8px; }
.msg-author { color: var(--text-strong); font-weight: 600; font-size: 15px; }
.msg-time { color: var(--text-faint); font-size: 12px; }
.msg-line { position: relative; padding: 1px 0; }
.msg-line.cont { display: grid; grid-template-columns: 56px 1fr; margin-left: -56px; }
.msg-line.cont .gutter-time { grid-column: 1; color: var(--text-faint); font-size: 10px; text-align: right;
  padding-right: 12px; opacity: 0; align-self: center; }
.msg-line.cont:hover .gutter-time { opacity: 1; }
.msg-line.cont .msg-content { grid-column: 2; }
.msg-content { color: var(--text); word-wrap: break-word; overflow-wrap: anywhere; white-space: pre-wrap; }
.msg-text { white-space: pre-wrap; }
.edited-tag { color: var(--text-faint); font-size: 10px; }
.msg-actions { position: absolute; top: -12px; right: 8px; display: none; background: #313338;
  border: 1px solid rgba(0,0,0,.3); border-radius: 6px; box-shadow: var(--shadow); }
.msg-line:hover .msg-actions { display: flex; }
.msg-act { background: none; border: none; color: var(--text-muted); padding: 6px 8px; display: inline-flex; }
.msg-act:hover { color: var(--text-strong); background: var(--hover-strong); }
.msg-act.danger:hover { color: #f77; }
.edit-area { width: 100%; background: var(--input-bg-2); border: none; border-radius: 8px; color: var(--text);
  padding: 10px; font-size: 15px; resize: none; outline: none; }
.edit-hint { font-size: 11px; color: var(--text-muted); margin-top: 4px; }

.attach-image-wrap { margin-top: 4px; }
.attach-image { max-width: min(420px, 100%); max-height: 340px; border-radius: 6px; cursor: pointer; display: block; }
.attach-file { display: inline-flex; align-items: center; gap: 10px; margin-top: 4px; background: var(--input-bg-2);
  border: 1px solid var(--divider); border-radius: 8px; padding: 10px 12px; color: var(--text); max-width: 400px; text-align: left; }
.attach-file:hover { border-color: var(--accent); }
.attach-file-icon { font-size: 22px; }
.attach-file-meta { min-width: 0; }
.attach-file-name { color: #00a8fc; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px; }
.attach-file-size { color: var(--text-muted); font-size: 12px; }
.attach-dl { margin-left: auto; color: var(--text-muted); }

/* ---------- composer ---------- */
.composer { flex: none; padding: 0 16px 22px; }
.typing-bar { height: 20px; font-size: 13px; color: var(--text); padding: 0 4px; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; }
.typing-bar.active::before { content: ""; }
.attach-preview { display: none; background: var(--input-bg); border-radius: 8px 8px 0 0; padding: 10px; }
.preview-card { display: flex; align-items: center; gap: 10px; }
.preview-thumb { width: 48px; height: 48px; object-fit: cover; border-radius: 6px; display: inline-flex;
  align-items: center; justify-content: center; background: var(--input-bg-2); font-size: 22px; }
.preview-meta { min-width: 0; flex: 1; }
.preview-name { font-size: 14px; color: var(--text-strong); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.preview-size { font-size: 12px; color: var(--text-muted); }
.preview-x { background: none; border: none; color: var(--text-muted); font-size: 14px; }
.preview-x:hover { color: #f77; }
.composer-row { display: flex; align-items: flex-end; gap: 8px; background: var(--input-bg); border-radius: 8px; padding: 6px 10px; }
.composer-attach { background: none; border: none; color: var(--text-muted); font-size: 24px; line-height: 1;
  width: 32px; height: 40px; border-radius: 50%; flex: none; }
.composer-attach:hover { color: var(--text-strong); }
.composer-input { flex: 1; background: none; border: none; color: var(--text); resize: none; outline: none;
  font-size: 15px; padding: 10px 0; max-height: 200px; min-height: 24px; }
.composer-input::placeholder { color: var(--text-faint); }
.composer-send { background: none; border: none; color: var(--accent); width: 36px; height: 40px; flex: none;
  display: inline-flex; align-items: center; justify-content: center; }
.composer-send:hover { color: var(--accent-hover); }
.chat.drag-over::after { content: "Drop to attach"; position: absolute; inset: 8px; border: 2px dashed var(--accent);
  border-radius: 10px; background: rgba(88,101,242,.12); display: grid; place-items: center; color: var(--text-strong);
  font-size: 18px; font-weight: 600; z-index: 5; pointer-events: none; }

/* ---------- modals ---------- */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: none; align-items: center;
  justify-content: center; z-index: 300; padding: 16px; }
.overlay.open { display: flex; }
.modal { background: var(--chat-bg); border-radius: 8px; width: 100%; max-width: 440px; box-shadow: var(--shadow);
  max-height: 86vh; display: flex; flex-direction: column; }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 4px; }
.modal-header h3 { margin: 0; color: var(--text-strong); font-size: 18px; }
.modal-body { padding: 8px 16px 16px; overflow-y: auto; }
.modal-text { color: var(--text); margin: 8px 0 0; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 16px; background: var(--sidebar-bg);
  border-radius: 0 0 8px 8px; }

/* ---------- user picker ---------- */
.user-picker { position: relative; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.chip { background: var(--accent); color: #fff; border-radius: 4px; padding: 3px 6px; font-size: 13px; display: inline-flex; align-items: center; gap: 4px; }
.chip-x { background: none; border: none; color: #fff; font-size: 11px; padding: 0; }
.user-results { max-height: 220px; overflow-y: auto; margin-top: 6px; }
.user-result { display: flex; align-items: center; gap: 10px; padding: 7px 8px; border-radius: 4px; cursor: pointer; }
.user-result:hover { background: var(--hover-strong); }
.user-result.muted { color: var(--text-muted); cursor: default; }
.ur-name { color: var(--text-strong); font-size: 14px; }
.ur-handle { color: var(--text-muted); font-size: 12px; }

/* ---------- settings ---------- */
.settings-row { display: flex; gap: 16px; align-items: center; }
.settings-avatar { flex: none; }
.settings-user { color: var(--text-strong); font-weight: 600; font-size: 16px; }
.settings-handle { color: var(--text-muted); font-size: 13px; margin-bottom: 6px; }
.settings-sep { border: none; border-top: 1px solid var(--divider); margin: 16px 0; }
.settings-inline { display: flex; gap: 8px; }
.settings-inline .input { margin-bottom: 0; }

/* ---------- responsive ---------- */
@media (max-width: 820px) { .members { display: none; } }
@media (max-width: 768px) {
  .app { grid-template-columns: 1fr; }
  .guild-rail, .sidebar { position: fixed; top: 0; bottom: 0; z-index: 60; transition: transform .2s ease; }
  .guild-rail { left: 0; width: 72px; transform: translateX(-100%); }
  .sidebar { left: 72px; width: min(240px, calc(100vw - 72px)); transform: translateX(calc(-100% - 72px)); }
  body.show-nav .guild-rail { transform: translateX(0); }
  body.show-nav .sidebar { transform: translateX(0); }
  body.show-nav .nav-scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 50; }
  .mobile-nav-btn { display: inline-flex; }
}

/* ---------- light theme (bonus) ---------- */
@media (prefers-color-scheme: light) {
  :root {
    --rail-bg: #e3e5e8; --sidebar-bg: #f2f3f5; --chat-bg: #ffffff; --members-bg: #f2f3f5;
    --input-bg: #ebedef; --input-bg-2: #ffffff; --hover: rgba(0,0,0,0.04); --hover-strong: rgba(0,0,0,0.08);
    --text: #2e3338; --text-strong: #060607; --text-muted: #5c5e66; --text-faint: #80848e;
    --divider: #e0e1e5;
  }
  .rail-item { background: #fff; }
  .menu { background: #fff; color: var(--text); }
  .attach-file-name, .link-btn { color: #0068d8; }
}
