:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --text: #14181f;
  --muted: #6b7280;
  --line: #e3e6ea;
  --accent: #2f7ce0;
  --accent-text: #ffffff;
  --bubble-in: #ffffff;
  --bubble-out: #d7ebff;
  --ok: #22a06b;
  --warn: #d97706;
  --err: #d64545;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 8px 24px rgba(0, 0, 0, .04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1319;
    --panel: #171c24;
    --panel-2: #1e242e;
    --text: #e7eaef;
    --muted: #98a1b0;
    --line: #29313d;
    --accent: #4b93ea;
    --bubble-in: #1e242e;
    --bubble-out: #21456e;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* --- login ------------------------------------------------------------- */

.login-wrap {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 24px;
}

.card h1 {
  margin: 0 0 4px;
  font-size: 20px;
}

.card p.sub {
  margin: 0 0 22px;
  color: var(--muted);
  font-size: 13px;
}

label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 6px;
}

input[type="text"], input[type="password"] {
  width: 100%;
  padding: 10px 12px;
  margin-bottom: 16px;
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
}

input:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button {
  font: inherit;
  cursor: pointer;
  border: 0;
  border-radius: 10px;
}

.btn-primary {
  width: 100%;
  padding: 11px;
  font-weight: 600;
  color: var(--accent-text);
  background: var(--accent);
}

.btn-primary:disabled { opacity: .6; cursor: default; }

.error {
  margin: 0 0 14px;
  padding: 9px 12px;
  font-size: 13px;
  color: #fff;
  background: var(--err);
  border-radius: 9px;
}

.error[hidden] { display: none; }

/* --- chat shell -------------------------------------------------------- */

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 900px;
  margin: 0 auto;
  background: var(--panel);
  border-inline: 1px solid var(--line);
}

header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.title { min-width: 0; flex: 1; }

.title h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  flex: none;
}

.dot.ok   { background: var(--ok); }
.dot.warn { background: var(--warn); }
.dot.err  { background: var(--err); }

.header-actions { display: flex; gap: 6px; flex: none; }

.btn-ghost {
  padding: 7px 11px;
  font-size: 13px;
  color: var(--muted);
  background: var(--panel-2);
  border: 1px solid var(--line);
}

.btn-ghost:hover { color: var(--text); }

/* --- messages ---------------------------------------------------------- */

.messages {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  background: var(--bg);
}

.load-more {
  align-self: center;
  margin-bottom: 10px;
  padding: 6px 14px;
  font-size: 13px;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--line);
}

.day {
  align-self: center;
  margin: 14px 0 8px;
  padding: 3px 12px;
  font-size: 12px;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: min(78%, 560px);
  padding: 7px 11px 6px;
  background: var(--bubble-in);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
  align-self: flex-start;
  word-break: break-word;
}

.msg.mine {
  align-self: flex-end;
  background: var(--bubble-out);
}

/* Consecutive messages from one sender read as a group. */
.msg.grouped { margin-top: 0; }
.msg:not(.grouped) { margin-top: 10px; }

.msg.service {
  align-self: center;
  max-width: 80%;
  background: transparent;
  border: 0;
  box-shadow: none;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.sender {
  font-size: 13px;
  font-weight: 650;
  color: var(--accent);
  margin-bottom: 2px;
}

.msg.mine .sender { color: var(--muted); }

.quote {
  margin-bottom: 5px;
  padding: 4px 8px;
  font-size: 13px;
  border-left: 3px solid var(--accent);
  background: rgba(127, 127, 127, .1);
  border-radius: 0 6px 6px 0;
}

.quote b { display: block; font-size: 12px; color: var(--accent); }
.quote span { color: var(--muted); }

.body { white-space: pre-wrap; }

.body a { color: var(--accent); }

.msg.removed .body {
  font-style: italic;
  color: var(--muted);
}

.meta {
  align-self: flex-end;
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
  font-size: 11px;
  color: var(--muted);
}

.meta button {
  padding: 0 2px;
  font-size: 11px;
  color: var(--muted);
  background: none;
}

.meta button:hover { color: var(--accent); }

/* --- composer ---------------------------------------------------------- */

.composer {
  border-top: 1px solid var(--line);
  background: var(--panel);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
}

.reply-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  padding: 6px 10px;
  font-size: 13px;
  background: var(--panel-2);
  border-left: 3px solid var(--accent);
  border-radius: 0 8px 8px 0;
}

.reply-bar[hidden] { display: none; }
.reply-bar span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }

.row { display: flex; gap: 8px; align-items: flex-end; }

textarea {
  flex: 1;
  resize: none;
  max-height: 40vh;
  padding: 10px 12px;
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 12px;
}

.send {
  flex: none;
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  color: var(--accent-text);
  background: var(--accent);
  border-radius: 50%;
}

.send:disabled { opacity: .45; cursor: default; }
.send svg { width: 19px; height: 19px; fill: currentColor; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 92px;
  transform: translateX(-50%);
  max-width: min(92vw, 460px);
  padding: 9px 15px;
  font-size: 13px;
  color: #fff;
  background: var(--err);
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 10;
}

.toast[hidden] { display: none; }

.empty {
  margin: auto;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

@media (max-width: 640px) {
  .app { border-inline: 0; }
  .msg { max-width: 88%; }
  .messages { padding: 12px; }
}
