:root {
  --brand: #7C6FE8;
  --brand-600: #6a5cd6;
  --brand-700: #5a4dc0;
  --sidebar: #2b2140;
  --sidebar-2: #34294f;
  --sidebar-active: #7C6FE8;
  --sidebar-text: #cdc6e6;
  --sidebar-text-dim: #9a90bd;
  --ink: #1b1027;
  --ink-soft: #5b5570;
  --line: #e7e3f0;
  --bg: #ffffff;
  --bg-soft: #f7f6fb;
  --ok: #2EB67D;
  --warn: #ECB22E;
  --danger: #E01E5A;
  --radius: 10px;
  --shadow: 0 8px 30px rgba(43, 33, 64, 0.18);
  font-synthesis: none;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg-soft);
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; cursor: pointer; }
input { font-family: inherit; }

/* The [hidden] attribute must always win over component display rules
   (.login-view/.app-view/.thread-panel set display, which would otherwise
   override the UA [hidden]{display:none} and leave hidden views on the page). */
[hidden] { display: none !important; }

/* ---------- Brand lockup ---------- */
.brand-logo { color: var(--brand); display: inline-flex; }
.brand-logo svg { width: 44px; height: 44px; }
.brand-logo.sm svg { width: 30px; height: 30px; }
.brand-lockup { display: flex; align-items: center; gap: 12px; }
.brand-text .brand-name { font-weight: 800; font-size: 22px; letter-spacing: -0.02em; line-height: 1; }
.brand-text .brand-sub { font-weight: 600; font-size: 15px; color: var(--brand); letter-spacing: 0.01em; }

