:root {
  --bg: #ffffff;
  --bg-alt: #f8f8f6;
  --fg: #1d1c1d;
  --fg-soft: #454245;
  --muted: #8a8a8a;
  --border: #e8e8e6;
  --accent: #2563eb;
  --focus: #b8b8b3;
  --hover: #f4f4f2;
  --mention-bg: #fff7d6;
  --mention-fg: #6b4f00;
  --mention-me-bg: #ffe680;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a19;
    --bg-alt: #141413;
    --fg: #f3f3f0;
    --fg-soft: #cfcfcb;
    --muted: #8d8d8a;
    --border: #2a2a28;
    --accent: #7aa2ff;
    --focus: #6e6e6a;
    --hover: #222220;
    --mention-bg: #3a2f00;
    --mention-fg: #ffd866;
    --mention-me-bg: #7a5d00;
  }
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
code { font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace; font-size: 12.5px; background: var(--bg-alt); padding: 1px 5px; border: 1px solid var(--border); }

/* ---------- Landing ---------- */
body.landing { display: flex; align-items: center; justify-content: center; padding: 24px; }
body.landing main { width: 100%; max-width: 380px; text-align: center; }
body.landing h1 { font-size: 56px; font-weight: 700; letter-spacing: -0.04em; margin: 0 0 8px; }
.tagline { color: var(--muted); margin: 0 0 28px; }
#enter { display: flex; flex-direction: column; gap: 8px; }
#enter input {
  padding: 12px 14px;
  border: 1px solid var(--border);
  background: var(--bg); color: var(--fg); font: inherit;
}
#enter button { padding: 12px 14px; }
body.landing .hint { color: var(--muted); font-size: 12px; margin-top: 14px; }

/* ---------- Buttons / inputs ---------- */
button {
  padding: 10px 14px; border: none;
  background: var(--accent); color: #fff; font: inherit; font-weight: 500; cursor: pointer;
}
button:hover { filter: brightness(1.05); }
input:focus-visible, button:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }

/* ---------- Room ---------- */
body.room {
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
}

header {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.back { color: var(--muted); text-decoration: none; font-size: 18px; line-height: 1; padding: 4px 6px; }
.back:hover { background: var(--hover); }
.channel { flex: 1; display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.channel h1 {
  font-size: 15px; font-weight: 700; margin: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.channel h1::before { content: '#'; color: var(--muted); margin-right: 2px; font-weight: 500; }
.users { font-size: 11px; color: var(--muted); }
.me {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border);
  padding: 4px 10px 4px 6px;
  font-size: 12px;
  font-weight: 500;
  display: inline-flex; align-items: center; gap: 6px;
}
.me::before {
  content: '';
  width: 8px; height: 8px;
  background: var(--mecolor, var(--accent));
  display: inline-block;
}
.me.has-icon::before { display: none; }
.me:hover { background: var(--hover); }

/* ---------- Message feed ---------- */
#messages {
  flex: 1; overflow-y: auto;
  padding: 14px 0 18px;
  display: flex; flex-direction: column;
}

.msggroup {
  display: grid;
  grid-template-columns: 44px 1fr;
  padding: 3px 16px 1px;
  margin-top: 4px;
}
.msggroup:hover { background: var(--hover); }
.avatar {
  width: 30px; height: 30px;
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  user-select: none;
}
.avatar.icon {
  background: transparent;
  font-size: 22px;
  line-height: 1;
}
.grouptext { min-width: 0; }
.head {
  display: flex; align-items: baseline; gap: 6px;
  margin-bottom: 0;
  line-height: 1.25;
}
.head .who { font-weight: 700; color: var(--fg); font-size: 14px; }
.head .ts { font-size: 10.5px; color: var(--muted); font-variant-numeric: tabular-nums; }

.lines { display: flex; flex-direction: column; }
.line {
  position: relative;
  padding: 0;
  display: grid;
  grid-template-columns: 1fr;
}
.line .ts {
  position: absolute;
  left: -44px;
  width: 38px;
  text-align: right;
  font-size: 10px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  opacity: 0;
  transition: opacity .12s;
  top: 2px;
}
.msggroup:hover .line .ts { opacity: 1; }
.body {
  color: var(--fg-soft);
  word-break: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.mention {
  background: var(--mention-bg);
  color: var(--mention-fg);
  padding: 0 4px;
  font-weight: 600;
}
.mention.me { background: var(--mention-me-bg); }
.line.hasmention { box-shadow: inset 2px 0 0 var(--mention-me-bg); padding-left: 6px; margin-left: -6px; }

.system {
  text-align: center;
  color: var(--muted);
  font-size: 11.5px;
  padding: 6px 16px;
}

.daydivider {
  display: flex; align-items: center;
  margin: 14px 16px 8px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  text-transform: lowercase;
}
.daydivider::before, .daydivider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}
.daydivider span { padding: 0 10px; }

/* ---------- Composer ---------- */
footer {
  padding: 10px 16px 14px;
  background: var(--bg);
}
#composer {
  display: flex;
  align-items: center;
  border: 1px solid var(--border);
  background: var(--bg);
  padding: 4px 4px 4px 12px;
  transition: border-color .12s, box-shadow .12s;
}
#composer:focus-within {
  border-color: var(--focus);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 35%, transparent);
}
#composer input {
  flex: 1;
  border: none; outline: none;
  background: transparent;
  color: var(--fg);
  font: inherit;
  padding: 10px 4px;
}
#composer button {
  width: 34px; height: 34px;
  padding: 0;
  font-size: 16px;
  line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
}
.hint {
  color: var(--muted);
  font-size: 11px;
  padding: 6px 2px 0;
}

/* ---------- Dialog ---------- */
dialog {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  padding: 24px;
  width: min(90vw, 320px);
  box-shadow: 0 12px 40px rgba(0,0,0,.18);
}
dialog::backdrop { background: rgba(0,0,0,.45); backdrop-filter: blur(4px); }
#nameform { display: flex; flex-direction: column; gap: 12px; }
#nameform h2 { margin: 0 0 6px; font-size: 17px; font-weight: 600; }
#nameform input {
  padding: 10px 14px;
  border: 1px solid var(--border);
  background: var(--bg); color: var(--fg); font: inherit;
}

/* ---------- Responsive ---------- */
@media (max-width: 540px) {
  .msggroup { padding: 3px 12px 1px; }
  .line .ts { display: none; }
  footer { padding: 8px 10px 12px; }
}
