:root {
  --bg: #121314;
  --bg-elev: #1a1b1d;
  --ink: #c4c7cb;
  --ink-soft: #9a9ea3;
  --muted: #6e7378;
  --line: #2a2c2f;
  --accent: #8b9096;
  --accent-fill: #d0d3d6;
  --accent-on: #161719;
  --ok: #6fbf8a;
  --danger: #e26d5a;
  --mine: #222426;
  --radius: 18px;
  --font-display: "Newsreader", Georgia, serif;
  --font-body: "Instrument Sans", system-ui, sans-serif;
  --app-max: 760px;
  --pad-x: clamp(0.75rem, 3vw, 1rem);
  --kb-inset: 0px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html {
  overflow: hidden;
}

body {
  font-family: var(--font-body);
  color: var(--ink);
  background: var(--bg);
  overflow: hidden;
  overscroll-behavior: none;
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
}

body.keyboard-open {
  --safe-bottom: 0px;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
  max-width: 100%;
}

button {
  cursor: pointer;
  border: 0;
  background: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.primary {
  background: var(--accent-fill);
  color: var(--accent-on);
  font-weight: 600;
  border-radius: 999px;
  padding: 0.85rem 1.4rem;
}

.primary:hover {
  background: #e4e6e8;
}

.primary.compact {
  padding: 0;
  width: 2.5rem;
  height: 2.5rem;
  min-width: 2.5rem;
  min-height: 2.5rem;
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
}

.ghost {
  border: 1px solid var(--line);
  background: var(--bg-elev);
  border-radius: 999px;
  padding: 0.7rem 1rem;
  color: var(--ink);
  flex: 0 0 auto;
}

.ghost:hover {
  background: #222426;
}

.ghost.compact {
  padding: 0.5rem 0.75rem;
  font-size: 0.85rem;
  min-height: 2.25rem;
}

.ghost.active {
  border-color: #4a4e53;
  background: #26282b;
  color: var(--accent-fill);
  font-weight: 600;
}

.field {
  display: grid;
  gap: 0.45rem;
  text-align: left;
  min-width: 0;
}

.field span {
  font-size: 0.8rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

.field input,
.composer textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  border-radius: 14px;
  padding: 0.85rem 1rem;
  outline: none;
  color: var(--ink);
}

.field input:focus,
.composer textarea:focus {
  border-color: #4a4e53;
  box-shadow: 0 0 0 3px rgba(208, 211, 214, 0.08);
}

.error {
  color: var(--danger);
  font-size: 0.9rem;
  margin: 0;
}

.hint {
  margin: 0.35rem 0 0;
  min-height: 1.1em;
  font-size: 0.78rem;
  color: var(--muted);
  overflow-wrap: anywhere;
}

.gate {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: var(--kb-inset);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(1rem, env(safe-area-inset-top)) var(--pad-x) max(1rem, var(--safe-bottom));
  overflow: auto;
  z-index: 5;
  background: var(--bg);
}

.gate[hidden] {
  display: none !important;
}

.gate-inner {
  width: min(440px, 100%);
  display: grid;
  gap: 1rem;
  text-align: center;
  min-width: 0;
}

.brand,
.brand-mark {
  font-family: var(--font-display);
  font-size: clamp(2rem, 7vw, 3.4rem);
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0;
  line-height: 1;
  color: var(--ink);
}

.gate h1 {
  margin: 0;
  font-size: clamp(1rem, 3.5vw, 1.15rem);
  font-weight: 500;
  color: var(--ink-soft);
}

.lede {
  margin: 0;
  color: var(--ink-soft);
  line-height: 1.5;
}

.name-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.5rem;
}

#join-btn {
  margin-top: 0.35rem;
  width: 100%;
}

.app {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: var(--kb-inset);
  width: 100%;
  max-width: var(--app-max);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  z-index: 1;
  background: var(--bg);
}

.app[hidden] {
  display: none !important;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex: 0 0 auto;
  padding: max(0.55rem, env(safe-area-inset-top)) var(--pad-x) 0.55rem;
  border-bottom: 1px solid var(--line);
  background: var(--bg-elev);
  min-width: 0;
}

.topbar-brand {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  min-width: 0;
  flex: 1 1 auto;
  overflow: hidden;
}

