/* ============================================================
   AM Insights · Mobile · Chat
   ============================================================ */

:root {
  --bg: #f9f8f5;
  --surface: #ffffff;
  --surface-2: #f3f2ee;
  --border: #e8e6df;
  --border-2: #d8d6cd;
  --text: #1c2954;
  --text-2: #4a5478;
  --text-3: #8a93b3;

  --navy: #1c2954;
  --navy-2: #2c3a73;
  --gold: #c9a154;
  --gold-2: #e8c987;
  --gold-bg: #f7eed8;

  --user-bg: #1c2954;
  --bot-bg: #ffffff;
  --error-bg: #fef2f2;
  --error-text: #b91c1c;

  --shadow-sm: 0 1px 3px rgba(28, 41, 84, 0.08);
  --shadow-md: 0 8px 24px rgba(28, 41, 84, 0.12);
  --shadow-drawer: 0 20px 60px rgba(28, 41, 84, 0.3);

  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
  --safe-left: env(safe-area-inset-left);
  --safe-right: env(safe-area-inset-right);

  --appbar-h: 56px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { min-height: 100%; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Inter', sans-serif;
  font-size: 14.5px;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  overflow: hidden;
}
button { font: inherit; cursor: pointer; border: 0; background: none; color: inherit; -webkit-tap-highlight-color: transparent; text-align: left; }
a { color: inherit; text-decoration: none; -webkit-tap-highlight-color: transparent; }
ul { list-style: none; }
img, svg { display: block; max-width: 100%; }

/* ============ APP BAR ============ */
.appbar {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  gap: 8px;
  align-items: center;
  padding: var(--safe-top) max(12px, var(--safe-right)) 0 max(12px, var(--safe-left));
  height: calc(var(--appbar-h) + var(--safe-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.appbar-icon {
  width: 40px; height: 40px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  color: var(--text);
  transition: background 0.15s var(--ease), transform 0.2s var(--ease);
}
.appbar-icon:active { background: var(--surface-2); transform: scale(0.94); }
.appbar-icon svg { width: 22px; height: 22px; }
.appbar-icon:last-child { justify-self: end; }
.appbar-meta { text-align: center; min-width: 0; }
.appbar-eyebrow {
  font-family: 'JetBrains Mono', monospace;
  font-size: 9.5px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-3);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  line-height: 1;
}
.grounded-dot {
  width: 6px; height: 6px;
  background: var(--gold);
  border-radius: 50%;
  box-shadow: 0 0 6px var(--gold);
  animation: gpulse 2s infinite;
}
@keyframes gpulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}
.appbar-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ============ CHAT STREAM ============ */
.chat-stream {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px max(14px, var(--safe-left));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Empty state */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 24px 12px;
  gap: 12px;
}
.es-mark {
  width: 64px; height: 64px;
  margin-bottom: 4px;
}
.es-mark svg { width: 100%; height: 100%; }
.empty-state h2 {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
  line-height: 1.15;
  margin-top: 8px;
}
.empty-state p {
  font-size: 14px;
  color: var(--text-2);
  max-width: 32ch;
  margin-bottom: 12px;
}

.suggestions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: 480px;
  margin-top: 8px;
}
.sug {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 100%;
  text-align: left;
  transition: background 0.15s var(--ease), transform 0.15s var(--ease), border-color 0.15s var(--ease);
}
.sug:active {
  background: var(--gold-bg);
  border-color: var(--gold-2);
  transform: scale(0.99);
}
.sug-cat {
  font-family: 'JetBrains Mono', monospace;
  font-size: 9.5px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--gold);
  background: var(--gold-bg);
  padding: 4px 8px;
  border-radius: 999px;
  font-weight: 700;
  flex-shrink: 0;
  border: 1px solid rgba(201, 161, 84, 0.3);
}
.sug-q {
  font-size: 13.5px;
  color: var(--text);
  font-weight: 500;
  line-height: 1.35;
}

