/* ── Design tokens ── */
:root {
  --sidebar-width: 260px;
  --sidebar-bg: #f9fafb;
  --main-bg: #ffffff;
  --brand: #0f766e;
  --brand-light: #14b8a6;
  --brand-hover: #0d6b63;
  --brand-xlight: #f0fdfa;
  --brand-subtle: #ccfbf1;
  --text: #111827;
  --text-muted: #6b7280;
  --text-light: #9ca3af;
  --border: #e5e7eb;
  --border-light: #f3f4f6;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.07), 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-md: 0 4px 24px rgba(0, 0, 0, 0.1), 0 2px 8px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.12);
  --radius: 14px;
  --radius-sm: 8px;
  --radius-xs: 6px;
}

/* ── Reset ── */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--main-bg);
  color: var(--text);
  display: flex;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
}

.hidden {
  display: none !important;
}

/* ── Sidebar ── */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: min-width 0.25s ease, width 0.25s ease;
  overflow: hidden;
  position: relative;
  z-index: 20;
}

.sidebar.collapsed {
  width: 0;
  min-width: 0;
  border-right: none;
}

.sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 12px 10px;
  flex-shrink: 0;
  gap: 8px;
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
}

.sidebar-logo-img {
  object-fit: contain;
  flex-shrink: 0;
}

.sidebar-logo-text {
  font-size: 19px;
  font-weight: 700;
  color: var(--brand);
  letter-spacing: -0.5px;
  white-space: nowrap;
}

.sidebar-actions {
  padding: 0 8px 8px;
  flex-shrink: 0;
}

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 9px 12px;
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
  box-shadow: var(--shadow-sm);
}

.new-chat-btn:hover {
  background: #f3f4f6;
  border-color: #d1d5db;
}

.sidebar-section-label {
  padding: 8px 16px 4px;
  font-size: 10.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-light);
  white-space: nowrap;
  flex-shrink: 0;
}

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.session-list::-webkit-scrollbar {
  width: 4px;
}

.session-list::-webkit-scrollbar-track {
  background: transparent;
}

.session-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 99px;
}

.session-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  background: transparent;
  border: none;
  border-radius: var(--radius-xs);
  font-size: 13.5px;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item:hover {
  background: #f0f0ef;
  color: var(--text);
}

.session-item.active {
  background: var(--brand-xlight);
  color: var(--brand);
  font-weight: 500;
}

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

.sidebar-login-cta {
  width: 100%;
  padding: 10px 11px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--brand-xlight);
}

.sidebar-login-copy {
  color: var(--brand);
  font-size: 12.5px;
  font-weight: 600;
  line-height: 1.35;
  margin-bottom: 10px;
}

.sidebar-login-btn {
  width: 100%;
  padding: 8px 11px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--brand);
  background: var(--brand);
  color: white;
  text-align: center;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.sidebar-login-btn:hover {
  background: var(--brand-hover);
}

.profile-trigger {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 8px 10px;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13.5px;
  color: var(--text);
  transition: background 0.15s;
  white-space: nowrap;
  overflow: hidden;
}

.profile-trigger:hover {
  background: #f0f0ef;
}

.avatar {
  width: 30px;
  height: 30px;
  background: linear-gradient(135deg, var(--brand-light), var(--brand));
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  flex-shrink: 0;
  font-size: 13px;
  font-weight: 600;
}

.profile-username {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: left;
  font-weight: 500;
}

.chevron {
  flex-shrink: 0;
  color: var(--text-light);
  transition: transform 0.2s;
}

.profile-trigger[aria-expanded="true"] .chevron {
  transform: rotate(180deg);
}

.auth-status-badge {
  font-size: 11px;
  color: var(--text-light);
  padding: 0 10px 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Main area ── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
  position: relative;
  background: var(--main-bg);
}

/* Topbar */
.chat-topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border-light);
  flex-shrink: 0;
  min-height: 52px;
}

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

.crisis-link {
  font-size: 11.5px;
  color: #dc2626;
  text-decoration: none;
  font-weight: 600;
  white-space: nowrap;
  padding: 4px 10px;
  border-radius: 99px;
  border: 1px solid #fecaca;
  background: #fff8f8;
  transition: background 0.15s;
  flex-shrink: 0;
}

.crisis-link:hover {
  background: #fee2e2;
}

/* Icon buttons */
.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  background: transparent;
  border: none;
  border-radius: var(--radius-xs);
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: #f3f4f6;
  color: var(--text);
}

/* ── Welcome screen ── */
.welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 24px 24px;
  text-align: center;
  gap: 14px;
  overflow-y: auto;
}

.welcome.hidden {
  display: none;
}

.welcome-logo {
  margin-bottom: 4px;
}

