/* ===== Design Tokens ===== */
:root {
  color-scheme: light;

  /* Core palette */
  --crimson: #8b1a1a;
  --crimson-dark: #641010;
  --crimson-glow: rgba(139, 26, 26, 0.12);
  --gold: #c9a84c;
  --gold-dark: #a3832a;
  --gold-glow: rgba(201, 168, 76, 0.15);
  --parchment: #f5f0e8;
  --parchment-warm: #ede4d4;
  --parchment-light: #faf7f0;
  --ink: #1a1614;
  --ink-soft: #3a3530;
  --ink-muted: #6b635a;
  --stone: #4a4238;
  --stone-light: #7a7268;
  --ivory: #fdfcf8;
  --sage: #5a7d6a;
  --sage-light: #e8f0ea;

  /* Functional */
  --surface: var(--ivory);
  --surface-elevated: #ffffff;
  --border: #e0d8c8;
  --border-light: #eee8da;
  --shadow-sm: 0 1px 3px rgba(26, 22, 20, 0.06), 0 1px 2px rgba(26, 22, 20, 0.04);
  --shadow-md: 0 4px 16px rgba(26, 22, 20, 0.08), 0 2px 6px rgba(26, 22, 20, 0.04);
  --shadow-lg: 0 12px 40px rgba(26, 22, 20, 0.10), 0 4px 12px rgba(26, 22, 20, 0.05);
  --shadow-glow: 0 0 0 1px var(--gold-glow), 0 4px 20px rgba(201, 168, 76, 0.12);

  /* Typography */
  --font-display: "Palatino", "Palatino Linotype", "Book Antiqua", Georgia, serif;
  --font-body: "Inter", ui-sans-serif, system-ui, -apple-system, sans-serif;
  --font-mono: "JetBrains Mono", "Fira Code", monospace;

  /* Motion */
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --duration-fast: 150ms;
  --duration-normal: 250ms;
  --duration-slow: 400ms;

  /* Layout */
  --content-max: 1280px;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
}

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

html {
  scroll-behavior: smooth;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.6;
  color: var(--ink);
  background:
    radial-gradient(ellipse at 20% 0%, rgba(201, 168, 76, 0.06), transparent 50%),
    radial-gradient(ellipse at 80% 100%, rgba(139, 26, 26, 0.04), transparent 50%),
    linear-gradient(180deg, var(--parchment-light) 0%, var(--parchment) 50%, var(--parchment-warm) 100%);
  background-attachment: fixed;
  min-height: 100vh;
}

/* Subtle texture overlay */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence baseFrequency='0.65' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.03'/%3E%3C/svg%3E");
  pointer-events: none;
  z-index: 0;
}

/* ===== App Shell ===== */
.app {
  position: relative;
  z-index: 1;
  max-width: var(--content-max);
  margin: 0 auto;
  padding: 32px 28px 64px;
}

/* ===== Header ===== */
.topbar {
  padding: 12px 0 32px;
  position: relative;
}

.topbar::after {
  content: "";
  display: block;
  width: 60px;
  height: 3px;
  margin-top: 18px;
  background: linear-gradient(90deg, var(--crimson), var(--gold));
  border-radius: 2px;
}

.eyebrow {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.12em;
  color: var(--sage);
  text-transform: uppercase;
  margin-bottom: 8px;
  display: inline-block;
  padding: 3px 10px;
  background: var(--sage-light);
  border-radius: 100px;
}

h1 {
  font-family: var(--font-display);
  font-size: 38px;
  font-weight: 700;
  line-height: 1.15;
  color: var(--ink);
  letter-spacing: -0.01em;
}

h1 span {
  color: var(--crimson);
}

/* ===== Query Area ===== */
.query-panel {
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 24px;
  transition: box-shadow var(--duration-normal) var(--ease-out);
  position: relative;
}

.query-panel:focus-within {
  box-shadow: var(--shadow-glow), var(--shadow-lg);
  border-color: var(--gold);
}

.query-head {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 20px;
  align-items: end;
  margin-bottom: 16px;
}

.query-head label {
  display: block;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink-soft);
  margin-bottom: 4px;
}

/* ===== Mode Switcher ===== */
.mode {
  display: flex;
  gap: 4px;
  background: #f0ebe0;
  border-radius: var(--radius-md);
  padding: 3px;
}

.mode button {
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 8px 18px;
  border: none;
  border-radius: calc(var(--radius-md) - 2px);
  background: transparent;
  color: var(--ink-muted);
  cursor: pointer;
  font: inherit;
  font-size: 13px;
  transition: all var(--duration-fast) var(--ease-out);
  position: relative;
}

