/* Calm palette: desaturated sage and warm greys, soft contrast, no bright accents.
   Text/accent pairs keep WCAG AA contrast (>= 4.5:1). */
:root {
  --bg: #f1f3f1;
  --surface: #f8f9f8;
  --bubble-bot: #fcfdfc;
  --bubble-user: #e1eae5;
  --text: #2b3431;
  --muted: #66736d;
  --accent: #46705f;
  --accent-ink: #ffffff;
  --accent-soft: #e7eeea;
  --line: #dce2de;
  --danger: #9c433b;
  --danger-soft: #f5e9e6;
  --warn-soft: #f2eddf;
  --shadow: 0 1px 2px rgba(40, 52, 47, .06);
  --radius: 18px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #181c1b;
    --surface: #1f2423;
    --bubble-bot: #252b29;
    --bubble-user: #2d3b36;
    --text: #dde3e0;
    --muted: #98a39e;
    --accent: #93b8a9;
    --accent-ink: #14201b;
    --accent-soft: #2a332f;
    --line: #333a38;
    --danger: #dc9a91;
    --danger-soft: #322624;
    --warn-soft: #302c23;
    --shadow: none;
  }
}

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

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); }
button { font: inherit; color: inherit; }

/* ── Sign-in ─────────────────────────────────────────────── */

.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 28px 22px;
  box-shadow: var(--shadow);
}
.logo {
  width: 52px; height: 52px;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-ink);
  display: grid; place-items: center;
  font-size: 28px; font-weight: 600;
}
.login h1 { margin: 16px 0 6px; font-size: 26px; }
.lead { margin: 0 0 20px; color: var(--muted); }

