:root {
  color-scheme: light;
  --surface: #f7f8fa;
  --raised: #ffffff;
  --wash: #eef1f5;
  --line: #e2e6ec;
  --text: #161a22;
  --muted: #5c6675;
  --accent: #a35f10;
  --accent-ink: #ffffff;
  --accent-wash: #fef6ea;
  --danger: #b3261e;
  --lamp-fill: var(--raised);
  --lamp-halo: none;
  --lift: 0 1px 2px rgba(22, 26, 34, 0.06);

  --face-text: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --face-code: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

  --caption: 0.75rem;
  --fine: 0.8125rem;
  --small: 0.875rem;
  --body: 1rem;
  --title: 1.25rem;
  --display: 1.75rem;

  --radius: 10px;
  --radius-tight: 6px;
  --radius-pill: 999px;
  --gap: 0.75rem;
  --track: 0.06em;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface: #141821;
    --raised: #1b202b;
    --wash: #222836;
    --line: #2a3140;
    --text: #e6eaf1;
    --muted: #8b95a6;
    --accent: #e0a04a;
    --accent-ink: #1b1206;
    --accent-wash: #2a2113;
    --danger: #ff8a8a;
    --lamp-fill: var(--accent);
    --lamp-halo: 0 0 0.55rem var(--accent-wash);
    --lift: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface: #141821;
  --raised: #1b202b;
  --wash: #222836;
  --line: #2a3140;
  --text: #e6eaf1;
  --muted: #8b95a6;
  --accent: #e0a04a;
  --accent-ink: #1b1206;
  --accent-wash: #2a2113;
  --danger: #ff8a8a;
  --lamp-fill: var(--accent);
  --lamp-halo: 0 0 0.55rem var(--accent-wash);
  --lift: 0 1px 2px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--surface);
  color: var(--text);
  font: 400 var(--body)/1.55 var(--face-text);
  -webkit-text-size-adjust: 100%;
}

main {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

h1, h2, h3 { font-size: var(--body); font-weight: 600; margin: 0; }

a { color: var(--accent); text-underline-offset: 0.2em; }

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

.wordmark {
  font-family: var(--face-code);
  font-weight: 500;
  letter-spacing: 0.16em;
  text-transform: lowercase;
}

.lede { color: var(--muted); font-size: var(--small); margin: 0.6rem 0 2rem; }
.aside { color: var(--muted); font-size: var(--small); margin-top: 2rem; }
.hint { color: var(--muted); font-size: var(--fine); margin: 0; }

.status {
  color: var(--muted);
  font-size: var(--fine);
  margin: 0;
  min-height: 1.4em;
}

.status:empty { min-height: 0; }
.status.error { color: var(--danger); }

.stack { display: flex; flex-direction: column; gap: var(--gap); }
.field { display: flex; flex-direction: column; gap: 0.3rem; }

label {
  color: var(--muted);
  font-size: var(--caption);
  letter-spacing: var(--track);
  text-transform: uppercase;
}

input, textarea, select, button {
  font: inherit;
  color: var(--text);
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-tight);
  padding: 0.55rem 0.7rem;
}

textarea { resize: vertical; }

input:disabled, textarea:disabled, select:disabled, button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

button { background: var(--wash); cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--muted); }

button[type="submit"], #invite-copy {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

button[type="submit"]:hover:not(:disabled), #invite-copy:hover:not(:disabled) {
  border-color: var(--accent);
  filter: brightness(1.08);
}

#login-username, #signup-username {
  font-family: var(--face-code);
  font-size: var(--small);
}

.theme-toggle {
  position: fixed;
  inset-block-start: 0.7rem;
  inset-inline-end: 0.7rem;
  z-index: 2;
  display: grid;
  place-items: center;
  width: 2rem;
  height: 2rem;
  padding: 0;
  border-radius: var(--radius-pill);
  background: var(--raised);
}

.lamp {
  width: 0.6rem;
  height: 0.6rem;
  border: 1px solid var(--accent);
  border-radius: var(--radius-pill);
  background: var(--lamp-fill);
  box-shadow: var(--lamp-halo);
}

.offscreen {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.gate {
  margin: auto;
  padding: 3.5rem 0 4rem;
  width: min(23rem, calc(100% - 2.5rem));
}

.gate .wordmark { font-size: var(--display); }

.invite-auth { display: flex; flex-direction: column; gap: var(--gap); }
.invite-auth[hidden] { display: none; }
.invite-auth h2 { margin-top: 1rem; }

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 17rem) minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  flex: 1;
  height: 100dvh;
  overflow: hidden;
}

.topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 0.9rem;
  padding: 0.6rem 1rem;
  padding-inline-end: 3.5rem;
  border-bottom: 1px solid var(--line);
}

