:root {
  color-scheme: light;
  --background: #f5f7f6;
  --surface: #fff;
  --text: #1b2e28;
  --muted: #536b61;
  --line: #dce5e0;
  --accent: #176a50;
  --accent-hover: #12553f;
  --highlight: #e8f3ed;
  --radius: 12px;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--background); color: var(--text); }
h1, h2, h3, p { margin: 0; }
h1 { font-size: clamp(25px, 3vw, 34px); line-height: 1.15; letter-spacing: -.035em; }
h2 { font-size: 21px; letter-spacing: -.015em; }
h3 { font-size: 18px; font-weight: 650; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled, input:disabled { cursor: not-allowed; opacity: .6; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.muted, small { color: var(--muted); font-size: 13px; }
.primary, .secondary, .menu-trigger, .icon-button {
  min-height: 40px;
  padding: 8px 14px;
  border: 1px solid transparent;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.35;
}
.primary { background: var(--accent); color: #fff; }
.primary:hover:not(:disabled) { background: var(--accent-hover); }
.secondary, .menu-trigger { border-color: var(--line); background: var(--surface); }
.secondary:hover:not(:disabled), .menu-trigger:hover { background: var(--highlight); }
.card { border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius); }

.topbar { background: var(--surface); border-bottom: 1px solid var(--line); }
.topbar-inner {
  max-width: 1320px;
  min-height: 68px;
  padding: 0 28px;
  margin-inline: auto;
  display: flex;
  align-items: center;
  gap: 34px;
}
.brand {
  border: 0;
  padding: 0;
  background: transparent;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  font-size: 16px;
  letter-spacing: .065em;
  font-weight: 750;
}
.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  letter-spacing: 0;
}
nav { display: flex; align-items: center; gap: 4px; }
nav button {
  padding: 9px 13px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
}
nav button:hover, nav [aria-current] { color: var(--accent); background: var(--highlight); }
.account { margin-inline-start: auto; display: flex; align-items: center; gap: 16px; }
.connection { display: flex; align-items: center; gap: 7px; white-space: nowrap; color: var(--muted); font-size: 13px; }
.connection i { width: 7px; height: 7px; border-radius: 50%; background: #b28542; }
.connection i.online { background: var(--accent); }

.menu { position: relative; }
.menu-trigger { display: grid; width: 40px; place-items: center; padding: 0; font-size: 21px; line-height: 1; list-style: none; cursor: pointer; }
.menu-trigger::-webkit-details-marker { display: none; }
.menu-panel {
  position: absolute;
  z-index: 2;
  inset: calc(100% + 6px) 0 auto auto;
  min-width: 190px;
  padding: 5px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 9px;
  box-shadow: 0 8px 24px #21372e14;
}
.menu-panel button { display: block; width: 100%; padding: 9px 10px; text-align: left; border: 0; border-radius: 5px; background: transparent; white-space: nowrap; }
.menu-panel button:hover { background: var(--highlight); }
.danger { color: #a32d2d; }

main { max-width: 1320px; padding: 34px 28px 46px; margin-inline: auto; }
.page-title { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.toolbar { display: flex; align-items: center; gap: 9px; min-width: 0; }
.profile-picker { width: 244px; }
.profile-trigger, .profile-option {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.profile-trigger {
  min-height: 48px;
  padding: 6px 12px 6px 8px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  list-style: none;
  cursor: pointer;
}
.profile-trigger::-webkit-details-marker { display: none; }
.profile-trigger:hover, .profile-picker[open] .profile-trigger { border-color: #83ac9b; }
.profile-initial {
  display: grid;
  flex: 0 0 32px;
  height: 32px;
  place-items: center;
  border-radius: 8px;
  background: var(--highlight);
  color: var(--accent);
  font-weight: 700;
  font-size: 14px;
}
.profile-label { flex: 1; min-width: 0; line-height: 1.25; }
.profile-label strong, .profile-label small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.profile-label strong { font-size: 13px; font-weight: 650; }
.profile-label small { margin-top: 2px; font-size: 12px; font-variant-numeric: tabular-nums; }
.profile-chevron {
  flex: 0 0 7px;
  height: 7px;
  margin: -4px 2px 0 0;
  border-right: 1.5px solid var(--muted);
  border-bottom: 1.5px solid var(--muted);
  transform: rotate(45deg);
}
.profile-picker[open] .profile-chevron { margin-top: 4px; transform: rotate(225deg); }
.profile-options {
  inset: calc(100% + 7px) auto auto 0;
  width: max(100%, 218px);
  min-width: 0;
  max-height: min(300px, 50dvh);
  overflow: auto;
  padding: 5px;
  border-radius: 10px;
}
.profile-options .profile-option { display: flex; padding: 7px; border-radius: 7px; }
.profile-options .profile-option[aria-pressed="true"] { background: var(--highlight); }
.profile-option:focus-visible { outline-offset: -2px; }
.profile-check { color: var(--accent); width: 16px; text-align: center; font-size: 14px; font-weight: 700; }
.new-chat-button {
  display: inline-flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding-inline: 15px;
  border-radius: 10px;
  white-space: nowrap;
}
.new-chat-plus {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 6px;
  background: #ffffff25;
  font-size: 19px;
  font-weight: 400;
  line-height: 1;
}
.empty { display: grid; justify-items: center; align-content: center; gap: 7px; min-height: 300px; padding: 32px 18px; text-align: center; }
.empty p { color: var(--muted); max-width: 35ch; }
.empty button { margin-top: 11px; }
.empty.compact { min-height: 115px; padding: 22px 12px; }
.empty.compact h3 { color: var(--muted); font-size: 14px; }

.profiles { list-style: none; margin: 0; padding: 0; }
.profile-row { display: grid; grid-template-columns: minmax(102px, 1fr) minmax(145px, 1fr) minmax(0, auto); gap: 16px; align-items: center; padding: 19px 22px; }
.profile-row + .profile-row, .event + .event { border-top: 1px solid var(--line); }
.profile-name { min-width: 0; overflow-wrap: anywhere; }
.profile-name small, .session-error, .event small { display: block; margin-top: 2px; }
.profile-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 5px; }
.profile-icon-button {
  display: grid;
  flex: 0 0 40px;
  width: 40px;
  place-items: center;
  padding: 0;
}
.profile-icon-button.secondary:not(.danger) { color: var(--accent); }
.profile-icon-button.danger:hover:not(:disabled) { background: #fdf0ef; }
.connection-pulse { flex: none; color: var(--accent); }
@media (prefers-reduced-motion: no-preference) {
  [aria-busy="true"] > .connection-pulse,
  #connection-check-dialog[data-checking] .connection-pulse {
    animation: connection-pulse 1s ease-in-out infinite;
  }
}
@keyframes connection-pulse {
  0%, 36%, 60%, 100% { transform: scale(1); }
  18% { transform: scale(1.24); }
  48% { transform: scale(1.13); }
}
.envelope-icon {
  position: relative;
  width: 18px;
  height: 14px;
  overflow: hidden;
  border: 1.5px solid currentColor;
  border-radius: 3px;
}
.envelope-icon::before {
  content: "";
  position: absolute;
  top: -6px;
  left: 50%;
  width: 10px;
  height: 10px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: translateX(-50%) rotate(45deg);
}
.pencil-icon {
  position: relative;
  width: 13px;
  height: 6px;
  margin-left: 3px;
  transform: rotate(-45deg);
  border: 1.5px solid currentColor;
  border-radius: 0 2px 2px 0;
}
.pencil-icon::before,
.pencil-icon::after {
  content: "";
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}
.pencil-icon::before {
  right: 100%;
  border: 3px solid transparent;
  border-left: 0;
  border-right-color: currentColor;
}
.pencil-icon::after {
  right: 2px;
  height: 100%;
  border-right: 1px solid currentColor;
}
.tag { display: inline-block; padding: 3px 9px; border-radius: 6px; background: var(--background); color: var(--muted); font-size: 12px; font-weight: 600; }
.tag.green { background: var(--highlight); color: var(--accent); }
.tag.amber { background: #fff2dc; color: #825211; }
.session-error { margin-top: 5px; max-width: 54ch; }
.event { display: flex; justify-content: space-between; gap: 16px; padding: 14px 20px; }
.event time { white-space: nowrap; font-size: 13px; color: var(--muted); }

.chat-layout { display: grid; grid-template-columns: minmax(235px, 30%) minmax(0, 1fr); height: clamp(450px, calc(100dvh - 172px), 900px); overflow: hidden; }
.chat-list { overflow-y: auto; border-right: 1px solid var(--line); }
.chat-search { padding: 12px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; }
.chat-row { display: block; width: 100%; min-width: 0; text-align: left; border: 0; border-bottom: 1px solid var(--line); background: var(--surface); padding: 13px 16px; font-weight: 600; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-row:hover, .chat-row.active { background: var(--highlight); }
.chat-row small { display: block; margin-top: 3px; overflow: hidden; text-overflow: ellipsis; font-weight: 400; }
#chat-items > .secondary { margin: 12px; }
.chat-pane { min-height: 0; min-width: 0; display: flex; flex-direction: column; background: #f8faf9; }
.chat-pane > .empty { flex: 1; min-height: 0; }
.chat-pane > .empty h3 { color: var(--muted); font-weight: 500; font-size: 15px; }
.chat-title { display: flex; align-items: center; gap: 8px; padding: 14px 18px; background: var(--surface); border-bottom: 1px solid var(--line); font-weight: 650; min-width: 0; }
.chat-title span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-back { display: none; }
.messages { display: flex; flex: 1; flex-direction: column; gap: 9px; overflow-y: auto; padding: 16px; }
#older-messages { align-self: center; }
.bubble { width: fit-content; max-width: min(80%, 65ch); border-radius: 10px; padding: 8px 12px; background: var(--surface); border: 1px solid var(--line); white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble.out { align-self: flex-end; background: var(--highlight); }
.bubble time { display: block; margin-top: 3px; text-align: right; color: var(--muted); font-size: 11px; white-space: normal; }
.composer { display: flex; gap: 8px; padding: 12px; background: var(--surface); border-top: 1px solid var(--line); }

.login-screen { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login-form { width: min(100%, 380px); padding: 28px; }
.login-form h1 { margin: 20px 0; font-size: 25px; }
.login-form .primary { width: 100%; }
label { display: grid; gap: 6px; margin: 13px 0; font-size: 14px; font-weight: 550; }
input, select { width: 100%; min-width: 0; min-height: 40px; padding: 8px 11px; border: 1px solid #bccbc2; border-radius: 8px; background: var(--surface); }
input::placeholder { color: var(--muted); }
dialog { width: min(460px, calc(100% - 32px)); max-height: calc(100dvh - 32px); border: 1px solid var(--line); border-radius: var(--radius); color: var(--text); padding: 21px; }
dialog::backdrop { background: #12271c66; }
.dialog-heading { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 9px; }
#connection-check-title { display: flex; align-items: center; gap: 8px; }
.icon-button { min-width: 40px; padding: 0; background: var(--background); font-size: 23px; font-weight: 400; }
dialog .muted { margin: 8px 0; }
dialog form > .primary { margin-top: 6px; }
.form-error, #profile-error, #settings-error, #new-chat-error { color: #a32d2d; font-size: 14px; margin: 8px 0; }
.qr-content { display: grid; justify-items: center; align-content: center; gap: 8px; text-align: center; min-height: 128px; padding: 10px 0; }
.qr-content img { max-width: 100%; height: auto; }
.qr-content p { color: var(--muted); font-size: 13px; }
.empty-icon { font-size: 23px; color: var(--accent); }
#toast { position: fixed; z-index: 3; right: 16px; bottom: 16px; max-width: calc(100% - 32px); padding: 10px 14px; border-radius: 8px; background: #21372e; color: #fff; }

@media (max-width: 1080px) {
  .profile-row { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .profile-actions { grid-column: 1 / -1; justify-content: flex-start; }
}

@media (max-width: 680px) {
  .topbar-inner { min-height: 60px; gap: 10px; padding: 0 12px; }
  .brand { gap: 5px; font-size: 13px; }
  .brand-mark { width: 26px; height: 26px; font-size: 15px; }
  nav button { padding: 8px; font-size: 13px; }
  .connection #socket-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .account { gap: 7px; }
  main { padding: 20px 12px 26px; }
  .page-title { margin-bottom: 14px; }
  .toolbar { width: 100%; }
  .chat-toolbar .profile-picker { flex: 1; width: auto; min-width: 0; }
  .chat-toolbar .new-chat-button { padding-inline: 11px; }
  .profile-row { gap: 10px 12px; padding: 14px; }
  .chat-layout { grid-template-columns: minmax(0, 1fr); height: clamp(320px, calc(100dvh - 180px), 650px); }
  .chat-list { border: 0; }
  .chat-layout:not(.chat-open) .chat-pane, .chat-layout.chat-open .chat-list { display: none; }
  .chat-back { display: inline-block; min-height: 34px; padding: 5px 10px; }
  .chat-title { padding: 8px 12px; }
}

@media (max-width: 360px) {
  .topbar .brand { font-size: 0; }
  .profile-trigger { gap: 7px; padding-right: 8px; }
  .profile-trigger .profile-initial { flex-basis: 24px; height: 26px; }
  .chat-toolbar .new-chat-button { gap: 5px; padding-inline: 8px; font-size: 13px; }
  .new-chat-plus { width: 19px; height: 19px; }
}
