/* Layout and components. Colours come ONLY from theme tokens (see themes/). */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font: 15px/1.45 var(--font-mono);
  -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
/* subtle noise over everything */
body::before {
  content: ""; position: fixed; inset: 0; z-index: 1000; pointer-events: none;
  background-image: var(--noise); opacity: var(--noise-opacity); mix-blend-mode: overlay;
}
a { color: var(--cyan); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
.dim { color: var(--fg-dim); } .faint { color: var(--fg-faint); }
.green { color: var(--accent); } .yellow { color: var(--yellow); } .red { color: var(--red); } .cyan { color: var(--cyan); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.boot { padding: 2rem; color: var(--fg-dim); }

/* No accidental text selection / native menus on the chrome. Inputs stay normal. */
.app, #toasts, .ctxmenu, .lightbox {
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
input, textarea { -webkit-user-select: text; user-select: text; -webkit-touch-callout: default; }

/* ── frame ─────────────────────────────────────────────── */
.app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
.statusbar {
  flex: none; display: flex; gap: .55rem; align-items: center; flex-wrap: nowrap; overflow: hidden;
  padding: calc(.3rem + env(safe-area-inset-top)) .7rem .3rem; background: var(--bg-elev);
  font-size: .8rem; white-space: nowrap;
}
.statusbar.bottom { padding: .3rem .7rem calc(.3rem + env(safe-area-inset-bottom)); }
.statusbar .sep { color: var(--fg-faint); }
.statusbar .brand { color: var(--yellow); font-weight: 700; }
.statusbar .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.statusbar .hide-s { display: none; }
@media (min-width: 640px) { .statusbar .hide-s { display: inline; } }

.layout { flex: 1; min-height: 0; display: flex; }
.side, .main { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.side { flex: 1 1 auto; }
.main { flex: 1 1 auto; display: none; }
.layout[data-pane="main"] .side { display: none; }
.layout[data-pane="main"] .main { display: flex; }
@media (min-width: 860px) {
  .side { flex: 0 0 22rem; border-right: 1px solid var(--line); }
  .main, .layout[data-pane="main"] .side { display: flex; }
  .back { display: none !important; }
}
.scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.pad { padding: .5rem .9rem 1.2rem; }

/* ── nav tabs ──────────────────────────────────────────── */
.tabs { flex: none; display: flex; border-bottom: 1px solid var(--line); }
.tab {
  flex: 1; padding: .6rem .3rem; text-align: center; color: var(--fg-dim); font-size: .85rem;
  border-right: 1px solid var(--line); position: relative; white-space: nowrap;
}
.tab:last-child { border-right: 0; }
.tab:hover { text-decoration: none; color: var(--fg); background: var(--accent-bg); }
.tab[aria-current="page"] { color: var(--accent); background: var(--accent-bg); }
.tab .badge { margin-left: .25rem; }
.badge { color: var(--accent-contrast); background: var(--accent); padding: 0 .35rem; font-size: .72rem; font-weight: 700; }

/* ── titled panel (the ┌ Title ┐ look) ─────────────────── */
.panel { position: relative; border: 1px solid var(--line); padding: 1.15rem .9rem .85rem; margin: 1.05rem 0 .3rem; }
.panel-title {
  position: absolute; top: 0; left: .8rem; transform: translateY(-52%); margin: 0; padding: 0 .4rem;
  background: var(--bg); color: var(--fg-dim); font: inherit; font-size: .85rem; font-weight: 400;
}
.panel.alert { border-color: var(--red); }
.hint { color: var(--fg-dim); font-size: .85rem; margin: .3rem 0 0; }
.hint.block { display: block; }
.err { color: var(--red); font-size: .88rem; min-height: 1.3em; margin: .4rem 0 0; }
.rowline { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; margin: .35rem 0; }
.rowline.between { justify-content: space-between; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .15rem 1rem; margin: 0; }
.kv dt { color: var(--cyan); } .kv dd { margin: 0; overflow-wrap: anywhere; }

/* ── forms ─────────────────────────────────────────────── */
label.field { display: block; margin: .6rem 0; color: var(--cyan); font-size: .85rem; }
input[type="text"], input[type="password"], input[type="search"], textarea {
  appearance: none; -webkit-appearance: none; width: 100%; margin-top: .25rem;
  font: inherit; font-size: 16px; color: var(--fg); caret-color: var(--accent);
  background: var(--bg-input); border: 1px solid var(--line-strong); border-radius: 0; padding: .5rem .6rem;
}
input::placeholder, textarea::placeholder { color: var(--fg-faint); }
input:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.btn {
  appearance: none; -webkit-appearance: none; font: inherit; font-size: .9rem; color: var(--fg); cursor: pointer;
  background: transparent; border: 1px solid var(--line-strong); border-radius: 0; padding: .4rem .8rem; min-height: 2.2rem;
}
.btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); background: var(--accent-bg); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); font-weight: 700; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); color: var(--accent-contrast); background: var(--accent); }
.btn.danger { color: var(--red); border-color: var(--red); }
.btn.danger:hover:not(:disabled) { background: var(--red); color: var(--bg); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.small { padding: .15rem .55rem; min-height: 1.8rem; font-size: .82rem; }
@media (pointer: coarse) { .btn { min-height: 2.75rem; } .btn.small { min-height: 2.25rem; } }
label.check { display: flex; gap: .6rem; align-items: flex-start; margin: .55rem 0; cursor: pointer; color: var(--fg); }
label.check input {
  appearance: none; -webkit-appearance: none; flex: none; width: 1.15rem; height: 1.15rem; margin: .12rem 0 0;
  border: 1px solid var(--line-strong); background: var(--bg-input); display: grid; place-items: center; cursor: pointer;
}
label.check input::after { content: "x"; color: var(--accent); font-weight: 700; line-height: 1; transform: scale(0); }
label.check input:checked { border-color: var(--accent); }
label.check input:checked::after { transform: scale(1); }
.seg { display: inline-flex; border: 1px solid var(--line-strong); }
.seg button { font: inherit; background: transparent; color: var(--fg-dim); border: 0; padding: .4rem .9rem; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--line-strong); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-contrast); font-weight: 700; }