.login label { display: block; margin: 14px 0 6px; font-weight: 600; font-size: 14px; }
.login input[type="email"], .login input[type="text"] {
  width: 100%;
  padding: 13px 14px;
  font-size: 18px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.login input:focus-visible, .composer input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
#code { letter-spacing: .4em; text-align: center; font-size: 24px; }

.consent { display: flex !important; gap: 10px; align-items: flex-start; font-weight: 400 !important; color: var(--muted); }
.consent input { margin-top: 4px; width: 18px; height: 18px; flex: none; accent-color: var(--accent); }

button.primary {
  width: 100%;
  margin-top: 18px;
  padding: 14px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  cursor: pointer;
}
button.primary:disabled { opacity: .45; cursor: default; }
button.primary:focus-visible, .link:focus-visible, .ibtn:focus-visible, .menu button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.row { display: flex; justify-content: space-between; gap: 12px; margin-top: 12px; }
.link { background: none; border: 0; padding: 6px 0; color: var(--accent); cursor: pointer; text-decoration: underline; }
.link:disabled { color: var(--muted); text-decoration: none; cursor: default; }
.sent-to { margin: 0 0 4px; color: var(--muted); }
.error { margin: 14px 0 0; padding: 10px 12px; border-radius: 10px; background: var(--danger-soft); color: var(--danger); font-size: 14px; }
.fineprint { margin: 20px 0 0; font-size: 13px; color: var(--muted); }

/* ── Chat shell ──────────────────────────────────────────── */

/* ── App shell: sidebar + main column ─────────────────────── */

.shell {
  height: 100dvh;
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  background: var(--bg);
}
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px 12px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-right: 1px solid var(--line);
  overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 2px 8px 10px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav button {
  display: flex; align-items: center; gap: 12px;
  width: 100%;
  padding: 11px 12px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
}
.nav button:hover:not(:disabled) { background: var(--accent-soft); }
.nav button[aria-current="page"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav button:disabled { opacity: .4; cursor: default; }
.nav button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.nav .ico { width: 1.6em; text-align: center; flex: none; }
.side-foot { margin-top: auto; display: flex; flex-direction: column; align-items: flex-start; gap: 8px; padding: 0 4px; }
.side-foot .tg-link { margin-left: 0; }

.main { display: flex; flex-direction: column; min-width: 0; height: 100dvh; }
.topbar {
  display: flex; align-items: center; gap: 12px;
  min-height: 56px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.section-title { margin: 0; font-size: 17px; font-weight: 600; }
.icon-btn {
  display: none;
  width: 40px; height: 40px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
}
.backdrop { display: none; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-weight: 600; font-size: 20px;
}
.title { font-weight: 600; }
.subtitle { font-size: 13px; color: var(--muted); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px max(12px, calc((100% - 820px) / 2));
  display: flex;
  flex-direction: column;
  gap: 8px;
  scroll-behavior: smooth;
}

.msg {
  max-width: min(88%, 560px);
  padding: 10px 14px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.bot { align-self: flex-start; background: var(--bubble-bot); border-bottom-left-radius: 4px; }
.msg.user { align-self: flex-end; background: var(--bubble-user); border-bottom-right-radius: 4px; }
.msg.emergency { border: 2px solid var(--danger); background: var(--danger-soft); }
.msg.notice { background: var(--warn-soft); }
.msg.wide { width: min(88%, 560px); }
.msg .muted { color: var(--muted); font-size: 14px; }
.msg small, .msg .small { font-size: 13px; color: var(--muted); }
.msg .gap { display: block; height: 6px; }

.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  .typing i { animation: none; opacity: .6; }
  .messages { scroll-behavior: auto; }
}

/* Inline buttons under a bubble, like Telegram's inline keyboard */
.ikb { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; white-space: normal; }
.ikb .irow { display: flex; gap: 6px; }
.ibtn {
  flex: 1;
  min-height: 44px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--text);
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
.ibtn:hover { border-color: var(--accent); }
.ibtn:disabled { opacity: .5; cursor: default; }
.ibtn.danger { color: var(--danger); }
.picked { margin-top: 8px; font-size: 14px; color: var(--muted); }

.bar { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 1px; }
canvas.chart { width: 100%; height: 220px; display: block; margin-top: 8px; }

/* ── Composer + reply keyboard ───────────────────────────── */

.composer {
  display: flex; gap: 8px;
  padding: 8px max(12px, calc((100% - 820px) / 2));
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.composer input {
  flex: 1;
  padding: 11px 14px;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 22px;
}
.composer input:disabled { opacity: .6; }
.composer button {
  width: 44px; height: 44px;
  border: 0; border-radius: 50%;
  background: var(--accent); color: var(--accent-ink);
  font-size: 18px; cursor: pointer;
}
.composer button:disabled { opacity: .35; cursor: default; }

/* Diary chart legend (colors match the series drawn in app.js) */
.legend { display: flex; gap: 14px; margin-top: 6px; font-size: 13px; color: var(--muted); white-space: normal; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot.sleep { background: #6f8fb3; }
.dot.energy { background: #c9a27a; }
.dot.stress { background: #b9807c; }

/* Static document pages (privacy policy) */
.doc { max-width: 720px; margin: 0 auto; padding: 24px 16px 48px; }
.doc h1 { font-size: 26px; }
.doc h2 { font-size: 18px; margin-top: 28px; }
.doc li { margin: 6px 0; }
.doc .muted { color: var(--muted); font-size: 14px; }
.doc-page { overflow-y: auto; height: auto; }

/* Link to the Telegram bot */
.tg { margin: 10px 0 0; font-size: 14px; color: var(--muted); }
.tg-link {
  margin-left: auto;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 14px;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
}
.tg-link:hover { border-color: var(--accent); }
.tg-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }


/* ── Questionnaire form (survey-style cards) ───────────────── */

.form-view {
  flex: 1;
  overflow-y: auto;
  padding: 0 max(12px, calc((100% - 720px) / 2)) 48px;
}
.main.is-form .messages, .main.is-form .composer { display: none; }

.fprogress {
  position: sticky; top: 0; z-index: 2;
  padding: 14px 0 10px;
  background: var(--bg);
}
.fprogress .label { display: flex; justify-content: space-between; gap: 12px; font-size: 13px; color: var(--muted); }
.fprogress .track { height: 6px; margin-top: 6px; border-radius: 99px; background: var(--line); overflow: hidden; }
.fprogress .fill { height: 100%; width: 0; background: var(--accent); transition: width .2s; }

.fhead {
  margin-bottom: 12px;
  padding: 20px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-top: 6px solid var(--accent);
  border-radius: 12px;
}
.fhead h2 { margin: 0 0 6px; font-size: 22px; font-weight: 600; }
.fhead p { margin: 0; color: var(--muted); }

.q {
  margin: 0 0 12px;
  padding: 16px 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.q.invalid { border-color: var(--danger); }
.qtitle { font-weight: 600; }
.q .req { color: var(--danger); }
.q .hint { margin: 4px 0 8px; color: var(--muted); font-size: 14px; }
.opt { display: flex; align-items: center; gap: 10px; padding: 8px 6px; border-radius: 8px; cursor: pointer; }
.opt:hover { background: var(--accent-soft); }
.opt input[type="radio"] { flex: none; width: 18px; height: 18px; margin: 0; accent-color: var(--accent); }
.opt .txt { flex: 1; min-width: 0; }
.finput {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 8px 2px;
  font: inherit;
  color: var(--text);
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
}
.finput:focus { outline: none; border-bottom: 2px solid var(--accent); padding-bottom: 7px; }
.q-error { margin: 8px 0 0; font-size: 13px; color: var(--danger); }
.factions { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 8px; }
.factions .primary { width: auto; min-width: 180px; margin-top: 0; padding: 12px 22px; }

/* ── Narrow screens: sidebar becomes a slide-over drawer ───── */

@media (max-width: 860px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 20;
    width: min(300px, 86vw);
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform .2s, visibility .2s;
    box-shadow: 0 0 24px rgba(30, 40, 35, .18);
  }
  .sidebar.open { transform: none; visibility: visible; }
  .icon-btn { display: grid; }
  .backdrop { display: block; position: fixed; inset: 0; z-index: 15; background: rgba(30, 40, 35, .4); }
}
@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
  .fprogress .fill { transition: none; }
}
