:root {
  --bg: #0f1117;
  --bg-2: #161922;
  --bg-3: #1d2230;
  --line: #272d3d;
  --txt: #e6e8ef;
  --muted: #8b93a7;
  --brand: #6366f1;
  --brand-2: #ec4899;
  --ok: #10b981;
  --warn: #f59e0b;
  --danger: #ef4444;
  --radius: 14px;
  --shadow: 0 8px 30px rgba(0,0,0,.4);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg); color: var(--txt);
  -webkit-font-smoothing: antialiased; overflow: hidden;
}
.hidden { display: none !important; }
button { font: inherit; cursor: pointer; border: none; color: inherit; }
input, textarea, select {
  font: inherit; background: var(--bg-3); border: 1px solid var(--line);
  color: var(--txt); padding: 11px 13px; border-radius: 10px; outline: none; width: 100%;
}
input:focus, textarea:focus, select:focus { border-color: var(--brand); }
a { color: var(--brand); }

/* ---- Buttons ---- */
.btn-primary { background: linear-gradient(135deg,var(--brand),var(--brand-2)); color:#fff;
  padding: 11px 16px; border-radius: 10px; font-weight: 600; transition: filter .15s; }
.btn-primary:hover { filter: brightness(1.1); }
.btn-ghost { background: var(--bg-3); border: 1px solid var(--line); padding: 9px 13px;
  border-radius: 10px; color: var(--txt); transition: background .15s; }
