/* =========================
   Design tokens
   ========================= */
:root {
  --bg:           #f4f7fb;
  --bg-elev:     #ffffff;
  --bg-sunken:   #eef2f8;
  --text:         #1c2434;
  --text-muted:   #5a6478;
  --text-soft:    #8590a4;
  --border:       #e3e8f0;
  --border-strong:#cbd3e1;
  --brand:        #1e6fdc;
  --brand-2:      #4f8cff;
  --brand-soft:   #e6efff;
  --user-bubble:  #1e6fdc;
  --user-text:    #ffffff;
  --shadow-sm:    0 1px 2px rgba(15,23,42,.06);
  --shadow:       0 8px 24px rgba(15,23,42,.08);
  --shadow-lg:    0 20px 50px rgba(15,23,42,.12);
  --radius:       14px;
  --radius-sm:    10px;

  /* Section accent colors */
  --c-answer:    #2e7d32;
  --c-explain:   #1565c0;
  --c-refs:      #ef6c00;
  --c-fq:        #6a1b9a;
  --c-fqa:       #00838f;
  --c-fqe:       #4527a0;
  --c-fqr:       #ad1457;
  --c-key:       #c62828;

  --bg-answer:   #e8f5e9;
  --bg-explain:  #e3f2fd;
  --bg-refs:     #fff3e0;
  --bg-fq:       #f3e5f5;
  --bg-fqa:      #e0f7fa;
  --bg-fqe:      #ede7f6;
  --bg-fqr:      #fce4ec;
  --bg-key:      #ffebee;
}

[data-theme="dark"] {
  --bg:           #0d1320;
  --bg-elev:      #151c2e;
  --bg-sunken:    #0a1020;
  --text:         #e6ebf5;
  --text-muted:   #a6b0c3;
  --text-soft:    #6b7591;
  --border:       #243049;
  --border-strong:#33415c;
  --brand:        #4f8cff;
  --brand-2:      #6ea0ff;
  --brand-soft:   #1c2a47;
  --user-bubble:  #2a6df0;
  --user-text:    #ffffff;
  --shadow:       0 8px 24px rgba(0,0,0,.4);
  --shadow-lg:    0 20px 50px rgba(0,0,0,.5);

  --bg-answer:   #14302a;
  --bg-explain:  #14253f;
  --bg-refs:     #2b1e10;
  --bg-fq:       #2a1736;
  --bg-fqa:      #102b30;
  --bg-fqe:      #1c1438;
  --bg-fqr:      #2e1224;
  --bg-key:      #2c1414;

  --c-answer:    #6cd292;
  --c-explain:   #6ba8ff;
  --c-refs:      #ffb466;
  --c-fq:        #c896ff;
  --c-fqa:       #5ed8e7;
  --c-fqe:       #9a86ff;
  --c-fqr:       #ff6fa3;
  --c-key:       #ff8a8a;
}

/* =========================
   Base
   ========================= */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Inter", "Noto Sans TC", system-ui, -apple-system, "Segoe UI", "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  line-height: 1.55;
}

.app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

/* =========================
   Top bar
   ========================= */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
  backdrop-filter: blur(8px);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand img { height: 44px; width: auto; }
.brand-text h1 { font-size: 16px; margin: 0; font-weight: 700; letter-spacing: -.01em; }
.brand-text p  { font-size: 12px; margin: 0; color: var(--text-muted); }

.icon-btn {
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  color: var(--text);
  width: 38px; height: 38px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 16px;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .15s ease, border-color .15s ease;
}
.icon-btn:hover { background: var(--brand-soft); border-color: var(--brand); }

/* =========================
   Layout
   ========================= */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 20px;
  max-width: 1280px;
  width: 100%;
  margin: 20px auto;
  padding: 0 20px;
}
.sidebar { display: flex; flex-direction: column; gap: 16px; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 0 12px; margin: 12px auto; }
}

/* =========================
   Cards / fields
   ========================= */
