:root {
  --cf-orange: #f6821f;
  --cf-orange-2: #ff9a4d;

  --bg-0: #0b0d10;
  --bg-1: #101419;
  --bg-2: #131922;

  --glass: rgba(255,255,255,.04);
  --border: #1e2631;
  --text: #e8eef5;
  --muted: #9aa7b2;
  --ring: rgba(246,130,31,.35);
  --shadow-lg: 0 20px 40px rgba(0,0,0,.45);

  --error-bg: rgba(239, 68, 68, 0.1);
  --error-border: rgba(239, 68, 68, 0.28);
  --error-text: #fca5a5;
}

[data-theme="light"] {
  --cf-orange: #f6821f;
  --cf-orange-2: #ff9a4d;

  --bg-0: #f7f9fc;
  --bg-1: #ffffff;
  --bg-2: #f1f5f9;

  --glass: rgba(0,0,0,.02);
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --ring: rgba(246,130,31,.25);
  --shadow-lg: 0 20px 40px rgba(0,0,0,.08);

  --error-bg: rgba(239, 68, 68, 0.08);
  --error-border: rgba(239, 68, 68, 0.2);
  --error-text: #dc2626;
}

:root {
  /* ...existing vars... */
  --btn-bg: #f3f4f6;      /* off-white for dark theme */
  --btn-fg: #111827;      /* near-black text */
  --btn-border: #d1d5db;  /* soft gray border */
  --btn-bg-hover: #eceff3;
  --btn-bg-active: #e6e9ee;
  --btn-shadow: 0 4px 10px rgba(0,0,0,.18);
}

[data-theme="light"] {
  /* ...existing vars... */
  --btn-bg: #e7eaee;      /* grey-ish for light theme */
  --btn-fg: #0f172a;      /* black text */
  --btn-border: #cbd5e1;
  --btn-bg-hover: #e1e6eb;
  --btn-bg-active: #dbe2e8;
  --btn-shadow: 0 4px 10px rgba(15, 23, 42, .06);
}

html {
  transition: background-color 0.3s ease, color 0.3s ease;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: Montserrat, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  color: var(--text);
  background:
    radial-gradient(90rem 40rem at 10% -20%, rgba(246,130,31,.08), transparent 60%),
    radial-gradient(90rem 40rem at 120% 120%, rgba(246,130,31,.06), transparent 60%),
    var(--bg-0);
  transition: background 0.3s ease;
}

/* Layout shell: fixed viewport height; chat never pushes page */
.shell {
  height: 100dvh;
  max-width: 980px;
  margin: 0 auto;
  padding: 18px clamp(14px, 2vw, 28px);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Header */
header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: linear-gradient(180deg, var(--bg-1), var(--bg-2));
  box-shadow: var(--shadow-lg);
  transition: all 0.3s ease;
}
.theme-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-2);
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 11px;
  color: var(--muted);
}
.theme-toggle:hover { background: var(--glass); border-color: var(--cf-orange); }
.theme-icon { width: 14px; height: 14px; fill: var(--cf-orange); }
.brand-dot { width: 12px; height: 12px; border-radius: 999px; background: var(--cf-orange);
  box-shadow: 0 0 32px var(--ring), 0 0 6px var(--cf-orange); flex: none; }
h1 { font-size: 16px; font-weight: 700; letter-spacing: .2px; margin: 0; }
.sub { margin-left: auto; font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 10px; }

/* High-contrast pills (clickable links, no underline)
   Light theme: black pill + white text
   Dark theme: white pill + black text */
.pill {
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11px;
  border: 1px solid var(--border);
  text-decoration: none;
  transition: transform .06s ease, box-shadow .15s ease, background .2s ease, color .2s ease, border-color .2s ease;
  display: inline-flex;
  align-items: center;
  font-weight: 600;
}
.pill-link { box-shadow: 0 6px 14px rgba(0,0,0,.18); }
[data-theme="light"] .pill-link {
  background: #0b0b0b;
  color: #ffffff;
  border-color: #101010;
}
[data-theme="dark"] .pill-link {
  background: #ffffff;
  color: #0f172a;
  border-color: #d0d7e2;
}
.pill-link:hover {
  transform: translateY(-1px);
  box-shadow: 0 10px 20px rgba(0,0,0,.22);
}
.pill-link:active { transform: translateY(0); }
.pill-link:focus-visible { outline: 3px solid var(--ring); outline-offset: 2px; }

