:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #16191f;
  --muted: #646b78;
  --accent: #2f6bed;
  --accent-soft: #e6eefe;
  --danger: #d93a3f;
  --danger-soft: #fdecec;
  --warn: #d97706;
  --ok: #16a34a;
  --p1: #b4bac4;
  --p2: #7d9cc0;
  --p3: transparent;
  --p4: #e8890c;
  --p5: #e5484d;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1f232b;
    --border: #2a2f39;
    --text: #e8eaee;
    --muted: #949bab;
    --accent: #5b8cff;
    --accent-soft: #1c2741;
    --danger: #f2555a;
    --danger-soft: #3a1c1e;
    --p1: #4b515c;
    --p2: #56708f;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }

html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}

svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.muted { color: var(--muted); }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 17px; letter-spacing: -.01em; }
.brand-mark { display: grid; place-items: center; width: 30px; height: 30px; border-radius: 8px; background: var(--accent); color: #fff; }
.brand-mark svg { width: 17px; height: 17px; }

.primary {
  border: 0; border-radius: 9px; padding: 10px 16px;
  background: var(--accent); color: #fff; font-weight: 600;
}
.primary:disabled { opacity: .6; cursor: default; }
.ghost {
  border: 1px solid var(--border); border-radius: 9px; padding: 8px 14px;
  background: var(--surface);
}
.ghost:hover { background: var(--surface-2); }
.wide { display: block; width: 100%; margin-top: 12px; }

.icon-btn {
  display: grid; place-items: center; width: 36px; height: 36px;
  border: 1px solid transparent; border-radius: 9px; background: transparent; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: .45; cursor: default; }
.icon-btn .off { display: none; }
.icon-btn[data-on="false"] .on { display: none; }
.icon-btn[data-on="false"] .off { display: block; }
.icon-btn[data-on="true"] { color: var(--accent); }

/* ---------- login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 360px; padding: 28px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 14px;
}
.login-card h1 { margin: 10px 0 -8px; font-size: 22px; letter-spacing: -.02em; }
.login-card p { margin: 0; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 550; color: var(--muted); }
.login-card input {
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 9px;
  background: var(--bg); color: var(--text); font-size: 15px;
}
.login-card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.form-error { color: var(--danger); background: var(--danger-soft); padding: 8px 12px; border-radius: 8px; font-size: 14px; }

/* ---------- app shell ---------- */
.app { min-height: 100%; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) 10px max(20px, env(safe-area-inset-left)); background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.spacer { flex: 1; }
.user { color: var(--muted); font-size: 14px; }

.status { display: flex; align-items: center; gap: 7px; font-size: 13px; color: var(--muted); padding: 3px 10px; border-radius: 99px; background: var(--surface-2); }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.status[data-state="live"] .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 22%, transparent); }
.status[data-state="offline"] .dot { background: var(--danger); }

.layout {
  flex: 1; width: 100%; max-width: 1080px; margin: 0 auto;
  display: grid; grid-template-columns: 220px minmax(0, 1fr); gap: 28px; padding: 24px 20px 48px;
  align-content: start; /* short feeds stay at the top instead of stretching rows */
}

/* ---------- topics ---------- */
.topics { position: sticky; top: 76px; align-self: start; }
.topics-label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 10px 8px; }
.topics ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.topic-btn {
  width: 100%; display: flex; align-items: center; gap: 8px;
  padding: 7px 10px; border: 0; border-radius: 8px; background: transparent; text-align: left; color: var(--text);
}
.topic-btn:hover { background: var(--surface-2); }
.topic-btn[aria-current="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.topic-btn .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topic-btn .count { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.topic-btn[aria-current="true"] .count { color: inherit; }
.topic-btn .hash { color: var(--muted); }
.topic-btn .new {
  min-width: 18px; padding: 0 6px; border-radius: 99px; background: var(--accent); color: #fff;
  font-size: 11.5px; font-weight: 700; line-height: 18px; text-align: center; font-variant-numeric: tabular-nums;
}
.topic-btn.pulse { animation: pulse 1.2s ease-out; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 55%, transparent); background: var(--accent-soft); }
  100% { box-shadow: 0 0 0 10px transparent; }
}

/* Desktop sidebar: its own scroll and a filter once there are many topics. */
.topics { max-height: calc(100vh - 100px); overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; }
.topic-filter { width: auto; max-width: none; margin: 0 0 8px; }
.topic-filter input { padding: 6px 0; font-size: 14px; }
.topics-row { position: relative; }
.topics-more { display: none; }
.topics-empty { padding: 6px 10px; color: var(--muted); font-size: 13px; }

