* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
:root {
  --bg: #0d0f13;
  --panel: #161a21;
  --panel2: #1e232c;
  --line: #262c37;
  --txt: #e8ecf3;
  --dim: #8b95a6;
  --accent: #3b82f6;
  --mine: #2563eb;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--txt);
  font: 15px/1.45 -apple-system, "SF Pro Text", "Segoe UI", Roboto, system-ui, sans-serif;
  overscroll-behavior: none;
}
.screen { position: fixed; inset: 0; }
.hidden { display: none !important; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }

/* ---------- join ---------- */
#join { display: flex; align-items: center; justify-content: center; padding: 24px; }
.joinbox { width: 100%; max-width: 340px; text-align: center; }
.logo {
  font-size: 40px; font-weight: 800; letter-spacing: -1.5px;
  background: linear-gradient(135deg, #60a5fa, #a78bfa 60%, #f472b6);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  margin-bottom: 2px;
}
.sub { color: var(--dim); margin: 0 0 28px; font-size: 14px; }
.joinbox input {
  width: 100%; padding: 14px 16px; margin-bottom: 10px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  color: var(--txt); font-size: 16px; outline: none;
}
.joinbox input:focus { border-color: var(--accent); }
#jgo {
  width: 100%; padding: 14px; border-radius: 12px; font-weight: 600; font-size: 16px;
  background: var(--accent); color: #fff; margin-top: 6px;
}
#jgo:active { opacity: .8; }
.err { color: #f87171; font-size: 13px; margin-top: 12px; min-height: 18px; }

/* ---------- app ---------- */
#app {
  display: flex; flex-direction: column;
  /* follow the visual viewport so the composer rides above the keyboard */
  top: var(--app-top, 0px);
  bottom: auto;
  height: var(--app-h, 100%);
}
header {
  padding: calc(10px + var(--safe-t)) 16px 10px;
  background: rgba(13,15,19,.88); backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line); z-index: 5;
}
.ttl { display: flex; align-items: center; gap: 10px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #ef4444; flex: 0 0 auto;
       transition: background .3s; }
