:root {
  --bg: #f6f3ee;
  --ink: #1d1a16;
  --muted: #6b635a;
  --line: #d9d0c4;
  --accent: #0f5c4c;
  --accent-ink: #f7fffc;
  --card: #fffdf9;
  --user: #e7f2ef;
  --assistant: #fff7ea;
  --error: #8a1f1f;
  --font: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(circle at top left, #efe6d8 0%, transparent 40%),
    linear-gradient(180deg, #f8f4ee 0%, #efe8dc 100%);
  min-height: 100vh;
}

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

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 253, 249, 0.85);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
}

.brand {
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--ink);
  text-decoration: none;
}

.topbar nav {
  display: flex;
  gap: 1rem;
  align-items: center;
}

.logout-form { margin: 0; }

.linkish {
  border: 0;
  background: transparent;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  padding: 0;
}

.linkish:hover { text-decoration: underline; }

.container {
  width: min(960px, calc(100% - 2rem));
  margin: 1.5rem auto 3rem;
}

.hero, .auth-card, section:not(.chat-layout) {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(40, 30, 10, 0.05);
}

.container:has(.chat-shell) {
  width: min(1200px, calc(100% - 2rem));
}

.chat-shell {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  gap: 1rem;
  align-items: stretch;
  min-height: 75vh;
}

.chat-sidebar {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 1rem;
  box-shadow: 0 10px 30px rgba(40, 30, 10, 0.05);
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  max-height: 75vh;
}

.sidebar-new {
  width: 100%;
  text-align: center;
}

.sidebar-empty {
  margin: 0.5rem 0 0;
  font-size: 0.92rem;
}

.chat-layout {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 75vh;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(40, 30, 10, 0.05);
}

.conversation-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.conversation-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.25rem;
  align-items: center;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 0.2rem 0.25rem 0.2rem 0.55rem;
}

.conversation-item.active,
.conversation-item:hover {
  border-color: var(--line);
  background: rgba(15, 92, 76, 0.05);
}

.conversation-item.active {
  border-color: var(--accent);
}

.conversation-link {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--ink);
  text-decoration: none;
  font-size: 0.92rem;
  padding: 0.35rem 0;
}

.conversation-link:hover {
  color: var(--accent);
  text-decoration: none;
}

.conversation-item.streaming .conversation-link::after {
  content: "";
  display: inline-block;
  width: 0.45rem;
  height: 0.45rem;
  margin-left: 0.4rem;
  border-radius: 50%;
  background: var(--accent);
  vertical-align: middle;
  animation: conversation-stream-pulse 1.1s ease-in-out infinite;
}

@keyframes conversation-stream-pulse {
  0%, 100% { opacity: 0.35; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1); }
}

.conversation-delete {
  margin: 0;
}

.conversation-delete button {
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.2rem;
  line-height: 1;
  padding: 0.25rem 0.45rem;
  border-radius: 6px;
}

.conversation-delete button:hover {
  color: var(--error);
  background: rgba(138, 31, 31, 0.08);
}

@media (max-width: 800px) {
  .chat-shell {
    grid-template-columns: 1fr;
  }

  .chat-sidebar {
    max-height: 14rem;
  }
}

.hero h1, .auth-card h1, .chat-header h1, section h1 {
  margin-top: 0;
}

.actions { display: flex; gap: 0.75rem; flex-wrap: wrap; }

.btn {
  display: inline-block;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  padding: 0.7rem 1.1rem;
  font: inherit;
  cursor: pointer;
}

.btn.secondary {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
}

.muted { color: var(--muted); }
.error { color: var(--error); }
code { font-family: var(--mono); font-size: 0.92em; }

.auth-card form,
.composer {
  display: grid;
  gap: 0.85rem;
}

label {
  display: grid;
  gap: 0.35rem;
}

input, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.7rem 0.8rem;
  font: inherit;
  background: #fff;
}

.kb-list {
  list-style: none;
  padding: 0;
  margin: 1rem 0 0;
}

.kb-list li {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 0;
  border-top: 1px solid var(--line);
}

.chat-history-bar {
  display: none;
}

.composer-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
}

.composer-input-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 0.65rem;
  align-items: end;
}

.composer-input-row #prompt {
  min-width: 0;
}