/* ── avatars: square, noisy, green-tinted ──────────────── */
.avwrap { position: relative; display: inline-block; flex: none; line-height: 0; }
.avatar {
  --sz: 44px; position: relative; display: block; width: var(--sz); height: var(--sz); overflow: hidden;
  border: 1px solid var(--accent); background: var(--bg-input);
}
.avatar.sm { --sz: 32px; } .avatar.md { --sz: 44px; } .avatar.xl { --sz: min(220px, 62vw); }
.avatar img { display: block; width: 100%; height: 100%; object-fit: cover; filter: grayscale(1) contrast(1.15) brightness(.95); }
.avatar::before { content: ""; position: absolute; inset: 0; z-index: 1; background: var(--accent); mix-blend-mode: multiply; opacity: .6; }
.avatar::after { content: ""; position: absolute; inset: 0; z-index: 2; pointer-events: none; background-image: var(--noise); mix-blend-mode: overlay; opacity: .5; }
.avatar .ini { display: grid; place-items: center; width: 100%; height: 100%; color: var(--accent); font-weight: 700; font-size: calc(var(--sz) * .42); line-height: 1; }
.dot {
  position: absolute; right: -4px; bottom: -4px; z-index: 3; width: 11px; height: 11px; border-radius: 50%;
  background: var(--fg-faint); border: 2px solid var(--bg);
}
.dot.on { background: var(--accent); box-shadow: 0 0 6px var(--accent); }

