:root {
  --tf-green: #1a7f37;
  --tf-green-dark: #0f5222;
  --tf-green-light: #e8f5ec;
  --tf-gold: #d4af37;
  --tf-gold-dark: #b8941f;
  --bg: #fafbfc;
  --bg-card: #ffffff;
  --text: #1a1a1a;
  --text-muted: #666;
  --border: #e1e4e8;
  --shadow: 0 2px 8px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 40px rgba(0,0,0,0.15);
  --conf-high: #1a7f37;
  --conf-med:  #d4af37;
  --conf-low:  #c0392b;
}

[data-theme="dark"] {
  --tf-green: #2ea04c;
  --tf-green-dark: #1a7f37;
  --tf-green-light: #0f3a1b;
  --bg: #0d1117;
  --bg-card: #161b22;
  --text: #e6edf3;
  --text-muted: #8b949e;
  --border: #30363d;
  --shadow: 0 2px 8px rgba(0,0,0,0.3);
  --shadow-lg: 0 10px 40px rgba(0,0,0,0.5);
  --conf-high: #2ea04c;
  --conf-med:  #f0c645;
  --conf-low:  #e86f64;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a { color: var(--tf-green); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Header ---------- */
header {
  background: linear-gradient(135deg, var(--tf-green) 0%, var(--tf-green-dark) 100%);
  color: white;
  padding: 1rem 1.5rem;
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 100;
}
.header-inner {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}
.header-brand { display: flex; align-items: center; gap: 0.75rem; }
.header-brand img {
  height: 52px;
  width: auto;
  background: white;
  padding: 4px 8px;
  border-radius: 6px;
}
.header-text h1 { font-size: 1.15rem; font-weight: 700; letter-spacing: 0.02em; }
.header-text .tagline { font-size: 0.75rem; opacity: 0.9; margin-top: 0.1rem; }

.header-nav { display: flex; align-items: center; gap: 0.75rem; }
.header-nav a {
  color: white;
  font-size: 0.85rem;
  padding: 0.4rem 0.7rem;
  border-radius: 6px;
}
.header-nav a:hover { background: rgba(255,255,255,0.15); text-decoration: none; }
.theme-toggle, .logout-btn {
  background: rgba(255,255,255,0.15);
  border: none;
  color: white;
  padding: 0.45rem 0.7rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
}
.theme-toggle:hover, .logout-btn:hover { background: rgba(255,255,255,0.25); }

/* ---------- Layout ---------- */
.layout {
  flex: 1;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  padding: 1.5rem 1rem;
}
.ask-grid {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 1.5rem;
}
@media (max-width: 900px) {
  .ask-grid { grid-template-columns: 1fr; }
  .sidebar { order: 2; }
}

.panel {
  background: var(--bg-card);
  border-radius: 12px;
  padding: 1.25rem;
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
  border: 1px solid var(--border);
}
.panel h2 { font-size: 1.1rem; margin-bottom: 0.75rem; color: var(--tf-green-dark); }
.panel h3 { font-size: 0.95rem; margin-bottom: 0.6rem; color: var(--tf-green-dark); }

.banner {
  padding: 0.75rem 1rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}
.banner-warn  { background: #fff7e0; color: #7a5a00; border: 1px solid #f0d574; }
.banner-error { background: #fde7e7; color: #8a1d1d; border: 1px solid #f0a0a0; }

[data-theme="dark"] .banner-warn  { background: #2a2410; color: #f0c645; border-color: #5a4720; }
[data-theme="dark"] .banner-error { background: #2a1212; color: #e86f64; border-color: #5a2828; }

/* ---------- Ask form ---------- */
.ask-panel textarea {
  width: 100%;
  padding: 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
  resize: vertical;
  min-height: 90px;
}
.ask-panel textarea:focus {
  outline: none;
  border-color: var(--tf-green);
  box-shadow: 0 0 0 3px var(--tf-green-light);
}

.ask-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-top: 0.75rem;
}
.ask-hint { color: var(--text-muted); font-size: 0.8rem; }

.btn-primary {
  background: var(--tf-green);
  color: white;
  border: none;
  padding: 0.6rem 1.4rem;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-primary:hover { background: var(--tf-green-dark); }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; }

.btn-ghost {
  background: transparent;
  color: var(--tf-green);
  border: 1px solid var(--border);
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
}
.btn-ghost:hover { border-color: var(--tf-green); background: var(--tf-green-light); }

[data-theme="dark"] .btn-ghost:hover { background: rgba(46,160,76,0.15); }

.htmx-indicator { display: none; margin-left: 0.5rem; opacity: 0.85; font-size: 0.85rem; }
.htmx-request .htmx-indicator { display: inline; }
.htmx-request.btn-primary { opacity: 0.7; cursor: wait; }

/* ---------- Answer card ---------- */
.answer-card {
  background: var(--bg-card);
  border-radius: 12px;
  padding: 1.25rem;
  box-shadow: var(--shadow);
  margin-top: 1rem;
  border: 1px solid var(--border);
  border-left: 4px solid var(--tf-green);
}
.answer-header {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
}
.answer-q { flex: 1; min-width: 200px; }
.answer-q-label {
  display: inline-block;
  background: var(--tf-green);
  color: white;
  padding: 0.1rem 0.5rem;
  border-radius: 4px;
  font-weight: 700;
  font-size: 0.8rem;
  margin-right: 0.5rem;
}
.answer-q-text { font-weight: 500; }

.answer-meta {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.85rem;
}
.elapsed { color: var(--text-muted); font-size: 0.8rem; }

.answer-body {
  font-size: 0.97rem;
  line-height: 1.65;
}
.answer-body h1, .answer-body h2, .answer-body h3 {
  margin: 1rem 0 0.5rem;
  color: var(--tf-green-dark);
}
.answer-body p  { margin: 0.6rem 0; }
.answer-body ul, .answer-body ol { margin: 0.6rem 0 0.6rem 1.5rem; }
.answer-body code {
  background: var(--bg);
  padding: 0.1rem 0.3rem;
  border-radius: 3px;
  font-size: 0.88em;
}
.answer-body pre {
  background: var(--bg);
  padding: 0.75rem;
  border-radius: 6px;
  overflow-x: auto;
  font-size: 0.85em;
  border: 1px solid var(--border);
}

.confidence-reason {
  margin-top: 0.75rem;
  padding: 0.6rem 0.75rem;
  background: var(--bg);
  border-radius: 6px;
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* ---------- Confidence badges ---------- */
.conf {
  display: inline-block;
  padding: 0.15rem 0.6rem;
  border-radius: 12px;
  font-weight: 700;
  font-size: 0.7rem;
  letter-spacing: 0.05em;
  color: white;
}
.conf-high { background: var(--conf-high); }
.conf-med, .conf-medium { background: var(--conf-med); color: #1a1a1a; }
.conf-low { background: var(--conf-low); }

/* ---------- Citations ---------- */
.citations {
  margin-top: 1rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}
.citations h3 { margin-bottom: 0.6rem; }
.citation-list { list-style: none; padding-left: 0; }
.citation {
  margin-bottom: 0.6rem;
  padding: 0.75rem;
  background: var(--bg);
  border-radius: 8px;
  border-left: 3px solid var(--tf-gold);
}
.citation-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}
.citation-type {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  color: white;
  background: var(--tf-green-dark);
  padding: 0.1rem 0.5rem;
  border-radius: 4px;
}
.citation-label {
  flex: 1;
  font-weight: 600;
  font-size: 0.92rem;
}
.btn-verify {
  font-size: 0.75rem;
  padding: 0.25rem 0.6rem;
  background: var(--tf-gold);
  color: #1a1a1a;
  border-radius: 4px;
  font-weight: 600;
}
.btn-verify:hover { background: var(--tf-gold-dark); text-decoration: none; }

.citation-snippet {
  margin-top: 0.4rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-style: italic;
}
.no-citations {
  margin-top: 0.75rem;
  padding: 0.6rem;
  background: #fdf3e7;
  color: #8a5a1d;
  border-radius: 6px;
  font-size: 0.9rem;
}
[data-theme="dark"] .no-citations { background: #2a2010; color: #f0c645; }

.retrieval-log {
  margin-top: 0.75rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}
.retrieval-log summary { cursor: pointer; user-select: none; }
.retrieval-log ol { margin: 0.4rem 0 0 1.5rem; }

/* ---------- Sidebar ---------- */
.sidebar { font-size: 0.9rem; }
.recent-list { list-style: none; padding: 0; }
.recent-list li { margin-bottom: 0.4rem; }
.recent-list a {
  display: block;
  padding: 0.5rem;
  border-radius: 6px;
  color: var(--text);
  border: 1px solid transparent;
}
.recent-list a:hover { background: var(--bg); border-color: var(--border); text-decoration: none; }
.recent-q { display: block; font-size: 0.85rem; }
.recent-meta {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.25rem;
  font-size: 0.7rem;
  color: var(--text-muted);
}
.see-all {
  display: inline-block;
  margin-top: 0.5rem;
  font-size: 0.8rem;
}

.tips { list-style: none; padding: 0; font-size: 0.85rem; }
.tips li {
  padding: 0.35rem 0 0.35rem 1.1rem;
  position: relative;
  color: var(--text-muted);
}
.tips li::before {
  content: "›";
  position: absolute;
  left: 0;
  color: var(--tf-gold);
  font-weight: bold;
}
.panel-tip { background: var(--tf-green-light); border-color: var(--tf-green); }
[data-theme="dark"] .panel-tip { background: var(--tf-green-light); }

.muted { color: var(--text-muted); font-size: 0.85rem; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
}
.modal-card {
  background: var(--bg-card);
  border-radius: 12px;
  max-width: 800px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  position: relative;
  padding: 1.5rem;
}
.modal-close {
  position: absolute;
  top: 0.5rem;
  right: 0.75rem;
  background: transparent;
  border: none;
  font-size: 1.6rem;
  cursor: pointer;
  color: var(--text-muted);
}
.modal-close:hover { color: var(--text); }
.source-header { margin-bottom: 0.75rem; padding-bottom: 0.5rem; border-bottom: 1px solid var(--border); }
.source-header h2 { font-size: 1.1rem; color: var(--tf-green-dark); }
.source-id { font-size: 0.75rem; margin-top: 0.2rem; }
.source-text {
  white-space: pre-wrap;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.85rem;
  background: var(--bg);
  padding: 0.85rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  max-height: 60vh;
  overflow-y: auto;
}
.source-footer { margin-top: 0.75rem; }

/* ---------- History ---------- */
.history-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}
.history-table th, .history-table td {
  padding: 0.6rem 0.5rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}
.history-table th { color: var(--tf-green-dark); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; }
.history-table .ts { white-space: nowrap; color: var(--text-muted); font-family: ui-monospace, monospace; font-size: 0.8rem; }
.history-table .q  { max-width: 600px; }

/* ---------- Footer ---------- */
.legal-footer {
  background: var(--bg-card);
  border-top: 2px solid var(--tf-green);
  margin-top: 2rem;
  padding: 1.25rem 1rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}
.footer-inner { max-width: 1200px; margin: 0 auto; }
.footer-inner p { margin-bottom: 0.4rem; }
.version { font-family: ui-monospace, monospace; opacity: 0.7; }

/* ---------- Login ---------- */
.login-body {
  background: linear-gradient(135deg, var(--tf-green) 0%, var(--tf-green-dark) 100%);
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.login-card {
  background: var(--bg-card);
  border-radius: 12px;
  padding: 2rem;
  max-width: 400px;
  width: 100%;
  box-shadow: var(--shadow-lg);
}
.login-brand {
  text-align: center;
  margin-bottom: 1.25rem;
}
.login-brand img {
  height: 60px;
  margin-bottom: 0.5rem;
}
.login-brand h1 { color: var(--tf-green-dark); font-size: 1.4rem; }
.login-card form { display: flex; flex-direction: column; gap: 0.5rem; }
.login-card label { font-size: 0.8rem; color: var(--text-muted); }
.login-card input {
  padding: 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  background: var(--bg);
  color: var(--text);
}
.login-card input:focus {
  outline: none;
  border-color: var(--tf-green);
  box-shadow: 0 0 0 3px var(--tf-green-light);
}
.login-card .btn-primary { margin-top: 0.5rem; }
.login-foot { text-align: center; margin-top: 1rem; font-size: 0.75rem; }
.error {
  padding: 0.75rem;
  background: #fde7e7;
  color: #8a1d1d;
  border-radius: 8px;
  border: 1px solid #f0a0a0;
}

/* ============== HERO + DEMO POLISH (added for live demo) ============== */

.hero {
  background: linear-gradient(135deg, var(--tf-green) 0%, var(--tf-green-dark) 100%);
  color: white;
  padding: 4rem 1rem 5rem;
  margin: -1rem -1rem 0;
  position: relative;
  overflow: hidden;
}

.hero::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    radial-gradient(circle at 20% 30%, rgba(212,175,55,0.15) 0%, transparent 50%),
    radial-gradient(circle at 80% 70%, rgba(212,175,55,0.1) 0%, transparent 50%);
  pointer-events: none;
}

.hero-inner {
  max-width: 900px;
  margin: 0 auto;
  position: relative;
  text-align: center;
}

.hero-badge {
  display: inline-block;
  background: rgba(255,255,255,0.15);
  backdrop-filter: blur(10px);
  padding: 0.5rem 1.2rem;
  border-radius: 100px;
  font-size: 0.9rem;
  font-weight: 600;
  border: 1px solid rgba(255,255,255,0.2);
  margin-bottom: 1.5rem;
  letter-spacing: 0.5px;
}

.hero-title {
  font-size: clamp(2.5rem, 6vw, 4.5rem);
  font-weight: 800;
  line-height: 1.05;
  margin: 0 0 1.5rem;
  letter-spacing: -0.02em;
  text-shadow: 0 2px 20px rgba(0,0,0,0.2);
}

.hero-accent {
  background: linear-gradient(135deg, var(--tf-gold) 0%, #ffd966 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  display: inline-block;
}

.hero-sub {
  font-size: clamp(1rem, 2vw, 1.25rem);
  font-weight: 400;
  opacity: 0.95;
  margin: 0 0 3rem;
  line-height: 1.6;
}

.hero-form {
  background: rgba(255,255,255,0.97);
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  border: 1px solid rgba(255,255,255,0.4);
  margin: 0 auto 2rem;
  max-width: 800px;
}

.hero-form textarea {
  width: 100%;
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 1rem;
  font-size: 1.1rem;
  font-family: inherit;
  color: var(--text);
  background: white;
  resize: vertical;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.hero-form textarea:focus {
  border-color: var(--tf-green);
  box-shadow: 0 0 0 4px rgba(26,127,55,0.15);
}

.hero-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-top: 1rem;
  justify-content: space-between;
  flex-wrap: wrap;
}

.btn-large {
  font-size: 1.15rem;
  padding: 1rem 2rem;
  font-weight: 700;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--tf-green) 0%, var(--tf-green-dark) 100%);
  color: white;
  border: none;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: 0 4px 14px rgba(26,127,55,0.3);
}

.btn-large:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(26,127,55,0.4);
}

.btn-large:active {
  transform: translateY(0);
}

.ask-hint {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.sample-queries {
  margin-top: 2rem;
}

.sample-label {
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 0.75rem;
  opacity: 0.9;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.sample-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  justify-content: center;
}

.chip {
  background: rgba(255,255,255,0.95);
  color: var(--tf-green-dark);
  border: 1px solid rgba(255,255,255,0.5);
  border-radius: 100px;
  padding: 0.6rem 1.2rem;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.chip:hover {
  background: var(--tf-gold);
  color: var(--tf-green-dark);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(212,175,55,0.4);
}

/* ============== ANSWER REGION ============== */

.answer-region {
  max-width: 900px;
  margin: -3rem auto 3rem;
  padding: 0 1rem;
  position: relative;
  z-index: 10;
}

.answer-region:empty {
  display: none;
}

/* ============== FEATURES ============== */

.features {
  background: var(--bg);
  padding: 4rem 1rem;
  margin: 0 -1rem;
}

.features-inner {
  max-width: 1100px;
  margin: 0 auto;
}

.section-title {
  text-align: center;
  font-size: 2.25rem;
  font-weight: 800;
  margin: 0 0 3rem;
  color: var(--text);
}

.section-subtitle {
  text-align: center;
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0 0 1.5rem;
  color: var(--text);
}

.feature-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1.5rem;
}

.feature-card {
  background: var(--bg-card);
  padding: 2rem;
  border-radius: 14px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  transition: transform 0.2s, box-shadow 0.2s;
}

.feature-card:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow-lg);
}

.feature-icon {
  font-size: 2.5rem;
  margin-bottom: 0.75rem;
}

.feature-card h3 {
  margin: 0 0 0.5rem;
  font-size: 1.25rem;
  color: var(--tf-green);
  font-weight: 700;
}

.feature-card p {
  margin: 0;
  color: var(--text-muted);
  line-height: 1.6;
}

/* ============== RECENT QUERIES (FLAT) ============== */

.recent-section {
  background: var(--bg-card);
  padding: 3rem 1rem;
  margin: 0 -1rem;
  border-top: 1px solid var(--border);
}

.recent-inner {
  max-width: 800px;
  margin: 0 auto;
}

.recent-list-flat {
  list-style: none;
  padding: 0;
  margin: 0 0 1.5rem;
}

.recent-list-flat li a {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.25rem;
  background: var(--bg);
  border-radius: 10px;
  margin-bottom: 0.5rem;
  text-decoration: none;
  color: var(--text);
  border: 1px solid var(--border);
  transition: all 0.15s;
  gap: 1rem;
}

.recent-list-flat li a:hover {
  background: var(--tf-green-light);
  border-color: var(--tf-green);
  transform: translateX(4px);
}

.recent-q {
  flex: 1;
  font-weight: 500;
}

.recent-meta {
  display: flex;
  gap: 0.6rem;
  align-items: center;
  flex-shrink: 0;
}

.recent-ts {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.see-all {
  display: block;
  text-align: center;
  color: var(--tf-green);
  font-weight: 600;
  text-decoration: none;
  padding: 0.75rem;
}

.see-all:hover {
  color: var(--tf-green-dark);
}

/* ============== ANSWER CARD ============== */

.answer-card {
  background: var(--bg-card);
  border-radius: 16px;
  padding: 2rem;
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
  margin-bottom: 1.5rem;
}

/* ============== MOBILE ============== */

@media (max-width: 640px) {
  .hero {
    padding: 2.5rem 1rem 3rem;
  }
  .hero-title {
    font-size: 2.2rem;
  }
  .hero-form {
    padding: 1rem;
  }
  .hero-actions {
    flex-direction: column;
    align-items: stretch;
  }
  .btn-large {
    width: 100%;
  }
  .sample-buttons {
    justify-content: flex-start;
  }
  .chip {
    font-size: 0.8rem;
    padding: 0.5rem 0.9rem;
  }
}

/* ============== CONFIDENCE BADGES ============== */

.conf {
  display: inline-block;
  padding: 0.2rem 0.6rem;
  border-radius: 100px;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.conf-high {
  background: var(--conf-high);
  color: white;
}

.conf-medium, .conf-med {
  background: var(--conf-med);
  color: white;
}

.conf-low, .conf-unknown {
  background: var(--conf-low);
  color: white;
}
