:root {
  --bg: #f7f3ea;
  --bg-soft: #efe9db;
  --bg-warm: #faf6ee;
  --bg-sidebar: #f0ebe0;
  --ink: #2a241d;
  --ink-soft: #685c4e;
  --ink-faint: #a99c87;
  --line: rgba(42, 36, 29, 0.10);
  --accent: #6b8073;
  --accent-deep: #4f6358;
  --accent-warm: #a0664a;
  --user-bg: #e8dfca;
  --danger: #c0392b;
  --shadow: rgba(42, 36, 29, 0.08);
}

[data-theme="dark"] {
  --bg: #1a1a1a;
  --bg-soft: #242424;
  --bg-warm: #1e1e1e;
  --bg-sidebar: #161616;
  --ink: #e0dbd3;
  --ink-soft: #a89e90;
  --ink-faint: #6b6259;
  --line: rgba(224, 219, 211, 0.10);
  --accent: #8baa97;
  --accent-deep: #a0c4ad;
  --accent-warm: #c4886a;
  --user-bg: #2e2a22;
  --shadow: rgba(0, 0, 0, 0.25);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: 'Newsreader', Georgia, serif;
  font-size: 17px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  display: flex;
  height: 100vh;
  height: 100dvh;
}

/* ==================== SIDEBAR ==================== */
.sidebar {
  width: 300px;
  min-width: 300px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  height: 100%;
  transition: transform 0.3s ease;
  z-index: 100;
}