.mode button span {
  font-weight: 700;
  font-size: 14px;
  color: var(--ink-soft);
  transition: color var(--duration-fast);
}

.mode button small {
  font-size: 11px;
  color: var(--ink-muted);
  transition: color var(--duration-fast);
}

.mode button:hover {
  color: var(--ink);
}

.mode button:hover span {
  color: var(--ink);
}

.mode button.active {
  background: var(--surface-elevated);
  color: var(--crimson);
  box-shadow: var(--shadow-sm);
}

.mode button.active span {
  color: var(--crimson);
}

.mode button.active small {
  color: var(--crimson);
  opacity: 0.7;
}

/* ===== Input Row ===== */
.ask-row {
  display: grid;
  grid-template-columns: 1fr 120px;
  gap: 12px;
}

textarea {
  width: 100%;
  min-height: 80px;
  resize: vertical;
  padding: 14px 16px;
  border: 2px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--ivory);
  color: var(--ink);
  font: inherit;
  font-size: 15px;
  line-height: 1.6;
  outline: none;
  transition: all var(--duration-fast) var(--ease-out);
}

textarea::placeholder {
  color: var(--ink-muted);
  opacity: 0.6;
}

textarea:focus {
  border-color: var(--gold);
  box-shadow: 0 0 0 3px var(--gold-glow);
}

.ask-row > button {
  min-height: 80px;
  border: none;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--crimson), var(--crimson-dark));
  color: white;
  font: inherit;
  font-size: 16px;
  font-weight: 800;
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: all var(--duration-normal) var(--ease-out);
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ask-row > button::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, rgba(255,255,255,0.1), transparent);
  transition: opacity var(--duration-normal);
}

.ask-row > button:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 24px rgba(139, 26, 26, 0.35);
}

.ask-row > button:hover::before {
  opacity: 0;
}

.ask-row > button:active {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(139, 26, 26, 0.3);
}

.ask-row > button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* ===== Example Chips ===== */
.examples {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
}

.examples button {
  padding: 6px 14px;
  border: 1.5px solid var(--border);
  border-radius: 100px;
  background: transparent;
  color: var(--ink-soft);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease-out);
}

.examples button:hover {
  border-color: var(--gold);
  color: var(--crimson);
  background: var(--gold-glow);
}

/* ===== Loading State ===== */
textarea:disabled,
.ask-row > button:disabled {
  opacity: 0.7;
}

.ask-row > button:disabled::after {
  content: "";
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== Layout Grid ===== */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  gap: 20px;
  margin-top: 24px;
  align-items: start;
}

.main-column,
.side-column {
  display: grid;
  gap: 16px;
}

/* ===== Cards ===== */
.verdict-card,
.support-card,
.source-card,
.side-card {
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
  transition: all var(--duration-normal) var(--ease-out);
}

.verdict-card {
  border-left: 4px solid var(--crimson);
  background: linear-gradient(135deg, #ffffff 0%, #fdf8f6 100%);
}

.card-label {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--crimson);
  text-transform: uppercase;
  margin-bottom: 12px;
  display: inline-block;
}

.verdict {
  font-family: var(--font-display);
  font-size: 26px;
  font-weight: 700;
  line-height: 1.4;
  color: var(--ink);
}

.mode-note {
  margin-top: 12px;
  padding: 10px 14px;
  background: var(--sage-light);
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--sage);
  line-height: 1.5;
}

/* ===== Section Headers ===== */
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border-light);
}

.section-head h2 {
  font-family: var(--font-display);
  font-size: 18px;
  font-weight: 700;
  color: var(--ink);
  letter-spacing: -0.01em;
}

.section-head span {
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-muted);
  padding: 3px 10px;
  background: var(--parchment);
  border-radius: 100px;
}

/* ===== Lists ===== */
.support-list,
.caveat-list {
  display: grid;
  gap: 12px;
  list-style: none;
  counter-reset: point;
}

.support-list li {
  position: relative;
  padding-left: 32px;
  line-height: 1.65;
  color: var(--ink-soft);
  font-size: 14px;
}