/* Messages */
.msg {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}
.msg-user {
  flex-direction: row-reverse;
  text-align: left;
}
.msg-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  font-size: 12px;
  font-weight: 700;
}
.msg-user .msg-avatar {
  background: var(--navy);
  color: #fff;
}
.msg-bot .msg-avatar {
  background: var(--gold-bg);
  color: var(--gold);
  border: 1px solid var(--gold-2);
}
.msg-bot .msg-avatar svg {
  width: 20px;
  height: 20px;
}
.msg-body {
  max-width: 78%;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.msg-name {
  font-size: 11px;
  color: var(--text-3);
  font-weight: 600;
  padding: 0 4px;
}
.msg-user .msg-body { align-items: flex-end; }
.msg-text {
  background: var(--bot-bg);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 16px;
  border: 1px solid var(--border);
  font-size: 14px;
  line-height: 1.45;
  word-wrap: break-word;
  border-bottom-left-radius: 4px;
}
.msg-user .msg-text {
  background: var(--user-bg);
  color: #fff;
  border: 0;
  border-bottom-right-radius: 4px;
  border-bottom-left-radius: 16px;
}
.msg-error .msg-text {
  background: var(--error-bg);
  color: var(--error-text);
  border-color: #fecaca;
}

/* Typing indicator */
.typing {
  display: inline-flex;
  gap: 5px;
  padding: 14px 18px;
  background: var(--bot-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  border-bottom-left-radius: 4px;
}
.typing span {
  width: 7px; height: 7px;
  background: var(--text-3);
  border-radius: 50%;
  animation: typebounce 1.4s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typebounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.3; }
  30% { transform: translateY(-6px); opacity: 1; }
}

/* ============ COMPOSER ============ */
.composer-wrap {
  flex-shrink: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 10px max(12px, var(--safe-right)) max(8px, var(--safe-bottom)) max(12px, var(--safe-left));
}
.composer {
  display: grid;
  grid-template-columns: 36px 1fr 40px;
  gap: 8px;
  align-items: end;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 4px;
  transition: border-color 0.2s var(--ease);
}
.composer:focus-within { border-color: var(--gold-2); }
.comp-icon {
  width: 36px; height: 36px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: var(--text-3);
}
.comp-icon:active { background: rgba(0,0,0,0.06); }
.comp-icon svg { width: 18px; height: 18px; }
#composer-input {
  font-family: inherit;
  font-size: 15px;
  color: var(--text);
  background: transparent;
  border: 0;
  outline: 0;
  resize: none;
  padding: 9px 4px;
  min-height: 24px;
  max-height: 140px;
  line-height: 1.4;
}
#composer-input::placeholder {
  color: var(--text-3);
}
.send-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--navy);
  color: #fff;
  display: grid;
  place-items: center;
  transition: background 0.2s var(--ease), transform 0.15s var(--ease), opacity 0.2s var(--ease);
}
.send-btn:not(:disabled):active { transform: scale(0.94); background: var(--navy-2); }
.send-btn:disabled { opacity: 0.4; pointer-events: none; }
.send-btn svg { width: 16px; height: 16px; }

.comp-foot {
  margin-top: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  font-size: 10.5px;
  color: var(--text-3);
}
.comp-foot > span:last-child {
  flex: 1;
  text-align: right;
}
.grounded-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--gold-bg);
  color: var(--gold);
  padding: 3px 8px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 10.5px;
  border: 1px solid rgba(201, 161, 84, 0.3);
}
.grounded-chip svg { width: 11px; height: 11px; }

/* ============ DRAWER ============ */
.drawer {
  position: fixed;
  inset: 0;
  z-index: 100;
  pointer-events: none;
}
.drawer.is-open { pointer-events: auto; }
.drawer-scrim {
  position: absolute;
  inset: 0;
  background: rgba(28, 41, 84, 0.5);
  opacity: 0;
  transition: opacity 0.3s var(--ease);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}
.drawer.is-open .drawer-scrim { opacity: 1; }

