:root {
  --bg: #f6f5f1; --surface: #ffffff; --text: #1f2421; --muted: #6b716c; --border: #e2e0d8;
  --accent: #3f6b3a; --accent-soft: #e6efe3; --warn: #8a5a00; --warn-soft: #fbf0d9;
  --error: #a3312a; --error-soft: #fbe6e4; --project: #2f5d7c; --project-soft: #e3edf4;
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161917; --surface: #1f2321; --text: #e7e9e6; --muted: #9aa19b; --border: #333a35;
    --accent: #8fc286; --accent-soft: #263325; --warn: #e5b55a; --warn-soft: #3a2f19;
    --error: #ef8a82; --error-soft: #3d2220; --project: #8fbbd9; --project-soft: #1f2e38;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0 0 .4rem; }
h2 { font-size: 1.05rem; margin: 1.4rem 0 .5rem; }
main { max-width: 1280px; margin: 0 auto; padding: 1.25rem 16px 3rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

.topbar { display: flex; align-items: center; gap: 1.5rem; padding: .6rem 16px; background: var(--surface); border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 1.15rem; color: var(--text); }
.topbar nav { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; flex: 1; }
.topbar nav a { color: var(--muted); }
.topbar nav a.active { color: var(--text); font-weight: 600; }
.topbar nav .button.primary { margin-left: auto; color: #fff; }
.count { background: var(--warn); color: var(--surface); border-radius: 999px; padding: 0 .45rem; font-size: .75rem; font-weight: 600; }

input[type=text], input[type=search], input[type=email], input[type=password], select, textarea {
  font: inherit; color: inherit; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: .45rem .6rem; max-width: 100%;
}
textarea { width: 100%; }
.button, input[type=submit], button { font: inherit; cursor: pointer; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 6px; padding: .45rem .9rem; display: inline-block; }
.button:hover { text-decoration: none; }
.button.primary, input[type=submit].primary { background: var(--accent); border-color: var(--accent); color: #fff; }
@media (prefers-color-scheme: dark) { .button.primary, input[type=submit].primary { color: #111; } .topbar nav .button.primary { color: #111; } }
.button.small { padding: .25rem .6rem; font-size: .85rem; }
.button.danger { color: var(--error); }
button.link { background: none; border: 0; color: var(--muted); padding: 0; }
form.button_to { display: inline; }

.flash { padding: .6rem .8rem; border-radius: var(--radius); margin: 0 0 1rem; }
.flash.notice { background: var(--accent-soft); }
.flash.alert { background: var(--error-soft); color: var(--error); }

.search .q { width: 100%; font-size: 1.1rem; padding: .7rem .9rem; }
.filters { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin: .6rem 0 1rem; }
.summary { margin-bottom: .6rem; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .5rem .7rem; border-bottom: 1px solid var(--border); vertical-align: top; text-align: left; }
thead th { font-size: .8rem; color: var(--muted); font-weight: 600; }
tbody tr:last-child td, tbody tr:last-child th { border-bottom: 0; }
table.documents td.what { max-width: 420px; }
tr.dup { opacity: .55; }
.tags { min-width: 180px; }
.empty-state { text-align: center; color: var(--muted); padding: 2rem; }
.pager { display: flex; gap: .3rem; flex-wrap: wrap; margin-top: 1rem; }
.pager a { padding: .2rem .55rem; border-radius: 4px; border: 1px solid var(--border); background: var(--surface); }
.pager a.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.badge { display: inline-block; font-size: .75rem; padding: .05rem .45rem; border-radius: 999px; background: var(--accent-soft); color: var(--accent); margin: 1px 0; white-space: nowrap; }
.badge.project { background: var(--project-soft); color: var(--project); }
.badge.empty { background: transparent; color: var(--muted); border: 1px dashed var(--border); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.error { background: var(--error-soft); color: var(--error); }
.badge.ok { background: var(--accent-soft); color: var(--accent); }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.1rem; margin-bottom: 1rem; }
.card h2 { margin-top: 0; }
.stack { display: flex; flex-direction: column; gap: .7rem; }
.stack label { display: flex; flex-direction: column; gap: .25rem; font-size: .9rem; color: var(--muted); }
.stack label.check { flex-direction: row; align-items: center; gap: .4rem; color: var(--text); }
.row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.inline { display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; }

.doc { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 1.5rem; align-items: start; }
.preview { position: sticky; top: 1rem; }
.preview iframe { width: 100%; height: 85vh; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; }
.preview img { width: 100%; border-radius: var(--radius); border: 1px solid var(--border); }
.amount { font-size: 1.6rem; font-weight: 700; margin: .2rem 0 .6rem; }
table.kv { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table.kv th { width: 140px; color: var(--muted); font-weight: 500; }
details { margin-top: 1rem; }
pre { white-space: pre-wrap; font-size: .8rem; background: var(--surface); border: 1px solid var(--border); padding: .8rem; border-radius: var(--radius); }
.actions { margin-top: 1.5rem; }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.category-row { padding: .3rem 0; border-bottom: 1px solid var(--border); }
.category-row input[type=text] { flex: 1; min-width: 120px; }
.category-row.new { border-bottom: 0; padding-top: .7rem; }
table.report td, table.report th { white-space: nowrap; }
table.report tfoot td, table.report tfoot th { border-top: 2px solid var(--border); }

.login { max-width: 340px; margin: 12vh auto; text-align: center; }
.login .stack { margin-top: 1.5rem; }

@media (max-width: 860px) {
  .doc, .two-col { grid-template-columns: 1fr; }
  .preview { position: static; }
  .preview iframe { height: 60vh; }
}

@media (max-width: 640px) {
  .topbar { gap: .5rem; }
  .topbar nav { gap: .4rem .9rem; font-size: .92rem; }
  .topbar nav .button.primary { margin-left: 0; }
  .filters select { flex: 1 1 45%; }
  table.documents thead { display: none; }
  table.documents, table.documents tbody, table.documents tr, table.documents td { display: block; }
  table.documents tr { display: grid; grid-template-columns: 1fr auto; gap: .15rem .6rem; padding: .7rem .8rem; border-bottom: 1px solid var(--border); }
  table.documents td { border: 0; padding: 0; }
  table.documents td:nth-child(1) { grid-column: 1; color: var(--muted); font-size: .85rem; }
  table.documents td.num { grid-column: 2; grid-row: 1 / 3; font-weight: 600; }
  table.documents td:nth-child(2) { grid-column: 1; }
  table.documents td.what, table.documents td.tags { grid-column: 1 / -1; max-width: none; min-width: 0; font-size: .9rem; }
}

/* --- Search --- */
mark { background: var(--warn-soft); color: inherit; font-weight: 600; padding: 0 1px; border-radius: 2px; }
.search-box { position: relative; max-width: 720px; }
.search-field { display: flex; align-items: center; gap: .5rem; background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: .15rem 1rem; }
.search-field:focus-within { border-color: var(--accent); box-shadow: 0 1px 6px rgba(0,0,0,.08); }
.search-field svg { width: 18px; height: 18px; flex: none; fill: none; stroke: var(--muted); stroke-width: 2; }
.search-field input[type=search] { border: 0; background: transparent; flex: 1; min-width: 0; padding: .55rem 0; font-size: 1rem; outline: none; }
.search-box.big { margin: 1.2rem auto 0; }
.search-box.big .search-field { padding: .35rem 1.2rem; }
.search-box.big input[type=search] { font-size: 1.15rem; }
.topbar .search-box { flex: 1; max-width: 420px; }
.suggestions { position: absolute; left: 0; right: 0; top: calc(100% + 4px); background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 6px 20px rgba(0,0,0,.12); z-index: 20; padding: .3rem 0; }
.suggestion { display: flex; justify-content: space-between; gap: 1rem; padding: .45rem 1.1rem; color: var(--text); }
.suggestion:hover, .suggestion.active { background: var(--bg); text-decoration: none; }
.suggestion .hint { color: var(--muted); font-size: .85rem; white-space: nowrap; }

.home { max-width: 760px; margin: 8vh auto 0; text-align: center; }
.home .logo { font-size: 3rem; letter-spacing: -.02em; color: var(--accent); margin: 0; }
.home .search-box { text-align: left; }
.try { margin: 1rem 0 2rem; display: flex; flex-wrap: wrap; gap: .4rem; justify-content: center; align-items: center; }
.browse { text-align: left; display: grid; gap: 1rem; }
.browse h2, .recent h2 { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 .4rem; }
.recent { text-align: left; margin-top: 1.8rem; }
.recent-item { display: flex; justify-content: space-between; gap: 1rem; padding: .45rem 0; border-bottom: 1px solid var(--border); color: var(--text); }
.help { text-align: left; margin: 1.5rem 0 3rem; color: var(--muted); }
.help summary { cursor: pointer; }
.help li { margin: .35rem 0; }

.chip { display: inline-block; padding: .25rem .7rem; border: 1px solid var(--border); border-radius: 999px; background: var(--surface); color: var(--text); font-size: .88rem; margin: 0 .15rem .3rem 0; }
.chip:hover { text-decoration: none; border-color: var(--accent); }
.chip.project { color: var(--project); }
.chip.ghost { border-style: dashed; color: var(--muted); }
.chip.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.tools { display: flex; flex-wrap: wrap; gap: .3rem; align-items: center; margin: .9rem 0 .2rem; max-width: 1080px; }
.tools .spacer { flex: 1; }
.tools .sort select { padding: .3rem .5rem; font-size: .88rem; }
.stats { color: var(--muted); margin: .3rem 0 1rem; }
.stats strong { color: var(--text); }

.serp { display: grid; grid-template-columns: minmax(0, 720px) 240px; gap: 2.5rem; align-items: start; }
.result { padding: .2rem 0 1.1rem; }
.result h3 { font-size: 1.08rem; font-weight: 500; margin: 0 0 .1rem; }
.result h3 a { color: var(--accent); }
.result .meta { margin: 0; font-size: .88rem; color: var(--muted); }
.result .meta strong { color: var(--text); }
.result .snippet { margin: .3rem 0 .2rem; font-size: .92rem; line-height: 1.5; }
.result .tags { margin: .2rem 0 0; }
.result.dup { opacity: .55; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: .9rem 1.1rem; margin-bottom: 1.4rem; }
.panel h2 { margin: 0; font-size: 1.25rem; }
.panel-stats { display: flex; flex-wrap: wrap; gap: .4rem 1.4rem; margin: .5rem 0; font-size: .92rem; }
.facets section { margin-bottom: 1.3rem; }
.facets h4 { margin: 0 0 .3rem; font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.facet { display: flex; justify-content: space-between; gap: .5rem; padding: .22rem .5rem; border-radius: 6px; color: var(--text); font-size: .9rem; }
.facet:hover { background: var(--surface); text-decoration: none; }

@media (max-width: 860px) {
  .serp { grid-template-columns: 1fr; }
  .facets { order: -1; display: flex; gap: .4rem; overflow-x: auto; }
  .facets section { margin: 0; flex: none; }
  .facets h4 { display: none; }
  .facets section { display: flex; gap: .3rem; }
  .facet { border: 1px solid var(--border); border-radius: 999px; background: var(--surface); white-space: nowrap; }
  .topbar .search-box { order: 3; flex-basis: 100%; max-width: none; }
  .home { margin-top: 3vh; }
  .home .logo { font-size: 2.3rem; }
}
.stats { display: flex; flex-wrap: wrap; gap: .5rem 1rem; align-items: center; justify-content: space-between; max-width: 720px; }
.correction { margin: .8rem 0 0; font-size: .95rem; }

/* --- Document list (feed + results) --- */
.home { max-width: 960px; margin: 3rem auto 0; text-align: left; }
.hero { text-align: center; max-width: 760px; margin: 0 auto; }
.hero .search-box { text-align: left; }
.chip.ask { border-color: var(--project); color: var(--project); }

.shortcuts { display: flex; gap: .45rem; overflow-x: auto; padding: .2rem 0 .8rem; margin: .4rem 0 .6rem; scrollbar-width: thin; }
.shortcut { flex: none; display: inline-flex; align-items: center; gap: .45rem; padding: .4rem .8rem .4rem .45rem; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border); color: var(--text); font-size: .9rem; }
.shortcut:hover { text-decoration: none; border-color: hsl(var(--h, 200) 40% 55%); }
.shortcut-icon { display: grid; place-items: center; width: 1.7rem; height: 1.7rem; border-radius: 50%; background: hsl(var(--h, 200) 55% 92%); font-size: .95rem; }
.shortcut.project .shortcut-icon { background: var(--project-soft); }
@media (prefers-color-scheme: dark) { .shortcut-icon { background: hsl(var(--h, 200) 25% 22%); } }

h3.month { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; flex-wrap: wrap;
  font-size: .95rem; margin: 1.6rem 0 .5rem; padding: 0 .2rem; }
h3.month a { color: var(--text); }
h3.month .muted { font-weight: 400; font-size: .85rem; }
.center { text-align: center; }

.doc-list { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; overflow: hidden; }
.doc-row { display: grid; grid-template-columns: 2.6rem minmax(0, 1fr) auto 8.5rem; gap: .9rem; align-items: center;
  padding: .75rem 1rem; color: var(--text); border-bottom: 1px solid var(--border); }
.doc-row:last-child { border-bottom: 0; }
.doc-row:hover { background: var(--bg); text-decoration: none; }
.doc-row.dup { opacity: .5; }
.doc-icon { display: grid; place-items: center; width: 2.6rem; height: 2.6rem; border-radius: 12px; font-size: 1.25rem;
  background: hsl(var(--h, 200) 55% 92%); }
.doc-icon.empty { background: transparent; border: 1px dashed var(--border); color: var(--muted); font-size: 1rem; }
@media (prefers-color-scheme: dark) { .doc-icon { background: hsl(var(--h, 200) 25% 22%); } }
.doc-main { display: flex; flex-direction: column; gap: .1rem; min-width: 0; }
.doc-title { font-weight: 600; font-size: .98rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-sub { color: var(--muted); font-size: .84rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-sub strong { color: var(--text); font-weight: 500; }
.doc-snippet { font-size: .86rem; color: var(--muted); margin-top: .15rem; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.doc-snippet mark { color: var(--text); }
.doc-tags { display: flex; gap: .3rem; flex-wrap: wrap; justify-content: flex-end; max-width: 300px; }
.tag { font-size: .75rem; padding: .12rem .5rem; border-radius: 999px; white-space: nowrap;
  background: hsl(var(--h, 200) 55% 93%); color: hsl(var(--h, 200) 45% 28%); }
.tag.project { background: var(--project-soft); color: var(--project); }
.tag.pending { background: transparent; border: 1px dashed var(--warn); color: var(--warn); }
@media (prefers-color-scheme: dark) { .tag { background: hsl(var(--h, 200) 25% 22%); color: hsl(var(--h, 200) 50% 80%); } }
.doc-amount { display: flex; flex-direction: column; align-items: flex-end; font-variant-numeric: tabular-nums; white-space: nowrap; }
.doc-amount strong { font-size: .98rem; }
.doc-amount .muted { font-size: .78rem; }

@media (max-width: 760px) {
  .home { margin-top: 1.2rem; }
  .doc-row { grid-template-columns: 2.4rem minmax(0, 1fr) auto; gap: .2rem .7rem; padding: .7rem .8rem; align-items: start; }
  .doc-icon { width: 2.4rem; height: 2.4rem; grid-row: span 2; }
  .doc-title { white-space: normal; }
  .doc-amount { grid-column: 3; grid-row: 1; }
  .doc-tags { grid-column: 2 / 4; justify-content: flex-start; max-width: none; }
  .doc-amount .muted { display: none; }
}

/* --- Ask Codex --- */
.ask { max-width: 820px; margin: 0 auto; }
.ask-form { display: flex; gap: .6rem; align-items: flex-end; margin: 1rem 0 .6rem; }
.ask-form textarea { flex: 1; font-size: 1.05rem; padding: .7rem .9rem; border-radius: 12px; resize: vertical; }
.ask .try { justify-content: flex-start; margin: .4rem 0 1.5rem; }
h1.question { font-size: 1.35rem; margin: .5rem 0 1rem; }
.answer { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--project); border-radius: 12px; padding: 1rem 1.2rem; line-height: 1.55; }
.answer p:first-child { margin-top: 0; } .answer p:last-child { margin-bottom: 0; }
.answer.pending { display: flex; gap: .7rem; align-items: center; color: var(--muted); }
.spinner { width: 18px; height: 18px; border: 2px solid var(--border); border-top-color: var(--project); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.answer-total { display: flex; flex-wrap: wrap; gap: .4rem 1rem; align-items: baseline; margin: 1.2rem 0 .6rem; }
.question-row { display: flex; justify-content: space-between; gap: 1rem; padding: .7rem 1rem; border-bottom: 1px solid var(--border); color: var(--text); }
.question-row:last-child { border-bottom: 0; }
.question-row:hover { background: var(--bg); text-decoration: none; }
@media (max-width: 640px) { .ask-form { flex-direction: column; align-items: stretch; } }
.tag.suggested { background: transparent; border: 1px dashed hsl(var(--h, 200) 40% 60%); }
.tag.project.suggested { border-color: var(--project); }
@media (min-width: 761px) { .shortcuts { flex-wrap: wrap; overflow: visible; } }
