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

:root {
  --bg:        #06061a;
  --surface:   #0d0d24;
  --panel:     rgba(255,255,255,0.045);
  --panel-b:   rgba(255,255,255,0.07);
  --border:    rgba(255,255,255,0.09);
  --accent-a:  #7c3aed;
  --accent-b:  #2563eb;
  --accent-c:  #0ea5e9;
  --grad:      linear-gradient(135deg, var(--accent-a), var(--accent-b));
  --text:      #e8eaf6;
  --text-2:    #9196b0;
  --text-3:    #5c6082;
  --user-bg:     rgba(124,58,237,0.18);
  --user-border: rgba(124,58,237,0.35);
  --ai-bg:       rgba(37,99,235,0.14);
  --ai-border:   rgba(14,165,233,0.3);
  --radius:    18px;
  --radius-sm: 10px;
  --header-h:  58px;
  --footer-h:  100px;
  --safe-top:  env(safe-area-inset-top, 0px);
  --safe-bot:  env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100vh;
  height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

/* ─── App Shell ─────────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;   /* fallback for older browsers */
  height: 100dvh;  /* dynamic viewport: adjusts with Safari toolbar */
  position: relative;
  overflow: hidden;
}

/* ─── Loading Overlay ───────────────────────────────────────────── */
.loading-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  padding-top: calc(24px + var(--safe-top));
  padding-bottom: calc(24px + var(--safe-bot));
  transition: opacity 0.4s ease, visibility 0.4s ease;
}
.loading-overlay.hidden {
  opacity: 0; visibility: hidden; pointer-events: none;
}

.loading-card {
  width: 100%; max-width: 420px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  backdrop-filter: blur(32px);
  -webkit-backdrop-filter: blur(32px);
  padding: 32px 28px;
  text-align: center;
}

.loading-glyph {
  font-size: 48px; line-height: 1;
  background: var(--grad);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 14px;
  animation: pulse-glyph 2s ease-in-out infinite;
}
@keyframes pulse-glyph {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%      { opacity: 0.7; transform: scale(0.92); }
}

.loading-title {
  font-size: 22px; font-weight: 700;
  letter-spacing: -0.5px; margin-bottom: 6px;
}
.loading-sub {
  font-size: 14px; color: var(--text-2);
  margin-bottom: 28px; line-height: 1.6;
}
.loading-note { font-size: 12px; color: var(--text-3); }

.load-rows {
  display: flex; flex-direction: column; gap: 16px; text-align: left;
}
.load-row {
  background: rgba(255,255,255,0.03);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
}
.load-row-top {
  display: flex; align-items: center; gap: 8px; margin-bottom: 8px;
}
.load-name  { font-size: 13px; font-weight: 600; flex: 1; }
.load-size  { font-size: 11px; color: var(--text-3); }
.load-pct   { font-size: 12px; font-weight: 600; color: var(--accent-c); min-width: 34px; text-align: right; }
.load-bar   { height: 4px; background: rgba(255,255,255,0.08); border-radius: 99px; overflow: hidden; margin-bottom: 6px; }
.load-fill  { height: 100%; width: 0%; background: var(--grad); border-radius: 99px; transition: width 0.3s ease; }
.load-msg   { font-size: 11px; color: var(--text-3); }