/* ---------- feed ---------- */
.feed-head { display: flex; align-items: center; gap: 16px; margin-bottom: 16px; }
.feed-head h2 { flex: 1; margin: 0; font-size: 20px; letter-spacing: -.02em; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search {
  display: flex; align-items: center; gap: 8px; width: 240px; max-width: 50%;
  padding: 0 12px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface); color: var(--muted);
}
.search:focus-within { border-color: var(--accent); }
.search svg { width: 16px; height: 16px; }
.search input { flex: 1; min-width: 0; border: 0; background: transparent; padding: 8px 0; outline: none; color: var(--text); }

.messages { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.msg {
  position: relative; padding: 14px 16px 12px 18px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden;
}
.msg::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--prio, transparent); }
.msg[data-p="1"] { --prio: var(--p1); }
.msg[data-p="2"] { --prio: var(--p2); }
.msg[data-p="4"] { --prio: var(--p4); }
.msg[data-p="5"] { --prio: var(--p5); background: color-mix(in srgb, var(--p5) 6%, var(--surface)); border-color: color-mix(in srgb, var(--p5) 35%, var(--border)); }
/* New message: slides in and glows in its priority colour for 3 s. The
   animation has no end state, so the card returns to its own background. */
.msg { --hl: var(--accent); }
.msg[data-p="4"] { --hl: var(--p4); }
.msg[data-p="5"] { --hl: var(--p5); }
.msg.fresh { animation: fresh 3s ease-out; }
@keyframes fresh {
  0% { opacity: 0; transform: translateY(-10px) scale(.985); box-shadow: 0 0 0 2px var(--hl); background: color-mix(in srgb, var(--hl) 18%, var(--surface)); }
  8% { opacity: 1; transform: none; }
  60% { box-shadow: 0 0 0 2px var(--hl); background: color-mix(in srgb, var(--hl) 10%, var(--surface)); }
}
@media (prefers-reduced-motion: reduce) {
  @keyframes fresh { 0%, 60% { box-shadow: 0 0 0 2px var(--hl); background: color-mix(in srgb, var(--hl) 12%, var(--surface)); } }
}

