:root {
  --bg: #f7f7f5;
  --fg: #1a1a1a;
  --muted: #6b6b6b;
  --primary: #166D5B;
  --primary-fg: #ffffff;
  --primary-soft: #d9ebe1;
  --border: #e5e3dc;
  --card: #ffffff;
  --sidebar-bg: #ececec;
  --sidebar-border: #dcdcd6;
  --sidebar-item-hover: #dcdcd2;
  --sidebar-item-active: #166D5B;
  --sidebar-item-active-fg: #ffffff;
  --error-bg: #fdecea;
  --error-fg: #8b1c13;
  --warn-bg: #fff4d6;
  --warn-fg: #6e4c00;
  --info-bg: #e3f0fb;
  --info-fg: #134c7e;
  --success-bg: #e1f4e6;
  --success-fg: #1d6b34;
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  height: 100%;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

button { font-family: inherit; }

/* ── floating flashes (top-right toast strip) ───────────────────── */
.flashes-floating {
  position: fixed; top: 16px; right: 16px;
  display: flex; flex-direction: column; gap: 8px;
  z-index: 100; max-width: 360px;
}
.flash { padding: 10px 12px; border-radius: 8px; font-size: 14px; box-shadow: 0 4px 12px rgba(0,0,0,.06); }
.flash-error   { background: var(--error-bg);   color: var(--error-fg); }
.flash-warning { background: var(--warn-bg);    color: var(--warn-fg); }
.flash-info    { background: var(--info-bg);    color: var(--info-fg); }
.flash-success { background: var(--success-bg); color: var(--success-fg); }

/* ── auth pages (centered card) ──────────────────────────────────── */
.container {
  max-width: 820px;
  margin: 0 auto;
  padding: 24px 16px 64px;
}
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 16px;
}
.card.narrow { max-width: 420px; margin: 48px auto; }
.card h1 { margin-top: 0; }
.card h2 { margin-top: 0; font-size: 20px; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.back-link { display: inline-block; margin-bottom: 8px; font-size: 14px; }

.stack { display: flex; flex-direction: column; gap: 12px; }
.stack label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); }
.stack input, textarea {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 16px; background: #fff; color: var(--fg); font-family: inherit;
}
.stack input:focus, textarea:focus {
  outline: 2px solid var(--primary-soft);
  border-color: var(--primary);
}

input.otp {
  text-align: center; font-size: 24px; letter-spacing: 8px;
  font-variant-numeric: tabular-nums;
}

button.primary, a.primary {
  background: var(--primary); color: var(--primary-fg);
  border: none; border-radius: 8px;
  padding: 10px 16px; font-size: 16px; font-weight: 600;
  cursor: pointer; text-align: center; text-decoration: none;
  display: inline-block;
}
button.primary:hover, a.primary:hover { filter: brightness(0.95); }

button.link, a.link {
  background: transparent; border: none; color: var(--primary);
  cursor: pointer; font: inherit; padding: 0;
}
.cancel { text-align: center; color: var(--muted); margin-top: 4px; display: block; }
.inline-form { display: inline; margin: 0; }
.links { margin-top: 16px; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.error { background: var(--error-bg); color: var(--error-fg); border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; font-size: 14px; }

/* ── app shell (sidebar + main) ──────────────────────────────────── */
body.app-mode { overflow: hidden; }

.app-shell {
  display: flex;
  height: 100vh;
  width: 100vw;
}

/* Sidebar */
.sidebar {
  width: 260px;
  flex: 0 0 260px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--sidebar-border);
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.sidebar-top {
  padding: 14px 12px 8px;
  display: flex; flex-direction: column; gap: 10px;
  border-bottom: 1px solid var(--sidebar-border);
}
.sidebar .brand {
  font-weight: 700; font-size: 17px; color: var(--fg);
  padding: 4px 6px;
}
.new-chat-btn {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--fg); font-size: 14px; font-weight: 500;
  text-decoration: none;
  transition: background .15s;
}
.new-chat-btn:hover { background: var(--primary-soft); text-decoration: none; }
.new-chat-btn .plus {
  flex: 0 0 auto;
  width: 22px; height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--primary); color: var(--primary-fg);
  border-radius: 50%;
}
.new-chat-btn .plus svg { display: block; }

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex; flex-direction: column; gap: 2px;
}
.chat-item {
  display: block;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--fg);
  font-size: 14px;
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-item:hover { background: var(--sidebar-item-hover); text-decoration: none; }
.chat-item.active {
  background: var(--sidebar-item-active);
  color: var(--sidebar-item-active-fg);
}
.chat-item .title { display: block; overflow: hidden; text-overflow: ellipsis; }
.chat-empty { padding: 12px 10px; font-size: 13px; }

.sidebar-bottom {
  border-top: 1px solid var(--sidebar-border);
  padding: 10px;
}
.user-menu summary {
  list-style: none;
  cursor: pointer;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 14px;
  display: flex; align-items: center; gap: 8px;
}
.user-menu summary::-webkit-details-marker { display: none; }
.user-menu summary:hover { background: var(--sidebar-item-hover); }
.user-menu .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--primary);
}
.user-menu .menu {
  margin-top: 4px;
  display: flex; flex-direction: column; gap: 2px;
  padding: 4px;
}
.user-menu .menu a, .user-menu .menu .link {
  display: block; padding: 8px 10px; border-radius: 6px; font-size: 14px;
  color: var(--fg); text-align: left;
}
.user-menu .menu a:hover, .user-menu .menu .link:hover {
  background: var(--sidebar-item-hover); text-decoration: none;
}