.welcome-logo-img {
  object-fit: contain;
  filter: drop-shadow(0 6px 16px rgba(15, 118, 110, 0.2));
}

.welcome-title {
  font-size: 26px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.5px;
  line-height: 1.2;
}

.welcome-subtitle {
  font-size: 15px;
  color: var(--text-muted);
  max-width: 580px;
  line-height: 1.65;
}

.welcome-faq {
  margin-top: 6px;
  font-size: 15px;
}

.welcome-faq a {
  color: var(--brand);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.welcome-faq a:hover {
  color: var(--brand-hover);
}

.suggestions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 10px;
  max-width: 560px;
  width: 100%;
}

.suggestion-chip {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  padding: 13px 15px;
  background: #fafafa;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 13.5px;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  line-height: 1.45;
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
  font-family: inherit;
}

.suggestion-chip:hover {
  border-color: var(--brand-light);
  background: var(--brand-xlight);
  box-shadow: 0 0 0 3px rgba(20, 184, 166, 0.1);
}

.chip-icon {
  font-size: 17px;
  flex-shrink: 0;
  line-height: 1.35;
}

/* ── Chat messages ── */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.chat-messages::-webkit-scrollbar {
  width: 5px;
}

.chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 99px;
}

.chat-messages.hidden {
  display: none;
}

.msg-row {
  display: flex;
  gap: 10px;
  padding: 6px 0;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  align-items: flex-start;
}

.msg-row.user {
  flex-direction: row-reverse;
}

.msg-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  margin-top: 3px;
}

.msg-row.user .msg-avatar {
  background: linear-gradient(135deg, var(--brand-light), var(--brand));
  color: white;
}

.msg-row.assistant .msg-avatar {
  background: transparent;
  border: none;
}

.msg-bubble {
  padding: 11px 15px;
  border-radius: 16px;
  max-width: calc(100% - 48px);
  font-size: 15px;
  line-height: 1.65;
  word-break: break-word;
}

.msg-row.user .msg-bubble {
  background: var(--brand);
  color: white;
  border-bottom-right-radius: 4px;
}

.msg-row.assistant .msg-bubble {
  background: #f7f7f5;
  color: var(--text);
  border-bottom-left-radius: 4px;
  border: 1px solid var(--border-light);
}

.msg-row.assistant.crisis .msg-bubble {
  border-color: #fca5a5;
  background: #fff1f2;
}

.msg-bubble.pending {
  color: var(--text-muted);
  font-style: italic;
}

.msg-bubble .inline-signin-cta {
  margin-top: 12px;
  display: inline-block;
}

/* Markdown inside messages */
.msg-bubble p {
  margin-bottom: 10px;
}

.msg-bubble p:last-child {
  margin-bottom: 0;
}

.msg-bubble ul,
.msg-bubble ol {
  padding-left: 20px;
  margin-bottom: 10px;
}

.msg-bubble li {
  margin-bottom: 5px;
}

.msg-bubble li:last-child {
  margin-bottom: 0;
}

.msg-bubble strong {
  font-weight: 600;
}

.msg-bubble em {
  font-style: italic;
}

.msg-bubble code {
  font-family: "SF Mono", "Fira Code", monospace;
  font-size: 13px;
  background: rgba(0, 0, 0, 0.07);
  padding: 2px 5px;
  border-radius: 4px;
}

.msg-row.user .msg-bubble code {
  background: rgba(255, 255, 255, 0.2);
}

.msg-bubble pre {
  background: rgba(0, 0, 0, 0.06);
  border-radius: var(--radius-sm);
  padding: 12px;
  overflow-x: auto;
  margin-bottom: 10px;
}

.msg-bubble pre code {
  background: none;
  padding: 0;
  font-size: 13px;
}

.msg-bubble h1,
.msg-bubble h2,
.msg-bubble h3 {
  font-weight: 600;
  margin-bottom: 8px;
  margin-top: 14px;
  line-height: 1.3;
}

.msg-bubble h1:first-child,
.msg-bubble h2:first-child,
.msg-bubble h3:first-child {
  margin-top: 0;
}

.msg-bubble h1 {
  font-size: 17px;
}

.msg-bubble h2 {
  font-size: 16px;
}

.msg-bubble h3 {
  font-size: 15px;
}

.msg-bubble a {
  color: var(--brand);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.msg-row.user .msg-bubble a {
  color: rgba(255, 255, 255, 0.88);
}

.msg-bubble blockquote {
  border-left: 3px solid rgba(0, 0, 0, 0.12);
  padding-left: 12px;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.msg-bubble hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}

/* Typing indicator */
.typing-dots {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 4px 0;
}

.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: blink 1.2s ease-in-out infinite;
}

.typing-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes blink {
  0%,
  80%,
  100% {
    opacity: 0.25;
  }
  40% {
    opacity: 1;
  }
}