.topbar .wordmark { font-size: var(--small); }
.topbar button { padding: 0.3rem 0.7rem; font-size: var(--fine); }

.whoami {
  color: var(--muted);
  font-family: var(--face-code);
  font-size: var(--fine);
  font-weight: 500;
}

.link-state {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin: 0 0 0 auto;
  color: var(--muted);
  font-size: var(--caption);
}

.link-state::before {
  content: "";
  width: 0.4rem;
  height: 0.4rem;
  border-radius: var(--radius-pill);
  background: currentColor;
}

.roster {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  border-right: 1px solid var(--line);
  padding: 0.85rem;
  overflow-y: auto;
  min-height: 0;
}

.roster form button { width: 100%; }

.invite-result {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.7rem;
  background: var(--wash);
  border-radius: var(--radius);
}

.invite-result[hidden] { display: none; }

.invite-line {
  margin: 0;
  color: var(--muted);
  font-family: var(--face-code);
  font-size: var(--fine);
  line-height: 1.5;
  overflow-wrap: anywhere;
}

#invite-secret { color: var(--accent); font-weight: 600; }

.invite-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.invite-expiry { color: var(--muted); font-size: var(--caption); }
.invite-foot button { padding: 0.25rem 0.8rem; font-size: var(--fine); }

.rooms {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.rooms-empty {
  padding: 0.4rem 0.5rem;
  color: var(--muted);
  font-size: var(--fine);
}

.room-link {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.4rem 0.5rem;
  text-align: left;
  font-size: var(--small);
  background: none;
  border-color: transparent;
  border-radius: var(--radius-tight);
}

.room-link:hover:not(:disabled) { background: var(--wash); border-color: transparent; }

.room-link[aria-current="true"] {
  background: var(--wash);
  border-color: transparent;
  font-weight: 600;
}

.unread {
  margin-left: auto;
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: var(--radius-pill);
  font-size: var(--caption);
  font-weight: 600;
  padding: 0 0.4rem;
}

.room {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-height: 0;
}

.roombar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--line);
}

#room-title { font-size: var(--small); }

.fingerprint {
  color: var(--muted);
  font-family: var(--face-code);
  font-size: var(--caption);
  letter-spacing: 0.08em;
}

#auto-delete {
  margin-left: auto;
  padding: 0.25rem 0.5rem;
  color: var(--muted);
  font-size: var(--fine);
  background: none;
  border-color: transparent;
}

#auto-delete:hover:not(:disabled) { border-color: var(--line); }

.messages {
  list-style: none;
  margin: 0;
  padding: 1rem;
  overflow-y: auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}

#message-list:empty::after {
  content: "No conversation selected";
  margin: auto;
  color: var(--muted);
  font-size: var(--small);
}

.message { max-width: min(34rem, 85%); }
.message.mine { margin-left: auto; }
.message.pending { opacity: 0.55; }

.meta {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  padding: 0 0.2rem;
  color: var(--muted);
  font-size: var(--caption);
}

.author { font-weight: 600; }
.stamp { font-variant-numeric: tabular-nums; }

.remove {
  margin-left: auto;
  padding: 0;
  border: none;
  background: none;
  color: var(--muted);
  font-size: var(--caption);
  opacity: 0;
}

.message:hover .remove, .remove:focus-visible { opacity: 1; }
.remove:hover:not(:disabled) { color: var(--danger); border-color: transparent; }

.body {
  margin: 0.15rem 0 0;
  padding: 0.45rem 0.7rem;
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message.mine .body { background: var(--wash); }
.message.failed .body { border-color: var(--danger); color: var(--danger); }

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.35rem 0.6rem;
  align-items: end;
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--line);
}

.composer .status { grid-column: 1 / -1; }

@media (max-width: 46rem) {
  .workspace { display: flex; flex-direction: column; }

  .topbar { flex-wrap: wrap; gap: 0.4rem 0.75rem; }

  .roster {
    flex: none;
    max-height: 38dvh;
    border-right: none;
    border-bottom: 1px solid var(--line);
  }

  .rooms {
    order: -1;
    flex-direction: row;
    gap: 0.35rem;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    padding-bottom: 0.2rem;
  }

  .room-link {
    width: auto;
    background: var(--wash);
    border-radius: var(--radius-pill);
    white-space: nowrap;
  }

  .rooms-empty { white-space: nowrap; }

  .room { flex: 1; min-height: 0; }

  .message { max-width: 100%; }
}

@media (prefers-reduced-motion: no-preference) {
  .room-link, button, input, textarea, select {
    transition: background-color 120ms ease, border-color 120ms ease, opacity 120ms ease;
  }
}
