:root {
  --bg: #f7f4ee;
  --surface: #ffffff;
  --surface-2: #f1ece2;
  --text: #1d1f23;
  --muted: #5f6470;
  --border: #e2dccf;
  --accent: #1f3a5f;
  --accent-text: #f6f1e7;
  --accent-soft: #e4ebf4;
  --mark: #fde9a9;
  --danger: #a3312a;
  --ok: #2c6b45;
  --serif: "Newsreader", Georgia, "Times New Roman", serif;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171b; --surface: #1d2026; --surface-2: #23272f; --text: #e8e6e1; --muted: #a0a4ad;
    --border: #30343c; --accent: #8fb3e0; --accent-text: #10141a; --accent-soft: #24303f; --mark: #5a4a17;
    --danger: #ef8a80; --ok: #7fcf9d; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15171b; --surface: #1d2026; --surface-2: #23272f; --text: #e8e6e1; --muted: #a0a4ad;
  --border: #30343c; --accent: #8fb3e0; --accent-text: #10141a; --accent-soft: #24303f; --mark: #5a4a17;
  --danger: #ef8a80; --ok: #7fcf9d; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.55 var(--sans); }
a { color: var(--accent); }
.wrap { max-width: 1160px; margin: 0 auto; padding: 0 16px; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.2; letter-spacing: -0.01em; }

.site-header { border-bottom: 1px solid var(--border); background: var(--bg); position: sticky; top: 0; z-index: 10; }
.header-inner { display: flex; align-items: center; justify-content: space-between; gap: 12px; height: 60px; }
.brand { font-family: var(--serif); font-size: 1.2rem; font-weight: 600; color: var(--text); text-decoration: none; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.brand-mark { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 7px; background: var(--accent); color: var(--accent-text); font-size: 1rem; }
nav { display: flex; align-items: center; gap: 16px; }
nav a { color: var(--muted); text-decoration: none; font-size: .95rem; }
nav a:hover { color: var(--text); }
nav a.btn { color: var(--accent-text); }

.btn { display: inline-flex; align-items: center; gap: 6px; border: 0; border-radius: 8px; background: var(--accent); color: var(--accent-text); font: 500 .95rem var(--sans); padding: 10px 16px; cursor: pointer; text-decoration: none; }
.btn:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .55; cursor: default; }
.btn-small { padding: 7px 12px; font-size: .88rem; }
.btn-ghost { background: transparent; color: var(--accent); border: 1px solid var(--border); }

.hero { padding: 48px 0 24px; max-width: 760px; }
.hero h1 { font-size: clamp(2rem, 5vw, 3rem); margin: 0 0 12px; }
.hero p { color: var(--muted); font-size: 1.1rem; margin: 0; }

.search-row { display: flex; gap: 10px; margin: 24px 0 14px; }
.search-row input { flex: 1; min-width: 0; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 11px 13px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.search-row input { font-size: 1.05rem; padding: 13px 15px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 20px; }
.chip { border: 1px solid var(--border); background: var(--surface); color: var(--muted); border-radius: 999px; padding: 5px 12px; font-size: .87rem; cursor: pointer; }
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.chip .n { opacity: .7; margin-left: 4px; }

.meta-line { color: var(--muted); font-size: .9rem; margin: 0 0 12px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.card { display: flex; flex-direction: column; gap: 8px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; text-decoration: none; color: inherit; transition: border-color .15s, transform .15s; }
.card:hover { border-color: var(--accent); transform: translateY(-1px); }
.card h3 { margin: 0; font-size: 1.2rem; }
.card p { margin: 0; color: var(--muted); font-size: .93rem; }
.card .snippet { color: var(--text); font-size: .88rem; background: var(--surface-2); padding: 8px 10px; border-radius: 6px; }
mark { background: var(--mark); color: inherit; padding: 0 2px; border-radius: 2px; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: auto; padding-top: 4px; }
.tag { font-size: .76rem; padding: 2px 8px; border-radius: 4px; background: var(--surface-2); color: var(--muted); }
.tag.lic { background: var(--accent-soft); color: var(--accent); }
.tag.community { background: transparent; border: 1px dashed var(--border); }
.pager { display: flex; justify-content: center; gap: 10px; margin: 28px 0; }
.empty { padding: 40px; text-align: center; color: var(--muted); background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }

/* Template view */
.back { display: inline-block; margin: 22px 0 6px; color: var(--muted); text-decoration: none; font-size: .92rem; }
.doc-layout { display: grid; grid-template-columns: minmax(0, 1fr) 400px; gap: 24px; align-items: start; padding-bottom: 40px; }
.doc-head h1 { font-size: clamp(1.7rem, 4vw, 2.4rem); margin: 6px 0 8px; }
.doc-head p { color: var(--muted); margin: 0 0 12px; }
.doc-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 18px; }
.attribution { font-size: .88rem; color: var(--muted); background: var(--surface-2); border-radius: 8px; padding: 10px 12px; }
.doc { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: clamp(18px, 4vw, 40px); margin-top: 16px; font-family: var(--serif); font-size: 1.06rem; line-height: 1.65; overflow-wrap: anywhere; }
.doc h1, .doc h2, .doc h3 { font-size: 1.25rem; margin: 1.6em 0 .5em; }
.doc h1:first-child, .doc h2:first-child { margin-top: 0; }
.doc table { border-collapse: collapse; width: 100%; display: block; overflow-x: auto; font-family: var(--sans); font-size: .9rem; }
.doc td, .doc th { border: 1px solid var(--border); padding: 6px 8px; vertical-align: top; }
.doc pre { white-space: pre-wrap; font-family: var(--serif); }

.chat { position: sticky; top: 76px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); display: flex; flex-direction: column; height: calc(100vh - 100px); min-height: 420px; }
.chat-head { padding: 14px 16px; border-bottom: 1px solid var(--border); }
.chat-head h2 { font-size: 1.1rem; margin: 0; }
.chat-head p { margin: 2px 0 0; color: var(--muted); font-size: .85rem; }
.chat-log { flex: 1; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { font-size: .94rem; line-height: 1.55; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-text); padding: 8px 12px; border-radius: 12px 12px 2px 12px; max-width: 88%; white-space: pre-wrap; }
.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3 { font-size: 1rem; margin: 1em 0 .3em; font-family: var(--sans); }
.msg.assistant ul, .msg.assistant ol { padding-left: 1.2em; }
.msg.assistant blockquote { margin: .5em 0; padding-left: 10px; border-left: 3px solid var(--border); color: var(--muted); }
.msg.error { color: var(--danger); font-size: .88rem; }
.typing { color: var(--muted); font-size: .88rem; }
.typing::after { content: "…"; animation: blink 1.2s infinite; }
@keyframes blink { 50% { opacity: .2; } }
.suggestions { display: flex; flex-direction: column; gap: 6px; }
.suggestions button { text-align: left; background: var(--surface-2); border: 1px solid var(--border); color: var(--text); border-radius: 8px; padding: 8px 10px; font: .88rem var(--sans); cursor: pointer; }
.suggestions button:hover { border-color: var(--accent); }
.chat-form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border); }
.chat-form textarea { resize: none; height: 44px; max-height: 140px; padding: 10px 12px; font-size: .94rem; }
.chat-toggle { display: none; }