.drawer-panel {
  position: absolute;
  top: 0; bottom: 0;
  left: 0;
  width: min(85%, 320px);
  background: var(--surface);
  box-shadow: var(--shadow-drawer);
  transform: translateX(-100%);
  transition: transform 0.3s var(--ease);
  display: flex;
  flex-direction: column;
  padding: var(--safe-top) 0 var(--safe-bottom);
  overflow: hidden;
}
.drawer.is-open .drawer-panel { transform: translateX(0); }

.drawer-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--navy);
  color: #fff;
}
.logo-pane {
  flex: 1;
  display: flex;
  align-items: center;
  height: 32px;
}
.brand-logo {
  height: 28px;
  width: auto;
  filter: brightness(0) invert(1);
}
.drawer-close {
  width: 36px; height: 36px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  color: #fff;
}
.drawer-close:active { background: rgba(255,255,255,0.12); }
.drawer-close svg { width: 18px; height: 18px; }

.drawer-new {
  margin: 14px 14px 8px;
  padding: 11px 14px;
  background: var(--gold-bg);
  color: var(--text);
  border: 1px solid var(--gold-2);
  border-radius: 12px;
  font-weight: 600;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: background 0.15s var(--ease);
}
.drawer-new:active { background: var(--gold-2); }
.drawer-new svg { width: 16px; height: 16px; color: var(--gold); }

.drawer-section {
  padding: 12px 14px 14px;
  border-bottom: 1px solid var(--border);
  overflow-y: auto;
}
.drawer-section:last-of-type { border-bottom: 0; }
.drawer-h {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--text-3);
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.src-count {
  background: var(--surface-2);
  color: var(--text-2);
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 10px;
  letter-spacing: 0;
}

.chat-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.chat-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 10px;
  border-radius: 8px;
  font-size: 13px;
  color: var(--text-2);
  cursor: pointer;
  transition: background 0.15s var(--ease);
}
.chat-item svg {
  width: 14px;
  height: 14px;
  color: var(--text-3);
  flex-shrink: 0;
}
.chat-item:active { background: var(--surface-2); }
.chat-item.is-active {
  background: var(--gold-bg);
  color: var(--text);
  font-weight: 600;
}
.chat-item.is-active svg { color: var(--gold); }
.chat-item span {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.src-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}
.src {
  display: grid;
  grid-template-columns: 28px 1fr 8px;
  gap: 10px;
  padding: 8px 6px;
  align-items: center;
  border-radius: 8px;
}
.src:active { background: var(--surface-2); }
.src-icon { width: 28px; height: 28px; }
.src-icon svg { width: 28px; height: 28px; }
.src-meta { min-width: 0; }
.src-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.src-info {
  font-size: 10.5px;
  color: var(--text-3);
  font-family: 'JetBrains Mono', monospace;
  margin-top: 1px;
}
.src-status {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--text-3);
}
.src-status.synced { background: #22c55e; box-shadow: 0 0 6px rgba(34, 197, 94, 0.5); }

.upload-more {
  width: 100%;
  margin-top: 6px;
  padding: 10px;
  border: 1px dashed var(--border-2);
  border-radius: 10px;
  font-size: 12.5px;
  color: var(--text-2);
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.upload-more svg { width: 14px; height: 14px; }
.upload-more:active { background: var(--surface-2); }

.drawer-foot {
  margin-top: auto;
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.user-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--navy), var(--navy-2));
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 13px;
  flex-shrink: 0;
}
.u-name { font-size: 13px; font-weight: 600; color: var(--text); line-height: 1.2; }
.u-role {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  color: var(--text-3);
  letter-spacing: 0.04em;
}
.back-hub {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11.5px;
  color: var(--text-2);
  font-weight: 500;
  padding: 4px 0;
}
.back-hub:active { color: var(--text); }
.back-hub svg { width: 14px; height: 14px; }

/* Constrain to phone width on desktop */
@media (min-width: 720px) {
  body {
    max-width: 480px;
    margin: 0 auto;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
    background: var(--bg);
  }
  .drawer-panel { left: max(0px, calc(50vw - 240px)); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
