:root {
  --bg: #F7F6F2;
  --surface: #FFFFFF;
  --surface-subtle: #EFEDE7;
  --ink: #1C1B19;
  --ink-secondary: #6E6A63;
  --ink-muted: #9C968C;
  --bubble-user: #222522;
  --bubble-user-ink: #FAF9F6;
  --bubble-assistant: #FFFFFF;
  --border: rgba(28, 27, 25, 0.08);
  --border-strong: rgba(28, 27, 25, 0.14);
  --shadow-soft: 0 1px 2px rgba(28, 27, 25, 0.04);
  --shadow-float: 0 12px 32px rgba(28, 27, 25, 0.12);
  --radius-bubble: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", sans-serif;
  font-size: 15.5px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.app-shell {
  position: relative;
  height: 100dvh;
  width: 100%;
  display: flex;
  justify-content: center;
  background: var(--bg);
}

/* Chat stage: mobile-first full-bleed, centered quiet column on desktop */
.chat-stage {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 740px;
  height: 100%;
  background: var(--bg);
  position: relative;
}

.topbar {
  flex-shrink: 0;
  height: calc(52px + var(--safe-top));
  padding: var(--safe-top) 12px 0;
  display: grid;
  grid-template-columns: 76px 1fr 76px;
  align-items: center;
  background: rgba(247, 246, 242, 0.88);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

.topbar-center {
  text-align: center;
}

.topbar-name {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--ink);
}

.topbar-right {
  display: flex;
  justify-content: flex-end;
  gap: 2px;
}

.icon-btn {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--ink-secondary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 140ms ease, color 140ms ease;
}

.icon-btn:hover {
  background: var(--surface-subtle);
  color: var(--ink);
}

.icon-btn:active {
  transform: scale(0.95);
}

/* Messages area */
.messages-scroll {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior-y: contain;
  padding: 20px 16px 24px;
  display: flex;
  flex-direction: column;
}

.empty-state {
  margin: auto 0;
  text-align: center;
  padding: 40px 20px;
  user-select: none;
}

.empty-title {
  margin: 0;
  font-size: 15px;
  color: var(--ink-muted);
  letter-spacing: 0.04em;
}

.messages-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
}

.msg-row {
  display: flex;
  flex-direction: column;
  max-width: 82%;
  animation: fadeUp 160ms ease-out;
}

.msg-row.user {
  align-self: flex-end;
  align-items: flex-end;
}

.msg-row.assistant {
  align-self: flex-start;
  align-items: flex-start;
}

.bubble {
  padding: 10px 14px;
  border-radius: var(--radius-bubble);
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 15.5px;
  line-height: 1.58;
}

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

.msg-row.assistant .bubble {
  background: var(--bubble-assistant);
  color: var(--ink);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-soft);
  border-bottom-left-radius: 6px;
}

.msg-row.assistant.streaming .bubble::after {
  content: "";
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-left: 6px;
  border-radius: 50%;
  background: var(--ink-muted);
  vertical-align: middle;
  animation: pulse 900ms ease-in-out infinite;
}

.msg-actions {
  margin-top: 4px;
  padding-left: 4px;
  display: flex;
  gap: 10px;
}

.subtle-action {
  border: none;
  background: transparent;
  color: var(--ink-muted);
  font-size: 12px;
  padding: 2px 4px;
  cursor: pointer;
  border-radius: 6px;
  transition: color 120ms ease, background 120ms ease;
}

.subtle-action:hover {
  color: var(--ink-secondary);
  background: var(--surface-subtle);
}

/* Composer */
.composer-wrap {
  flex-shrink: 0;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  background: rgba(247, 246, 242, 0.92);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
}

.composer-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 22px;
  padding: 6px 6px 6px 14px;
  box-shadow: var(--shadow-soft);
  transition: border-color 140ms ease;
}

.composer-form:focus-within {
  border-color: rgba(28, 27, 25, 0.32);
}

.composer-input {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--ink);
  font-family: inherit;
  font-size: 16px; /* >=16px prevents iOS Safari auto-zoom */
  line-height: 1.45;
  max-height: 120px;
  padding: 5px 0;
}

.composer-input::placeholder {
  color: var(--ink-muted);
}

.send-btn {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: var(--bubble-user);
  color: var(--bubble-user-ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: opacity 140ms ease, transform 120ms ease;
}

.send-btn:disabled {
  opacity: 0.25;
  cursor: not-allowed;
}

.send-btn:not(:disabled):active {
  transform: scale(0.92);
}

.stop-btn {
  background: #57534E;
}

/* Slide-over conversation drawer */
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(28, 27, 25, 0.26);
  z-index: 30;
  animation: fadeIn 160ms ease;
}

.drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(290px, 82vw);
  background: var(--surface);
  border-right: 1px solid var(--border);
  box-shadow: var(--shadow-float);
  z-index: 40;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  transform: translateX(-104%);
  transition: transform 220ms cubic-bezier(0.22, 1, 0.36, 1);
}

.drawer.open {
  transform: translateX(0);
}

.drawer-header {
  height: 52px;
  padding: 0 12px 0 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

.drawer-title {
  font-size: 15px;
  font-weight: 600;
}

.drawer-actions {
  padding: 12px;
}

.new-conv-btn {
  width: 100%;
  height: 40px;
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
}

.new-conv-btn:hover {
  background: var(--surface-subtle);
}

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: 10px;
  transition: background 120ms ease;
}

.conv-item.active {
  background: var(--surface-subtle);
}

.conv-select {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  padding: 10px 10px;
  text-align: left;
  font-size: 14px;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

.conv-del {
  flex-shrink: 0;
  width: 30px;
  height: 30px;
  border: none;
  background: transparent;
  color: var(--ink-muted);
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  opacity: 0.7;
}

.conv-del:hover {
  color: #B91C1C;
  background: rgba(185, 28, 28, 0.08);
  opacity: 1;
}

.drawer-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.text-link-btn {
  border: none;
  background: transparent;
  color: var(--ink-secondary);
  font-size: 13px;
  padding: 4px 0;
  cursor: pointer;
}

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

/* About bottom sheet / modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(28, 27, 25, 0.3);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
  padding: 12px;
  padding-bottom: calc(12px + var(--safe-bottom));
}

.modal-overlay[hidden] {
  display: none;
}

.modal-sheet {
  width: 100%;
  max-width: 440px;
  background: var(--surface);
  border-radius: 20px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-float);
  overflow: hidden;
  animation: fadeUp 180ms ease-out;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 14px 10px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-head h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.modal-body {
  padding: 16px 20px 22px;
  color: var(--ink-secondary);
  font-size: 14.5px;
  line-height: 1.65;
}

.modal-body p {
  margin: 0 0 10px;
}

.modal-body p:last-child {
  margin-bottom: 0;
}

/* Toast */
.toast {
  position: absolute;
  bottom: calc(74px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--bubble-user);
  color: var(--bubble-user-ink);
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 999px;
  box-shadow: var(--shadow-float);
  z-index: 25;
  white-space: nowrap;
}

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

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes pulse {
  0%, 100% { opacity: 0.25; }
  50% { opacity: 0.9; }
}

@media (min-width: 768px) {
  .messages-scroll {
    padding: 28px 24px 32px;
  }
  .composer-wrap {
    padding: 12px 24px 20px;
  }
  .modal-overlay {
    align-items: center;
  }
  .msg-row {
    max-width: 74%;
  }
}

.turnstile-bar {
  max-width: var(--max-col);
  margin: 0 auto 8px;
  display: flex;
  justify-content: center;
}
