:root {
  --bg: #f6f1e7;
  --surface: #fffdf8;
  --ink: #26221c;
  --muted: #6d655a;
  --line: #ddd4c4;
  --mine: #a8431f;
  --mine-ink: #fffaf5;
  --theirs: #fffdf8;
  --accent: #a8431f;
  --accent-ink: #fffaf5;
  --bot: #4b4f9e;
  --bot-soft: #eceaf8;
  --danger: #a3262a;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1a18;
    --surface: #262421;
    --ink: #f1ebdf;
    --muted: #aaa196;
    --line: #3b3833;
    --mine: #a8431f;
    --mine-ink: #fff6ef;
    --theirs: #2d2a26;
    --accent: #e0855f;
    --accent-ink: #231208;
    --bot: #b6b9f2;
    --bot-soft: #2c2b45;
    --danger: #f08a8c;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

h1, h2, h3, p { margin: 0; }

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

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

.primary, .secondary, .quiet {
  min-height: 44px; padding: 0 16px; border-radius: 10px; font-weight: 600;
}
.primary { background: var(--accent); color: var(--accent-ink); border: 0; }
.secondary { background: transparent; border: 1px solid var(--line); }
.quiet { background: transparent; border: 0; color: var(--muted); }

.icon-button {
  width: 44px; height: 44px; display: grid; place-items: center;
  background: transparent; border: 0; border-radius: 10px;
}

/* Login */
.login {
  max-width: 400px; margin: 0 auto; padding: 18vh 16px 16px;
  display: grid; gap: 16px;
}
.login h1 { font-size: 2rem; letter-spacing: -0.02em; }
.login form { display: grid; gap: 8px; }
.login label { font-weight: 600; }
.login input {
  min-height: 44px; padding: 0 12px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px;
}
.login .primary { margin-top: 8px; }
.error { color: var(--danger); }

/* Chat layout */
.chat {
  position: relative;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px 16px;
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 1.15rem; line-height: 1.2; }
.connection { font-size: 0.8rem; color: var(--muted); min-height: 1.1em; }

.scroller {
  flex: 1; min-height: 0; overflow-y: auto;
  padding: 12px 16px; display: flex; flex-direction: column;
  overscroll-behavior: contain;
}
.scroller > .quiet { align-self: center; }
.empty { margin: auto; color: var(--muted); text-align: center; }

.messages {
  list-style: none; margin: auto 0 0; padding: 0;
  display: flex; flex-direction: column; gap: 4px;
}

.day {
  align-self: center; margin: 12px 0 8px; font-size: 0.8rem; color: var(--muted);
}

.message { max-width: 82%; align-self: flex-start; }
.message.mine { align-self: flex-end; }

.bubble {
  padding: 8px 12px; border-radius: 16px 16px 16px 4px;
  background: var(--theirs); border: 1px solid var(--line);
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.mine .bubble {
  background: var(--mine); color: var(--mine-ink);
  border-color: transparent; border-radius: 16px 16px 4px 16px;
}

/* A bot's words never look like a person's: dashed edge, named on top. */
.message.bot .bubble { border: 1.5px dashed var(--bot); }
.mine.bot .bubble { border-color: var(--mine-ink); outline: 1px solid var(--mine); }

.bot-label {
  display: block; margin-bottom: 2px; padding: 0 4px;
  font-size: 0.75rem; font-weight: 600; color: var(--bot);
}
.mine .bot-label { text-align: right; }

.meta {
  display: block; margin-top: 2px; padding: 0 4px;
  font-size: 0.72rem; color: var(--muted);
}
.mine .meta { text-align: right; }

.jump {
  position: absolute; left: 50%; bottom: 84px; transform: translateX(-50%);
  min-height: 36px; padding: 0 14px; border: 0; border-radius: 18px;
  background: var(--accent); color: var(--accent-ink); font-size: 0.85rem; font-weight: 600;
}

/* Drafts waiting for approval */
.drafts {
  max-height: 45dvh; overflow-y: auto;
  padding: 8px 16px; display: grid; gap: 8px;
  border-top: 1px solid var(--line); background: var(--bot-soft);
}
.draft { display: grid; gap: 8px; }
.draft-title { font-size: 0.85rem; font-weight: 600; color: var(--bot); }
.draft textarea {
  width: 100%; padding: 8px 10px; resize: vertical;
  background: var(--surface); border: 1px dashed var(--bot); border-radius: 10px;
}
.draft-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* Composer */
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom)) 16px;
  border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; min-width: 0; min-height: 44px; max-height: 140px; padding: 10px 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 22px;
  resize: none;
}
.send {
  width: 44px; height: 44px; flex: none; display: grid; place-items: center;
  border: 0; border-radius: 50%; background: var(--accent); color: var(--accent-ink);
}
.send:disabled { opacity: 0.5; }

/* Settings */
.settings {
  width: min(460px, calc(100vw - 32px)); padding: 16px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: 16px;
}
.settings::backdrop { background: rgb(0 0 0 / 0.45); }
.settings-head { display: flex; align-items: center; justify-content: space-between; }
.settings section { display: grid; gap: 8px; padding: 16px 0; border-top: 1px solid var(--line); }
.settings section:first-of-type { margin-top: 8px; }
.settings h3 { font-size: 0.95rem; }
.hint { font-size: 0.85rem; color: var(--muted); }
.hint:empty { display: none; }
.switch { display: flex; align-items: center; gap: 10px; min-height: 44px; }
.switch input { width: 22px; height: 22px; accent-color: var(--accent); }

.toast {
  position: fixed; left: 16px; right: 16px; top: calc(12px + env(safe-area-inset-top));
  max-width: 480px; margin: 0 auto; padding: 10px 14px; border-radius: 10px;
  background: var(--ink); color: var(--bg); text-align: center;
}
