@import url('https://fonts.googleapis.com/css2?family=Instrument+Serif:ital@0;1&family=Geist:wght@300..700&display=swap');

/* ---------- Tokens ---------- */
:root,
:root[data-theme="dark"] {
  --ink: #0c0d10;
  --surface: #14161b;
  --raised: #1c1f26;
  --paper: #eceae4;
  --muted: #8e929c;
  --line: #272a33;
  --signal: #e8b44a;
  --signal-ink: #1a1305;
  --danger: #e08a7a;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --ink: #f6f4ef;
  --surface: #ffffff;
  --raised: #efebe2;
  --paper: #17181c;
  --muted: #6b6f78;
  --line: #ddd8cc;
  --signal: #8a5f08;
  --signal-ink: #ffffff;
  --danger: #b5483a;
  color-scheme: light;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --ink: #f6f4ef;
    --surface: #ffffff;
    --raised: #efebe2;
    --paper: #17181c;
    --muted: #6b6f78;
    --line: #ddd8cc;
    --signal: #8a5f08;
    --signal-ink: #ffffff;
    --danger: #b5483a;
    color-scheme: light;
  }
}

/* ---------- Base ---------- */
*,
*::before,
*::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--ink);
  color: var(--paper);
  font-family: 'Geist', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 {
  font-family: 'Instrument Serif', Georgia, 'Times New Roman', serif;
  font-weight: 400;
  line-height: 1.1;
  margin: 0 0 0.5em;
  letter-spacing: -0.01em;
}

p { margin: 0 0 1em; }
a { color: inherit; }
[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--signal); outline-offset: 2px; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.wrap { max-width: 1120px; margin: 0 auto; padding: 0 20px; width: 100%; }
.lede { font-size: 1.15rem; color: var(--muted); max-width: 34rem; }
.hint { color: var(--muted); font-size: 0.9rem; margin: 0; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  padding: 0 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--paper);
  font: inherit;
  font-size: 0.95rem;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn-primary { background: var(--signal); color: var(--signal-ink); border-color: transparent; font-weight: 500; }
.btn-primary:hover { filter: brightness(1.08); border-color: transparent; }
.btn-quiet { min-height: 32px; padding: 0 10px; font-size: 0.85rem; color: var(--muted); border-color: transparent; }
.btn:disabled { opacity: 0.5; cursor: default; }

/* ---------- Site header and footer ---------- */
.site-header { position: relative; border-bottom: 1px solid var(--line); }
.site-header .wrap { display: flex; align-items: center; gap: 16px; height: 64px; }
.brand { font-family: 'Instrument Serif', serif; font-size: 1.45rem; text-decoration: none; letter-spacing: 0.02em; margin-right: auto; }
.brand span { color: var(--signal); }
.site-nav { display: flex; gap: 22px; }
.site-nav a { text-decoration: none; color: var(--muted); font-size: 0.95rem; }
.site-nav a:hover { color: var(--paper); }
.site-nav .nav-auth { display: none; }
.header-actions { display: flex; gap: 8px; align-items: center; }
.nav-toggle { display: none; }

.site-footer { border-top: 1px solid var(--line); padding: 32px 0; color: var(--muted); font-size: 0.9rem; }
.site-footer .wrap { display: flex; flex-wrap: wrap; gap: 16px; justify-content: space-between; align-items: center; }
.site-footer nav { display: flex; gap: 18px; }
.site-footer a { text-decoration: none; }
.site-footer a:hover { color: var(--paper); }

/* ---------- Home ---------- */
.hero { padding: 96px 0 64px; }
.hero h1 { font-size: clamp(3rem, 8vw, 6.2rem); max-width: 12ch; line-height: 0.98; }
.ask {
  display: flex;
  gap: 8px;
  align-items: center;
  margin: 36px 0 14px;
  max-width: 640px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 6px;
}
.ask:focus-within { border-color: var(--muted); }
.ask input {
  flex: 1;
  min-width: 0;
  background: none;
  border: 0;
  color: var(--paper);
  font: inherit;
  font-size: 1.02rem;
  padding: 10px 12px;
  outline: none;
}
.hero-links { display: flex; gap: 20px; flex-wrap: wrap; margin-top: 24px; font-size: 0.95rem; }

.section { padding: 56px 0; border-top: 1px solid var(--line); }
.section h2 { font-size: clamp(1.9rem, 4vw, 2.6rem); margin-bottom: 24px; }

.facts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 28px 32px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.facts strong { display: block; font-weight: 500; margin-bottom: 4px; }
.facts span { color: var(--muted); font-size: 0.95rem; }

.rows { list-style: none; margin: 0; padding: 0; }
.rows a {
  display: grid;
  grid-template-columns: 200px 1fr auto;
  gap: 16px;
  align-items: baseline;
  padding: 18px 0;
  border-top: 1px solid var(--line);
  text-decoration: none;
}
.rows li:last-child a { border-bottom: 1px solid var(--line); }
.rows .name { font-family: 'Instrument Serif', serif; font-size: 1.5rem; }
.rows .desc { color: var(--muted); }
.rows .open { color: var(--muted); font-size: 0.9rem; }
.rows a:hover .name { color: var(--signal); }
.rows a:hover .open { color: var(--paper); }

/* ---------- Tool pages ---------- */
.tool-page { max-width: 760px; margin: 0 auto; padding: 48px 20px 64px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 24px 0; }
.chip {
  background: none;
  border: 1px solid var(--line);
  color: var(--paper);
  border-radius: 999px;
  padding: 7px 14px;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}