.btn-ghost:hover { background: var(--line); }
.btn-sm { padding: 5px 10px; font-size: 12px; border-radius: 8px; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-ok { background: var(--ok); color: #fff; }
.btn-warn { background: var(--warn); color: #1b1b1b; }

/* ---- Auth ---- */
.auth { position: fixed; inset: 0; display: grid; place-items: center; padding: 20px;
  background: radial-gradient(1200px 600px at 50% -10%, #1c2030, var(--bg)); }
.auth-card { width: 100%; max-width: 380px; background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 20px; padding: 30px; box-shadow: var(--shadow); }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-logo { font-size: 26px; }
.brand h1 { font-size: 24px; background: linear-gradient(135deg,var(--brand),var(--brand-2));
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
.auth-tag { color: var(--muted); margin: 6px 0 20px; }
.tabs { display: flex; gap: 6px; background: var(--bg-3); padding: 4px; border-radius: 12px; margin-bottom: 18px; }
.tab { flex: 1; padding: 9px; border-radius: 9px; color: var(--muted); font-weight: 600; }
.tab.active { background: var(--brand); color: #fff; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-error { color: var(--danger); font-size: 13px; margin-top: 12px; min-height: 18px; }
.auth-hint { color: var(--muted); font-size: 12px; margin-top: 14px; text-align: center; }

/* ---- App layout ---- */
.app { display: grid; grid-template-columns: 260px 1fr; height: 100%; }
.sidebar { background: var(--bg-2); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: center; gap: 8px; padding: 16px; font-weight: 700;
  font-size: 18px; border-bottom: 1px solid var(--line); }
.nav { display: flex; flex-direction: column; gap: 2px; padding: 10px; }
.nav-btn { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 10px;
  color: var(--muted); font-weight: 600; text-align: left; }
.nav-btn:hover { background: var(--bg-3); color: var(--txt); }
.nav-btn.active { background: var(--bg-3); color: var(--txt); }
.rooms { flex: 1; overflow-y: auto; padding: 8px 10px; border-top: 1px solid var(--line); }
.room-item { display: flex; align-items: center; gap: 10px; padding: 9px 11px; border-radius: 10px;
  cursor: pointer; color: var(--muted); }
.room-item:hover { background: var(--bg-3); }
.room-item.active { background: var(--brand); color: #fff; }
.room-item .ri-icon { font-size: 18px; }
.room-item .ri-name { flex: 1; font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-item .ri-count { font-size: 11px; opacity: .7; }
.new-room-btn { margin: 8px 12px; }
.side-user { padding: 12px; border-top: 1px solid var(--line); display: flex; align-items: center; gap: 10px; }

/* ---- Avatar ---- */
.avatar { width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center;
  font-weight: 700; color: #fff; flex-shrink: 0; overflow: hidden; background-size: cover; background-position: center; }
.avatar.sm { width: 30px; height: 30px; font-size: 12px; }
.avatar.lg { width: 88px; height: 88px; font-size: 30px; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }

/* ---- Level pill / xp bar ---- */
.lvl-pill { background: var(--bg-3); border: 1px solid var(--line); border-radius: 20px;
  padding: 2px 8px; font-size: 11px; font-weight: 700; color: var(--brand); }
.role-badge { font-size: 10px; padding: 1px 6px; border-radius: 6px; font-weight: 700; text-transform: uppercase; }
.role-admin { background: rgba(239,68,68,.2); color: #fda4af; }
.role-moderator { background: rgba(245,158,11,.2); color: #fcd34d; }
.role-merchant { background: rgba(16,185,129,.2); color: #6ee7b7; }
.role-bot { background: rgba(99,102,241,.25); color: #c7d2fe; }
.msg-text.bot-msg { background: rgba(99,102,241,.08); border-left: 2px solid var(--brand); padding: 6px 10px; border-radius: 6px; }
.xp-bar { height: 6px; background: var(--bg-3); border-radius: 6px; overflow: hidden; }
.xp-fill { height: 100%; background: linear-gradient(90deg,var(--brand),var(--brand-2)); transition: width .4s; }

/* ---- Main ---- */
.main { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--bg); }
.view-head { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex;
  align-items: center; gap: 12px; background: var(--bg-2); }
.view-head h2 { font-size: 17px; }
.view-head .sub { color: var(--muted); font-size: 12px; }
.view-body { flex: 1; overflow-y: auto; padding: 18px; }
.spacer { flex: 1; }

/* ---- Chat ---- */
.messages { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain; padding: 12px 14px; display: flex; flex-direction: column; gap: 2px; }
/* Compact IRC-style line: "username: message" */
.msg { padding: 3px 8px; border-radius: 6px; }
.msg:hover { background: var(--bg-2); }
.msg:hover .msg-actions { opacity: 1; }
.msg.is-bot { background: rgba(99,102,241,.07); border-left: 2px solid var(--brand); }
.msg-line { display: flex; align-items: baseline; gap: 5px; font-size: 14px; line-height: 1.5; }
.msg-author { font-weight: 700; white-space: nowrap; flex-shrink: 0; }
.msg-colon { color: var(--muted); margin-left: -4px; flex-shrink: 0; }
.msg-text { flex: 1; min-width: 0; word-break: break-word; overflow-wrap: anywhere; white-space: pre-wrap; }
.msg-text.deleted { color: var(--muted); font-style: italic; }
.msg-text b { color: var(--txt); }
.msg-actions { display: inline-flex; gap: 2px; opacity: 0; flex-shrink: 0; transition: opacity .15s; }
.msg-actions button { background: transparent; padding: 0 5px; font-size: 13px; border-radius: 5px; line-height: 1.6; }
.msg-actions button:hover { background: var(--bg-3); }
.reactions { display: flex; gap: 5px; margin: 2px 0 0 4px; flex-wrap: wrap; }
.reaction { background: var(--bg-3); border: 1px solid var(--line); border-radius: 20px;
  padding: 1px 8px; font-size: 12px; cursor: pointer; }
.reaction.mine { border-color: var(--brand); background: rgba(99,102,241,.15); }
.composer { padding: 12px 16px; border-top: 1px solid var(--line); display: flex; gap: 8px; align-items: center; background: var(--bg-2); }
.composer input { flex: 1; }
.typing { padding: 2px 18px; height: 18px; color: var(--muted); font-size: 12px; font-style: italic; }

/* ---- Members panel ---- */
.chat-wrap { display: grid; grid-template-columns: 1fr 220px; flex: 1; min-height: 0; }
.chat-col { display: flex; flex-direction: column; min-width: 0; }
.members { border-left: 1px solid var(--line); overflow-y: auto; padding: 12px; background: var(--bg-2); }
.members h4 { font-size: 11px; text-transform: uppercase; color: var(--muted); margin-bottom: 8px; }
.member { display: flex; align-items: center; gap: 8px; padding: 6px; border-radius: 8px; }
.member:hover { background: var(--bg-3); }
.member .m-name { font-size: 13px; font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot.on { background: var(--ok); }

/* ---- Cards / grid ---- */
.grid { display: grid; grid-template-columns: repeat(auto-fill,minmax(220px,1fr)); gap: 14px; }
.card { background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.card h3 { font-size: 15px; margin-bottom: 6px; }
.muted { color: var(--muted); font-size: 13px; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fill,minmax(140px,1fr)); gap: 12px; }
.stat { background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.stat .n { font-size: 26px; font-weight: 800; }
.stat .l { color: var(--muted); font-size: 12px; }

/* ---- Tables ---- */
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; }
tr:hover td { background: var(--bg-2); }

/* ---- Leaderboard ---- */
.lb-row { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px; background: var(--bg-2); margin-bottom: 6px; border: 1px solid var(--line); }
.lb-rank { width: 28px; font-weight: 800; text-align: center; color: var(--muted); }
.lb-rank.top { color: var(--warn); }

/* ---- Profile ---- */
.profile-head { display: flex; gap: 18px; align-items: center; margin-bottom: 18px; }
.achievements { display: flex; flex-wrap: wrap; gap: 10px; }
.ach { background: var(--bg-2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px;
  display: flex; align-items: center; gap: 8px; }
.ach .ai { font-size: 22px; }
.avatar-picker { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.swatch { width: 34px; height: 34px; border-radius: 50%; cursor: pointer; border: 2px solid transparent; }
.swatch.sel { border-color: #fff; }

/* ---- Toast / modal ---- */
.toasts { position: fixed; top: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 1000; }
.toast { background: var(--bg-3); border: 1px solid var(--line); border-left: 3px solid var(--brand);
  padding: 12px 16px; border-radius: 10px; box-shadow: var(--shadow); animation: slidein .2s; max-width: 320px; font-size: 14px; }
.toast.ok { border-left-color: var(--ok); }
.toast.err { border-left-color: var(--danger); }
.toast.xp { border-left-color: var(--warn); }
@keyframes slidein { from { transform: translateX(30px); opacity: 0; } }
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; z-index: 1001; padding: 16px; }
.modal { background: var(--bg-2); border: 1px solid var(--line); border-radius: 16px; padding: 22px;
  width: 100%; max-width: 420px; box-shadow: var(--shadow); }
.modal h3 { margin-bottom: 14px; }
.modal .row { display: flex; gap: 8px; margin-top: 14px; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }

.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.flex { display: flex; align-items: center; gap: 8px; }
.flex-col { display: flex; flex-direction: column; gap: 10px; }
.pill-row { display: flex; gap: 6px; flex-wrap: wrap; }

.mobile-nav { display: none; }
.mobile-only { display: none; }
.react-pop { box-shadow: var(--shadow); }

/* ===== Tabbed workspace UI ================================================ */
.app { display: grid; grid-template-columns: 66px 1fr; height: 100%; }

/* Icon rail */
.rail { background: var(--bg-2); border-right: 1px solid var(--line); display: flex;
  flex-direction: column; align-items: center; gap: 6px; padding: 10px 0; }
.rail-logo { font-size: 24px; margin-bottom: 6px; }
.rail-btn { position: relative; width: 46px; height: 46px; border-radius: 12px; font-size: 20px;
  display: grid; place-items: center; color: var(--muted); background: transparent; }
.rail-btn:hover { background: var(--bg-3); }
.rail-btn.active { background: var(--brand); color: #fff; }
.rail-spacer { flex: 1; }
.rail-ava { display: inline-grid; place-items: center; }
.rail-btn .badge { position: absolute; top: 2px; right: 2px; background: var(--danger); color: #fff;
  font-size: 10px; font-weight: 700; min-width: 16px; height: 16px; border-radius: 8px; display: grid; place-items: center; padding: 0 3px; }
#rail-user { position: relative; }
#rail-user .st-dot { position: absolute; right: 6px; bottom: 6px; border: 2px solid var(--bg-2); }

/* Workspace = tab strip + tab area */
.workspace { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.tabbar { display: flex; gap: 4px; padding: 8px 10px 0; background: var(--bg-2);
  border-bottom: 1px solid var(--line); overflow-x: auto; }
.tab-chip { display: flex; align-items: center; gap: 7px; padding: 9px 14px; border-radius: 10px 10px 0 0;
  background: var(--bg-3); color: var(--muted); cursor: pointer; white-space: nowrap; font-size: 13px; font-weight: 600;
  border: 1px solid transparent; border-bottom: none; }
.tab-chip:hover { color: var(--txt); }
.tab-chip.active { background: var(--bg); color: var(--txt); border-color: var(--line); }
.tc-close { opacity: .6; font-size: 12px; padding: 0 2px; border-radius: 4px; }
.tc-close:hover { opacity: 1; background: var(--danger); color: #fff; }
.tabarea { flex: 1; min-height: 0; position: relative; }
.tabpane { position: absolute; inset: 0; display: flex; flex-direction: column; overflow: hidden; }

/* Status dots */
.st-dot { width: 11px; height: 11px; border-radius: 50%; background: var(--muted); display: inline-block; flex-shrink: 0; }
.st-dot.lg { width: 18px; height: 18px; }
.st-dot.online { background: var(--ok); }
.st-dot.busy { background: var(--danger); }
.st-dot.away { background: var(--warn); }
.st-dot.dnd { background: #a855f7; }
.st-dot.offline { background: #4b5563; }

/* Home tab */
.home { overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 16px; max-width: 720px; width: 100%; margin: 0 auto; }
.home-id .xp-bar { max-width: 360px; }
.alert-row { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 10px; background: var(--bg-3); }
.alert-row.unread { border-left: 3px solid var(--brand); }
.ar-icon { font-size: 18px; }
.friend-row { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; }
.friend-row:hover { background: var(--bg-3); }

/* Rooms browser */
.room-card { cursor: default; }
.cap-pill { background: var(--bg-3); border: 1px solid var(--line); border-radius: 20px; padding: 3px 10px; font-size: 12px; font-weight: 600; }
.cap-pill.full { color: var(--danger); border-color: var(--danger); }

/* Room chat view */
.room-view { display: grid; grid-template-columns: 1fr 230px; grid-template-rows: minmax(0, 1fr); flex: 1; min-height: 0; }
.room-view .chat-col { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.sys-line { text-align: center; color: var(--muted); font-size: 12px; padding: 6px; font-style: italic; }

/* ---- Responsive: rail becomes a bottom nav bar on phones ---- */
@media (max-width: 820px) {
  /* Stack: workspace fills the screen, rail pinned to the bottom. */
  .app { display: flex; flex-direction: column; }
  .workspace { order: 1; flex: 1; min-height: 0; }
  .rail {
    order: 2; flex-direction: row; width: 100%; height: 58px;
    border-right: none; border-top: 1px solid var(--line);
    justify-content: space-around; align-items: center;
    padding: 4px 6px; gap: 2px; padding-bottom: max(4px, env(safe-area-inset-bottom));
  }
  .rail-logo { display: none; }
  .rail-spacer { display: none; }
  .rail-btn { width: 44px; height: 44px; font-size: 21px; }

  .room-view { grid-template-columns: 1fr; }
  .room-view .members { display: none; }
  .messages { padding: 10px 10px; }
  .msg-actions { opacity: .5; }           /* no hover on touch — keep tappable */
  .composer { padding: 8px 10px; }
  .composer input { font-size: 16px; }    /* prevent iOS zoom-on-focus */
  .chat-wrap { grid-template-columns: 1fr; }
  .members { display: none; }
  .home { padding: 14px; }
  .tabbar { padding: 6px 8px 0; }
  .mobile-only { display: inline-flex; }
}
