:root {
  --bg: #120818;
  --bg2: #1a0f24;
  --panel: #221530;
  --panel2: #2d1b3d;
  --text: #f8eef8;
  --muted: #b89ec4;
  --accent: #e879f9;
  --accent2: #c084fc;
  --accent3: #f472b6;
  --user: #4c1d95;
  --user-text: #f3e8ff;
  --caleb: #2d1b3d;
  --caleb-text: #fce7f3;
  --border: rgba(232, 121, 249, 0.15);
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --header-h: 56px;
  --composer-h: 72px;
  --touch: 48px;
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: 'Nunito', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

body {
  background: radial-gradient(ellipse at top, #2d1040 0%, var(--bg) 60%);
}

a { color: var(--accent); text-decoration: none; }

.input, .textarea {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 14px;
  padding: 14px 16px;
  font: inherit;
  font-size: 16px;
  outline: none;
  transition: border-color 0.15s;
}
.input:focus, .textarea:focus { border-color: var(--accent); }

.label {
  display: block;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--muted);
  margin-bottom: 6px;
}

.field { margin-bottom: 1rem; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--touch);
  min-width: var(--touch);
  padding: 0 18px;
  border: none;
  border-radius: 999px;
  font: inherit;
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.btn-primary {
  width: 100%;
  background: linear-gradient(135deg, var(--accent3), var(--accent));
  color: #1a0520;
  box-shadow: 0 4px 20px rgba(232, 121, 249, 0.35);
}

.btn-icon {
  background: transparent;
  color: var(--text);
  padding: 0;
  width: var(--touch);
  height: var(--touch);
  flex-shrink: 0;
}

.btn-send {
  background: linear-gradient(135deg, var(--accent3), var(--accent));
  color: #1a0520;
  width: var(--touch);
  height: var(--touch);
  padding: 0;
  flex-shrink: 0;
}

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

.alert.err {
  background: rgba(244, 63, 94, 0.15);
  border: 1px solid rgba(244, 63, 94, 0.4);
  color: #fecdd3;
  padding: 12px 14px;
  border-radius: 12px;
  margin-bottom: 1rem;
  font-size: 0.95rem;
}

/* Login */
.login-page {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(24px + var(--safe-top)) calc(20px + var(--safe-right)) calc(24px + var(--safe-bottom)) calc(20px + var(--safe-left));
}

.login-wrap { width: 100%; max-width: 400px; }

.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 28px 24px;
  box-shadow: var(--shadow);
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 1.75rem;
}

.brand-avatar {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent3), var(--accent2));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
  font-weight: 800;
  color: #1a0520;
  flex-shrink: 0;
}

.brand-name {
  font-size: 1.5rem;
  font-weight: 800;
  line-height: 1.1;
}

.brand-tag {
  font-size: 0.9rem;
  color: var(--muted);
  margin-top: 2px;
}

/* App shell */
.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

.header {
  display: flex;
  align-items: center;
  gap: 8px;
  height: calc(var(--header-h) + var(--safe-top));
  padding: var(--safe-top) calc(8px + var(--safe-right)) 0 calc(8px + var(--safe-left));
  background: rgba(18, 8, 24, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

.header-center {
  flex: 1;
  min-width: 0;
  text-align: center;
}

.header-title {
  font-weight: 800;
  font-size: 1.1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-sub {
  font-size: 0.75rem;
  color: var(--muted);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}

/* Drawer */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 90;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}

.drawer-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(320px, 88vw);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  z-index: 100;
  transform: translateX(-100%);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-left: var(--safe-left);
}

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

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

.drawer-head h2 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 800;
}

.drawer-search {
  margin: 12px 16px 8px;
  font-size: 16px;
}

.drawer-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 8px 16px;
}

.session-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  color: var(--text);
  padding: 14px 12px;
  border-radius: 14px;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  min-height: var(--touch);
}

.session-item:active,
.session-item.active {
  background: var(--panel2);
}

.session-item-title {
  font-weight: 700;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item-meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 2px;
}