.dot.on { background: #22c55e; box-shadow: 0 0 8px rgba(34,197,94,.6); }
.grp { font-weight: 700; font-size: 17px; }
.sub2 { font-size: 12px; color: var(--dim); }

main {
  flex: 1; overflow-y: auto; overflow-x: hidden; padding: 12px 10px 4px;
  -webkit-overflow-scrolling: touch;
}
.topmore { text-align: center; padding: 4px 0 10px; }
.topmore button {
  font-size: 12px; color: var(--dim); background: var(--panel);
  padding: 6px 14px; border-radius: 999px;
}

/* ---------- messages ---------- */
.row { display: flex; gap: 8px; animation: pop .16s ease-out; }
.row + .row { margin-top: 2px; }
.row.first { margin-top: 14px; }
@keyframes pop { from { opacity: 0; transform: translateY(6px); } }

.av {
  width: 28px; height: 28px; border-radius: 50%; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; color: #0b0d11;
  align-self: flex-end; margin-bottom: 3px; user-select: none;
}
.row:not(.last) .av { visibility: hidden; }
.row.mine .av { display: none; }

.wrap { display: flex; flex-direction: column; max-width: 76%; min-width: 0; }
.row.mine .wrap { align-items: flex-end; margin-left: auto; }

.who {
  display: none; font-size: 11.5px; color: var(--dim);
  margin: 0 0 3px 13px; font-weight: 600;
}
.row.first .who { display: block; }
.row.mine .who { display: none; }

.line { display: flex; align-items: flex-end; gap: 6px; min-width: 0; }
.row.mine .line { flex-direction: row-reverse; }

.meta {
  display: none; font-size: 10.5px; color: #6b7482;
  flex: 0 0 auto; padding-bottom: 3px;
}
.row.last .meta { display: block; }

.bub {
  padding: 8px 13px; border-radius: 18px; background: var(--panel2);
  white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere;
  font-size: 15px; min-width: 0;
}
.row.mine .bub { background: var(--mine); color: #fff; }

/* stacked bubbles get squared-off corners so a run reads as one block */
.row:not(.first) .bub { border-top-left-radius: 7px; }
.row:not(.last) .bub { border-bottom-left-radius: 7px; }
.row.mine:not(.first) .bub { border-top-left-radius: 18px; border-top-right-radius: 7px; }
.row.mine:not(.last) .bub { border-bottom-left-radius: 18px; border-bottom-right-radius: 7px; }

.bub.media { padding: 3px; }
.bub.media img, .bub.media video {
  display: block; max-width: 100%; max-height: 340px;
  /* width/height attributes are on the element (to reserve space before decode),
     so both axes must stay auto or the max-* constraints squash the picture */
  width: auto; height: auto;
  border-radius: 15px; min-width: 120px; background: #000;
}
.bub.media video { width: 240px; height: auto; }
.cap { padding: 6px 10px 4px; font-size: 14px; }

.pending { opacity: .5; }
.daysep {
  text-align: center; font-size: 11.5px; color: var(--dim);
  margin: 18px 0 8px; font-weight: 600;
}

/* ---------- composer ---------- */
.typing { height: 18px; padding: 0 18px; font-size: 12px; color: var(--dim); }
footer {
  display: flex; align-items: flex-end; gap: 6px;
  padding: 6px 8px calc(6px + var(--safe-b));
  border-top: 1px solid var(--line); background: var(--bg);
}
.icon {
  width: 38px; height: 38px; flex: 0 0 auto; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; color: var(--dim);
}
.icon.send { background: var(--accent); color: #fff; }
.icon:active { opacity: .6; }
#input {
  flex: 1; resize: none; max-height: 120px; min-height: 38px;
  padding: 9px 14px; border-radius: 19px; border: 1px solid var(--line);
  background: var(--panel); color: var(--txt); font: inherit; outline: none;
  line-height: 1.35;
}
#input:focus { border-color: #33405a; }

/* ---------- misc ---------- */
.lightbox {
  position: fixed; inset: 0; background: #000; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  touch-action: none; overflow: hidden; overscroll-behavior: contain;
}
.lightbox img {
  max-width: 100%; max-height: 100%; border-radius: 4px;
  transform-origin: center center; will-change: transform;
  user-select: none; -webkit-user-drag: none; touch-action: none;
}
.lbclose {
  position: absolute; top: calc(10px + var(--safe-t)); right: 12px;
  width: 40px; height: 40px; border-radius: 50%;
  background: rgba(40,46,58,.85); color: #fff; font-size: 24px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(6px); z-index: 2;
}
.lbclose:active { opacity: .6; }
.toast {
  position: fixed; bottom: calc(80px + var(--safe-b)); left: 50%;
  transform: translateX(-50%) translateY(20px); opacity: 0;
  background: #2a3140; padding: 9px 16px; border-radius: 999px;
  font-size: 13px; transition: .22s; pointer-events: none; z-index: 200;
  white-space: nowrap; max-width: 90vw; overflow: hidden; text-overflow: ellipsis;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- header / drawer ---------- */
header { display: flex; align-items: center; gap: 4px; }
.ttl { flex: 1; min-width: 0; }
.ttl .grp { display: flex; align-items: baseline; gap: 1px; overflow: hidden; }
.hash { color: var(--dim); font-weight: 600; margin-right: 1px; }
.bell { color: var(--dim); }
.bell.on { color: #f5c542; }
.bell.off { opacity: .35; }

.scrim {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 40;
  backdrop-filter: blur(1px);
}
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; width: min(78vw, 320px);
  background: var(--panel); border-right: 1px solid var(--line);
  transform: translateX(-102%); transition: transform .22s ease;
  z-index: 50; display: flex; flex-direction: column;
  padding-top: var(--safe-t); box-shadow: 12px 0 40px rgba(0,0,0,.5);
}
.drawer.open { transform: translateX(0); }
.dhead { padding: 16px 14px 6px; }
.dheadrow { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.namebtn {
  width: 40px; height: 40px; flex: 0 0 auto; border-radius: 50%;
  background: var(--panel2); color: var(--txt);
  display: flex; align-items: center; justify-content: center;
  border: 1px solid var(--line);
}
.namebtn:active { background: #2b3444; transform: scale(.96); }
.logo.small { font-size: 22px; }
.dsub { font-size: 12px; color: var(--dim); margin-top: 2px; cursor: pointer; }
.dsub:active { color: var(--txt); }
.dlabel {
  font-size: 11px; text-transform: uppercase; letter-spacing: .08em;
  color: #5c6674; padding: 14px 18px 6px; font-weight: 700;
}
#chanList { flex: 1; overflow-y: auto; padding: 0 8px; }
.chan {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 9px 10px; border-radius: 9px; text-align: left; color: var(--dim);
}
.chan:active { background: var(--panel2); }
.chan.active { background: #26303f; color: var(--txt); }
.chan .cn { font-size: 15px; font-weight: 600; flex: 1; min-width: 0;
            overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chan .hash { font-size: 15px; }
.badge {
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 700;
  min-width: 18px; height: 18px; border-radius: 9px; padding: 0 5px;
  display: flex; align-items: center; justify-content: center;
}
.newchan {
  margin: 8px 14px 12px; padding: 11px; border-radius: 10px;
  border: 1px dashed var(--line); color: var(--dim); font-size: 14px;
}
.newchan:active { background: var(--panel2); }
.dfoot { padding: 10px 14px calc(14px + var(--safe-b)); border-top: 1px solid var(--line); }
.ghost { color: var(--dim); font-size: 13px; padding: 8px 2px; }
#pushBtn.on { color: #5ac48a; }

/* ---------- modal ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 60;
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.mbox {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 20px; width: 100%; max-width: 340px;
}
.mbox h3 { margin: 0 0 14px; font-size: 17px; }
.mbox input {
  width: 100%; padding: 12px 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); color: var(--txt); font-size: 16px; outline: none;
}
.mbox input:focus { border-color: var(--accent); }
.mact { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.mact button { padding: 9px 16px; border-radius: 9px; font-size: 14px; }
#mOk { background: var(--accent); color: #fff; font-weight: 600; }
#mCancel { color: var(--dim); }

/* AI messages read as tool output, not as a person */
.row.ai .av { background: linear-gradient(135deg,#60a5fa,#a78bfa) !important; }
.row.ai .bub { background: #1b2233; border: 1px solid #2b3a55; }
.row.ai .who { color: #8ba8e8; }

/* ---------- empty channel ---------- */
.empty { text-align: center; color: var(--dim); padding: 60px 30px 20px; }
.ebig {
  font-size: 44px; font-weight: 800; color: #2a3242; margin-bottom: 6px;
}
#emptyTxt { font-size: 14px; line-height: 1.5; }

/* ---------- update bar ---------- */
.updatebar {
  position: fixed; left: 50%; bottom: calc(74px + var(--safe-b));
  transform: translateX(-50%) translateY(16px); opacity: 0;
  display: flex; align-items: center; gap: 12px;
  background: #2a3140; border: 1px solid #3a4356; border-radius: 999px;
  padding: 8px 8px 8px 16px; font-size: 13px; color: var(--txt);
  z-index: 150; transition: .22s; box-shadow: 0 8px 30px rgba(0,0,0,.5);
}
.updatebar.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.updatebar span { white-space: nowrap; }
.updatebar button {
  background: var(--accent); color: #fff; font-weight: 600; font-size: 13px;
  padding: 6px 14px; border-radius: 999px;
}

/* ---------- private channels / DMs ---------- */
#chanList .dlabel:first-child { padding-top: 6px; }
.chan .ci { width: 18px; text-align: center; font-size: 14px; flex: 0 0 auto; color: var(--dim); }
.chan.active .ci { color: var(--txt); }
.check {
  display: flex; align-items: center; gap: 9px; font-size: 14px;
  color: var(--dim); margin: 12px 2px 2px;
}
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.memberlist { max-height: 220px; overflow-y: auto; margin-top: 6px; }
.mrow {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 10px 8px; border-radius: 9px; font-size: 15px; color: var(--txt);
}
.mrow:active { background: var(--panel2); }
.mrow .av { width: 26px; height: 26px; font-size: 11px; align-self: center; margin: 0; visibility: visible; }
.mrow.sel { background: #26303f; }
.mrow .tick { margin-left: auto; color: var(--accent); font-weight: 700; }

/* ---------- version line ---------- */
.verline {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 11px; color: #5c6674; padding: 2px 2px 0;
}
.verline button { color: #6b7482; font-size: 11px; text-decoration: underline; }
.verline button:active { color: var(--txt); }

/* ---------- confirm sheet ---------- */
.sheet-msg { color: var(--dim); font-size: 14px; line-height: 1.5; margin: 0 0 4px; }
.mact button.danger { background: #b91c1c; color: #fff; font-weight: 600; }
.person { display:flex; align-items:center; gap:10px; padding:10px 8px; font-size:15px; }
.person .av { width:26px; height:26px; font-size:11px; margin:0; visibility:visible; align-self:center; }
.person .who2 { flex:1; min-width:0; }
.person .role { font-size:11px; color:var(--dim); }
.person button { color:#f87171; font-size:13px; padding:6px 10px; }