/* ---------- Login ---------- */
.login-view {
  height: 100%; display: grid; place-items: center;
  background: linear-gradient(135deg, #8b7ff0 0%, #6a5cd6 55%, #4a3fae 100%);
  padding: 20px;
}
.login-card {
  width: 100%; max-width: 380px; background: var(--bg); border-radius: 16px;
  padding: 34px 30px; box-shadow: var(--shadow);
}
.login-tag { color: var(--ink-soft); margin: 18px 0 22px; font-size: 14px; }
.login-card label { display: block; font-size: 13px; font-weight: 600; color: var(--ink-soft); margin-bottom: 14px; }
.login-card input {
  width: 100%; margin-top: 6px; padding: 11px 12px; border: 1px solid var(--line);
  border-radius: var(--radius); font-size: 15px; outline: none; transition: border-color .15s;
}
.login-card input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(124,111,232,.15); }
.btn-primary {
  width: 100%; padding: 12px; border: none; border-radius: var(--radius); background: var(--brand);
  color: #fff; font-weight: 700; font-size: 15px; margin-top: 4px; transition: background .15s;
}
.btn-primary:hover { background: var(--brand-600); }
.login-error { color: var(--danger); font-size: 13px; min-height: 18px; margin: 10px 0 0; }
.login-hint { margin-top: 18px; font-size: 12px; color: #9a90bd; text-align: center; line-height: 1.5; }

/* ---------- App shell ---------- */
.app-view { display: grid; grid-template-columns: 260px 1fr; height: 100%; }
.app-view:has(.thread-panel:not([hidden])) { grid-template-columns: 260px 1fr 340px; }

/* ---------- Sidebar ---------- */
.sidebar { background: var(--sidebar); color: var(--sidebar-text); display: flex; flex-direction: column; min-height: 0; }
.workspace-header {
  display: flex; align-items: center; gap: 10px; padding: 16px 16px 14px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}
.workspace-header .brand-logo { color: #fff; }
.ws-name { font-weight: 800; font-size: 16px; color: #fff; line-height: 1.1; }
.ws-me { font-size: 12px; color: var(--sidebar-text-dim); margin-top: 2px; }

.channel-nav { flex: 1; overflow-y: auto; padding: 12px 8px; min-height: 0; }
.nav-section { margin-bottom: 18px; }
.nav-section-title {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 10px; font-size: 12px; font-weight: 700; letter-spacing: .04em;
  text-transform: uppercase; color: var(--sidebar-text-dim);
}
.nav-add {
  background: transparent; border: none; color: var(--sidebar-text-dim); font-size: 18px;
  line-height: 1; width: 22px; height: 22px; border-radius: 5px;
}
.nav-add:hover { background: rgba(255,255,255,.1); color: #fff; }
.channel-nav ul { list-style: none; margin: 0; padding: 0; }
.nav-item {
  width: 100%; text-align: left; background: transparent; border: none; color: var(--sidebar-text);
  padding: 6px 10px; border-radius: 6px; font-size: 14.5px; display: flex; align-items: center;
  gap: 8px; margin: 1px 0;
}
.nav-item:hover { background: rgba(255,255,255,.07); }
.nav-item.active { background: var(--sidebar-active); color: #fff; font-weight: 600; }
.nav-item .hash { color: var(--sidebar-text-dim); }
.nav-item.active .hash { color: #fff; }
.nav-item .unread {
  margin-left: auto; background: var(--danger); color: #fff; font-size: 11px; font-weight: 700;
  border-radius: 10px; padding: 1px 7px; min-width: 18px; text-align: center;
}
.nav-item .presence-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.presence-dot.online { background: var(--ok); }
.presence-dot.away { background: var(--warn); }
.presence-dot.offline { background: transparent; border: 1.5px solid var(--sidebar-text-dim); }
.admin-link { color: var(--sidebar-text); }

.sidebar-footer { padding: 12px; border-top: 1px solid rgba(255,255,255,.08); display: flex; flex-direction: column; gap: 8px; }
.download-link { color: var(--sidebar-text); text-decoration: none; font-size: 13px; padding: 6px 8px; border-radius: 6px; }
.download-link:hover { background: rgba(255,255,255,.08); color: #fff; }
.logout-btn { background: transparent; border: 1px solid rgba(255,255,255,.16); color: var(--sidebar-text); padding: 7px; border-radius: 7px; font-size: 13px; }
.logout-btn:hover { background: rgba(255,255,255,.08); color: #fff; }

/* ---------- Main ---------- */
.main { display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.channel-header {
  height: 60px; border-bottom: 1px solid var(--line); padding: 0 20px; display: flex;
  align-items: center; gap: 12px; flex: none;
}
.channel-title { font-weight: 800; font-size: 17px; }
.channel-topic { color: var(--ink-soft); font-size: 13px; border-left: 1px solid var(--line); padding-left: 12px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.channel-header-actions { display: flex; gap: 8px; }
.pill-btn { border: 1px solid var(--line); background: var(--bg); border-radius: 20px; padding: 5px 12px; font-size: 13px; color: var(--ink-soft); }
.pill-btn:hover { border-color: var(--brand); color: var(--brand); }

.messages { flex: 1; overflow-y: auto; padding: 18px 20px 8px; min-height: 0; }
.msg { display: flex; gap: 10px; padding: 6px 0; }
.msg:hover { background: var(--bg-soft); border-radius: 8px; }
.avatar {
  width: 36px; height: 36px; border-radius: 8px; flex: none; color: #fff; font-weight: 700;
  display: grid; place-items: center; font-size: 15px;
}
.msg-body { min-width: 0; flex: 1; }
.msg-meta { display: flex; align-items: baseline; gap: 8px; }
.msg-author { font-weight: 700; font-size: 14.5px; }
.msg-role { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding: 1px 5px; border-radius: 4px; }
.msg-role.admin { background: #efe9ff; color: var(--brand-700); }
.msg-role.bot { background: #e4f7ef; color: var(--ok); }
.msg-time { font-size: 12px; color: #a49fb5; }
.msg-text { font-size: 14.5px; line-height: 1.5; white-space: pre-wrap; word-break: break-word; }
.msg-text.deleted { color: #a49fb5; font-style: italic; }
.msg-actions { display: flex; gap: 10px; margin-top: 3px; opacity: 0; transition: opacity .1s; }
.msg:hover .msg-actions { opacity: 1; }
.msg-action { background: none; border: none; color: var(--ink-soft); font-size: 12px; padding: 0; }
.msg-action:hover { color: var(--brand); }
.reactions { display: flex; gap: 6px; margin-top: 5px; flex-wrap: wrap; }
.reaction { border: 1px solid var(--line); background: var(--bg-soft); border-radius: 12px; padding: 1px 8px; font-size: 12.5px; }
.thread-link { color: var(--brand); font-size: 12.5px; margin-top: 4px; background: none; border: none; padding: 0; }
.day-divider { text-align: center; margin: 14px 0; font-size: 12px; color: #a49fb5; position: relative; }
.day-divider::before { content: ""; position: absolute; left: 0; right: 0; top: 50%; height: 1px; background: var(--line); z-index: 0; }
.day-divider span { background: var(--bg); padding: 0 12px; position: relative; z-index: 1; }

.composer { padding: 12px 20px 18px; flex: none; }
.composer form { display: flex; gap: 8px; border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 6px 6px 12px; align-items: center; }
.composer form:focus-within { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(124,111,232,.12); }
.composer input { flex: 1; border: none; outline: none; font-size: 15px; padding: 6px 0; background: transparent; }
.btn-send { background: var(--brand); color: #fff; border: none; border-radius: 8px; padding: 8px 16px; font-weight: 700; font-size: 14px; }
.btn-send:hover { background: var(--brand-600); }
.btn-send:disabled { opacity: .5; }
.btn-mic { background: transparent; border: none; font-size: 18px; line-height: 1; padding: 6px 8px; border-radius: 8px; }
.btn-mic:hover { background: var(--bg-soft); }
.btn-mic.recording { color: #fff; background: var(--danger); animation: micpulse 1.1s ease-in-out infinite; }
@keyframes micpulse { 0%,100% { box-shadow: 0 0 0 0 rgba(224,30,90,.5); } 50% { box-shadow: 0 0 0 6px rgba(224,30,90,0); } }
.voice-att { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.voice-att .voice-ico { font-size: 16px; }
.voice-att audio { height: 34px; max-width: 260px; }
.voice-att .voice-dur { font-size: 12px; color: var(--ink-soft); }
.file-att { display: inline-block; margin-top: 6px; font-size: 13px; color: var(--brand); text-decoration: none; }
.file-att:hover { text-decoration: underline; }
.typing-indicator { height: 16px; font-size: 12px; color: #a49fb5; padding-left: 4px; margin-top: 4px; }

/* ---------- Thread panel ---------- */
.thread-panel { border-left: 1px solid var(--line); background: var(--bg); display: flex; flex-direction: column; min-width: 0; }
.thread-header { height: 60px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; padding: 0 16px; font-weight: 700; }
.thread-header button { background: none; border: none; font-size: 16px; color: var(--ink-soft); }
.thread-messages { flex: 1; overflow-y: auto; padding: 14px 16px; min-height: 0; }

/* ---------- Modals ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(27,16,39,.4); display: grid; place-items: center; z-index: 50; padding: 20px; }
.modal { background: var(--bg); border-radius: 14px; width: 100%; max-width: 480px; box-shadow: var(--shadow); max-height: 86vh; overflow: hidden; display: flex; flex-direction: column; }
.modal-head { padding: 18px 20px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; }
.modal-head h3 { margin: 0; font-size: 17px; }
.modal-head button { background: none; border: none; font-size: 18px; color: var(--ink-soft); }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-body label { display: block; font-size: 13px; font-weight: 600; color: var(--ink-soft); margin-bottom: 14px; }
.modal-body input, .modal-body select, .modal-body textarea {
  width: 100%; margin-top: 6px; padding: 10px 11px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; outline: none;
}
.modal-body input:focus, .modal-body select:focus { border-color: var(--brand); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 8px; }
.btn-ghost { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 9px 16px; font-weight: 600; font-size: 14px; }
.list-row { display: flex; align-items: center; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.list-row:last-child { border-bottom: none; }
.tag { font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 5px; background: var(--bg-soft); color: var(--ink-soft); }
.tag.admin { background: #efe9ff; color: var(--brand-700); }
.tag.bot { background: #e4f7ef; color: var(--ok); }
.tag.worker { background: #eef7ff; color: #2f7fb8; }
.scope-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 8px; }
.scope-chk { display: flex; align-items: center; gap: 6px; font-size: 12.5px; font-weight: 500; color: var(--ink); margin: 0; }
.scope-chk input { width: auto; margin: 0; }
.token-reveal { background: #1b1027; color: #b9ffcf; font-family: ui-monospace, Menlo, monospace; font-size: 13px; padding: 12px; border-radius: 8px; word-break: break-all; margin: 10px 0; }
.notice { background: #fff8e6; border: 1px solid #f0e0b0; color: #7a5c00; padding: 10px 12px; border-radius: 8px; font-size: 13px; }
.muted { color: var(--ink-soft); font-size: 13px; }
.empty-state { text-align: center; color: #a49fb5; padding: 60px 20px; }

/* ---------- Live calls ---------- */
.call-overlay {
  position: fixed; inset: 0; z-index: 60; background: #1a1330;
  display: flex; flex-direction: column; color: #fff;
}
.call-head { padding: 14px 20px; display: flex; align-items: center; gap: 12px; font-weight: 700; border-bottom: 1px solid rgba(255,255,255,.1); }
.call-status { font-weight: 500; font-size: 13px; color: #b7add9; }
.call-grid { flex: 1; display: grid; gap: 12px; padding: 16px; min-height: 0;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); align-content: center; }
.call-tile { position: relative; background: #000; border-radius: 12px; overflow: hidden; aspect-ratio: 4/3; }
.call-tile video { width: 100%; height: 100%; object-fit: cover; background: #241a38; }
.call-tile .call-name { position: absolute; left: 8px; bottom: 8px; background: rgba(0,0,0,.55);
  padding: 2px 8px; border-radius: 6px; font-size: 12px; }
.call-controls { padding: 16px; display: flex; gap: 12px; justify-content: center; border-top: 1px solid rgba(255,255,255,.1); }
.call-btn { background: rgba(255,255,255,.14); color: #fff; border: none; border-radius: 24px;
  padding: 10px 18px; font-size: 15px; font-weight: 600; }
.call-btn:hover { background: rgba(255,255,255,.24); }
.call-btn.off { background: #6b6480; }
.call-btn.danger { background: var(--danger); }
.call-btn.danger:hover { background: #c01850; }

.call-incoming { position: fixed; top: 18px; right: 18px; z-index: 70; background: #2b2140; color: #fff;
  border-radius: 14px; box-shadow: var(--shadow); padding: 16px 18px; display: flex; flex-direction: column; gap: 12px; min-width: 260px; }
.ci-sub { font-size: 12px; color: #b7add9; margin-top: 2px; }
.ci-actions { display: flex; gap: 8px; }
.ci-actions button { flex: 1; border: none; border-radius: 8px; padding: 9px; font-weight: 700; }
.ci-accept { background: var(--ok); color: #fff; }
.ci-decline { background: var(--danger); color: #fff; }

@media (max-width: 720px) {
  .app-view, .app-view:has(.thread-panel:not([hidden])) { grid-template-columns: 1fr; }
  .sidebar { display: none; }
}