.brand-mark {
  font-size: clamp(1.2rem, 4.5vw, 1.5rem);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.presence {
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: flex;
  gap: 0.35rem;
  flex: 0 0 auto;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.filter-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem var(--pad-x);
  border-bottom: 1px solid var(--line);
  background: #161719;
  color: var(--ink-soft);
  font-size: 0.85rem;
  min-width: 0;
}

.filter-bar[hidden] {
  display: none !important;
}

.filter-bar-text {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pin-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: stretch;
  gap: 0.65rem;
  width: 100%;
  margin: 0;
  padding: 0.45rem var(--pad-x) 0.45rem 0.75rem;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: #161719;
  color: inherit;
  text-align: left;
  cursor: pointer;
  min-width: 0;
}

.pin-bar[hidden] {
  display: none !important;
}

.pin-bar:hover {
  background: #1a1c1e;
}

.pin-bar-accent {
  flex: 0 0 3px;
  width: 3px;
  border-radius: 999px;
  background: #d96a6a;
  align-self: stretch;
  min-height: 2.2rem;
}

.pin-bar-body {
  flex: 1 1 auto;
  min-width: 0;
  display: grid;
  gap: 0.1rem;
  align-content: center;
}

.pin-bar-label {
  font-size: 0.78rem;
  font-weight: 600;
  color: #d96a6a;
  line-height: 1.2;
}

.pin-bar-preview {
  font-size: 0.86rem;
  color: var(--ink-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.3;
}

.pin-bar-meta {
  flex: 0 0 auto;
  align-self: center;
  font-size: 0.72rem;
  color: var(--muted);
  padding-left: 0.25rem;
}

.feed {
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0.75rem var(--pad-x);
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  -webkit-overflow-scrolling: touch;
}

.msg {
  display: grid;
  gap: 0.3rem;
  padding: 0.65rem 0.8rem;
  border-radius: var(--radius);
  background: var(--bg-elev);
  border: 1px solid var(--line);
  max-width: 100%;
  width: 100%;
  min-width: 0;
  overflow-wrap: anywhere;
  word-break: break-word;
  color: var(--ink);
}

.msg.mine {
  background: var(--mine);
  align-self: end;
  width: min(100%, 34rem);
}

.msg.pinned-item {
  border-color: #4a3333;
  box-shadow: inset 3px 0 0 #d96a6a;
}

.msg.pin-flash {
  outline: 1px solid #6ea8d9;
  background: #1e2429;
}

.msg-meta {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  flex-wrap: wrap;
  min-width: 0;
}

.msg-name {
  font-weight: 600;
  font-size: 0.9rem;
  color: #6ea8d9;
  background: none;
  padding: 0;
  border: 0;
  cursor: pointer;
  text-align: left;
}

.msg-name:hover {
  color: #8cbcE3;
  text-decoration: underline;
}

.msg-time {
  font-size: 0.74rem;
  color: var(--muted);
  flex-shrink: 0;
}

.msg-quote {
  display: grid;
  gap: 0.1rem;
  padding: 0.35rem 0.55rem;
  margin: 0.1rem 0 0.15rem;
  border-radius: 10px;
  border-left: 3px solid #6ea8d9;
  background: rgba(110, 168, 217, 0.08);
  cursor: pointer;
  min-width: 0;
  text-align: left;
  width: 100%;
  color: inherit;
}

.msg-quote:hover {
  background: rgba(110, 168, 217, 0.14);
}

.msg-quote-name {
  font-size: 0.78rem;
  font-weight: 600;
  color: #6ea8d9;
}

.msg-quote-text {
  font-size: 0.82rem;
  color: var(--ink-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reply-bar {
  display: flex;
  align-items: stretch;
  gap: 0.55rem;
  min-width: 0;
}

.reply-bar[hidden] {
  display: none !important;
}

.reply-bar-accent {
  flex: 0 0 3px;
  border-radius: 999px;
  background: #6ea8d9;
}

.reply-bar-body {
  flex: 1 1 auto;
  min-width: 0;
  display: grid;
  gap: 0.05rem;
}

.reply-bar-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: #6ea8d9;
}

.reply-bar-preview {
  font-size: 0.82rem;
  color: var(--ink-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.emoji-panel {
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 0.25rem;
  padding: 0.15rem 0 0.2rem;
}

.emoji-panel[hidden] {
  display: none !important;
}

.emoji-btn-item {
  display: grid;
  place-items: center;
  min-height: 2.35rem;
  padding: 0.2rem 0;
  border-radius: 10px;
  font-size: 1.25rem;
  line-height: 1.3;
  background: transparent;
  border: 0;
  cursor: pointer;
}

.emoji-btn-item:hover {
  background: #222426;
}

.attach.active {
  color: var(--accent-fill);
  background: #222426;
}

.msg-text {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  line-height: 1.45;
  min-width: 0;
  color: #d2d5d9;
}

.msg-photo {
  display: block;
  max-width: 100%;
  width: min(100%, 360px);
  max-height: min(36vh, 260px);
  height: auto;
  object-fit: cover;
  border-radius: 12px;
  cursor: zoom-in;
}

.msg-actions {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 0.25rem;
  margin-top: 0.2rem;
  padding: 0.15rem 0 0.2rem;
  min-width: 0;
  min-height: 1.45rem;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.msg-actions::-webkit-scrollbar {
  display: none;
}

.msg-react {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.12rem 0.08rem;
  min-height: 1.35rem;
  min-width: 1.25rem;
  border: 0;
  border-radius: 0;
  background: none;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.35;
  opacity: 0.22;
}

.msg-react.on,
.msg-react.mine {
  opacity: 1;
}

.msg-react.mine {
  color: #8cbcE3;
}

.msg-react:hover {
  opacity: 0.85;
}

.msg-reply {
  flex: 0 0 auto;
  font-size: 0.7rem;
  font-weight: 500;
  padding: 0.12rem 0.15rem;
  min-height: 1.35rem;
  line-height: 1.35;
  border: 0;
  border-radius: 0;
  background: none;
  color: var(--muted);
  margin-left: 0.15rem;
  display: inline-flex;
  align-items: center;
}

.msg-reply:hover {
  color: #6ea8d9;
}

.msg-admin-icon {
  flex: 0 0 auto;
  font-size: 0.72rem;
  padding: 0 0.15rem;
  min-height: 0;
  line-height: 1;
  border: 0;
  background: none;
  color: var(--muted);
}

.msg-admin-icon.danger:hover,
.msg-actions .danger:hover {
  color: var(--danger);
}

.composer {
  flex: 0 0 auto;
  z-index: 4;
  padding: 0.55rem var(--pad-x) calc(0.65rem + var(--safe-bottom));
  background: transparent;
  border-top: 0;
  min-width: 0;
}

.composer-shell {
  display: grid;
  gap: 0.45rem;
  padding: 0.55rem 0.6rem;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--bg-elev);
  min-width: 0;
}

.preview {
  position: relative;
  width: fit-content;
  max-width: 100%;
}

.preview img {
  display: block;
  max-height: 88px;
  max-width: min(150px, 40vw);
  border-radius: 12px;
  object-fit: cover;
}

#preview-clear {
  position: absolute;
  top: -0.35rem;
  right: -0.35rem;
  width: 1.75rem;
  height: 1.75rem;
  min-width: 1.75rem;
  padding: 0;
  display: inline-grid;
  place-items: center;
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 0.35rem;
  min-width: 0;
}

.attach {
  width: 2.5rem;
  height: 2.5rem;
  min-width: 2.5rem;
  min-height: 2.5rem;
  display: grid;
  place-items: center;
  border-radius: 999px;
  border: 0;
  background: transparent;
  font-size: 1.2rem;
  line-height: 1;
  flex: 0 0 auto;
  user-select: none;
  color: var(--ink-soft);
}

.attach:hover {
  color: var(--accent-fill);
  background: #222426;
}

.composer textarea {
  resize: none;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 2.5rem;
  max-height: 5.5rem;
  line-height: 1.35;
  padding: 0.55rem 0.35rem;
  overflow-y: auto;
  border: 0;
  background: transparent;
  border-radius: 0;
  box-shadow: none;
}

.composer textarea:focus {
  border: 0;
  box-shadow: none;
}

.composer .hint {
  padding: 0 0.15rem;
}

.dialog {
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 0;
  background: var(--bg-elev);
  color: var(--ink);
  width: min(400px, calc(100vw - 2rem));
  max-height: 85vh;
  overflow: auto;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
}

.dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.dialog form,
.dialog .dialog-body {
  display: grid;
  gap: 0.85rem;
  padding: 1.25rem;
}

.dialog h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.45rem;
  color: var(--ink);
}

.dialog p {
  margin: 0;
  color: var(--ink-soft);
  line-height: 1.4;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.user-list {
  display: grid;
  gap: 0.35rem;
  max-height: min(45vh, 320px);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.45rem;
  background: var(--bg);
}

.user-row {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.45rem 0.5rem;
  border-radius: 8px;
  cursor: pointer;
  color: var(--ink);
}

.user-row:hover {
  background: #222426;
}

.user-row input {
  width: 1rem;
  height: 1rem;
  accent-color: var(--accent-fill);
}

.user-list-empty {
  margin: 0;
  padding: 0.75rem;
  color: var(--muted);
  text-align: center;
}

.lightbox {
  border: 0;
  padding: 0;
  max-width: min(96vw, 960px);
  max-height: 90vh;
  background: transparent;
}

.lightbox::backdrop {
  background: rgba(0, 0, 0, 0.8);
}

.lightbox img {
  display: block;
  max-width: min(96vw, 960px);
  max-height: 82vh;
  object-fit: contain;
  border-radius: 12px;
  margin: 0 auto;
}

.lightbox-close {
  position: fixed;
  top: max(0.75rem, env(safe-area-inset-top));
  right: max(0.75rem, env(safe-area-inset-right));
  width: 2.5rem;
  height: 2.5rem;
  display: inline-grid;
  place-items: center;
  z-index: 2;
}

.admin-on #admin-btn {
  border-color: var(--ok);
  color: var(--ok);
}

@media (max-width: 640px) {
  .name-row {
    grid-template-columns: 1fr;
  }

  .msg.mine {
    width: 100%;
  }

  .topbar-actions .ghost.compact {
    padding: 0.4rem 0.55rem;
    font-size: 0.8rem;
  }

  .emoji-panel {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }
}

@media (max-height: 500px) {
  .presence {
    display: none;
  }

  .composer textarea {
    max-height: 3.8rem;
  }
}
