:root {
  --bg: #f6f1e8;
  --bg-deep: #ebe3d6;
  --ink: #2c241c;
  --ink-soft: #5c5348;
  --muted: #8a7f72;
  --line: #d9cfc0;
  --card: #fffcf7;
  --user: #e8dfd2;
  --accent: #8b5e3c;
  --accent-soft: rgba(139, 94, 60, 0.12);
  --mark: rgba(196, 149, 90, 0.28);
  --mark-hover: rgba(196, 149, 90, 0.45);
  --shadow: 0 10px 40px rgba(44, 36, 28, 0.08);
  --radius: 16px;
  --font-serif: "Cormorant Garamond", Georgia, serif;
  --font-sans: "Source Sans 3", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(1200px 600px at 10% -10%, #fff9f0 0%, transparent 55%),
    radial-gradient(900px 500px at 100% 0%, #efe6d8 0%, transparent 50%),
    var(--bg);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.55;
}

#app {
  max-width: 1100px;
  margin: 0 auto;
  min-height: 100vh;
  padding: 1.25rem 1.25rem 2rem;
  display: flex;
  flex-direction: column;
}

.top {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem 1.5rem;
  align-items: flex-end;
  justify-content: space-between;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--line);
  margin-bottom: 1rem;
}

.brand h1 {
  margin: 0;
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 2rem;
  letter-spacing: 0.01em;
}

.tagline {
  margin: 0.15rem 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1rem;
  align-items: center;
}

.ctrl {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.ctrl.ctrl-inline {
  flex-direction: row;
  align-items: center;
  gap: 0.55rem;
}

.ctrl select {
  font: inherit;
  text-transform: none;
  letter-spacing: 0;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.4rem 0.65rem;
  min-width: 8rem;
}

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr;
  gap: 0;
  min-height: 0;
  position: relative;
}

.layout.drawer-open {
  grid-template-columns: minmax(0, 1fr) minmax(280px, 360px);
  gap: 1rem;
}