/* ── lists ─────────────────────────────────────────────── */
.list { list-style: none; margin: 0; padding: 0; }
.item {
  display: flex; gap: .75rem; align-items: center; width: 100%; padding: .65rem .9rem; text-align: left;
  color: inherit; background: none; border: 0; border-bottom: 1px solid var(--line); font: inherit; cursor: pointer;
}
.item:hover, .item[aria-current="true"] { background: var(--accent-bg); text-decoration: none; }
.item .grow { flex: 1; min-width: 0; }
.item .name { color: var(--fg); font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { color: var(--fg-dim); font-size: .85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .meta { flex: none; text-align: right; font-size: .75rem; color: var(--fg-dim); display: flex; flex-direction: column; gap: .2rem; align-items: flex-end; }
.static-item { cursor: default; }
.empty { padding: 1.6rem 1rem; color: var(--fg-dim); text-align: center; }
.empty .btn { margin-top: .8rem; }

/* ── chat ──────────────────────────────────────────────── */
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chat-head {
  flex: none; display: flex; gap: .7rem; align-items: center; padding: .55rem .8rem; border-bottom: 1px solid var(--line);
  background: var(--bg-elev);
}
.chat-head .who { flex: 1; min-width: 0; text-align: left; background: none; border: 0; font: inherit; color: inherit; display: flex; gap: .7rem; align-items: center; cursor: pointer; padding: 0; }
.chat-head .who .t { min-width: 0; }
.chat-head .nm { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; }
.chat-head .st { font-size: .8rem; color: var(--fg-dim); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .st.typing { color: var(--yellow); }
.chat-head .st.online { color: var(--accent); }
.iconbtn { flex: none; background: none; border: 1px solid var(--line-strong); color: var(--fg); font: inherit; width: 2.4rem; height: 2.4rem; cursor: pointer; }
.iconbtn:hover { border-color: var(--accent); color: var(--accent); }

.log { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: .8rem; display: flex; flex-direction: column; gap: .45rem; position: relative; }
.daysep { align-self: center; color: var(--fg-faint); font-size: .75rem; margin: .5rem 0 .2rem; }
.msg {
  max-width: min(84%, 36rem); padding: .4rem .6rem .3rem; border: 1px solid var(--theirs-line); background: var(--theirs-bg);
  overflow-wrap: anywhere; position: relative;
}
.msg.mine { align-self: flex-end; background: var(--mine-bg); border-color: var(--mine-line); }
.msg.theirs { align-self: flex-start; }
.msg.pending { opacity: .62; }
.msg.failed { border-color: var(--red); opacity: 1; }
.msg.gone { border-style: dashed; color: var(--fg-dim); font-style: italic; background: transparent; }
.msg .text { white-space: pre-wrap; }
.msg .media { display: block; max-width: 100%; max-height: 22rem; margin: .1rem 0 .25rem; background: #000; cursor: zoom-in; }
.msg video.media { cursor: default; width: min(100%, 22rem); }
.msg .meta { display: flex; gap: .5rem; justify-content: flex-end; align-items: center; font-size: .72rem; color: var(--fg-dim); margin-top: .1rem; }
.msg .ticks { letter-spacing: -.15em; }
.msg .ticks.seen { color: var(--accent); }
.msg .ticks.failed { color: var(--red); letter-spacing: 0; }
.progress { height: 3px; background: var(--line); margin: .25rem 0; }
.progress > i { display: block; height: 100%; width: 0; background: var(--accent); }
.loadmore { align-self: center; }
.logwrap { position: relative; flex: 1; min-height: 0; display: flex; }
.logwrap .log { flex: 1; }
.jump { position: absolute; right: .8rem; bottom: .6rem; z-index: 5; background: var(--bg-elev); }

.composer {
  flex: none; display: flex; gap: .5rem; align-items: flex-end; padding: .55rem .7rem calc(.55rem + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: var(--bg-elev);
}
.composer textarea { margin: 0; resize: none; max-height: 8rem; min-height: 2.5rem; line-height: 1.35; }
.composer .btn, .composer .iconbtn { height: 2.5rem; }
.placeholder-pane { margin: auto; text-align: center; color: var(--fg-dim); padding: 2rem; }
.placeholder-pane pre { margin: 0 0 1rem; color: var(--accent); font-size: .8rem; line-height: 1.15; }

/* ── context menu, toasts, dialogs, lightbox ───────────── */
.ctxmenu {
  position: fixed; z-index: 900; min-width: 10rem; background: var(--bg-elev); border: 1px solid var(--accent);
  box-shadow: 0 6px 24px rgba(0,0,0,.45);
}
.ctxmenu button { display: block; width: 100%; text-align: left; font: inherit; color: var(--fg); background: none; border: 0; padding: .65rem .9rem; cursor: pointer; }
.ctxmenu button:hover, .ctxmenu button:focus-visible { background: var(--accent-bg); color: var(--accent); outline: none; }
.ctxmenu button.danger { color: var(--red); }
#toasts { position: fixed; z-index: 950; left: .7rem; right: .7rem; bottom: calc(5rem + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: .4rem; align-items: center; pointer-events: none; }
.toast { pointer-events: auto; max-width: 30rem; padding: .5rem .8rem; background: var(--bg-elev); border: 1px solid var(--accent); }
.toast.error { border-color: var(--red); color: var(--red); }
dialog.confirm { background: var(--bg-elev); color: var(--fg); border: 1px solid var(--accent); padding: 1.1rem 1.2rem; max-width: min(26rem, 92vw); font: inherit; }
dialog.confirm::backdrop { background: var(--scrim, rgba(0, 0, 0, .6)); }
dialog.confirm .rowline { justify-content: flex-end; margin-top: 1rem; }
.lightbox { position: fixed; inset: 0; z-index: 920; background: var(--scrim); display: grid; place-items: center; padding: 1rem; }
.lightbox img { max-width: 100%; max-height: 92dvh; display: block; border: 1px solid var(--accent); }
.lightbox .iconbtn { position: absolute; top: calc(.7rem + env(safe-area-inset-top)); right: .7rem; background: var(--bg-elev); }

/* ── auth screen ───────────────────────────────────────── */
.authwrap { min-height: 100dvh; display: grid; place-items: center; padding: 1rem; }
.authbox { width: min(26rem, 100%); }
.logo { color: var(--accent); font-size: .78rem; line-height: 1.1; margin: 0 0 .5rem; white-space: pre; overflow: hidden; }
.cursor { display: inline-block; width: .6em; height: 1em; background: var(--accent); vertical-align: -.12em; animation: blink 1.1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .cursor { animation: none; } }

/* ── replies ───────────────────────────────────────────── */
.msg:not(.pending):not(.gone) { cursor: pointer; }
.msg .quote { display: block; margin: .1rem 0 .35rem; padding: .15rem .5rem; border-left: 3px solid var(--yellow); background: var(--accent-bg); font-size: .85rem; overflow: hidden; cursor: pointer; }
.msg .quote .qwho { display: block; color: var(--yellow); font-size: .75rem; }
.msg .quote .qtext { display: block; color: var(--fg-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg.replying { outline: 1px solid var(--yellow); outline-offset: 2px; }
.msg.flash { outline: 2px solid var(--accent); outline-offset: 2px; }
.replybar { flex: none; display: flex; gap: .6rem; align-items: center; padding: .4rem .7rem; border-top: 1px solid var(--line); background: var(--bg-elev); }
.replybar .rq { flex: 1; min-width: 0; border-left: 3px solid var(--yellow); padding-left: .55rem; }
.replybar .rq span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .85rem; }
