:root {
  --bg: #f5f3ef;
  --surface: #ffffff;
  --surface-2: #f0ede7;
  --text: #1f1d1a;
  --muted: #6b665e;
  --border: #e0dbd2;
  --accent: #b4532a;
  --accent-hover: #963f1b;
  --accent-soft: #f6e6dc;
  --link: #9a4520;
  --ok: #2f7d4f;
  --ok-soft: #e1f2e7;
  --warn: #9a6a08;
  --warn-soft: #fbf0d6;
  --err: #b3261e;
  --err-soft: #fbe3e1;
  --code-bg: #f2efe9;
  --shadow: 0 1px 2px rgba(31, 29, 26, .06), 0 2px 8px rgba(31, 29, 26, .04);
  --radius: 10px;
  --mono: ui-monospace, "JetBrains Mono", "Cascadia Code", Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161514;
    --surface: #1f1e1c;
    --surface-2: #2a2825;
    --text: #ece8e1;
    --muted: #a39d93;
    --border: #36332f;
    --accent: #d9774a;
    --accent-hover: #e8906a;
    --accent-soft: #3a261c;
    --link: #eb9a72;
    --ok: #6cc58f;
    --ok-soft: #1d3326;
    --warn: #e0b04a;
    --warn-soft: #362c14;
    --err: #f0857d;
    --err-soft: #3d1f1d;
    --code-bg: #2a2825;
    --shadow: none;
    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.6 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
a { color: var(--link); text-decoration-thickness: 1px; text-underline-offset: 2px; }
a:hover { color: var(--accent-hover); }
h1, h2, h3, h4 { line-height: 1.25; margin: 0 0 .6em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
code, pre { font-family: var(--mono); font-size: .9em; }
small, .small { font-size: .875rem; }
.muted { color: var(--muted); }

.topbar {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: .7rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-wrap: wrap;
}
.brand { font-weight: 700; color: var(--text); text-decoration: none; letter-spacing: .01em; }
.brand::before { content: "◆ "; color: var(--accent); }
.nav { display: flex; gap: .25rem; flex-wrap: wrap; }
.nav a {
  color: var(--muted);
  text-decoration: none;
  padding: .35rem .7rem;
  border-radius: 7px;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.account { margin-left: auto; display: flex; align-items: center; gap: .75rem; }
.account form { margin: 0; }
.account .user { color: var(--text); text-decoration: none; font-weight: 500; }

.container { width: 100%; max-width: 900px; margin: 0 auto; padding: 1.5rem 1rem 2rem; flex: 1; }
.auth { width: 100%; max-width: 420px; margin: 0 auto; padding: 3rem 1rem; flex: 1; }
.footer { text-align: center; color: var(--muted); font-size: .8rem; padding: 1rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.4rem 1.5rem;
  margin-bottom: 1.25rem;
}
.card.narrow { max-width: 520px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.row h2, .row h1 { margin: 0; }
.stack { display: flex; flex-direction: column; gap: .9rem; }
label { display: flex; flex-direction: column; gap: .3rem; font-weight: 500; font-size: .95rem; }
input, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .55rem .7rem;
  width: 100%;
  font-weight: 400;
}
textarea { resize: vertical; min-height: 6rem; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.code-input { font-family: var(--mono); letter-spacing: .08em; text-transform: uppercase; }

button { font: inherit; cursor: pointer; border-radius: 8px; padding: .55rem 1.1rem; border: 1px solid transparent; }
button.primary { background: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { background: var(--accent-hover); }
button.primary:disabled { opacity: .6; cursor: progress; }
button.secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); padding: .35rem .8rem; font-size: .9rem; }
button.secondary:hover { border-color: var(--accent); }
.link-button { background: none; border: 0; padding: 0; color: var(--muted); text-decoration: underline; text-underline-offset: 2px; }
.link-button:hover { color: var(--text); }
.icon-button { background: none; border: 0; padding: .2rem .5rem; color: var(--muted); font-size: 1.1rem; }
.icon-button:hover { color: var(--text); }

.alert { padding: .65rem .9rem; border-radius: 8px; margin: 0 0 1rem; }
.alert.error { background: var(--err-soft); color: var(--err); }
.alert.success { background: var(--ok-soft); color: var(--ok); }

.badge { display: inline-block; font-size: .75rem; font-weight: 600; padding: .05rem .5rem; border-radius: 999px; white-space: nowrap; }
.badge-done { background: var(--ok-soft); color: var(--ok); }
.badge-pending, .badge-running { background: var(--warn-soft); color: var(--warn); }
.badge-error { background: var(--err-soft); color: var(--err); }

.qlist { list-style: none; margin: .5rem 0 0; padding: 0; }
.qlist li + li { border-top: 1px solid var(--border); }
.qlist a { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; padding: .7rem .2rem; text-decoration: none; color: var(--text); }
.qlist a:hover .qtext { color: var(--link); }
.qmeta { color: var(--muted); font-size: .85rem; white-space: nowrap; display: flex; gap: .5rem; align-items: center; }
.pager { display: flex; justify-content: space-between; margin-top: 1rem; }

.eyebrow { text-transform: uppercase; letter-spacing: .06em; font-size: .75rem; color: var(--muted); margin: 0 0 .3rem; }
.question-text { font-size: 1.3rem; font-weight: 600; white-space: pre-wrap; overflow-wrap: anywhere; }
.question-head { border-bottom: 1px solid var(--border); margin-bottom: 1.2rem; padding-bottom: .4rem; }
.progress { display: flex; align-items: center; gap: .75rem; padding: 1rem 0; }
.spinner { width: 1.2rem; height: 1.2rem; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.consulted { margin-top: 1.5rem; border-top: 1px solid var(--border); padding-top: .8rem; }
.consulted summary { cursor: pointer; color: var(--muted); }
.consulted ul { margin: .5rem 0 0; padding-left: 1.2rem; }
.consulted code { color: var(--muted); font-size: .8em; }
.meta-line { margin-top: .8rem; }

.markdown { overflow-wrap: break-word; }
.markdown > :first-child { margin-top: 0; }
.markdown h1 { font-size: 1.55rem; margin-top: 1.2em; }
.markdown h2 { font-size: 1.25rem; margin-top: 1.4em; padding-bottom: .2em; border-bottom: 1px solid var(--border); }
.markdown h3 { font-size: 1.08rem; margin-top: 1.2em; }
.markdown p, .markdown ul, .markdown ol, .markdown table, .markdown blockquote, .markdown pre { margin: 0 0 1em; }
.markdown ul, .markdown ol { padding-left: 1.4rem; }
.markdown li + li { margin-top: .2em; }
.markdown code { background: var(--code-bg); padding: .1em .35em; border-radius: 4px; }
.markdown pre { background: var(--code-bg); padding: .8rem 1rem; border-radius: 8px; overflow-x: auto; line-height: 1.45; }
.markdown pre code { background: none; padding: 0; }
.markdown blockquote { border-left: 3px solid var(--border); margin-left: 0; padding: .2rem 0 .2rem 1rem; color: var(--muted); }
.markdown blockquote.callout { border-left-color: var(--accent); background: var(--accent-soft); color: var(--text); padding: .6rem 1rem; border-radius: 0 8px 8px 0; }
.markdown table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; font-size: .93rem; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: .4rem .65rem; text-align: left; vertical-align: top; }
.markdown th { background: var(--surface-2); }
.markdown hr { border: 0; border-top: 1px solid var(--border); margin: 1.5em 0; }
a.wikilink { color: var(--link); text-decoration-style: dotted; }
a.wikilink:hover { text-decoration-style: solid; }
.wikilink.missing { color: var(--err); text-decoration: line-through dotted; cursor: help; }

.wiki-layout { display: grid; grid-template-columns: 290px minmax(0, 1fr); gap: 1.25rem; width: 100%; max-width: 1300px; margin: 0 auto; padding: 1.25rem 1rem 2rem; flex: 1; }
.wiki-sidebar { position: sticky; top: 4.2rem; align-self: start; max-height: calc(100vh - 5.5rem); overflow-y: auto; padding-right: .3rem; }
.wiki-search { margin-bottom: .8rem; }
.tree { font-size: .9rem; }
.tree details { margin: .1rem 0; }
.tree summary { cursor: pointer; font-weight: 600; color: var(--muted); padding: .15rem 0; }
.tree details > details, .tree details > ul { margin-left: .8rem; }
.tree ul { list-style: none; padding: 0; margin: .1rem 0 .3rem; }
.tree li a { display: block; padding: .15rem .45rem; border-radius: 5px; color: var(--text); text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree li a:hover { background: var(--surface-2); }
.tree li a.current { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.wiki-main { min-width: 0; }
.wiki-meta { display: flex; flex-wrap: wrap; gap: .35rem; align-items: center; margin: 0 0 1rem; }
.tag { font-size: .78rem; background: var(--surface-2); color: var(--muted); padding: .05rem .45rem; border-radius: 5px; }
.wiki-path { font-family: var(--mono); text-transform: none; letter-spacing: 0; }
.backlinks { margin-top: 2rem; border-top: 1px solid var(--border); padding-top: 1rem; font-size: .93rem; }
.backlinks ul { columns: 2 240px; padding-left: 1.2rem; }
.search-results li { margin-bottom: 1rem; }
.search-results p { margin: .2rem 0 0; font-size: .9rem; }

.inline-form { display: flex; gap: .75rem; align-items: flex-end; flex-wrap: wrap; }
.inline-form .grow { flex: 1; min-width: 200px; }
.table-wrap { overflow-x: auto; margin-top: 1rem; }
table.users { width: 100%; border-collapse: collapse; font-size: .93rem; }
table.users th, table.users td { text-align: left; padding: .55rem .5rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.users th { color: var(--muted); font-weight: 600; font-size: .82rem; text-transform: uppercase; letter-spacing: .04em; }
table.users .actions { text-align: right; }
table.users form { margin: 0; }
.code-card { border-color: var(--accent); background: var(--accent-soft); }
.code-box { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }
.code-box code { font-size: 1.45rem; font-weight: 700; letter-spacing: .1em; background: var(--surface); padding: .35rem .8rem; border-radius: 8px; border: 1px dashed var(--accent); }

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(640px, 100vw);
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: -8px 0 24px rgba(0, 0, 0, .12);
  z-index: 40;
  display: flex;
  flex-direction: column;
}
.drawer[hidden] { display: none; }
.drawer-head { display: flex; align-items: center; gap: .75rem; padding: .6rem 1rem; border-bottom: 1px solid var(--border); }
.drawer-head a { margin-right: auto; font-size: .9rem; }
.drawer-body { overflow-y: auto; padding: 1.2rem 1.4rem 2rem; flex: 1; }
.drawer-body .backlinks ul { columns: 1; }

@media (max-width: 860px) {
  .wiki-layout { grid-template-columns: 1fr; }
  .wiki-sidebar { position: static; max-height: 45vh; border: 1px solid var(--border); border-radius: var(--radius); padding: .8rem; background: var(--surface); }
}
@media (max-width: 560px) {
  .topbar { gap: .4rem .8rem; padding: .6rem .8rem; }
  .nav { order: 3; width: 100%; }
  .card { padding: 1.1rem 1rem; }
  .qlist a { flex-direction: column; gap: .2rem; }
}
