/* ─── Design Tokens ──────────────────────────────────────────────────────────── */
:root {
  --cf-orange: #F6821F;
  --cf-orange-light: #FF9A45;
  --cf-orange-dim: rgba(246, 130, 31, 0.15);
  --cf-orange-glow: rgba(246, 130, 31, 0.35);

  --bg-900: #0A0B0E;
  --bg-800: #111318;
  --bg-700: #181B22;
  --bg-600: #1E2230;
  --surface: #1A1D27;
  --surface-hover: #22263A;
  --border: rgba(255,255,255,0.08);
  --border-strong: rgba(255,255,255,0.14);

  --text-100: #F0F2FF;
  --text-200: #C8CCDD;
  --text-300: #8B90A8;
  --text-400: #565A72;

  --green: #22D3A4;
  --yellow: #FBBF24;
  --red: #F87171;

  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-xl: 28px;

  --font-sans: 'Inter', system-ui, -apple-system, sans-serif;
  --font-mono: 'JetBrains Mono', monospace;

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.4);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.5);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.6);
  --shadow-glow: 0 0 24px var(--cf-orange-glow);
}

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

html {
  height: 100%;
  font-size: 16px;
}

body {
  height: 100%;
  font-family: var(--font-sans);
  background: var(--bg-900);
  color: var(--text-100);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

/* ─── Background ─────────────────────────────────────────────────────────────── */
.bg-mesh {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse 60% 40% at 10% 0%, rgba(246,130,31,0.12) 0%, transparent 60%),
    radial-gradient(ellipse 50% 50% at 90% 100%, rgba(99,102,241,0.08) 0%, transparent 60%),
    radial-gradient(ellipse 80% 60% at 50% 50%, rgba(10,11,14,0) 0%, var(--bg-900) 100%);
  pointer-events: none;
  z-index: 0;
}

/* ─── App Shell ──────────────────────────────────────────────────────────────── */
.app-shell {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 900px;
  margin: 0 auto;
}

/* ─── Header ─────────────────────────────────────────────────────────────────── */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(10,11,14,0.8);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  flex-shrink: 0;
  gap: 12px;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.cf-logo {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: rgba(246,130,31,0.1);
  border: 1px solid rgba(246,130,31,0.25);
  border-radius: var(--radius-sm);
  flex-shrink: 0;
}

.header-text {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.brand-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-100);
  letter-spacing: -0.01em;
}

.brand-sub {
  font-size: 0.7rem;
  color: var(--cf-orange);
  font-family: var(--font-mono);
  letter-spacing: 0.03em;
}

.header-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--text-300);
}

.progress-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-700);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 0.78rem;
  font-family: var(--font-mono);
}

.progress-dots {
  display: flex;
  gap: 4px;
}

.progress-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--bg-600);
  border: 1px solid var(--border-strong);
  transition: background 0.3s, border-color 0.3s;
}

.progress-dot.answered {
  background: var(--green);
  border-color: var(--green);
}

.progress-dot.current {
  background: var(--cf-orange);
  border-color: var(--cf-orange);
  box-shadow: 0 0 6px var(--cf-orange);
  animation: pulse-dot 1.5s ease-in-out infinite;
}

@keyframes pulse-dot {
  0%, 100% { box-shadow: 0 0 6px var(--cf-orange); }
  50% { box-shadow: 0 0 12px var(--cf-orange-light); }
}

/* ─── Chat Area ──────────────────────────────────────────────────────────────── */
.chat-wrapper {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  scroll-behavior: smooth;
}

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

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

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

.chat-messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 100%;
  justify-content: flex-end;
}

/* ─── Message Bubbles ────────────────────────────────────────────────────────── */
.msg {
  display: flex;
  gap: 10px;
  max-width: 100%;
  animation: msg-in 0.3s cubic-bezier(0.2, 0.8, 0.3, 1) both;
}