.chip:hover { border-color: var(--muted); }
.tool-form { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px; }
.tool-form textarea {
  width: 100%;
  min-height: 120px;
  background: none;
  border: 0;
  color: var(--paper);
  font: inherit;
  resize: vertical;
  outline: none;
  padding: 6px;
}
.form-row { display: flex; justify-content: flex-end; margin-top: 8px; }
.results { margin-top: 32px; display: flex; flex-direction: column; gap: 20px; }
.tool-form-plain { max-width: 420px; }
.status { color: var(--muted); min-height: 1.5em; margin-top: 20px; }
.status.is-error { color: var(--danger); }
.result-prompt { font-weight: 500; margin: 0 0 6px; white-space: pre-wrap; }
.result-note { color: var(--muted); margin: 0; }

/* ---------- App shell (chat) ---------- */
.app {
  display: grid;
  grid-template-columns: 272px minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  position: relative;
}
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  background: var(--surface);
  border-right: 1px solid var(--line);
  min-height: 0;
}
.sidebar .btn { width: 100%; }
.brand-small { font-family: 'Instrument Serif', serif; font-size: 1.3rem; text-decoration: none; padding: 4px; }
.brand-small span { color: var(--signal); }
.side-label { font-size: 0.8rem; color: var(--muted); margin: 12px 4px 4px; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow: auto; flex: 1; min-height: 0; }
.conv-list button {
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  color: var(--paper);
  font: inherit;
  font-size: 0.92rem;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conv-list button:hover,
.conv-list button[aria-current="true"] { background: var(--raised); }
.side-foot { border-top: 1px solid var(--line); padding-top: 12px; font-size: 0.85rem; color: var(--muted); }
.side-foot p { margin: 0 0 8px; }
.side-foot a { color: var(--paper); margin-right: 14px; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 56px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  flex: none;
}
.topbar h1 { font-size: 1.25rem; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.topbar .spacer { margin-left: auto; }
.menu-btn { display: none; }

.thread { flex: 1; overflow: auto; padding: 24px 16px; min-height: 0; }
.thread-inner { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 24px; }
.empty { color: var(--muted); text-align: center; margin-top: 18vh; }
.msg-user {
  align-self: flex-end;
  max-width: 82%;
  background: var(--raised);
  padding: 10px 14px;
  border-radius: 16px 16px 4px 16px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg-ai { max-width: 100%; }
.msg-ai .body p:last-child { margin-bottom: 0; }
.msg-ai pre {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  overflow: auto;
  font-size: 0.88rem;
}
code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.9em; }
.msg-actions { margin-top: 4px; }
.typing { display: flex; gap: 6px; padding: 8px 0; }
.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: pulse 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes pulse {
  0%, 80%, 100% { opacity: 0.25; }
  40% { opacity: 1; }
}

.composer { flex: none; padding: 12px 16px 18px; border-top: 1px solid var(--line); background: var(--ink); }
.composer-inner {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 8px;
}
.composer-inner:focus-within { border-color: var(--muted); }
.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  border: 0;
  background: none;
  color: var(--paper);
  font: inherit;
  padding: 9px 10px;
  max-height: 180px;
  outline: none;
}
.scrim { display: none; }

/* ---------- Dashboard ---------- */
.dash { max-width: 720px; margin: 0 auto; padding: 32px 20px 64px; }
.tabs { display: flex; gap: 4px; overflow-x: auto; border-bottom: 1px solid var(--line); margin-bottom: 28px; }
.tab {
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  font: inherit;
  padding: 12px 10px;
  cursor: pointer;
  white-space: nowrap;
}
.tab[aria-selected="true"] { color: var(--paper); border-bottom-color: var(--signal); }
.field { margin-bottom: 18px; }
.field label { display: block; font-size: 0.9rem; color: var(--muted); margin-bottom: 6px; }
.field input,
.field select,
.field textarea {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--paper);
  font: inherit;
  padding: 10px 12px;
  border-radius: 10px;
}
.field input:disabled { opacity: 0.6; }
.field-help { font-size: 0.85rem; color: var(--muted); margin: 6px 0 0; }
.panel-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.dl-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 0;
  border-top: 1px solid var(--line);
}
.dl-row span { color: var(--muted); }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 12px);
  background: var(--raised);
  border: 1px solid var(--line);
  color: var(--paper);
  padding: 11px 16px;
  border-radius: 10px;
  font-size: 0.92rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  max-width: 90vw;
  z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Responsive ---------- */
@media (max-width: 800px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(84%, 300px);
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }
  .app.nav-open .sidebar { transform: none; }
  .app.nav-open .scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 25;
  }
  .menu-btn { display: inline-flex; }
}

@media (max-width: 640px) {
  .site-nav {
    display: none;
    position: absolute;
    top: 64px;
    left: 0;
    right: 0;
    flex-direction: column;
    gap: 0;
    background: var(--ink);
    border-bottom: 1px solid var(--line);
    padding: 4px 20px 12px;
    z-index: 20;
  }
  .site-nav.is-open { display: flex; }
  .site-nav a { padding: 12px 0; border-top: 1px solid var(--line); color: var(--paper); }
  .site-nav .nav-auth { display: block; }
  .nav-toggle { display: inline-flex; }
  .header-actions .hide-sm { display: none; }
  .hero { padding: 64px 0 48px; }
  .rows a { grid-template-columns: 1fr; gap: 4px; }
  .rows .open { display: none; }
}

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