.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow-sm);
}
.card.subtle { background: var(--bg-sunken); }
.card-title { font-size: 13px; text-transform: uppercase; letter-spacing: .1em; margin: 0 0 12px; color: var(--text-muted); }

.field { display: block; margin-bottom: 14px; }
.label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; color: var(--text); }
.hint  { display: block; margin-top: 4px; font-size: 11px; color: var(--text-soft); line-height: 1.45; }

input[type="email"], select, textarea {
  width: 100%;
  font-family: inherit;
  font-size: 14px;
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg-elev);
  color: var(--text);
  transition: border-color .15s ease, box-shadow .15s ease;
}
input[type="email"]:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--brand-soft);
}

/* Segmented control for language */
.seg {
  display: grid; grid-template-columns: repeat(3, 1fr);
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 3px;
}
.seg input { display: none; }
.seg label {
  text-align: center;
  font-size: 13px; font-weight: 500;
  padding: 7px 6px;
  border-radius: 7px;
  cursor: pointer;
  color: var(--text-muted);
  transition: background .15s ease, color .15s ease;
}
.seg input:checked + label {
  background: var(--brand);
  color: #fff;
  box-shadow: 0 1px 3px rgba(30,111,220,.4);
}

.tips { padding-left: 18px; margin: 0; font-size: 13px; color: var(--text-muted); }
.tips li { margin-bottom: 6px; }
.tips code, kbd {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 12px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

.ghost-btn {
  width: 100%; padding: 9px 12px;
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  font-weight: 500;
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
}
.ghost-btn:hover { background: var(--bg-sunken); color: var(--text); }

/* =========================
   Chat panel
   ========================= */
.chat {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-height: 60vh;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex; flex-direction: column; gap: 18px;
  scroll-behavior: smooth;
}

/* Welcome card */
.welcome {
  text-align: center;
  padding: 24px 16px 8px;
  color: var(--text-muted);
}
.welcome-emoji { font-size: 40px; margin-bottom: 6px; }
.welcome h2 { margin: 0 0 6px; color: var(--text); font-size: 22px; font-weight: 700; letter-spacing: -.01em; }
.welcome p  { margin: 0 auto 18px; max-width: 520px; font-size: 14px; line-height: 1.6; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.suggest {
  background: var(--brand-soft);
  border: 1px solid var(--border);
  color: var(--brand);
  padding: 8px 14px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: transform .12s ease, background .15s ease;
}
.suggest:hover { transform: translateY(-1px); background: var(--bg-elev); }

/* Message bubbles */
.msg { display: flex; gap: 12px; align-items: flex-start; }
.msg.user { flex-direction: row-reverse; }

.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
}
.msg.user .avatar { background: var(--user-bubble); color: var(--user-text); border-color: transparent; }
.msg.bot .avatar  { background: linear-gradient(135deg, #f6d365 0%, #fda085 100%); border-color: transparent; }

.bubble {
  max-width: 78%;
  padding: 12px 16px;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-radius: 16px;
  font-size: 14.5px;
  line-height: 1.65;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg.user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-color: transparent;
  border-bottom-right-radius: 4px;
}
.msg.bot .bubble {
  background: var(--bg-elev);
  border-bottom-left-radius: 4px;
  padding: 6px 8px;
  max-width: 88%;
}

/* Typing dots */
.typing { display: inline-flex; align-items: center; gap: 5px; padding: 8px 14px; }
.typing span {
  display: inline-block;
  width: 7px; height: 7px;
  background: var(--text-soft);
  border-radius: 50%;
  animation: bounce 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .6; }
  30%           { transform: translateY(-6px); opacity: 1; }
}

/* =========================
   Bot section cards (parsed from response)
   ========================= */
.sections { display: flex; flex-direction: column; gap: 12px; padding: 8px; }

.greeting {
  font-size: 15px;
  font-weight: 600;
  padding: 4px 8px 8px;
  color: var(--text);
}

.section {
  border-left: 4px solid var(--c-explain);
  background: var(--bg-explain);
  border-radius: 10px;
  padding: 14px 16px;
}
.section h3 {
  margin: 0 0 8px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .08em;
  font-weight: 700;
  color: var(--c-explain);
  display: flex; align-items: center; gap: 8px;
}
.section h3 .emoji { font-size: 16px; }
.section .body { font-size: 14px; line-height: 1.7; color: var(--text); }
.section .body :first-child { margin-top: 0; }
.section .body :last-child  { margin-bottom: 0; }
.section .body p { margin: 0 0 .7em; }
.section .body ul, .section .body ol { padding-left: 22px; margin: .3em 0 .7em; }
.section .body li { margin-bottom: 4px; }
.section .body strong { color: var(--text); }
.section .body code {
  background: rgba(0,0,0,.06);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 13px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
[data-theme="dark"] .section .body code { background: rgba(255,255,255,.08); }
.section .body .katex-display { margin: .8em 0; overflow-x: auto; overflow-y: hidden; }

/* Per-section accent colours */
.section.answer  { border-color: var(--c-answer);  background: var(--bg-answer);  }
.section.answer  h3 { color: var(--c-answer); }
.section.explain { border-color: var(--c-explain); background: var(--bg-explain); }
.section.explain h3 { color: var(--c-explain); }
.section.refs    { border-color: var(--c-refs);    background: var(--bg-refs);    }
.section.refs    h3 { color: var(--c-refs); }
.section.fq      { border-color: var(--c-fq);      background: var(--bg-fq);      }
.section.fq      h3 { color: var(--c-fq); }
.section.fqa     { border-color: var(--c-fqa);     background: var(--bg-fqa);     }
.section.fqa     h3 { color: var(--c-fqa); }
.section.fqe     { border-color: var(--c-fqe);     background: var(--bg-fqe);     }
.section.fqe     h3 { color: var(--c-fqe); }
.section.fqr     { border-color: var(--c-fqr);     background: var(--bg-fqr);     }
.section.fqr     h3 { color: var(--c-fqr); }
.section.key     { border-color: var(--c-key);     background: var(--bg-key);     }
.section.key     h3 { color: var(--c-key); }

/* MC options inside FQA */
.section.fq .body p { font-weight: 500; }

/* Error bubble */
.error-bubble {
  border: 1px solid #f5c2c7;
  background: #fdecea;
  color: #842029;
  padding: 12px 14px;
  border-radius: 10px;
  font-size: 14px;
}
[data-theme="dark"] .error-bubble { background: #3a1f23; color: #ff9aa3; border-color: #6e2a30; }

/* =========================
   Composer
   ========================= */
.composer {
  border-top: 1px solid var(--border);
  padding: 14px 16px 16px;
  background: var(--bg-elev);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.composer textarea {
  resize: vertical;
  min-height: 56px;
  max-height: 200px;
  font-size: 15px;
}
.composer-actions { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.status { color: var(--text-soft); font-size: 12px; }

.primary-btn {
  background: linear-gradient(180deg, var(--brand-2) 0%, var(--brand) 100%);
  color: #fff;
  border: 0;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  display: inline-flex; align-items: center; gap: 8px;
  box-shadow: 0 2px 6px rgba(30,111,220,.3);
  transition: transform .12s ease, box-shadow .15s ease, opacity .15s ease;
}
.primary-btn:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 4px 10px rgba(30,111,220,.4); }
.primary-btn:disabled { opacity: .65; cursor: not-allowed; }

.btn-spinner {
  width: 14px; height: 14px;
  border: 2px solid rgba(255,255,255,.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* =========================
   Footer
   ========================= */
.footer {
  text-align: center;
  color: var(--text-soft);
  font-size: 12px;
  padding: 16px 20px 24px;
  display: flex; justify-content: center; gap: 8px;
}
.footer .dot { opacity: .5; }

/* Mobile fine-tuning */
@media (max-width: 600px) {
  .brand img { height: 36px; }
  .brand-text h1 { font-size: 14px; }
  .brand-text p  { font-size: 11px; }
  .messages { padding: 16px; }
  .bubble { max-width: 92%; font-size: 14px; }
  .section { padding: 12px 14px; }
}