/* Chat card fills remaining height */
.chat-card {
  flex: 1;
  min-height: 0;
  border: 1px solid var(--border);
  border-radius: 18px;
  background:
    linear-gradient(180deg, rgba(255,255,255,.02), rgba(255,255,255,.01)),
    linear-gradient(180deg, var(--bg-1), var(--bg-2));
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Messages area scrolls; page height remains fixed */
.chat-messages {
  flex: 1;
  min-height: 0;
  padding: 18px;
  overflow-y: auto;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: #2a3442 transparent;
}

/* Ensure perfectly even vertical spacing between ALL children (messages & errors) */
.chat-messages > * + * { margin-top: 14px; }

/* Message bubbles: dynamic width based on content length; capped for readability */
.message {
  max-width: 85%;
  width: fit-content;
  border: 1px solid var(--border);
  padding: 14px 16px;
  border-radius: 16px;
  line-height: 1.6;
  background: linear-gradient(180deg, rgba(255,255,255,.02), rgba(255,255,255,.01));
  backdrop-filter: saturate(120%) blur(2px);
  word-wrap: break-word;
  transition: all 0.3s ease;
  animation: slideIn 0.3s ease;
}
@keyframes slideIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

.assistant-message {
  background: linear-gradient(135deg, var(--bg-1), var(--bg-2));
  border-color: var(--border);
}
.user-message {
  background: linear-gradient(135deg, rgba(246,130,31,.12), rgba(246,130,31,.06));
  border-color: rgba(246,130,31,.3);
  margin-left: auto;
}

/* Error: modern red card with subtle depth; same spacing as messages via container rule */
.error-message {
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  border-radius: 14px;
  padding: 14px 16px;
  box-shadow: 0 10px 20px rgba(239,68,68,.08);
}
.error-title {
  font-weight: 800;
  color: var(--error-text);
  font-size: 14px;
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.error-details { font-size: 13px; color: var(--muted); line-height: 1.55; }
.error-icon { width: 16px; height: 16px; flex-shrink: 0; }

/* Markdown */
.message-content { font-size: 14px; }
.message-content p { margin: 0 0 0.8em 0; }
.message-content p:last-child { margin-bottom: 0; }
.message-content code {
  background: rgba(0,0,0,.3);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.9em;
  font-family: Montserrat, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}
[data-theme="light"] .message-content code { background: rgba(0,0,0,.08); }
.message-content pre {
  background: rgba(0,0,0,.4);
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 0.8em 0;
}
[data-theme="light"] .message-content pre { background: rgba(0,0,0,.06); }
.message-content pre code { background: none; padding: 0; }
.message-content ul, .message-content ol { margin: 0.8em 0; padding-left: 1.5em; }
.message-content li { margin: 0.3em 0; }
.message-content h1, .message-content h2, .message-content h3 {
  margin: 1em 0 0.5em 0;
  font-weight: 600;
}
.message-content h1 { font-size: 1.4em; }
.message-content h2 { font-size: 1.2em; }
.message-content h3 { font-size: 1.1em; }
.message-content blockquote {
  border-left: 3px solid var(--cf-orange);
  padding-left: 12px;
  margin: 0.8em 0;
  color: var(--muted);
}
.message-content a { color: var(--cf-orange); text-decoration: underline; }

/* Typing indicator */
.typing {
  display: none; color: var(--muted); font-size: 13px; padding: 0 18px 10px;
}
.typing.visible { display: flex; align-items: center; gap: 8px; }
.dots { display: inline-flex; gap: 3px; }
.dot { width: 6px; height: 6px; border-radius: 999px; background: var(--muted); opacity: .5; animation: pulse 1s infinite; }
.dot:nth-child(2){ animation-delay: .15s } .dot:nth-child(3){ animation-delay: .3s }
@keyframes pulse { 0%,100%{opacity:.35; transform: translateY(0)} 50%{opacity:1; transform: translateY(-2px)} }

/* Composer stays pinned; doesn’t grow page */
.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  padding: 12px;
  border-top: 1px solid var(--border);
  background: #0c1116;
}
[data-theme="light"] .composer { background: #f8fafc; }

#user-input {
  min-height: 44px; max-height: 200px; resize: none;
  border: 1px solid var(--border); background: #0e141b; color: var(--text);
  border-radius: 12px; padding: 12px 14px; outline: none;
  transition: box-shadow .15s ease, border-color .15s ease;
  font-size: 14px;
  font-family: Montserrat, system-ui, sans-serif;
}
[data-theme="light"] #user-input { background: #ffffff; color: #0f172a; }
#user-input::placeholder { color: var(--muted); opacity: 0.6; }
#user-input:focus { border-color: var(--cf-orange); box-shadow: 0 0 0 3px var(--ring); }

/* Modern Send button */
#send-button {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  height: 44px;
  border-radius: 12px;
  border: 1px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--btn-fg);
  font-weight: 700;
  letter-spacing: .2px;
  cursor: pointer;
  transition: transform .06s ease, background-color .15s ease, filter .15s ease, box-shadow .15s ease, border-color .15s ease;
  box-shadow: var(--btn-shadow);
  font-family: Montserrat, system-ui, sans-serif;
}

#send-button:hover {
  background: var(--btn-bg-hover);
  transform: translateY(-1px);
}

#send-button:active {
  background: var(--btn-bg-active);
  transform: translateY(0);
}

#send-button:disabled {
  opacity: .6;
  cursor: not-allowed;
  box-shadow: none;
}

/* ensure the paper-plane uses the button's text color */
.send-icon { width: 16px; height: 16px; flex: none; }
.send-icon path { stroke: currentColor !important; }

.visually-hidden { position: absolute; left: -9999px; }

/* Footer */
footer { text-align: center; color: var(--muted); font-size: 12px; padding: 2px 0 0; }
a, a:visited { color: var(--cf-orange); text-underline-offset: 2px; }