/* Submit */
.form-page { max-width: 760px; padding: 32px 0 48px; }
.form-page h1 { font-size: 2.2rem; margin: 0 0 8px; }
.form-page > p { color: var(--muted); margin: 0 0 24px; }
.field { margin-bottom: 18px; }
.field label { display: block; font-weight: 500; margin-bottom: 6px; font-size: .95rem; }
.field .hint { color: var(--muted); font-size: .85rem; margin: 4px 0 0; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field textarea { min-height: 280px; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: .9rem; }
.check { display: flex; gap: 10px; align-items: flex-start; font-size: .93rem; }
.check input { width: auto; margin-top: 4px; }
.hp { position: absolute; left: -10000px; width: 1px; height: 1px; overflow: hidden; }
.notice { padding: 12px 14px; border-radius: 8px; margin-bottom: 16px; font-size: .94rem; }
.notice.error { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }
.notice.ok { background: color-mix(in srgb, var(--ok) 12%, transparent); color: var(--ok); }

.site-footer { border-top: 1px solid var(--border); color: var(--muted); font-size: .85rem; padding-top: 18px; padding-bottom: 32px; }

@media (max-width: 960px) {
  .doc-layout { grid-template-columns: 1fr; }
  .chat { position: fixed; inset: auto 0 0 0; top: auto; height: 78vh; border-radius: 14px 14px 0 0; z-index: 20; transform: translateY(105%); transition: transform .25s; box-shadow: 0 -8px 30px rgba(0,0,0,.18); }
  .chat.open { transform: none; }
  .chat-toggle { display: inline-flex; position: fixed; right: 16px; bottom: 16px; z-index: 21; box-shadow: 0 6px 18px rgba(0,0,0,.2); }
  .chat.open + .chat-toggle { display: none; }
  .chat-close { display: inline-flex !important; }
}
@media (max-width: 560px) {
  .brand-text { display: none; }
  nav a:not(.btn) { display: none; }
  .field-row { grid-template-columns: 1fr; }
  .grid { grid-template-columns: 1fr; }
}
.chat-close { display: none; float: right; background: none; border: 0; color: var(--muted); font-size: 1.3rem; cursor: pointer; line-height: 1; }