.sidebar-header {
  padding: 24px 20px 16px;
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex-shrink: 0;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.wordmark {
  font-family: 'Fraunces', serif;
  font-weight: 400;
  font-size: 24px;
  letter-spacing: -0.015em;
  font-style: italic;
  line-height: 1;
}

.wordmark .em {
  font-style: normal;
  color: var(--accent);
  font-weight: 500;
}

.btn-new-chat {
  width: 100%;
  padding: 10px 16px;
  background: var(--accent);
  color: var(--bg-warm);
  border: none;
  border-radius: 12px;
  font-family: 'Newsreader', serif;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s;
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.btn-new-chat:hover { background: var(--accent-deep); }

/* Usage bar */
.usage-bar {
  padding: 10px 14px;
  background: var(--bg);
  border-radius: 10px;
  font-size: 12px;
  color: var(--ink-soft);
}

.usage-bar .usage-label {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
}

.usage-bar .usage-track {
  height: 4px;
  background: var(--line);
  border-radius: 2px;
  overflow: hidden;
}

.usage-bar .usage-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.3s ease;
}

.usage-bar .usage-fill.exhausted {
  background: var(--accent-warm);
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.conversation-item {
  padding: 12px 14px;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 2px;
}

.conversation-item:hover { background: var(--line); }
.conversation-item.active { background: var(--bg); box-shadow: 0 1px 3px var(--shadow); }

.conversation-item .title {
  font-size: 14px;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.conversation-item .date {
  font-size: 11px;
  color: var(--ink-faint);
  white-space: nowrap;
}

.conversation-item .btn-delete {
  background: none;
  border: none;
  color: var(--ink-faint);
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s;
  flex-shrink: 0;
  display: flex;
  align-items: center;
}

.conversation-item:hover .btn-delete { opacity: 1; }
.conversation-item .btn-delete:hover { color: var(--danger); }

.sidebar-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.sidebar-footer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.btn-small {
  background: none;
  border: 1px solid var(--line);
  color: var(--ink-soft);
  cursor: pointer;
  padding: 7px 12px;
  border-radius: 10px;
  font-size: 12px;
  font-family: 'Newsreader', serif;
  transition: background 0.15s;
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn-small:hover { background: var(--line); }

.btn-auth {
  background: var(--accent);
  color: var(--bg-warm);
  border: none;
}

.btn-auth:hover { background: var(--accent-deep); }

.user-info {
  font-size: 12px;
  color: var(--ink-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 140px;
}

.version-tag {
  font-size: 11px;
  color: var(--ink-faint);
  letter-spacing: 0.08em;
}

/* ==================== OVERLAY ==================== */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 99;
}

.sidebar-overlay.visible { display: block; }

/* ==================== MAIN ==================== */
.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  min-width: 0;
}

.chat-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 16px;
  flex-shrink: 0;
  background: var(--bg);
}

.btn-menu {
  display: none;
  background: none;
  border: none;
  color: var(--ink);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
}

.btn-menu:hover { background: var(--line); }

.chat-title {
  font-family: 'Fraunces', serif;
  font-size: 16px;
  font-weight: 400;
  color: var(--ink-soft);
  font-style: italic;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 32px 24px 16px;
  position: relative;
}

.chat-messages::before {
  content: "";
  position: sticky;
  top: 0;
  display: block;
  height: 20px;
  margin: -32px -24px 12px;
  background: linear-gradient(to bottom, var(--bg), transparent);
  pointer-events: none;
  z-index: 1;
}

.messages-inner {
  max-width: 680px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

/* ==================== WELCOME ==================== */
.welcome {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.welcome-content {
  text-align: center;
  max-width: 420px;
}

.welcome-logo {
  font-family: 'Fraunces', serif;
  font-size: 42px;
  font-style: italic;
  font-weight: 400;
  margin-bottom: 12px;
}

.welcome-logo .em { font-style: normal; color: var(--accent); font-weight: 500; }

.welcome-sub {
  color: var(--ink-soft);
  font-size: 18px;
  line-height: 1.6;
  margin-bottom: 32px;
}

.welcome-start {
  padding: 14px 32px;
  background: var(--accent);
  color: var(--bg-warm);
  border: none;
  border-radius: 14px;
  font-family: 'Newsreader', serif;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}

.welcome-start:hover { background: var(--accent-deep); }
.welcome-start:active { transform: scale(0.97); }

/* ==================== MESSAGES ==================== */
.msg-user {
  align-self: flex-end;
  max-width: 78%;
  background: var(--user-bg);
  padding: 14px 20px;
  border-radius: 22px 22px 6px 22px;
  font-size: 16px;
  line-height: 1.55;
  color: var(--ink);
  box-shadow: 0 1px 0 var(--shadow);
  animation: rise 0.35s ease-out;
}

.msg-bot {
  align-self: flex-start;
  max-width: 88%;
  padding: 4px 0 4px 18px;
  border-left: 2px solid var(--accent);
  animation: rise 0.45s ease-out;
}

.msg-bot .who {
  display: block;
  font-family: 'Fraunces', serif;
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 6px;
  font-weight: 500;
}

.msg-bot .body {
  font-family: 'Newsreader', serif;
  font-size: 18px;
  line-height: 1.65;
  color: var(--ink);
  white-space: pre-wrap;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.typing { display: inline-flex; gap: 5px; padding: 4px 0; }
.typing span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--ink-faint);
  animation: pulse 1.4s ease-in-out infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes pulse {
  0%, 100% { opacity: 0.25; transform: scale(0.85); }
  50%      { opacity: 1;    transform: scale(1); }
}

/* ==================== COMPOSER ==================== */
.composer {
  padding: 16px 24px 20px;
  border-top: 1px solid var(--line);
  background: var(--bg);
  flex-shrink: 0;
}

.composer-inner {
  max-width: 680px;
  margin: 0 auto;
  position: relative;
}

.composer textarea {
  width: 100%;
  background: var(--bg-warm);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 14px 56px 14px 20px;
  font-family: 'Newsreader', serif;
  font-size: 16px;
  line-height: 1.5;
  color: var(--ink);
  resize: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  max-height: 160px;
  overflow-y: auto;
}

.composer textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(107, 128, 115, 0.12);
}

.composer textarea::placeholder { color: var(--ink-faint); font-style: italic; }

.composer .btn-send {
  position: absolute;
  right: 8px;
  bottom: 8px;
  width: 40px; height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--bg-warm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.1s;
}

.composer .btn-send:hover:not(:disabled) { background: var(--accent-deep); }
.composer .btn-send:active:not(:disabled) { transform: scale(0.94); }
.composer .btn-send:disabled { opacity: 0.35; cursor: not-allowed; }

.disclaimer {
  max-width: 680px;
  margin: 10px auto 0;
  font-size: 11px;
  color: var(--ink-faint);
  text-align: center;
  line-height: 1.5;
}

.disclaimer strong { color: var(--accent-warm); font-weight: 500; }

/* ==================== LIMIT REACHED OVERLAY ==================== */
.limit-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.limit-overlay.visible { display: flex; }

.limit-card {
  background: var(--bg);
  border-radius: 20px;
  padding: 40px 36px;
  max-width: 420px;
  width: 100%;
  text-align: center;
  box-shadow: 0 8px 40px rgba(0,0,0,0.15);
  animation: rise 0.4s ease-out;
}

.limit-card h2 {
  font-family: 'Fraunces', serif;
  font-size: 22px;
  font-weight: 400;
  color: var(--ink);
  margin-bottom: 12px;
}

.limit-card p {
  color: var(--ink-soft);
  font-size: 15px;
  line-height: 1.6;
  margin-bottom: 8px;
}

.limit-card .crisis-note {
  font-size: 13px;
  color: var(--accent-warm);
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.limit-card .limit-actions {
  margin-top: 24px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.limit-card .btn-primary {
  padding: 14px 28px;
  background: var(--accent);
  color: var(--bg-warm);
  border: none;
  border-radius: 14px;
  font-family: 'Newsreader', serif;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.2s;
}

.limit-card .btn-primary:hover { background: var(--accent-deep); }

.limit-card .btn-secondary {
  padding: 10px;
  background: none;
  border: none;
  color: var(--ink-faint);
  font-family: 'Newsreader', serif;
  font-size: 14px;
  cursor: pointer;
}

/* ==================== AUTH MODAL ==================== */
.auth-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.auth-overlay.visible { display: flex; }

.auth-card {
  background: var(--bg);
  border-radius: 20px;
  padding: 36px 32px;
  max-width: 400px;
  width: 100%;
  box-shadow: 0 8px 40px rgba(0,0,0,0.15);
  animation: rise 0.4s ease-out;
}

.auth-card h2 {
  font-family: 'Fraunces', serif;
  font-size: 22px;
  font-weight: 400;
  text-align: center;
  margin-bottom: 24px;
}

.auth-card .form-group {
  margin-bottom: 16px;
}

.auth-card label {
  display: block;
  font-size: 13px;
  color: var(--ink-soft);
  margin-bottom: 6px;
}

.auth-card input {
  width: 100%;
  padding: 12px 16px;
  background: var(--bg-warm);
  border: 1px solid var(--line);
  border-radius: 12px;
  font-family: 'Newsreader', serif;
  font-size: 15px;
  color: var(--ink);
  transition: border-color 0.2s;
}

.auth-card input:focus {
  outline: none;
  border-color: var(--accent);
}

.auth-card .auth-error {
  color: var(--danger);
  font-size: 13px;
  text-align: center;
  margin-bottom: 12px;
  min-height: 18px;
}

.auth-card .btn-primary {
  width: 100%;
  padding: 14px;
  background: var(--accent);
  color: var(--bg-warm);
  border: none;
  border-radius: 14px;
  font-family: 'Newsreader', serif;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.2s;
  margin-top: 8px;
}

.auth-card .btn-primary:hover { background: var(--accent-deep); }

.auth-card .auth-toggle {
  text-align: center;
  margin-top: 16px;
  font-size: 13px;
  color: var(--ink-faint);
}

.auth-card .auth-toggle a {
  color: var(--accent);
  cursor: pointer;
  text-decoration: none;
}

.auth-card .auth-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: none;
  border: none;
  color: var(--ink-faint);
  cursor: pointer;
  font-size: 20px;
}

.auth-card .auth-footer {
  text-align: center;
  margin-top: 16px;
}

.auth-card .auth-footer a {
  color: var(--ink-faint);
  font-size: 13px;
  cursor: pointer;
}

/* ==================== MOBILE ==================== */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    box-shadow: 4px 0 20px var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .btn-menu { display: flex; }
  .chat-header { padding: 14px 18px; }
  .chat-messages { padding: 24px 18px 12px; }
  .composer { padding: 12px 18px 16px; }
  .msg-user { max-width: 88%; }
  .msg-bot { max-width: 95%; }
  .msg-bot .body { font-size: 17px; }
  .welcome-logo { font-size: 34px; }
  .welcome-sub { font-size: 16px; }
  .welcome { padding: 24px; }
}

@supports (padding: max(0px)) {
  .chat-header { padding-top: max(16px, env(safe-area-inset-top)); }
  .composer { padding-bottom: max(20px, env(safe-area-inset-bottom)); }
  .sidebar { padding-top: max(0px, env(safe-area-inset-top)); }
}

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--ink-faint); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--ink-soft); }