.msg-head { display: flex; align-items: baseline; gap: 10px; }
.msg-title { flex: 1; min-width: 0; margin: 0; font-size: 15px; font-weight: 620; overflow-wrap: anywhere; }
.msg-time { flex: none; font-size: 12.5px; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.msg-body { margin: 4px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; color: var(--text); }
.msg-head + .msg-body { margin-top: 2px; }
.msg:not(:has(.msg-title)) .msg-body { margin-top: 0; }

.msg-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 10px; font-size: 12.5px; color: var(--muted); }
.chip { display: inline-flex; align-items: center; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); border: 0; font-size: 12.5px; }
button.chip:hover { color: var(--accent); }
.chip.topic { font-weight: 550; }
.chip.prio { color: #fff; font-weight: 600; }
.msg[data-p="1"] .chip.prio, .msg[data-p="2"] .chip.prio { background: var(--prio); }
.msg[data-p="4"] .chip.prio { background: var(--p4); }
.msg[data-p="5"] .chip.prio { background: var(--p5); }
.meta-sep { flex: 1; }
.msg-link { display: inline-flex; align-items: center; gap: 4px; color: var(--accent); text-decoration: none; font-weight: 550; }
.msg-link:hover { text-decoration: underline; }
.msg-link svg { width: 14px; height: 14px; }

.empty { text-align: center; padding: 56px 16px; color: var(--muted); border: 1px dashed var(--border); border-radius: var(--radius); }
.empty svg { width: 32px; height: 32px; opacity: .6; }
.empty p { margin: 10px 0 14px; font-size: 15px; }
.empty code { display: inline-block; max-width: 100%; overflow-x: auto; white-space: nowrap; font: 12.5px var(--mono); background: var(--surface-2); padding: 8px 12px; border-radius: 8px; color: var(--text); }
.empty code:empty { display: none; }

/* ---------- banner (install / enable push) ---------- */
.banner {
  display: flex; align-items: center; gap: 12px; margin-bottom: 18px; padding: 12px 12px 12px 14px;
  background: var(--accent-soft); border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent); border-radius: var(--radius);
}
.banner-icon { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 9px; background: var(--accent); color: #fff; flex: none; }
.banner-text { flex: 1; min-width: 0; display: flex; flex-direction: column; font-size: 14px; line-height: 1.4; }
.banner-text span { color: var(--muted); }
.banner-text svg { width: 15px; height: 15px; vertical-align: -2px; }
.primary.small { padding: 7px 12px; font-size: 14px; flex: none; }

/* ---------- deleting ---------- */
.msg-del { width: 28px; height: 28px; margin: -4px -6px -4px auto; border-radius: 7px; }
.msg-del svg { width: 15px; height: 15px; }
.msg-del:hover, #clear:hover { color: var(--danger); background: var(--danger-soft); }
@media (hover: hover) {
  .msg .msg-del { opacity: 0; transition: opacity .12s; }
  .msg:hover .msg-del, .msg .msg-del:focus-visible { opacity: 1; }
}
.msg.removing { animation: removing .18s ease-in forwards; }
@keyframes removing { to { opacity: 0; transform: translateX(12px); } }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 20; display: flex; align-items: center; gap: 14px; max-width: calc(100% - 32px);
  padding: 10px 10px 10px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-size: 14px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .18);
}
#toast-text { white-space: nowrap; }
.toast-btn { border: 0; border-radius: 8px; padding: 6px 10px; background: transparent; color: var(--accent); font-weight: 650; }
.toast-btn:hover { background: color-mix(in srgb, var(--bg) 12%, transparent); }
@media (prefers-color-scheme: dark) { .toast-btn { color: #2f6bed; } }

/* ---------- "new message" pill when scrolled down ---------- */
.new-pill {
  position: fixed; z-index: 6; left: 50%; transform: translateX(-50%);
  top: calc(64px + env(safe-area-inset-top));
  display: inline-flex; align-items: center; gap: 6px; padding: 7px 14px 7px 11px;
  border: 0; border-radius: 99px; background: var(--accent); color: #fff; font-size: 14px; font-weight: 600;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .18); animation: pill-in .2s ease-out;
}
.new-pill svg { width: 16px; height: 16px; }
.new-pill span { white-space: nowrap; }
@keyframes pill-in { from { opacity: 0; transform: translate(-50%, -8px); } }

/* ---------- topics sheet (phones) ---------- */
.sheet { position: fixed; inset: 0; z-index: 30; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .4); animation: fade-in .2s ease-out; }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 78vh; max-height: 78dvh;
  display: flex; flex-direction: column; gap: 10px;
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--surface); border-radius: 18px 18px 0 0; box-shadow: 0 -8px 30px rgba(0, 0, 0, .2);
  animation: sheet-in .24s cubic-bezier(.2, .8, .2, 1);
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; font-size: 17px; }
.sheet-search { width: 100%; max-width: none; }
.sheet-list { list-style: none; margin: 0 -8px; padding: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.sheet-list .topic-btn { padding: 11px 10px; border-radius: 10px; border: 0; background: transparent; }
.sheet-list .topic-btn[aria-current="true"] { background: var(--accent-soft); }
.sheet-list .when { font-size: 12px; color: var(--muted); white-space: nowrap; }
@keyframes sheet-in { from { transform: translateY(100%); } }
@keyframes fade-in { from { opacity: 0; } }
html.sheet-open, html.sheet-open body { overflow: hidden; }

/* ---------- narrow screens ---------- */
@media (max-width: 760px) {
  .topbar { padding: calc(8px + env(safe-area-inset-top)) 16px 8px; gap: 8px; }
  .banner { flex-wrap: wrap; }
  .banner-text { flex-basis: calc(100% - 100px); }
  .banner .primary.small { order: 4; width: 100%; }
  .user { display: none; }
  .layout { grid-template-columns: minmax(0, 1fr); gap: 14px; padding: 14px 16px calc(40px + env(safe-area-inset-bottom)); }
  .topics { position: static; max-height: none; overflow: visible; }
  .topics-label, .topic-filter { display: none !important; }
  .topics-row { display: flex; align-items: center; gap: 6px; }
  .topics-row ul {
    flex: 1; min-width: 0; position: relative;
    flex-direction: row; overflow-x: auto; gap: 6px; padding: 2px 0 4px; scrollbar-width: none;
    -webkit-overflow-scrolling: touch; scroll-padding: 0 24px;
  }
  .topics-row ul::-webkit-scrollbar { display: none; }
  .topics-row.overflow ul { mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent); -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent); }
  .topics-more { display: grid; flex: none; border: 1px solid var(--border); background: var(--surface); border-radius: 99px; width: 38px; height: 34px; }
  .sheet-list .topic-btn { font-size: 16px; }
  input { font-size: 16px !important; } /* below 16px iOS zooms the page on focus */
  .topics li { flex: none; }
  .topic-btn { border: 1px solid var(--border); background: var(--surface); border-radius: 99px; padding: 5px 12px; }
  .topic-btn[aria-current="true"] { border-color: transparent; }
  .feed-head { flex-wrap: wrap; gap: 10px; }
  .feed-head h2 { flex-basis: calc(100% - 50px); }
  .search { width: 100%; max-width: none; }
}
