:root {
  --bg: #0f1012; --panel: #141518; --panel-2: #1a1b1f; --line: #26282d;
  --text: #e8e6e3; --muted: #8b8f98; --accent: #e8743b; --accent-soft: rgba(232, 116, 59, .14);
  --green: #3fb97a; --radius: 10px;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 -apple-system, "Segoe UI", Inter, system-ui, sans-serif; }
.hidden { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }

/* auth */
.auth { height: 100%; display: grid; place-items: center; }
.auth-card { width: min(380px, 92vw); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; display: grid; gap: 14px; text-align: center; }
.auth-card input { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; }
.logo { font-weight: 700; font-size: 26px; letter-spacing: .04em; color: var(--accent); }
.primary { background: var(--accent); color: #1a0d05; border: 0; border-radius: 8px; padding: 10px 14px; font-weight: 600; }
.err { color: #ff7b72; min-height: 1.2em; margin: 0; font-size: 13px; }

/* layout */
.app { height: 100vh; height: 100dvh; display: grid; grid-template-columns: 300px minmax(0, 1fr); overflow: hidden; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); padding: 18px 12px; overflow-y: auto; display: flex; flex-direction: column; }
.ws { font-weight: 600; font-size: 17px; padding: 4px 10px 14px; }
.section { display: flex; justify-content: space-between; padding: 14px 10px 6px; font-size: 11px; letter-spacing: .12em; color: var(--muted); }
.row { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 8px; cursor: pointer; border: 0; background: none; width: 100%; text-align: left; }
.row:hover { background: var(--panel-2); }
.row.active { background: var(--accent-soft); box-shadow: inset 2px 0 0 var(--accent); }
.row .hash { color: var(--accent); font-weight: 600; }
.row .badge { margin-left: auto; background: var(--accent); color: #1a0d05; border-radius: 10px; font-size: 11px; font-weight: 700; padding: 0 7px; }
.row .you { margin-left: auto; font-size: 10px; letter-spacing: .1em; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 1px 7px; }
.add { background: none; border: 0; color: var(--muted); text-align: left; padding: 7px 10px; }
.add:hover { color: var(--text); }
.side-foot { margin-top: auto; padding-top: 16px; display: flex; gap: 14px; border-top: 1px solid var(--line); }
.linkish { background: none; border: 0; color: var(--muted); font-size: 12px; padding: 0; }
.linkish:hover { color: var(--text); }
.avatar { position: relative; width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; font-weight: 700; color: #fff; flex: none; }
.avatar.lg { width: 38px; height: 38px; font-size: 15px; }
.avatar .dot { position: absolute; right: -2px; bottom: -2px; width: 9px; height: 9px; border-radius: 50%; background: #6b6f76; border: 2px solid var(--panel); }
.avatar .dot.on { background: var(--green); }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100%; overflow: hidden; }
.chan-head { display: flex; align-items: baseline; gap: 14px; padding: 16px 24px; border-bottom: 1px solid var(--line); }
.chan-head h1 { margin: 0; font-size: 20px; }
.presence { color: var(--muted); font-size: 13px; }
.status { font-size: 12px; padding: 2px 8px; border-radius: 10px; background: var(--accent-soft); color: var(--accent); }
.messages { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 18px 24px; }
.msg { display: grid; grid-template-columns: 38px 1fr; gap: 12px; padding: 6px 0; }
.msg.cont { padding-top: 0; }
.msg.cont .avatar { visibility: hidden; height: 0; }
.msg .meta { display: flex; gap: 10px; align-items: baseline; }
.msg .meta b { font-weight: 600; }
.msg .meta time { color: var(--muted); font-size: 12px; }
.msg .body { overflow-wrap: anywhere; }
.msg.cont .meta { display: none; }
.msg.mentions-me .body-wrap { background: var(--accent-soft); border-left: 2px solid var(--accent); margin-left: -10px; padding-left: 8px; border-radius: 4px; }
.chip { padding: 0 5px; border-radius: 5px; font-weight: 500; background: rgba(255,255,255,.06); }
code { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: .92em; background: var(--panel-2); padding: 1px 5px; border-radius: 5px; }
pre { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; overflow-x: auto; margin: 6px 0; }
pre code { background: none; padding: 0; }
.quote { border-left: 2px solid var(--line); color: var(--muted); padding-left: 10px; margin-bottom: 2px; font-size: 13px; }
.empty { color: var(--muted); text-align: center; margin-top: 15vh; }

.composer-wrap { padding: 0 24px 10px; position: relative; }
.composer { border: 1px solid var(--accent); border-radius: 12px; background: var(--panel); padding: 12px 14px 8px; }
.composer textarea { width: 100%; background: none; border: 0; outline: none; resize: none; max-height: 40vh; }
.composer-row { display: flex; align-items: center; gap: 12px; margin-top: 6px; }
.tools button { background: none; border: 0; color: var(--muted); font-size: 16px; padding: 2px 8px; }
.hint { margin-left: auto; color: var(--muted); font-size: 12px; }
.me { color: #7bd88f; font-weight: 500; }
.send { background: #3a3b40; border: 0; border-radius: 8px; padding: 6px 18px; font-weight: 600; }
.send.ready { background: var(--accent); color: #1a0d05; }
.tz { text-align: right; color: var(--muted); font-size: 11px; padding: 6px 4px 0; }
.mention-pop { position: absolute; bottom: 100px; left: 40px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 4px; min-width: 200px; box-shadow: 0 8px 30px rgba(0,0,0,.4); }
.mention-pop .row { padding: 5px 8px; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; z-index: 10; }
.modal-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 22px; width: min(560px, 92vw); display: grid; gap: 12px; }
.modal-card input { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 9px 12px; }
.modal-card pre { margin: 0; user-select: all; }
.modal-card .actions { display: flex; gap: 10px; justify-content: flex-end; }
.modal-card .ghost { background: none; border: 1px solid var(--line); border-radius: 8px; padding: 8px 14px; }

/* hamburger + drawer plumbing (hidden on desktop) */
.menu-btn { display: none; position: relative; background: none; border: 0; color: var(--text); width: 40px; height: 40px; border-radius: 10px; align-items: center; justify-content: center; flex: none; margin-left: -8px; }
.menu-btn:active { background: var(--panel-2); }
.menu-badge { position: absolute; top: 3px; right: 2px; min-width: 17px; height: 17px; padding: 0 4px; border-radius: 9px; background: var(--accent); color: #1a0d05; font-size: 11px; font-weight: 700; line-height: 17px; text-align: center; }
.backdrop { display: none; }
html { -webkit-text-size-adjust: 100%; }
button, .row, .msg { -webkit-tap-highlight-color: transparent; }

.row .gear { margin-left: 6px; color: var(--muted); opacity: 0; padding: 0 4px; border-radius: 4px; }
.row:hover .gear { opacity: 1; }
.row .gear:hover { background: var(--line); color: var(--text); }
.row .you.chief { color: var(--accent); border-color: var(--accent); }
.row .badge + .you, .row .badge + .gear { margin-left: 6px; }
.row > .you { margin-left: auto; }
.modal-card select, .modal-card textarea { font: inherit; }

.reacts { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; position: relative; }
.react { background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; padding: 0 8px; font-size: 13px; line-height: 22px; }
.react span { color: var(--muted); font-size: 12px; }
.react.mine { border-color: var(--accent); background: var(--accent-soft); }
.react.add { opacity: 0; color: var(--muted); }
.react.reply { display: none; }
.msg:hover .react.add, .react.add:focus { opacity: 1; }
.react-pop { position: absolute; bottom: 28px; left: 0; display: flex; gap: 2px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 4px; z-index: 5; box-shadow: 0 8px 30px rgba(0,0,0,.4); }
.react-pop button { background: none; border: 0; font-size: 18px; border-radius: 6px; padding: 2px 6px; }
.react-pop button:hover { background: var(--line); }
@media (hover: none) { .react.add { opacity: .7; } }

/* custom scrollbars */
* { scrollbar-width: thin; scrollbar-color: #34363c transparent; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: #2d2f35; border-radius: 10px; border: 2px solid transparent; background-clip: content-box; }
*::-webkit-scrollbar-thumb:hover { background: #4a4d55; background-clip: content-box; }
*::-webkit-scrollbar-corner { background: transparent; }
.messages::-webkit-scrollbar-thumb { background: #34363c; background-clip: content-box; }

/* message hover actions + reply bar */
.msg { position: relative; }
.msg-actions { position: absolute; top: -6px; right: 0; display: none; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 2px; z-index: 4; box-shadow: 0 4px 16px rgba(0,0,0,.35); }
.msg:hover .msg-actions, .msg:focus-within .msg-actions { display: block; }
.msg-actions button { background: none; border: 0; color: var(--muted); font-size: 12px; padding: 3px 8px; border-radius: 6px; }
.msg-actions button:hover { background: var(--line); color: var(--text); }
@media (hover: none) { .msg-actions { display: block; opacity: .85; } }
.reply-bar { display: flex; align-items: center; gap: 10px; border-left: 2px solid var(--accent); background: var(--accent-soft); border-radius: 6px; padding: 5px 10px; margin-bottom: 8px; color: var(--muted); font-size: 13px; }
.reply-bar span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reply-bar b { color: var(--text); font-weight: 600; }
.reply-bar button { background: none; border: 0; color: var(--muted); font-size: 18px; line-height: 1; padding: 0 4px; }
.reply-bar button:hover { color: var(--text); }

/* ================= mobile ================= */
@media (max-width: 760px) {
  html, body { height: var(--vvh, 100%); }
  body { overscroll-behavior: none; }
  .app { display: block; height: var(--vvh, 100dvh); position: relative; }
  .main { height: 100%; }

  .sidebar {
    position: fixed; z-index: 30; top: 0; bottom: 0; left: 0; width: min(86vw, 330px);
    padding: calc(18px + env(safe-area-inset-top)) 12px calc(14px + env(safe-area-inset-bottom));
    transform: translateX(-102%); transition: transform .24s cubic-bezier(.2, .8, .2, 1);
    box-shadow: none; display: flex;
  }
  body.drawer-open .sidebar { transform: none; box-shadow: 12px 0 40px rgba(0, 0, 0, .5); }
  .backdrop { display: block; position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .6); opacity: 0; pointer-events: none; transition: opacity .2s ease; }
  body.drawer-open .backdrop { opacity: 1; pointer-events: auto; }
  .row { padding: 11px 12px; font-size: 16px; }
  .add { padding: 11px 12px; font-size: 15px; }
  .side-foot { flex-wrap: wrap; gap: 2px 18px; }
  .linkish { font-size: 14px; padding: 8px 0; white-space: nowrap; }
  .avatar { width: 30px; height: 30px; }

  .menu-btn { display: inline-flex; }
  .chan-head { gap: 10px; padding: calc(8px + env(safe-area-inset-top)) 12px 8px 14px; flex-wrap: nowrap; align-items: center; }
  .chan-head h1 { font-size: 18px; flex: none; max-width: 48vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .presence { flex: 1; min-width: 0; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: right; }
  .status { flex: none; }

  .messages { padding: 10px 12px; -webkit-overflow-scrolling: touch; }
  .msg { grid-template-columns: 34px minmax(0, 1fr); gap: 10px; padding: 7px 0; }
  .avatar.lg { width: 34px; height: 34px; font-size: 14px; }
  .msg .body { font-size: 16px; line-height: 1.45; }
  .msg-actions { display: none !important; }
  .react { line-height: 28px; padding: 0 10px; font-size: 15px; }
  .react.add { opacity: 1; }
  .react.add, .react.reply { display: none; color: var(--muted); }
  .msg.active .react.add, .msg.active .react.reply { display: inline-block; }
  .msg:not(.active) .reacts.empty { display: none; }
  .msg.active { background: rgba(255, 255, 255, .035); margin: 0 -12px; padding-left: 12px; padding-right: 12px; border-radius: 10px; }
  .react-pop { bottom: 36px; }
  .react-pop button { font-size: 22px; padding: 6px 8px; }
  pre { font-size: 13px; }
  .empty { margin-top: 8vh; }

  .composer-wrap { padding: 0 10px calc(8px + env(safe-area-inset-bottom)); }
  .composer { border-radius: 16px; padding: 8px 10px 6px; }
  .composer textarea { font-size: 16px; max-height: 32vh; }
  .composer-row { margin-top: 2px; }
  .tools button { font-size: 20px; padding: 6px 12px; }
  .hint, .tz { display: none; }
  .send { margin-left: auto; padding: 9px 20px; }
  .reply-bar { font-size: 12.5px; }
  .mention-pop { left: 10px; right: 10px; bottom: calc(100% - 4px); min-width: 0; }

  .modal { align-items: end; }
  .modal-card { width: 100%; max-width: none; border-radius: 18px 18px 0 0; padding: 20px 16px calc(20px + env(safe-area-inset-bottom)); max-height: 88dvh; overflow-y: auto; }
  .modal-card input, .modal-card select, .modal-card textarea { font-size: 16px; }
  .modal-card .actions { flex-wrap: wrap; }
  .modal-card .actions button { flex: 1 1 auto; padding: 11px 14px; }

  .auth-card input, .auth-card button { font-size: 16px; padding: 12px; }
}
@media (max-width: 380px) {
  .chan-head h1 { max-width: 40vw; }
  .presence { display: none; }
}

/* markdown inside messages */
.body > :first-child { margin-top: 0; }
.body > :last-child { margin-bottom: 0; }
.body p { margin: 0 0 .45em; }
.body strong { font-weight: 650; }
.body del { opacity: .65; }
.body a { color: #7fb4ff; text-decoration: none; border-bottom: 1px solid rgba(127, 180, 255, .35); }
.body a:hover { border-bottom-color: currentColor; }
.body ul, .body ol { margin: .2em 0 .5em; padding-left: 1.3em; }
.body li { margin: .12em 0; }
.body li::marker { color: var(--muted); }
.body hr { border: 0; border-top: 1px solid var(--line); margin: .7em 0; }
.body blockquote.quote { margin: .3em 0 .5em; padding: 2px 0 2px 12px; border-left: 3px solid var(--line); color: var(--muted); font-size: inherit; }
.md-h { font-weight: 650; margin: .7em 0 .25em; line-height: 1.3; }
.md-h1 { font-size: 1.25em; } .md-h2 { font-size: 1.15em; } .md-h3 { font-size: 1.05em; } .md-h4 { font-size: 1em; color: var(--muted); }
.md-table { overflow-x: auto; margin: .4em 0 .6em; max-width: 100%; }
.md-table table { border-collapse: collapse; font-size: .95em; min-width: 40%; }
.md-table th, .md-table td { border: 1px solid var(--line); padding: 5px 10px; text-align: left; vertical-align: top; }
.md-table th { background: var(--panel-2); font-weight: 600; }
.body pre { margin: .4em 0 .6em; }

/* live "agent is working" indicator */
.working { padding: 0 24px 6px; color: var(--muted); font-size: 13px; }
.working-row { display: flex; align-items: center; gap: 8px; padding: 2px 0; min-width: 0; }
.working-row span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.pulse { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex: none; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: .35; transform: scale(.85); } 50% { opacity: 1; transform: scale(1.1); } }
@media (max-width: 760px) { .working { padding: 0 14px 6px; } }
@media (prefers-reduced-motion: reduce) { .pulse { animation: none; } }