@keyframes msg-in {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg.agent {
  align-self: flex-start;
  max-width: 82%;
}

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

.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  margin-top: 2px;
}

.msg.agent .msg-avatar {
  background: linear-gradient(135deg, #F6821F, #FF6B35);
  color: #fff;
  font-weight: 700;
  box-shadow: 0 0 12px rgba(246,130,31,0.4);
}

.msg.user .msg-avatar {
  background: linear-gradient(135deg, #6366F1, #8B5CF6);
  color: #fff;
  font-size: 0.9rem;
}

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

.msg-bubble {
  padding: 12px 16px;
  border-radius: var(--radius-md);
  font-size: 0.9rem;
  line-height: 1.65;
  word-break: break-word;
}

.msg.agent .msg-bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-100);
  border-top-left-radius: 4px;
}

.msg.user .msg-bubble {
  background: linear-gradient(135deg, rgba(99,102,241,0.25), rgba(139,92,246,0.2));
  border: 1px solid rgba(99,102,241,0.3);
  color: var(--text-100);
  border-top-right-radius: 4px;
  text-align: left;
}

/* Thinking bubble */
.msg.thinking .msg-bubble {
  background: rgba(246,130,31,0.07);
  border-color: rgba(246,130,31,0.2);
  color: var(--cf-orange);
  font-style: italic;
  font-size: 0.85rem;
}

/* Question bubble */
.msg.question-msg .msg-bubble {
  background: linear-gradient(135deg, rgba(246,130,31,0.08), rgba(255,154,69,0.05));
  border: 1px solid rgba(246,130,31,0.25);
  border-left: 3px solid var(--cf-orange);
}

/* Feedback bubble */
.msg.feedback-msg .msg-bubble {
  border-left: 3px solid var(--green);
}

/* Summary bubble */
.msg.summary-msg .msg-bubble {
  background: linear-gradient(135deg, rgba(34,211,164,0.07), rgba(99,102,241,0.05));
  border: 1px solid rgba(34,211,164,0.2);
  border-radius: var(--radius-lg);
}

/* ─── Markdown Rendering ─────────────────────────────────────────────────────── */
.msg-bubble h1,
.msg-bubble h2,
.msg-bubble h3 {
  color: var(--text-100);
  margin-top: 0.8em;
  margin-bottom: 0.4em;
  font-weight: 600;
}

.msg-bubble h1 { font-size: 1.05rem; }
.msg-bubble h2 { font-size: 0.95rem; }
.msg-bubble h3 { font-size: 0.88rem; }

.msg-bubble p { margin: 0.3em 0; }
.msg-bubble p:first-child { margin-top: 0; }
.msg-bubble p:last-child { margin-bottom: 0; }

.msg-bubble strong { color: var(--text-100); font-weight: 600; }
.msg-bubble em { color: var(--text-200); }
.msg-bubble code {
  font-family: var(--font-mono);
  font-size: 0.82em;
  background: var(--bg-700);
  border: 1px solid var(--border);
  padding: 1px 5px;
  border-radius: 4px;
  color: var(--cf-orange-light);
}

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

.msg-bubble ol, .msg-bubble ul {
  padding-left: 1.4em;
  margin: 0.4em 0;
}

.msg-bubble li { margin: 0.2em 0; }

/* ─── Score Badge ────────────────────────────────────────────────────────────── */
.score-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 500;
  padding: 3px 8px;
  border-radius: 999px;
  margin-top: 6px;
  align-self: flex-start;
}

.score-badge.high { background: rgba(34,211,164,0.12); color: var(--green); border: 1px solid rgba(34,211,164,0.3); }
.score-badge.mid { background: rgba(251,191,36,0.12); color: var(--yellow); border: 1px solid rgba(251,191,36,0.3); }
.score-badge.low { background: rgba(248,113,113,0.12); color: var(--red); border: 1px solid rgba(248,113,113,0.3); }