.load-row.done .load-fill   { background: linear-gradient(90deg,#10b981,#34d399); }
.load-row.done .load-pct    { color: #34d399; }
.load-row.active .load-fill { animation: shimmer 1.5s linear infinite; }
@keyframes shimmer { 0% { opacity: 0.7; } 50% { opacity: 1; } 100%{ opacity: 0.7; } }

.loading-error {
  margin-top: 16px; padding: 10px 14px;
  background: rgba(239,68,68,0.12);
  border: 1px solid rgba(239,68,68,0.3);
  border-radius: var(--radius-sm);
  color: #f87171; font-size: 13px; text-align: left; white-space: pre-wrap;
}

/* ─── Header ──────────────────────────────────────────────────── */
.app-header {
  display: flex; align-items: center; justify-content: space-between;
  height: var(--header-h);
  padding: 0 18px;
  padding-top: var(--safe-top);
  background: rgba(6,6,26,0.85);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

.header-brand { display: flex; align-items: center; gap: 8px; }
.brand-glyph {
  font-size: 22px;
  background: var(--grad);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.brand-name { font-size: 17px; font-weight: 700; letter-spacing: -0.3px; }

.model-badges { display: flex; gap: 7px; }
.badge {
  display: flex; align-items: center; gap: 5px;
  font-size: 11px; font-weight: 600; letter-spacing: 0.5px;
  padding: 4px 9px; border-radius: 99px;
  border: 1px solid var(--border);
  background: var(--panel);
  user-select: none;
}
.badge-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-3); transition: background 0.4s;
}
.badge[data-s="loading"] .badge-dot { background: #f59e0b; animation: blink 1s infinite; }
.badge[data-s="ready"]   .badge-dot { background: #10b981; }
.badge[data-s="error"]   .badge-dot { background: #ef4444; }
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0.3} }

/* ─── Chat Area ─────────────────────────────────────────────────── */
.chat-area {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.chat-area::-webkit-scrollbar { display: none; }

/* Empty state */
.empty-state {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  text-align: center; gap: 10px;
  color: var(--text-2);
  user-select: none;
  padding: 32px;
}
.empty-glyph { font-size: 52px; line-height: 1; margin-bottom: 6px; }
.empty-state p { font-size: 15px; }
.empty-sub { font-size: 13px; color: var(--text-3); }

/* ─── Chat Messages ─────────────────────────────────────────────── */
.msg-row {
  display: flex; flex-direction: column;
  max-width: 85%;
  animation: msg-in 0.25s ease;
}
@keyframes msg-in {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

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

.msg-label {
  font-size: 10px; font-weight: 600;
  letter-spacing: 0.8px; text-transform: uppercase;
  color: var(--text-3);
  margin-bottom: 4px; padding: 0 4px;
}

.msg-bubble {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 15px; line-height: 1.6;
  white-space: pre-wrap; word-break: break-word;
}
.msg-row.user .msg-bubble {
  background: var(--user-bg);
  border: 1px solid var(--user-border);
  border-bottom-right-radius: 5px;
}
.msg-row.ai .msg-bubble {
  background: var(--ai-bg);
  border: 1px solid var(--ai-border);
  border-bottom-left-radius: 5px;
}

/* Streaming cursor */
.msg-bubble.streaming::after {
  content: '▋'; display: inline-block;
  animation: cursor-blink 0.7s infinite;
  margin-left: 2px; color: var(--accent-c);
}
@keyframes cursor-blink { 0%,100%{opacity:1} 50%{opacity:0} }

/* Audio player inside AI message */
.msg-audio {
  margin-top: 8px; display: flex; align-items: center; gap: 10px;
  padding: 8px 12px;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.audio-play-btn {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--grad); border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: opacity 0.2s, transform 0.15s;
}
.audio-play-btn:active { transform: scale(0.92); }
.audio-play-btn svg { fill: white; width: 14px; height: 14px; }
.audio-play-btn[data-playing="true"] svg.play-icon  { display: none; }
.audio-play-btn[data-playing="true"] svg.pause-icon { display: block; }
.audio-play-btn svg.pause-icon { display: none; }
.audio-duration { font-size: 12px; color: var(--text-3); white-space: nowrap; }
.audio-waveform { flex: 1; height: 28px; }

.msg-time {
  font-size: 10px; color: var(--text-3);
  margin-top: 4px; padding: 0 4px;
}

/* Processing / typing indicator */
.typing-indicator {
  display: flex; align-items: center; gap: 5px;
  padding: 14px 18px;
  background: var(--ai-bg);
  border: 1px solid var(--ai-border);
  border-radius: var(--radius);
  border-bottom-left-radius: 5px;
  align-self: flex-start;
  animation: msg-in 0.2s ease;
}
.typing-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent-c);
  animation: typing-bounce 1.2s infinite;
}
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing-bounce {
  0%,60%,100% { transform: translateY(0); opacity: 0.5; }
  30%          { transform: translateY(-6px); opacity: 1; }
}

/* ─── Controls Bar ──────────────────────────────────────────────── */
.controls-bar {
  display: flex;
  align-items: center;
  /* Use a CSS grid to perfectly center the record button regardless of
     sibling widths. Three columns: left (status) | center (button) | right (waveform).
     The center column is always centered in the bar. */
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  padding: 14px 18px;
  /* env(safe-area-inset-bottom) covers the home indicator on notched
     iPhones. Android has no safe-area but its nav bar can overlap,
     so use a generous base padding. */
  padding-bottom: calc(22px + var(--safe-bot));
  min-height: var(--footer-h);
  background: rgba(6,6,26,0.9);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.status-pill {
  justify-self: start;
  min-width: 0;         /* let grid column control width */
  width: fit-content;
  max-width: 100%;
  font-size: 12px; color: var(--text-3);
  padding: 5px 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 99px;
  text-align: center;
  transition: all 0.3s ease;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.status-pill.active {
  color: var(--accent-c);
  border-color: rgba(14,165,233,0.4);
  background: rgba(14,165,233,0.08);
}
.status-pill.error {
  color: #f87171;
  border-color: rgba(239,68,68,0.3);
  background: rgba(239,68,68,0.06);
}

/* Record button */
.record-wrap {
  justify-self: center;
  position: relative;
  display: flex; align-items: center; justify-content: center;
}

.rings {
  position: absolute; inset: -18px;
  pointer-events: none; opacity: 0; transition: opacity 0.3s;
}
.rings.active { opacity: 1; }
.ring {
  position: absolute; inset: 0; border-radius: 50%;
  border: 2px solid var(--accent-a);
  animation: ring-pulse 1.8s ease-out infinite;
}
.r1 { animation-delay: 0s;   opacity: 0.6; }
.r2 { animation-delay: 0.5s; opacity: 0.4; }
.r3 { animation-delay: 1.0s; opacity: 0.2; }
@keyframes ring-pulse {
  0%   { transform: scale(0.85); opacity: inherit; }
  100% { transform: scale(1.6);  opacity: 0; }
}

.record-btn {
  width: 68px; height: 68px; border-radius: 50%;
  background: var(--grad); border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  position: relative; z-index: 1;
  transition: transform 0.15s ease, box-shadow 0.3s ease, opacity 0.3s;
  box-shadow: 0 4px 24px rgba(124,58,237,0.4);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.record-btn:disabled {
  opacity: 0.35; cursor: default; box-shadow: none;
}
.record-btn:not(:disabled):active { transform: scale(0.91); }
.record-btn.recording {
  background: linear-gradient(135deg, #ef4444, #dc2626);
  box-shadow: 0 4px 28px rgba(239,68,68,0.5);
}
.record-btn svg { color: white; width: 26px; height: 26px; }
.record-btn .icon-stop { display: none; }
.record-btn.recording .icon-mic  { display: none; }
.record-btn.recording .icon-stop { display: block; }

/* Waveform canvas */
.waveform-canvas {
  justify-self: end;
  width: 100%;          /* shrink to fit the 1fr column */
  max-width: 180px;     /* never grow larger than design size */
  opacity: 0; transition: opacity 0.3s;
  border-radius: 8px;
}
.waveform-canvas.active { opacity: 1; }

/* ─── Responsive tweaks ─────────────────────────────────────────── */
@media (min-width: 640px) {
  .chat-area   { padding: 28px; }
  .msg-row     { max-width: 72%; }
  .controls-bar { padding-left: 32px; padding-right: 32px; }
}
@media (min-width: 960px) {
  #app { max-width: 700px; margin: 0 auto; }
  body { background: #02020d; }
}

/* ─── Scrollbar (desktop) ───────────────────────────────────────── */
@media (hover: hover) {
  .chat-area::-webkit-scrollbar       { display: block; width: 4px; }
  .chat-area::-webkit-scrollbar-track  { background: transparent; }
  .chat-area::-webkit-scrollbar-thumb  { background: var(--border); border-radius: 99px; }
}

/* ─── LLM Thinking panel ────────────────────────────────────────── */
.llm-thinking {
  margin-top: 6px;
  border: 1px solid rgba(124,58,237,0.3);
  border-radius: var(--radius-sm);
  background: rgba(124,58,237,0.06);
  overflow: hidden;
}

.llm-thinking-toggle {
  display: flex; align-items: center;
  padding: 7px 12px;
  font-size: 11px; font-weight: 600; letter-spacing: 0.5px; text-transform: uppercase;
  color: #a78bfa;
  cursor: pointer; list-style: none; user-select: none;
}
.llm-thinking-toggle::-webkit-details-marker { display: none; }
.llm-thinking-toggle::before {
  content: "▶"; font-size: 8px; margin-right: 6px;
  transition: transform 0.2s; display: inline-block;
}
.llm-thinking[open] .llm-thinking-toggle::before { transform: rotate(90deg); }

.llm-thinking-body {
  padding: 6px 12px 10px;
  border-top: 1px solid rgba(124,58,237,0.15);
  font-size: 12px; color: var(--text-3); line-height: 1.6;
  white-space: pre-wrap;
  max-height: 220px; overflow-y: auto;
}

/* ─── RAG Sources panel ─────────────────────────────────────────── */
.rag-sources {
  margin-top: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: rgba(255,255,255,0.02);
  overflow: hidden;
}

.rag-sources-toggle {
  display: flex; align-items: center;
  padding: 7px 12px;
  font-size: 11px; font-weight: 600;
  color: var(--text-3); letter-spacing: 0.5px; text-transform: uppercase;
  cursor: pointer; list-style: none; user-select: none;
}
.rag-sources-toggle::-webkit-details-marker { display: none; }
.rag-sources-toggle::before {
  content: "▶"; font-size: 8px; margin-right: 6px;
  transition: transform 0.2s; display: inline-block;
}
details[open] .rag-sources-toggle::before { transform: rotate(90deg); }

.rag-sources-list { padding: 4px 8px 10px; }

.rag-source-item {
  margin-bottom: 6px;
  border-radius: 7px;
  background: rgba(255,255,255,0.03);
  border: 1px solid var(--border);
  overflow: hidden;
}
.rag-source-item:last-child { margin-bottom: 0; }

.rag-source-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 7px 8px;
  cursor: pointer; list-style: none; user-select: none;
  gap: 6px;
}
.rag-source-header::-webkit-details-marker { display: none; }
.rag-source-header::before {
  content: "▶"; font-size: 7px; color: var(--text-3);
  transition: transform 0.2s; display: inline-block; flex-shrink: 0;
}
.rag-source-item[open] > .rag-source-header::before { transform: rotate(90deg); }

.rag-source-title {
  font-size: 12px; font-weight: 600; color: var(--text);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  flex: 1; min-width: 0;
}
.rag-source-score {
  font-size: 10px; color: var(--accent-c); font-weight: 600; flex-shrink: 0;
}
.rag-source-body {
  padding: 0 10px 10px;
  border-top: 1px solid var(--border);
}
.rag-source-question {
  font-size: 12px; font-weight: 600; color: var(--text-2);
  margin: 8px 0 4px; line-height: 1.5;
}
.rag-source-question::before { content: "Q: "; color: var(--accent-c); }
.rag-source-answer {
  font-size: 12px; color: var(--text-2); line-height: 1.5; margin: 0;
}
.rag-source-question + .rag-source-answer::before { content: "A: "; color: #a78bfa; }

/* ─── KB nav link (in header) ───────────────────────────────────── */
.kb-nav-link {
  font-size: 12px; font-weight: 600; letter-spacing: 0.5px;
  padding: 5px 12px; border-radius: 99px;
  background: var(--panel); border: 1px solid var(--border);
  color: var(--text-2); text-decoration: none;
  transition: color 0.2s, border-color 0.2s;
  white-space: nowrap; flex-shrink: 0;
}
.kb-nav-link:hover { color: var(--text); border-color: var(--accent-a); }