.chat-panel {
  display: flex;
  flex-direction: column;
  min-height: 70vh;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.25rem 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.msg {
  max-width: min(40rem, 92%);
  animation: rise 0.25s ease;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

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

.msg.assistant {
  align-self: flex-start;
}

.bubble {
  padding: 0.85rem 1.05rem;
  border-radius: 14px;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg.user .bubble {
  background: var(--user);
  color: var(--ink);
  border-bottom-right-radius: 4px;
}

.msg.assistant .bubble {
  background: #fff;
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
  font-size: 1.02rem;
  line-height: 1.6;
}

.wisdom-span {
  background: transparent;
  color: var(--accent);
  font-weight: 600;
  border-bottom: 1px solid transparent;
  border-radius: 0;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, font-weight 0.15s ease;
  padding: 0;
  box-shadow: none;
}

.wisdom-span:hover,
.wisdom-span:focus {
  outline: none;
  border-bottom-color: rgba(139, 94, 60, 0.45);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.55rem;
  padding-left: 0.15rem;
}

.chip {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--accent-soft);
  color: var(--accent);
  font: inherit;
  font-size: 0.78rem;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.chip:hover {
  border-color: var(--accent);
  background: rgba(139, 94, 60, 0.18);
}

.welcome {
  margin: auto;
  text-align: center;
  max-width: 22rem;
  color: var(--ink-soft);
  padding: 2rem 1rem;
}

.welcome h2 {
  font-family: var(--font-serif);
  font-weight: 500;
  font-size: 1.75rem;
  color: var(--ink);
  margin: 0 0 0.5rem;
}

.status {
  padding: 0.5rem 1.25rem;
  font-size: 0.9rem;
  color: var(--muted);
}

.status.error {
  color: #8b3a2a;
  background: #f8e8e4;
}

.thinking-msg {
  max-width: none;
  animation: thinking-fade 1.8s ease-in-out infinite;
}

.thinking-indicator {
  color: var(--muted);
  font-style: italic;
  padding: 0.35rem 0.15rem;
  background: none;
  border: none;
  box-shadow: none;
}

.thinking-label {
  letter-spacing: 0.02em;
}

.thinking-dots span {
  display: inline-block;
  animation: thinking-bounce 1.2s ease-in-out infinite;
  opacity: 0.35;
}

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

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

@keyframes thinking-bounce {
  0%, 80%, 100% {
    opacity: 0.25;
    transform: translateY(0);
  }
  40% {
    opacity: 1;
    transform: translateY(-2px);
  }
}

@keyframes thinking-fade {
  0%, 100% { opacity: 0.55; }
  50% { opacity: 1; }
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.65rem;
  padding: 0.85rem 1rem 1rem;
  border-top: 1px solid var(--line);
  background: linear-gradient(180deg, #fffcf7, #faf6ef);
}

.composer textarea {
  resize: none;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.7rem 0.85rem;
  font: inherit;
  color: var(--ink);
  background: #fff;
  min-height: 3rem;
  max-height: 10rem;
}

.composer textarea:focus {
  outline: 2px solid var(--accent-soft);
  border-color: var(--accent);
}

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  font: inherit;
  font-weight: 500;
  padding: 0.55rem 1rem;
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fffaf4;
}

.btn.primary:hover {
  filter: brightness(1.05);
}

.btn.ghost {
  background: transparent;
}

.drawer {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: auto;
  max-height: calc(100vh - 8rem);
  position: sticky;
  top: 1rem;
}

.drawer[hidden] { display: none !important; }

.drawer-inner {
  padding: 1.1rem 1.15rem 1.5rem;
  position: relative;
}

.drawer-close {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  font-size: 1.4rem;
  line-height: 1;
  padding: 0.2rem 0.55rem;
}

.drawer-kicker {
  margin: 0;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
}

.drawer-inner h2 {
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 1.65rem;
  margin: 0.25rem 0 0.15rem;
  padding-right: 2rem;
}

.drawer-date {
  margin: 0 0 1rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.drawer-body {
  white-space: pre-wrap;
  font-size: 0.98rem;
  line-height: 1.65;
  color: var(--ink);
  border-top: 1px solid var(--line);
  padding-top: 1rem;
}

.drawer-body.collapsed {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 6;
  overflow: hidden;
  white-space: normal;
}

.drawer-actions {
  margin-top: 1.1rem;
}

.drawer-actions .btn {
  width: 100%;
}

.drawer-actions .btn[hidden] {
  display: none !important;
}

.related {
  margin-top: 1.5rem;
  border-top: 1px solid var(--line);
  padding-top: 1rem;
}

.related h3 {
  margin: 0 0 0.65rem;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 600;
}

.related-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.related-item {
  text-align: left;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 12px;
  padding: 0.65rem 0.75rem;
  cursor: pointer;
  font: inherit;
  color: inherit;
}

.related-item:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.related-item strong {
  display: block;
  font-family: var(--font-serif);
  font-size: 1.1rem;
  font-weight: 600;
}

.related-item span {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  margin: 0.15rem 0 0.35rem;
}

.related-item em {
  display: block;
  font-style: normal;
  font-size: 0.85rem;
  color: var(--ink-soft);
  line-height: 1.4;
}

.tooltip {
  position: fixed;
  z-index: 100;
  max-width: 280px;
  padding: 0.55rem 0.7rem;
  background: #2c241c;
  color: #f6f1e8;
  border-radius: 10px;
  font-size: 0.82rem;
  line-height: 1.4;
  box-shadow: 0 8px 24px rgba(0,0,0,0.18);
  pointer-events: none;
}

.tooltip .t-title {
  font-family: var(--font-serif);
  font-size: 1rem;
  font-weight: 600;
}

.tooltip .t-meta {
  opacity: 0.75;
  margin: 0.15rem 0 0.35rem;
}

.tooltip .t-ex {
  opacity: 0.9;
  font-size: 0.78rem;
}

@media (max-width: 860px) {
  .layout.drawer-open {
    grid-template-columns: 1fr;
  }
  .drawer {
    position: fixed;
    inset: auto 0 0 0;
    max-height: 70vh;
    border-radius: var(--radius) var(--radius) 0 0;
    z-index: 50;
  }
}