.drawer-empty {
  padding: 24px 16px;
  color: var(--muted);
  text-align: center;
  font-size: 0.9rem;
}

.drawer-foot {
  padding: 12px 16px calc(12px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  font-size: 0.8rem;
  color: var(--muted);
}

/* Messages */
.messages-wrap {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px calc(12px + var(--safe-right)) 16px calc(12px + var(--safe-left));
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg {
  max-width: 88%;
  padding: 12px 16px;
  border-radius: 20px;
  font-size: 1rem;
  line-height: 1.45;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.msg.user {
  align-self: flex-end;
  background: linear-gradient(135deg, #6b21a8, var(--user));
  color: var(--user-text);
  border-bottom-right-radius: 6px;
}

.msg.assistant {
  align-self: flex-start;
  background: var(--caleb);
  color: var(--caleb-text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}

.msg.typing {
  color: var(--muted);
  font-style: italic;
  padding: 8px 16px;
}

.welcome {
  text-align: center;
  padding: 32px 16px;
  color: var(--muted);
}

.welcome-emoji {
  font-size: 3rem;
  margin-bottom: 12px;
}

.welcome h2 {
  margin: 0 0 8px;
  color: var(--text);
  font-size: 1.3rem;
}

.welcome p {
  margin: 0 0 16px;
  font-size: 0.95rem;
  line-height: 1.5;
}

.welcome-talk {
  width: auto;
  padding: 0 24px;
  margin-top: 8px;
}

.btn-mic {
  background: var(--panel2);
  color: var(--text);
  width: var(--touch);
  height: var(--touch);
  padding: 0;
  flex-shrink: 0;
  border: 1px solid var(--border);
  font-size: 1.2rem;
}

.btn-mic.listening {
  background: linear-gradient(135deg, #dc2626, #f472b6);
  border-color: #f472b6;
  animation: pulse-mic 1.2s ease-in-out infinite;
}

@keyframes pulse-mic {
  0%, 100% { box-shadow: 0 0 0 0 rgba(244, 114, 182, 0.5); }
  50% { box-shadow: 0 0 0 10px rgba(244, 114, 182, 0); }
}

.voice-toggle.on { color: var(--accent); }
.voice-toggle:not(.on) { opacity: 0.5; }

/* Composer */
.composer {
  flex-shrink: 0;
  padding: 10px calc(10px + var(--safe-right)) calc(10px + var(--safe-bottom)) calc(10px + var(--safe-left));
  background: rgba(18, 8, 24, 0.95);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}

.composer-stack { width: 100%; }

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.attach-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding: 0 0 8px;
}

.attach-thumb {
  position: relative;
  width: 64px;
  height: 64px;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.attach-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.attach-thumb button {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 22px;
  height: 22px;
  border: none;
  border-radius: 50%;
  background: rgba(0,0,0,0.65);
  color: #fff;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
}

.btn-attach {
  background: var(--panel2);
  color: var(--text);
  width: var(--touch);
  height: var(--touch);
  padding: 0;
  flex-shrink: 0;
  border: 1px solid var(--border);
  font-size: 1.1rem;
  cursor: pointer;
}

.msg-media {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.msg-media img {
  max-width: 220px;
  max-height: 220px;
  border-radius: 12px;
  object-fit: cover;
}

.composer .textarea {
  flex: 1;
  min-height: 48px;
  max-height: 120px;
  resize: none;
  border-radius: 24px;
  padding: 12px 18px;
  line-height: 1.35;
}

.status-bar {
  text-align: center;
  font-size: 0.8rem;
  color: var(--muted);
  padding: 4px 16px;
  min-height: 24px;
}

.status-bar.error { color: #fca5a5; }

@media (min-width: 768px) {
  .messages { padding: 24px; max-width: 720px; margin: 0 auto; width: 100%; }
  .composer { max-width: 720px; margin: 0 auto; width: 100%; border-radius: 0; }
  .msg { max-width: 75%; }
}