/* Main pane */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  background: var(--bg);
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 24px;
  display: flex; flex-direction: column; gap: 16px;
}
.messages > .welcome {
  margin: auto;
  max-width: 640px;
  text-align: center;
  padding: 32px 16px;
}
.messages > .welcome h1 {
  font-size: 32px; margin: 0 0 8px;
  color: var(--primary);
}
.welcome .daily-card {
  margin-top: 28px;
  padding: 20px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-align: left;
}
.welcome .daily-head {
  font-size: 12px; text-transform: uppercase; letter-spacing: 0.6px;
  font-weight: 700; color: var(--primary); margin-bottom: 6px;
}
.welcome blockquote {
  margin: 6px 0;
  padding: 12px 14px;
  background: var(--primary-soft);
  border-radius: 8px;
  font-size: 16px; font-style: italic;
}
.welcome .ref { color: var(--muted); font-size: 13px; margin: 0 0 12px; }
.welcome .reflection { margin: 0; }

.msg {
  display: flex;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
}
.msg .bubble {
  padding: 12px 16px;
  border-radius: 16px;
  white-space: pre-wrap;
  word-wrap: break-word;
  max-width: 80%;
  font-size: 15.5px;
}
.msg-user { justify-content: flex-end; }
.msg-user .bubble {
  background: var(--primary); color: var(--primary-fg);
  border-bottom-right-radius: 4px;
}
.msg-assistant { flex-direction: column; align-items: flex-start; }
.msg-assistant .bubble {
  background: var(--card); color: var(--fg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

/* Markdown rendering inside assistant bubbles */
.msg-assistant .bubble p { margin: 0 0 8px; }
.msg-assistant .bubble p:last-child { margin-bottom: 0; }
.msg-assistant .bubble h1,
.msg-assistant .bubble h2,
.msg-assistant .bubble h3,
.msg-assistant .bubble h4 {
  margin: 12px 0 6px;
  font-weight: 600;
  line-height: 1.3;
}
.msg-assistant .bubble h1 { font-size: 19px; }
.msg-assistant .bubble h2 { font-size: 17px; }
.msg-assistant .bubble h3 { font-size: 16px; }
.msg-assistant .bubble h4 { font-size: 15px; }
.msg-assistant .bubble ul,
.msg-assistant .bubble ol { margin: 4px 0 8px; padding-left: 22px; }
.msg-assistant .bubble li { margin: 2px 0; }
.msg-assistant .bubble code {
  background: #f1efe8;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.92em;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.msg-assistant .bubble pre {
  background: #f1efe8;
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 8px 0;
}
.msg-assistant .bubble pre code {
  background: transparent;
  padding: 0;
  font-size: 0.9em;
}
.msg-assistant .bubble blockquote {
  margin: 8px 0;
  padding: 4px 12px;
  border-left: 3px solid var(--primary);
  color: var(--muted);
}
.msg-assistant .bubble strong { font-weight: 700; }
.msg-assistant .bubble em { font-style: italic; }
.msg-assistant .bubble a { color: var(--primary); text-decoration: underline; }
.citations {
  list-style: none;
  padding: 0;
  margin: 8px 0 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 80%;
}
.citations li {
  padding: 10px 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.citations li strong {
  color: var(--primary);
  font-size: 13px;
}
.citations li .cite-text {
  color: var(--fg);
  font-size: 13.5px;
  line-height: 1.45;
}

.error {
  margin: 8px auto; max-width: 760px;
}

/* Composer */
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  margin: 0 auto 8px;
  padding: 8px 8px 8px 16px;
  width: calc(100% - 32px);
  max-width: 760px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 24px;
  box-shadow: 0 2px 8px rgba(0,0,0,.04);
}
.composer textarea {
  flex: 1;
  resize: none;
  border: none;
  outline: none;
  padding: 10px 0;
  font-size: 15.5px;
  line-height: 1.45;
  background: transparent;
  font-family: inherit;
  max-height: 200px;
  overflow-y: hidden;
}
.composer textarea:focus { outline: none; border: none; }

.send-btn {
  flex: 0 0 auto;
  width: 36px; height: 36px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--primary); color: var(--primary-fg);
  border: none; border-radius: 50%;
  cursor: pointer;
  transition: background .15s, color .15s, transform .1s;
}
.send-btn:hover:not(:disabled) { filter: brightness(0.95); }
.send-btn:active:not(:disabled) { transform: scale(0.95); }
.send-btn:disabled {
  background: #d0d0c8;
  color: #8b8b85;
  cursor: not-allowed;
}

.composer-hint {
  text-align: center;
  font-size: 12px;
  margin: 0 auto 12px;
  max-width: 760px;
}

/* Mobile: collapse sidebar to a drawer-style toggle (basic) */
@media (max-width: 720px) {
  .sidebar { width: 220px; flex-basis: 220px; }
  .composer-hint { display: none; }
}
