:root {
  --bg: #f4f5f8; --panel: #ffffff; --panel-2: #f8f9fb; --border: #e3e6ec; --text: #1a1d24; --muted: #667085;
  --accent: #4f46e5; --accent-fg: #ffffff; --accent-soft: #eef0ff;
  --mine-bg: var(--accent); --mine-fg: #ffffff; --other-bg: #eceff4; --other-fg: #1a1d24;
  --input-bg: #ffffff; --input-fg: var(--text);
  --danger: #d92d20; --ok: #12a05c; --warn: #b54708; --warn-bg: #fff4e5;
  --text-size: 15px; --chat-max: 760px; --radius: 14px;
  --shadow: 0 8px 30px rgba(16, 24, 40, .12);
}
html[data-theme="dark"] {
  --bg: #0f1115; --panel: #171a21; --panel-2: #1c2029; --border: #2a2f3a; --text: #e8eaf0; --muted: #9aa3b2;
  --accent: #7c83ff; --accent-fg: #0f1115; --accent-soft: #232846;
  --mine-bg: #5b62e8; --mine-fg: #ffffff; --other-bg: #262b36; --other-fg: #e8eaf0;
  --input-bg: #1c2029; --input-fg: var(--text);
  --danger: #f97066; --ok: #4ade80; --warn: #fdb022; --warn-bg: #2b2112;
  --shadow: 0 8px 30px rgba(0, 0, 0, .5);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
#app, #root { height: 100%; }
#root { height: auto; min-height: 100%; }
body { background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
a.btn { text-decoration: none; color: var(--text); display: inline-block; }
.hidden { display: none !important; }
.noscript { padding: 2rem; text-align: center; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- buttons / inputs ---------- */
.btn { border: 1px solid var(--border); background: var(--panel); padding: .5rem .9rem; border-radius: 10px; transition: background .12s, border-color .12s; }
.btn:hover { background: var(--panel-2); border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.btn.small { padding: .25rem .6rem; font-size: .85rem; border-radius: 8px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.input, textarea.input, select.input { width: 100%; padding: .6rem .75rem; border: 1px solid var(--border); border-radius: 10px; background: var(--input-bg); color: var(--input-fg); }
.input:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
label.field { display: block; margin: .9rem 0 0; font-weight: 600; font-size: .9rem; }
label.field .input { margin-top: .3rem; font-weight: 400; }
.hint { font-size: .82rem; color: var(--muted); margin-top: .3rem; min-height: 1.1em; }
.hint.ok { color: var(--ok); } .hint.bad { color: var(--danger); }
.error { color: var(--danger); font-size: .9rem; margin-top: .75rem; min-height: 1.2em; }
.row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
label.row { flex-wrap: nowrap; align-items: flex-start; margin-top: .8rem; }
label.row input[type=checkbox] { flex: none; margin-top: .25rem; }
.muted { color: var(--muted); }

/* ---------- auth ---------- */
.auth-wrap { min-height: 100%; display: grid; place-items: center; padding: 1.5rem; }
.auth-card { width: 100%; max-width: 440px; background: var(--panel); border: 1px solid var(--border); border-radius: 20px; padding: 1.75rem; box-shadow: var(--shadow); }
.brand { display: flex; align-items: center; gap: .7rem; margin-bottom: .25rem; }
.brand img { width: 40px; height: 40px; border-radius: 10px; }
.brand h1 { font-size: 1.5rem; margin: 0; letter-spacing: -.02em; }
.tagline { color: var(--muted); margin: 0 0 1.2rem; }
.tabs { display: flex; gap: .25rem; background: var(--panel-2); padding: .25rem; border-radius: 12px; margin-bottom: .5rem; }
.tabs button { flex: 1; border: 0; background: transparent; padding: .5rem; border-radius: 9px; font-weight: 600; color: var(--muted); }
.tabs button[aria-selected="true"] { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.dropzone { margin-top: .3rem; border: 2px dashed var(--border); border-radius: 12px; padding: 1.1rem; text-align: center; color: var(--muted); font-weight: 400; transition: border-color .12s, background .12s; }
.dropzone.over, .dropzone:hover { border-color: var(--accent); background: var(--accent-soft); }
.dropzone strong { color: var(--text); }
.notice { background: var(--accent-soft); border-radius: 12px; padding: .8rem 1rem; margin: .9rem 0; font-size: .92rem; }
.notice.warn { background: var(--warn-bg); color: var(--warn); }
.auth-card .btn.wide { width: 100%; margin-top: 1.1rem; padding: .7rem; }
.footnote { margin-top: 1.2rem; font-size: .8rem; color: var(--muted); text-align: center; }

/* ---------- app shell ---------- */
.shell { height: 100%; display: flex; flex-direction: column; }
.shell > .main { flex: 1; min-height: 0; }
.topbar { display: flex; align-items: center; gap: .6rem; padding: .55rem .9rem; background: var(--panel); border-bottom: 1px solid var(--border); }
.topbar .brand { margin: 0; } .topbar .brand img { width: 28px; height: 28px; border-radius: 8px; } .topbar .brand h1 { font-size: 1.05rem; }
.pill { font-size: .75rem; padding: .15rem .55rem; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); }
.pill.online { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, var(--border)); }
.pill.reconnecting { color: var(--warn); } .pill.offline { color: var(--danger); }
.pill.unsaved { color: var(--warn); background: var(--warn-bg); border-color: transparent; cursor: pointer; }
.banner { padding: .5rem .9rem; background: var(--accent-soft); font-size: .9rem; display: flex; gap: .6rem; align-items: center; }
.banner.warn { background: var(--warn-bg); color: var(--warn); }
.main { display: grid; grid-template-columns: 290px minmax(0, 1fr); min-height: 0; }
.sidebar { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.side-actions { display: flex; gap: .5rem; padding: .75rem; }
.side-actions .btn { flex: 1; }
.side-scroll { overflow-y: auto; flex: 1; padding: 0 .5rem 1rem; }
.side-title { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); padding: .9rem .5rem .3rem; font-weight: 700; }
.chat-item { display: flex; align-items: center; gap: .6rem; width: 100%; text-align: left; border: 0; background: transparent; padding: .55rem .6rem; border-radius: 10px; }
.chat-item:hover { background: var(--panel-2); } .chat-item[aria-current="true"] { background: var(--accent-soft); }
.avatar { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff; flex: none; position: relative; font-size: .95rem; }
.avatar .dot { position: absolute; right: -1px; bottom: -1px; width: 11px; height: 11px; border-radius: 50%; background: var(--ok); border: 2px solid var(--panel); }
.chat-item .meta { min-width: 0; flex: 1; }
.chat-item .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .preview { color: var(--muted); font-size: .82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { background: var(--accent); color: var(--accent-fg); border-radius: 999px; font-size: .72rem; min-width: 1.35rem; text-align: center; padding: .05rem .4rem; font-weight: 700; }
.invite { border: 1px solid var(--border); border-radius: 12px; padding: .65rem; margin: .3rem 0; background: var(--panel-2); font-size: .9rem; }
.empty { color: var(--muted); font-size: .88rem; padding: .4rem .6rem; }

.pane { display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) auto; min-width: 0; min-height: 0; position: relative; background: var(--bg); }
.pane-head { display: flex; align-items: center; gap: .7rem; padding: .65rem 1rem; background: var(--panel); border-bottom: 1px solid var(--border); }
.pane-head .title { font-weight: 700; } .pane-head .sub { font-size: .8rem; color: var(--muted); }
.mobile-back { display: none; }
.messages { overflow-y: auto; padding: 1rem; }
.messages-inner { max-width: var(--chat-max); margin: 0 auto; display: flex; flex-direction: column; gap: .35rem; }
.day { align-self: center; font-size: .75rem; color: var(--muted); margin: .7rem 0 .2rem; }
.sys { align-self: center; font-size: .8rem; color: var(--muted); background: var(--panel); border: 1px solid var(--border); padding: .15rem .7rem; border-radius: 999px; text-align: center; }
.msg { display: flex; flex-direction: column; max-width: 78%; font-size: var(--text-size); }
.msg.mine { align-self: flex-end; align-items: flex-end; } .msg.theirs { align-self: flex-start; }
.msg .who { font-size: .75rem; color: var(--muted); margin: .3rem .5rem .1rem; }
.bubble { padding: .5rem .8rem; border-radius: 16px; overflow-wrap: anywhere; white-space: pre-wrap; }
.msg.mine .bubble { background: var(--mine-bg); color: var(--mine-fg); border-bottom-right-radius: 5px; }
.msg.theirs .bubble { background: var(--other-bg); color: var(--other-fg); border-bottom-left-radius: 5px; }
.msg.mine .bubble a { color: inherit; } .msg.theirs .bubble a { color: inherit; }
.bubble a { text-decoration: underline; }
.bubble.media-bubble { padding: .3rem; }
.time { font-size: .68rem; color: var(--muted); margin: .1rem .5rem; }
.media img, .media video { display: block; max-width: min(100%, 360px); max-height: 320px; border-radius: 12px; }
.media-btn { border: 1px dashed currentColor; background: transparent; border-radius: 10px; padding: .6rem .9rem; opacity: .9; }
.earlier { align-self: center; }
.placeholder { height: 100%; display: grid; place-items: center; color: var(--muted); text-align: center; padding: 2rem; }
.composer { background: var(--panel); border-top: 1px solid var(--border); padding: .6rem .8rem; position: relative; }
.composer-inner { max-width: var(--chat-max); margin: 0 auto; display: flex; gap: .5rem; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; max-height: 9rem; min-height: 2.6rem; padding: .6rem .8rem; border-radius: 14px; border: 1px solid var(--border); background: var(--input-bg); color: var(--input-fg); font-size: var(--text-size); }
.composer textarea:focus { outline: none; border-color: var(--accent); }
.icon-btn { border: 1px solid var(--border); background: var(--panel); width: 2.6rem; height: 2.6rem; border-radius: 12px; font-size: 1.15rem; }
.icon-btn:hover { background: var(--panel-2); }
.send-btn { height: 2.6rem; padding: 0 1.1rem; }
.counter { font-size: .72rem; color: var(--muted); text-align: right; max-width: var(--chat-max); margin: .2rem auto 0; }
.popover { position: absolute; bottom: calc(100% + 6px); left: .8rem; width: min(380px, calc(100% - 1.6rem)); background: var(--panel); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); padding: .8rem; z-index: 20; }
.emoji-tabs { display: flex; gap: .2rem; margin-bottom: .4rem; flex-wrap: wrap; }
.emoji-tabs button { border: 0; background: transparent; padding: .2rem .5rem; border-radius: 8px; color: var(--muted); font-size: .8rem; }
.emoji-tabs button[aria-selected="true"] { background: var(--accent-soft); color: var(--text); }
.emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; max-height: 190px; overflow-y: auto; }
.emoji-grid button { border: 0; background: transparent; font-size: 1.4rem; padding: .2rem; border-radius: 8px; }
.emoji-grid button:hover { background: var(--panel-2); }
.preview-box { margin-top: .6rem; min-height: 40px; }
.preview-box img, .preview-box video { max-width: 100%; max-height: 180px; border-radius: 10px; }
.gif-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: .4rem; margin-top: .6rem; max-height: 200px; overflow-y: auto; }
.gif-grid .cell { position: relative; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--panel-2); aspect-ratio: 1; }
.gif-grid .cell button.pick { border: 0; background: none; padding: 0; width: 100%; height: 100%; }
.gif-grid img, .gif-grid video { width: 100%; height: 100%; object-fit: cover; }
.gif-grid .x { position: absolute; top: 2px; right: 2px; border: 0; background: rgba(0,0,0,.6); color: #fff; border-radius: 50%; width: 20px; height: 20px; line-height: 1; font-size: .8rem; }

.drawer { position: absolute; top: 0; right: 0; bottom: 0; width: min(340px, 100%); background: var(--panel); border-left: 1px solid var(--border); padding: 1rem; overflow-y: auto; z-index: 15; box-shadow: var(--shadow); }
.drawer h3 { margin: 0 0 .6rem; }
.member { display: flex; align-items: center; gap: .5rem; padding: .35rem 0; border-bottom: 1px solid var(--border); }
.tag { font-size: .68rem; background: var(--accent-soft); border-radius: 999px; padding: .05rem .5rem; font-weight: 700; }

/* ---------- settings ---------- */
.settings { overflow-y: auto; padding: 1.2rem; min-width: 0; }
.settings-inner { max-width: 720px; margin: 0 auto; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.1rem 1.2rem; margin-bottom: 1rem; }
.card h2 { margin: 0 0 .2rem; font-size: 1.05rem; }
.card > p.muted { margin: 0 0 .6rem; font-size: .88rem; }
.setting { display: flex; justify-content: space-between; gap: 1rem; align-items: center; padding: .65rem 0; border-top: 1px solid var(--border); flex-wrap: wrap; }
.setting:first-of-type { border-top: 0; }
.setting .lbl { font-weight: 600; } .setting .desc { font-size: .82rem; color: var(--muted); font-weight: 400; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: .4rem .8rem; }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-fg); font-weight: 600; }
input[type="color"] { width: 44px; height: 32px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); padding: 2px; }
input[type="range"] { accent-color: var(--accent); width: 180px; }
.switch { width: 44px; height: 26px; accent-color: var(--accent); transform: scale(1.3); }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .6rem; }
.chip { display: inline-flex; align-items: center; gap: .3rem; background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; padding: .15rem .3rem .15rem .7rem; }
.chip button { border: 0; background: transparent; color: var(--muted); border-radius: 50%; width: 22px; height: 22px; }
.sample { display: flex; flex-direction: column; gap: .3rem; padding: .8rem; background: var(--bg); border-radius: 12px; margin-top: .6rem; }
.sample .msg { max-width: 90%; }

