:root {
  color-scheme: light dark;
  --bg: light-dark(#fdfcfb, #1a1b20);
  --surface: light-dark(#fff, #24252c);
  --sidebar: light-dark(#f4f1ed, #15161a);
  --quiet: light-dark(#eee8e2, #30323c);
  --line: light-dark(#e6ded7, #3d3e48);
  --text: light-dark(#262323, #f5f0eb);
  --muted: light-dark(#685e58, #c4bcb8);
  --accent: light-dark(#983c31, #ffc3b3);
  --on-accent: light-dark(#fff, #40231e);
  --error: light-dark(#a12b37, #ffadba);
  --header-height: 64px;
  --content-width: 760px;
  --page-padding: clamp(1rem, 4vw, 3.5rem);
  font-family: "Avenir Next", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; }
button {
  cursor: pointer;
  min-height: 42px;
  padding: .55rem .85rem;
  border: 1px solid var(--line);
  border-radius: 11px;
  color: var(--text);
  background: var(--surface);
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { cursor: default; opacity: .55; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.primary {
  color: var(--on-accent);
  background: var(--accent);
  border-color: var(--accent);
  font-weight: 700;
}
.primary:hover:not(:disabled) { filter: brightness(.93); }
input:not([type="checkbox"]), select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text);
  background: var(--surface);
  padding: .75rem;
}
label { display: block; margin: 1rem 0 .35rem; font-weight: 600; }
ul { list-style: none; margin: 0; padding: 0; }
.error { color: var(--error); }
.error:empty { display: none; }
.loading { padding: 1.25rem; color: var(--muted); }
.brand {
  display: flex;
  align-items: center;
  gap: .65rem;
  font-size: 1.07rem;
  font-weight: 700;
  letter-spacing: -.035em;
}
.brand img { flex: none; border-radius: 7px; object-fit: cover; }

.login-shell {
  display: grid;
  min-height: 100dvh;
  grid-template-rows: auto 1fr;
  background: radial-gradient(ellipse at 50% 100%, var(--sidebar), var(--bg) 70%);
}
.login-brand { padding: 1.25rem var(--page-padding); }
.login-panel {
  display: grid;
  align-items: center;
  justify-items: center;
  padding: 1.25rem var(--page-padding) clamp(2.5rem, 12vh, 7rem);
}
.login-panel form {
  width: min(100%, 405px);
  padding: clamp(1.3rem, 5vw, 2.2rem);
  border: 1px solid var(--line);
  border-radius: 21px;
  background: var(--surface);
}
.login-panel h1 { font-size: clamp(1.8rem, 4vw, 2.35rem); letter-spacing: -.055em; margin: 0; }
.login-lead { margin: .45rem 0 1.5rem; color: var(--muted); }
.login-panel .primary { margin-top: 1.35rem; width: 100%; }

.workspace { height: 100dvh; display: grid; grid-template-columns: 258px minmax(0, 1fr); }
#sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 1.15rem .95rem;
  gap: 1.25rem;
  min-height: 0;
}
#sidebar .brand { padding-inline: .25rem; }
#new-chat { align-self: stretch; text-align: left; }
#new-chat span { margin-right: .25rem; }
#sidebar nav { min-height: 0; flex: 1; display: flex; flex-direction: column; }
#sidebar nav h2 { color: var(--muted); font-size: .8rem; margin: 0 0 .45rem .5rem; }
#history { overflow: auto; }
#history button {
  width: 100%;
  text-align: left;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  background: transparent;
  border-color: transparent;
}
#history button:hover, #history button[aria-current="true"] { background: var(--quiet); }
.account { display: grid; gap: .4rem; border-top: 1px solid var(--line); padding-top: .65rem; }
.account strong { overflow: hidden; text-overflow: ellipsis; padding-inline: .5rem; }
.account button { text-align: left; background: transparent; }

.chat { display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) auto; min-width: 0; min-height: 0; }
.chat-header {
  display: flex;
  align-items: center;
  gap: .75rem;
  height: var(--header-height);
  padding: .55rem var(--page-padding);
  border-bottom: 1px solid var(--line);
}
.chat-header h1 {
  margin: 0;
  font-size: 1.02rem;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#menu { display: none; }
.feed { overflow: auto; padding: 1.4rem var(--page-padding); }
.welcome { min-height: 100%; display: grid; place-content: center; text-align: center; }
.welcome h2 {
  margin: 0;
  font-size: clamp(1.7rem, 4.2vw, 2.9rem);
  font-weight: 620;
  letter-spacing: -.055em;
  text-wrap: balance;
}
.older-messages { display: block; margin: 0 auto 1.35rem; }
.messages { max-width: var(--content-width); margin: 0 auto; }
.entry { margin: 0 0 1.15rem; padding: .85rem 0; }
.entry.user {
  width: fit-content;
  max-width: 88%;
  margin-left: auto;
  padding: .8rem 1rem;
  border: 1px solid var(--line);
  border-radius: 18px 18px 5px 18px;
  background: var(--quiet);
}
.entry strong { color: var(--accent); font-size: .82rem; }
.entry p { white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.65; margin: .3rem 0 0; }

.composer {
  width: min(100%, calc(var(--content-width) + var(--page-padding) * 2));
  justify-self: center;
  padding: 0 var(--page-padding) max(1.1rem, env(safe-area-inset-bottom));
}
#chat-form {
  display: grid;
  gap: .4rem;
  border: 1px solid var(--line);
  border-radius: 19px;
  padding: .45rem .65rem .65rem;
  background: var(--surface);
}
#chat-form:focus-within { border-color: var(--accent); }
#chat-form textarea {
  resize: vertical;
  min-height: 3.5rem;
  max-height: 35vh;
  padding: .55rem .45rem;
  border: 0;
  outline: none;
  border-radius: 8px;
}
#chat-form textarea::placeholder { color: var(--muted); }
.compose-actions, .send-actions { display: flex; align-items: center; gap: .5rem; }
.compose-actions { justify-content: flex-end; }
#send span { margin-inline-start: .32rem; }
.status { font-size: .87rem; color: var(--muted); margin: .3rem .5rem; }
.status:empty { display: none; }

dialog {
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 1.25rem;
  width: min(92vw, 480px);
}
dialog::backdrop { background: #15121bb5; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: .8rem; }
.dialog-head h2 { margin: 0; font-size: 1.2rem; }
#admin-form .primary, #password-form button { margin-top: .85rem; }
#users li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .6rem;
  padding: .55rem 0;
  border-top: 1px solid var(--line);
  overflow-wrap: anywhere;
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }

@media (max-width: 710px) {
  :root { --page-padding: .85rem; }
  .workspace { position: relative; grid-template-columns: minmax(0, 1fr); }
  #sidebar {
    display: none;
    position: absolute;
    inset: var(--header-height) auto 0 0;
    width: min(88vw, 280px);
    z-index: 2;
    box-shadow: 20px 0 38px #0003;
  }
  #sidebar.open { display: flex; }
  #menu { display: block; }
  .entry.user { max-width: 95%; }
  .login-panel input, #chat-form textarea { font-size: max(1rem, 16px); }
}

#ai-form h3, #admin-form h3 { margin: 1rem 0 .4rem; font-size: .95rem; }
#admin-form { border-top: 1px solid var(--line); }
#ai-status, #admin-status, #ai-key-note { font-size: .85rem; }
.muted { color: var(--muted); }