/* ── Input area ── */
.chat-input-wrapper {
  padding: 10px 12px 14px;
  flex-shrink: 0;
  background: var(--main-bg);
}

.chat-form {
  max-width: 760px;
  margin: 0 auto;
}

.chat-input-inner {
  display: flex;
  align-items: center;
  gap: 6px;
  background: #f7f7f5;
  border: 1.5px solid var(--border);
  border-radius: 16px;
  padding: 10px 10px 10px 16px;
  transition: border-color 0.2s, background 0.2s, box-shadow 0.2s;
}

.chat-input-inner:focus-within {
  border-color: var(--brand-light);
  background: white;
  box-shadow: 0 0 0 3px rgba(20, 184, 166, 0.12);
}

#chat-text {
  flex: 1;
  border: none;
  background: transparent;
  resize: none;
  font: inherit;
  font-size: 15px;
  line-height: 1.5;
  min-height: 24px;
  max-height: 180px;
  overflow-y: auto;
  outline: none;
  color: var(--text);
  padding: 2px 0;
  scrollbar-width: thin;
}

#chat-text::placeholder {
  color: var(--text-light);
}

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--brand);
  border: none;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.1s, opacity 0.15s;
}

.send-btn:hover {
  background: var(--brand-hover);
}

.send-btn:active {
  transform: scale(0.93);
}

.send-btn:disabled {
  opacity: 0.38;
  cursor: not-allowed;
  transform: none;
}

.input-disclaimer {
  text-align: center;
  font-size: 11px;
  color: var(--text-light);
  margin-top: 8px;
  max-width: 760px;
  margin-left: auto;
  margin-right: auto;
  line-height: 1.5;
}

.input-disclaimer a {
  color: var(--brand);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.input-disclaimer a:hover {
  color: var(--brand-hover);
}

/* ── Profile modal ── */
.profile-modal {
  position: fixed;
  bottom: 80px;
  left: 12px;
  width: 340px;
  max-height: calc(100vh - 108px);
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  z-index: 100;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.profile-modal.hidden {
  display: none;
}

.profile-modal-inner {
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  scrollbar-width: thin;
}

.profile-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.profile-modal-header h3 {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.profile-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.profile-section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.profile-section-title {
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-light);
}

.profile-label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12.5px;
  color: var(--text-muted);
  font-weight: 500;
}

.profile-input {
  width: 100%;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font: inherit;
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s;
  color: var(--text);
}

.profile-input:focus {
  border-color: var(--brand-light);
}

#profile-fields {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field label {
  font-size: 11.5px;
  color: var(--text-muted);
  font-weight: 500;
}

.field input {
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 10px;
  font: inherit;
  font-size: 13.5px;
  outline: none;
  transition: border-color 0.15s;
  color: var(--text);
}

.field input:focus {
  border-color: var(--brand-light);
}

.meta {
  font-size: 11.5px;
  color: var(--text-light);
}

.btn-primary {
  padding: 9px 16px;
  background: var(--brand);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  text-align: center;
  width: 100%;
}

.btn-primary:hover {
  background: var(--brand-hover);
}

.btn-ghost {
  padding: 6px 10px;
  background: transparent;
  color: var(--brand);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-ghost:hover {
  background: var(--brand-xlight);
}

.btn-small {
  padding: 4px 8px;
  font-size: 11.5px;
}

#signout-btn {
  width: 100%;
}

/* ── Mobile overlay ── */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 15;
  backdrop-filter: blur(1px);
}

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

/* ── Responsive ── */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: var(--sidebar-width) !important;
    min-width: var(--sidebar-width) !important;
    transform: translateX(-100%);
    z-index: 20;
    transition: transform 0.25s ease;
  }

  .sidebar.mobile-open {
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
  }

  .sidebar.collapsed {
    transform: translateX(-100%);
  }

  /* On mobile the in-sidebar toggle is redundant; use topbar one only */
  #sidebar-toggle {
    display: none;
  }


  .main {
    width: 100%;
  }

  .suggestions {
    grid-template-columns: 1fr;
    max-width: 400px;
  }

  .welcome-title {
    font-size: 22px;
  }

  .welcome-subtitle {
    font-size: 14px;
  }

  .profile-modal {
    left: 8px;
    right: 8px;
    width: auto;
    bottom: 0;
    border-radius: var(--radius) var(--radius) 0 0;
    max-height: 80vh;
  }

  .msg-row {
    padding: 4px 0;
  }

  .chat-input-wrapper {
    padding: 8px 10px 12px;
  }
}

/* Hide the in-sidebar toggle on desktop — the topbar hamburger handles both open/collapse */
@media (min-width: 769px) {
  #sidebar-toggle {
    display: none;
  }
}

/* ── Scrollbar global ── */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