.support-list li::before {
  counter-increment: point;
  content: counter(point);
  position: absolute;
  left: 0;
  top: 0;
  width: 22px;
  height: 22px;
  background: var(--crimson);
  color: white;
  font-size: 11px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.caveat-list {
  padding-left: 0;
}

.caveat-list li {
  position: relative;
  padding: 12px 14px;
  background: linear-gradient(135deg, rgba(201, 168, 76, 0.06), transparent);
  border: 1px solid rgba(201, 168, 76, 0.15);
  border-radius: var(--radius-sm);
  font-size: 14px;
  line-height: 1.65;
  color: var(--ink-soft);
}

.caveat-list li::before {
  content: "!";
  display: inline-block;
  width: 20px;
  height: 20px;
  background: var(--gold);
  color: white;
  font-size: 12px;
  font-weight: 900;
  text-align: center;
  line-height: 20px;
  border-radius: 50%;
  margin-right: 10px;
}

/* ===== Citations ===== */
.citation-list,
.entry-list,
.evidence-list {
  display: grid;
  gap: 0;
}

.citation,
.entry,
.evidence {
  padding: 14px 0;
  border-bottom: 1px solid var(--border-light);
}

.citation:last-child,
.entry:last-child,
.evidence:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.citation:first-child,
.entry:first-child,
.evidence:first-child {
  padding-top: 0;
}

/* ===== Clickable Cards ===== */
.link-card {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  text-align: left;
  cursor: pointer;
  font: inherit;
  transition: all var(--duration-fast) var(--ease-out);
  position: relative;
}

.link-card:hover {
  background: var(--parchment-light);
  border-color: var(--gold);
  transform: translateX(4px);
  box-shadow: var(--shadow-sm);
}

.link-card:active {
  transform: translateX(2px);
}

.link-card .title {
  font-weight: 700;
  font-size: 14px;
  color: var(--ink);
  transition: color var(--duration-fast);
}

.link-card:hover .title {
  color: var(--crimson);
}

.link-card .meta {
  font-size: 11px;
  color: var(--ink-muted);
  margin-top: 4px;
}

.snippet {
  margin-top: 8px;
  font-size: 13px;
  color: var(--ink-soft);
  line-height: 1.6;
}

/* ===== Passages (evidence) ===== */
.passages {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.passage {
  padding: 8px 12px;
  border-left: 3px solid var(--gold);
  background: rgba(201, 168, 76, 0.04);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-size: 13px;
  line-height: 1.6;
  color: var(--ink-soft);
}

/* ===== Score Grid ===== */
.score-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin-top: 10px;
}

.score-grid span {
  padding: 5px 8px;
  background: #f5f0e8;
  border-radius: var(--radius-sm);
  font-size: 11px;
  color: var(--ink-muted);
  text-align: center;
  font-family: var(--font-mono);
}

/* ===== Entry Detail ===== */
.entry-detail-card {
  border-color: rgba(201, 168, 76, 0.3);
  background: linear-gradient(135deg, #ffffff 0%, #fdfaef 100%);
}

.entry-detail h3 {
  font-family: var(--font-display);
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 10px;
  color: var(--ink);
}

.entry-detail .tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.entry-detail .tags span {
  padding: 2px 8px;
  background: var(--parchment-warm);
  border-radius: 100px;
  font-size: 11px;
  color: var(--ink-muted);
}

.entry-detail pre {
  white-space: pre-wrap;
  font: inherit;
  font-size: 14px;
  line-height: 1.7;
  color: var(--ink-soft);
  max-height: 340px;
  overflow: auto;
  padding: 14px;
  background: var(--parchment-light);
  border-radius: var(--radius-sm);
  margin-top: 10px;
}

/* ===== Debug Panel ===== */
.debug {
  border-top: 1px dashed var(--border);
}

.debug summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  padding: 4px 0;
  font-weight: 700;
  font-size: 14px;
  color: var(--ink-muted);
  transition: color var(--duration-fast);
  user-select: none;
}

.debug summary:hover {
  color: var(--ink);
}

.debug summary::marker {
  color: var(--ink-muted);
  font-size: 12px;
}

.debug small {
  font-weight: 400;
  color: var(--ink-muted);
  font-size: 12px;
}

.debug[open] summary {
  margin-bottom: 14px;
  color: var(--ink);
}

/* ===== Empty State ===== */
.empty {
  color: var(--ink-muted);
  font-size: 14px;
  line-height: 1.6;
  padding: 8px 0;
  opacity: 0.7;
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--stone-light);
}

/* ===== Selection ===== */
::selection {
  background: var(--crimson-glow);
  color: var(--ink);
}

/* ===== Responsive ===== */
@media (max-width: 960px) {
  .app {
    padding: 20px 16px 48px;
  }

  .layout {
    grid-template-columns: 1fr;
  }

  .query-head {
    grid-template-columns: 1fr;
  }

  .ask-row {
    grid-template-columns: 1fr;
  }

  .ask-row > button {
    min-height: 48px;
  }

  .verdict {
    font-size: 22px;
  }

  h1 {
    font-size: 30px;
  }
}

@media (max-width: 480px) {
  .mode {
    flex-direction: column;
  }

  .examples {
    flex-direction: column;
  }

  .verdict {
    font-size: 20px;
  }
}

/* ===== Focus ring for accessibility ===== */
:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

/* ===== Reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