/* ---------- modal + toast ---------- */
.overlay { position: fixed; inset: 0; background: rgba(8, 10, 15, .55); display: grid; place-items: center; padding: 1rem; z-index: 100; }
.modal { background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 1.3rem; width: 100%; max-width: 440px; box-shadow: var(--shadow); max-height: 90vh; overflow-y: auto; }
.modal h2 { margin: 0 0 .4rem; font-size: 1.15rem; }
.modal .actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1.1rem; }
.toasts { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: .4rem; z-index: 200; align-items: center; pointer-events: none; }
.toast { background: var(--text); color: var(--bg); padding: .55rem 1rem; border-radius: 12px; box-shadow: var(--shadow); font-size: .9rem; max-width: 90vw; }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- admin ---------- */
.admin-wrap { max-width: 1000px; margin: 0 auto; padding: 1.2rem; }
.admin-nav { display: flex; gap: .3rem; flex-wrap: wrap; margin: 1rem 0; }
.admin-nav button { border: 1px solid var(--border); background: var(--panel); padding: .4rem .9rem; border-radius: 999px; }
.admin-nav button[aria-selected="true"] { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .7rem; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: .9rem; }
.stat b { display: block; font-size: 1.6rem; letter-spacing: -.02em; } .stat span { color: var(--muted); font-size: .82rem; }
table { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; font-size: .9rem; }
th, td { text-align: left; padding: .5rem .7rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { background: var(--panel-2); font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
tr:last-child td { border-bottom: 0; }
.table-scroll { overflow-x: auto; }
.status-ok { color: var(--ok); font-weight: 600; } .status-bad { color: var(--danger); font-weight: 600; }
textarea.input { min-height: 4.5rem; resize: vertical; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: .8rem; }

@media (max-width: 760px) {
  .main { grid-template-columns: minmax(0, 1fr); }
  .topbar { flex-wrap: wrap; gap: .4rem; padding: .4rem .6rem; }
  .topbar .brand h1, .topbar > .muted { display: none; }
  .messages { padding: .6rem; } .composer { padding: .5rem; } .composer-inner { gap: .3rem; }
  .icon-btn { width: 2.3rem; } .send-btn { padding: 0 .8rem; }
  .sidebar { grid-column: 1; grid-row: 1; } .pane, .settings { grid-column: 1; grid-row: 1; }
  .main.in-chat .sidebar { display: none; } .main:not(.in-chat) .pane { display: none; }
  .mobile-back { display: inline-block; }
  .msg { max-width: 90%; }
}