/* ─── Average Score Bar ──────────────────────────────────────────────────────── */
.avg-bar-wrap {
  margin-top: 8px;
  font-size: 0.75rem;
  color: var(--text-300);
  font-family: var(--font-mono);
  display: flex;
  align-items: center;
  gap: 8px;
}

.avg-bar {
  height: 4px;
  background: var(--bg-600);
  border-radius: 99px;
  flex: 1;
  overflow: hidden;
}

.avg-bar-fill {
  height: 100%;
  border-radius: 99px;
  background: linear-gradient(to right, var(--cf-orange), var(--green));
  transition: width 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ─── Chat Footer ────────────────────────────────────────────────────────────── */
.chat-footer {
  border-top: 1px solid var(--border);
  background: rgba(10,11,14,0.8);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  padding: 12px 16px 16px;
  flex-shrink: 0;
}

.input-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.input-container {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  padding: 8px 8px 8px 16px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.input-container:focus-within {
  border-color: rgba(246,130,31,0.5);
  box-shadow: 0 0 0 3px rgba(246,130,31,0.08);
}

.msg-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-100);
  font-family: var(--font-sans);
  font-size: 0.9rem;
  line-height: 1.5;
  resize: none;
  max-height: 140px;
  overflow-y: auto;
  padding: 4px 0;
}

.msg-input::placeholder {
  color: var(--text-400);
}

.msg-input::-webkit-scrollbar {
  width: 3px;
}

.msg-input::-webkit-scrollbar-thumb {
  background: var(--bg-600);
}

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--cf-orange);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.18s, transform 0.15s, box-shadow 0.18s;
}

.send-btn svg {
  width: 16px;
  height: 16px;
}

.send-btn:hover:not(:disabled) {
  background: var(--cf-orange-light);
  transform: scale(1.07);
  box-shadow: 0 0 12px rgba(246,130,31,0.4);
}

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

.send-btn:disabled {
  background: var(--bg-600);
  color: var(--text-400);
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.input-hint {
  font-size: 0.72rem;
  color: var(--text-400);
  text-align: center;
}

.input-hint kbd {
  display: inline-block;
  background: var(--bg-700);
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  color: var(--text-300);
}

/* ─── Score Toast ────────────────────────────────────────────────────────────── */
.score-toast {
  position: fixed;
  top: 80px;
  right: 20px;
  padding: 10px 18px;
  border-radius: var(--radius-md);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  box-shadow: var(--shadow-lg);
  font-family: var(--font-mono);
  font-size: 0.85rem;
  z-index: 100;
  animation: toast-in 0.3s cubic-bezier(0.2, 0.8, 0.3, 1) both;
  pointer-events: none;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateX(24px); }
  to { opacity: 1; transform: translateX(0); }
}

@keyframes toast-out {
  from { opacity: 1; transform: translateX(0); }
  to { opacity: 0; transform: translateX(24px); }
}

.score-toast[hidden] {
  display: none;
}

/* ─── Loading dots ───────────────────────────────────────────────────────────── */
.dots-typing {
  display: flex;
  gap: 4px;
  align-items: center;
  height: 20px;
}

.dots-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--cf-orange);
  animation: dots-bounce 1.2s ease-in-out infinite;
}

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

@keyframes dots-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-6px); opacity: 1; }
}

/* ─── Connection Banner ──────────────────────────────────────────────────────── */
.conn-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  text-align: center;
  padding: 8px;
  font-size: 0.8rem;
  font-family: var(--font-mono);
  background: rgba(248,113,113,0.15);
  border-top: 1px solid rgba(248,113,113,0.3);
  color: var(--red);
  z-index: 50;
  animation: msg-in 0.3s ease both;
}

/* ─── Responsive ─────────────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  .app-header {
    padding: 10px 14px;
  }

  .chat-wrapper {
    padding: 14px 10px;
  }

  .msg.agent {
    max-width: 92%;
  }

  .brand-sub {
    display: none;
  }

  .chat-footer {
    padding: 10px 10px 14px;
  }
}
