:root {
  --bg: #ffffff;
  --panel: #f4f6f5;
  --line: #dde3e0;
  --text: #17211e;
  --muted: #5f6f69;
  --accent: #1f6f5c;
  --accent-text: #ffffff;
  --mine: #d9efe7;
  --theirs: #eef1f0;
  --danger: #b3261e;
  --read: #1a73c8;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121816;
    --panel: #1a2320;
    --line: #2b3733;
    --text: #e6ece9;
    --muted: #9aaaa4;
    --accent: #4fb89d;
    --accent-text: #0c1512;
    --mine: #1f4a3e;
    --theirs: #232e2a;
    --danger: #f2867f;
    --read: #6db4f2;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  overscroll-behavior: none;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 8px;
  padding: 8px 14px;
}

button:disabled {
  opacity: 0.5;
  cursor: default;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

button.danger {
  color: var(--danger);
}

button.icon {
  border: 0;
  background: none;
  padding: 6px 8px;
  font-size: 20px;
  line-height: 1;
  border-radius: 8px;
}

button.icon:hover {
  background: var(--line);
}

input[type='text'],
input[type='email'],
input[type='number'],
select,
textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

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

.small {
  font-size: 13px;
}

/* Layout: list and chat side by side on wide screens, one at a time on phones. */

.layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  height: 100dvh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--line);
  background: var(--panel);
}

.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  padding-top: calc(10px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  min-height: 60px;
}

.bar .grow {
  flex: 1;
  min-width: 0;
}

.bar .title {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.back {
  display: none;
}

.offline {
  padding: 6px 12px;
  background: var(--danger);
  color: #fff;
  font-size: 13px;
  text-align: center;
}

.list {
  flex: 1;
  overflow-y: auto;
  padding-bottom: env(safe-area-inset-bottom);
}

.list h2 {
  margin: 14px 12px 4px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 12px;
  border: 0;
  border-radius: 0;
  background: none;
  text-align: left;
}

.item:hover,
.item.current {
  background: var(--line);
}

.item .grow {
  flex: 1;
  min-width: 0;
}

.item .name,
.item .preview {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item .preview {
  font-size: 13px;
  color: var(--muted);
}

.badge {
  min-width: 22px;
  padding: 1px 7px;
  border-radius: 11px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 12px;
  font-weight: 700;
  text-align: center;
}

.avatar {
  position: relative;
  flex: none;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  color: #fff;
  font-size: 14px;
  font-weight: 700;
}

.avatar.small {
  width: 26px;
  height: 26px;
  font-size: 10px;
}

.avatar.online::after {
  content: '';
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: #2fbf71;
  border: 2px solid var(--panel);
}

/* Messages */

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.empty {
  margin: auto;
  padding: 24px;
  text-align: center;
  color: var(--muted);
}

.day {
  align-self: center;
  margin: 10px 0 6px;
  padding: 2px 10px;
  border-radius: 10px;
  background: var(--panel);
  color: var(--muted);
  font-size: 12px;
}

.msg {
  display: flex;
  gap: 8px;
  max-width: min(78%, 640px);
  align-self: flex-start;
}

.msg.mine {
  align-self: flex-end;
}

.msg .gutter {
  flex: none;
  width: 26px;
}

.bubble {
  padding: 7px 11px;
  border-radius: 14px;
  background: var(--theirs);
  min-width: 0;
}

.msg.mine .bubble {
  background: var(--mine);
}

.msg.pending .bubble {
  opacity: 0.6;
}

.bubble .sender {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
}

.bubble .body {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.bubble .meta {
  margin-top: 1px;
  font-size: 11px;
  color: var(--muted);
  text-align: right;
}

.bubble .meta .read {
  color: var(--read);
  font-weight: 700;
}

.bubble a {
  color: inherit;
}

.more {
  align-self: center;
  margin-bottom: 8px;
  font-size: 13px;
}

.typing {
  min-height: 20px;
  padding: 0 14px;
  font-size: 13px;
  color: var(--muted);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 140px;
  border-radius: 18px;
}

/* Dialogs and forms */

dialog {
  width: min(520px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--bg);
  color: var(--text);
}

dialog::backdrop {
  background: rgb(0 0 0 / 45%);
}

dialog header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: var(--bg);
}

dialog header h1 {
  margin: 0;
  font-size: 17px;
}

dialog .content {
  padding: 4px 16px 16px;
}

dialog h2 {
  margin: 18px 0 8px;
  font-size: 14px;
}

.field {
  display: block;
  margin: 10px 0;
}

.field > span {
  display: block;
  margin-bottom: 4px;
  font-size: 13px;
  color: var(--muted);
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 8px 0;
}

.row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
}

.row .grow {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.qr {
  display: block;
  width: 220px;
  height: 220px;
  margin: 10px auto;
  border-radius: 8px;
  image-rendering: pixelated;
  background: #fff;
}

.error {
  color: var(--danger);
}

.join {
  max-width: 420px;
  margin: 0 auto;
  padding: calc(40px + env(safe-area-inset-top)) 20px 40px;
}

.join h1 {
  font-size: 24px;
}

.join ol {
  padding-left: 20px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(80px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  z-index: 10;
}

@media (max-width: 720px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .chat {
    display: none;
  }

  .show-chat .sidebar {
    display: none;
  }

  .show-chat .chat {
    display: flex;
  }

  .back {
    display: block;
  }
}