.composer-input-row #attach-btn,
.composer-input-row #send-btn {
  white-space: nowrap;
}

.attachment-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  max-width: 100%;
  padding: 0.35rem 0.55rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  font-size: 0.9rem;
  color: var(--ink);
}

.attachment-chip-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 16rem;
}

.attachment-chip-remove {
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 0;
  line-height: 1;
  font-size: 1.05rem;
}

.attachment-chip-remove:hover {
  color: var(--error);
}

@media (max-width: 720px) {
  .composer-input-row {
    grid-template-columns: 1fr auto;
  }

  .composer-input-row #attach-btn {
    grid-column: 1 / -1;
    justify-self: start;
  }
}

.model-picker {
  display: flex;
  gap: 0.55rem;
  align-items: center;
  font-size: 0.95rem;
  color: var(--muted);
}

.model-picker-label {
  white-space: nowrap;
}

.model-select,
#model-select {
  appearance: none;
  -webkit-appearance: none;
  width: auto;
  min-width: 14rem;
  max-width: min(100%, 22rem);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.65rem 2.2rem 0.65rem 0.8rem;
  font: inherit;
  color: var(--ink);
  background-color: #fff;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%230f5c4c' d='M1.4.6 6 5.2 10.6.6 12 2 6 8 0 2z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.85rem center;
  background-size: 12px 8px;
  cursor: pointer;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.model-select:hover,
#model-select:hover {
  border-color: var(--accent);
}

.model-select:focus,
#model-select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 92, 76, 0.15);
}

.model-select:disabled,
#model-select:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.msg.editing {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.msg.editing .msg-actions {
  display: none;
}

.msg-actions {
  margin-top: 0.45rem;
  display: flex;
  justify-content: flex-end;
  gap: 0.15rem;
}

.msg-edit,
.msg-copy {
  border: 0;
  background: transparent;
  color: var(--accent);
  cursor: pointer;
  padding: 0.2rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
}

.msg-edit:hover,
.msg-copy:hover {
  background: rgba(15, 92, 76, 0.1);
}

.msg-inline-edit {
  position: relative;
  margin-top: 0.25rem;
}

.msg-edit-input {
  width: 100%;
  border: 1px solid var(--accent);
  border-radius: 10px;
  padding: 0.65rem 2.25rem 0.65rem 0.75rem;
  font: inherit;
  background: #fff;
  resize: vertical;
  min-height: 4rem;
}

.msg-edit-input:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(15, 92, 76, 0.15);
}

.msg-edit-cancel {
  position: absolute;
  top: 0.35rem;
  right: 0.35rem;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.35rem;
  line-height: 1;
  padding: 0.2rem 0.4rem;
  border-radius: 6px;
}

.msg-edit-cancel:hover {
  color: var(--error);
  background: rgba(138, 31, 31, 0.08);
}

.timer {
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  font-weight: 600;
}

.msg-thinking {
  margin-bottom: 0.75rem;
  border: 1px dashed var(--line);
  border-radius: 10px;
  padding: 0.5rem 0.75rem;
  background: rgba(15, 92, 76, 0.04);
  color: var(--muted);
}

.msg-thinking summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--ink);
}

.msg-thinking-body {
  margin-top: 0.5rem;
  white-space: pre-wrap;
  font-size: 0.92rem;
  line-height: 1.4;
  max-height: 12rem;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.msg-content {
  line-height: 1.5;
}

.msg-content > *:first-child { margin-top: 0; }
.msg-content > *:last-child { margin-bottom: 0; }

.msg-content pre {
  overflow-x: auto;
  padding: 0.75rem;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.06);
}

.msg-content .katex-display {
  margin: 0.75rem 0;
  overflow-x: auto;
  overflow-y: hidden;
}

.msg-content .tex-math {
  font-family: var(--mono);
}

.msg-meta {
  margin-top: 0.55rem;
  font-size: 0.85rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.messages {
  overflow-y: auto;
  display: grid;
  gap: 0.75rem;
  padding: 1rem 0;
  max-height: 50vh;
}

.msg {
  padding: 0.85rem 1rem;
  border-radius: 12px;
  line-height: 1.45;
}

.msg.user { background: var(--user); }
.msg.assistant { background: var(--assistant); }
