:root {
  color-scheme: dark;
  --bg: #111316;
  --surface: #191c21;
  --edge: #343a44;
  --text: #f0f2f5;
  --muted: #b2bac6;
  --accent: #bedaff;
  --ink: #10243e;
  --mine: #283e57;
  --other: #272c34;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #eef1f5;
  --surface: #fff;
  --edge: #d2d9e2;
  --text: #161c26;
  --muted: #4f5c6d;
  --accent: #254f80;
  --ink: #fff;
  --mine: #deecff;
  --other: #edf0f5;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, sans-serif;
}

main {
  max-width: 1080px;
  height: min(900px, calc(100dvh - 40px));
  margin: 20px auto;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--edge);
  border-radius: 18px;
  background: var(--surface);
}

.topbar, .thread-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--edge);
}

.topbar { justify-content: space-between; }
h1, h2, p { margin: 0; }
h1 { display: flex; align-items: center; gap: 9px; font-size: 1rem; }
h1 span { color: var(--accent); font-size: 2rem; line-height: 1; }
h2 { font-size: 1.25rem; }

button, input {
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--edge);
  border-radius: 9px;
  background: var(--surface);
  color: inherit;
  font: inherit;
}

button { cursor: pointer; }
button:disabled { opacity: .55; cursor: default; }
button:hover:not(:disabled),
button[aria-current="page"] { border-color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.primary {
  color: var(--ink);
  background: var(--accent);
  border-color: transparent;
  font-weight: 650;
}

.login { display: grid; flex: 1; place-items: center; padding: 20px; }
.login form { width: min(100%, 360px); }
form { display: grid; gap: 8px; }
small, .placeholder, #status { color: var(--muted); }
#status { padding: 8px 16px; overflow-wrap: anywhere; }
#status:empty { display: none; }

#app {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(260px, 32%) minmax(0, 1fr);
}

aside {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--edge);
}

nav {
  display: flex;
  gap: 6px;
  padding: 10px;
  border-bottom: 1px solid var(--edge);
}

nav button { flex: 1; padding-inline: 4px; }
.panel { overflow: auto; padding: 12px; }
#result, #logout, #profile-form { margin-top: 12px; }

.contact {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
  text-align: left;
}

.contact span:last-child, .peer { min-width: 0; overflow-wrap: anywhere; }
.contact small, .peer small { display: block; }

.avatar {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  border-radius: 50%;
  background: var(--mine);
  font-weight: 700;
}

#conversation, #thread {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#thread, .peer { flex: 1; }
.placeholder { margin: auto; padding: 20px; text-align: center; }
#history { flex: 1; overflow: auto; padding: 16px; }
#older { display: block; margin: 0 auto; }

#messages {
  display: grid;
  gap: 8px;
  margin: 10px 0 0;
  padding: 0;
  list-style: none;
}

.bubble {
  justify-self: start;
  max-width: 86%;
  min-width: 0;
  padding: 8px 12px;
  border-radius: 12px;
  background: var(--other);
  overflow-wrap: anywhere;
}

.bubble.mine { justify-self: end; background: var(--mine); }
.bubble p { white-space: pre-wrap; }

.bubble blockquote {
  max-height: 4.5em;
  overflow: hidden;
  margin: 0 0 5px;
  padding-left: 8px;
  border-left: 2px solid var(--accent);
}

.meta {
  display: flex;
  justify-content: end;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}

.meta button { padding: 2px 5px; font-size: .75rem; background: transparent; }

#compose, #reply {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px;
  border-top: 1px solid var(--edge);
}

#compose input, #reply span { flex: 1; }
#reply span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#back { display: none; }

@media (max-width: 680px) {
  main { height: 100dvh; margin: 0; border: 0; border-radius: 0; }
  #app { grid-template-columns: minmax(0, 1fr); }
  aside { border: 0; }
  #conversation, #app.in-chat aside { display: none; }
  #app.in-chat #conversation { display: flex; }
  #back { display: block; }
  .thread-header { padding-inline: 8px; gap: 6px; }
  .thread-header button { font-size: .85rem; padding-inline: 6px; }
}