:root {
  color-scheme: light dark;
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-sunken: #f1f1ee;
  --text: #1d1d1b;
  --text-secondary: #5f5e5a;
  --text-muted: #8a8984;
  --border: #e3e2dd;
  --border-strong: #cfcec8;
  --accent: #d92d2d;
  --accent-hover: #c02525;
  --on-accent: #ffffff;
  --brand: #ff4040;
  --ok-bg: #eaf3de;
  --ok-text: #27500a;
  --warn-bg: #faeeda;
  --warn-text: #633806;
  --err-bg: #fcebeb;
  --err-text: #791f1f;
  --add-bg: #e6f4e3;
  --add-text: #1f4d17;
  --del-bg: #fbe7e7;
  --del-text: #7a1f1f;
  --radius: 8px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --surface: #1f1f1d;
    --surface-sunken: #191918;
    --text: #ecebe6;
    --text-secondary: #b0afa8;
    --text-muted: #85847e;
    --border: #33322f;
    --border-strong: #46453f;
    --accent: #e5484d;
    --accent-hover: #f0595e;
    --ok-bg: #1f2e14;
    --ok-text: #c0dd97;
    --warn-bg: #3a2a0e;
    --warn-text: #fac775;
    --err-bg: #3b1717;
    --err-text: #f7c1c1;
    --add-bg: #1c2f1a;
    --add-text: #b6e0a8;
    --del-bg: #3a1a1a;
    --del-text: #f4b8b8;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 400 15px/1.6 var(--font);
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; }
h1, h2, h3 { font-weight: 500; line-height: 1.3; margin: 0; }
h1 { font-size: 20px; }
h2 { font-size: 16px; }
p { margin: 0; }
code { font-family: var(--mono); font-size: 0.92em; }

.muted { color: var(--text-secondary); }
.small { font-size: 13px; }
.hint { font-size: 13px; color: var(--text-muted); }

/* --- layout --- */
.layout { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh; }

.sidebar {
  border-right: 1px solid var(--border);
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 8px 16px;
  text-decoration: none;
  font-weight: 500;
}

.brand img { width: 28px; height: 28px; border-radius: 7px; }

.nav a {
  display: block;
  padding: 7px 10px;
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text-secondary);
}

.nav a:hover { background: var(--surface-sunken); color: var(--text); }
.nav a[aria-current="page"] { background: var(--surface); color: var(--text); font-weight: 500; box-shadow: inset 0 0 0 1px var(--border); }
.nav .group { font-size: 12px; color: var(--text-muted); padding: 14px 10px 4px; }

.sidebar-foot { margin-top: auto; padding: 8px 10px; font-size: 13px; color: var(--text-muted); }
.sidebar-foot form { margin-top: 6px; }

.topbar { display: none; }

main { padding: 28px 32px 64px; max-width: 940px; width: 100%; }

.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.page-head .actions { display: flex; gap: 8px; flex-shrink: 0; }
.crumbs { font-size: 13px; color: var(--text-muted); margin-bottom: 4px; }
.crumbs a { color: var(--text-secondary); text-decoration: none; }
.crumbs a:hover { text-decoration: underline; }

/* --- controls --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 36px;
  padding: 0 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font: 500 14px var(--font);
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover { background: var(--surface-sunken); }
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-secondary); padding: 0 8px; height: 30px; font-weight: 400; }
.btn-ghost:hover { color: var(--text); }
.btn-block { width: 100%; height: 42px; }

input[type="text"], select, textarea {
  width: 100%;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: 400 15px var(--font);
  padding: 0 12px;
}

input[type="text"], select { height: 38px; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, a:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--accent) 55%, transparent);
  outline-offset: 1px;
}

textarea.editor {
  display: block;
  min-height: 420px;
  padding: 14px 16px;
  font: 400 14px/1.65 var(--mono);
  resize: vertical;
  tab-size: 2;
}

label.field { display: block; font-size: 13px; color: var(--text-secondary); margin: 14px 0 6px; }

/* --- cards, lists --- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px 20px; }

.doc-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin-bottom: 28px; }
.doc-card { display: block; text-decoration: none; }
.doc-card:hover { border-color: var(--border-strong); }
.doc-card h2 { margin-bottom: 4px; }

.rows { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 11px 16px; border-top: 1px solid var(--border); }
.row:first-child { border-top: 0; }
.row-main { min-width: 0; }
.row-main .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-actions { display: flex; gap: 4px; flex-shrink: 0; }
a.row { text-decoration: none; }
a.row:hover { background: var(--surface-sunken); }

.section-title { margin: 28px 0 10px; }

.tag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--surface-sunken); color: var(--text-secondary); border: 1px solid var(--border); }
.tag-user { background: var(--ok-bg); color: var(--ok-text); border-color: transparent; }
.tag-paused { background: var(--warn-bg); color: var(--warn-text); border-color: transparent; }

/* --- notices --- */
.notice { border-radius: var(--radius); padding: 10px 14px; margin-bottom: 16px; font-size: 14px; }
.notice-ok { background: var(--ok-bg); color: var(--ok-text); }
.notice-warn { background: var(--warn-bg); color: var(--warn-text); }
.notice-err { background: var(--err-bg); color: var(--err-text); }

/* --- editor --- */
.editor-tabs { display: flex; gap: 4px; margin-bottom: 8px; }
.editor-tabs button { height: 30px; padding: 0 12px; border-radius: var(--radius); border: 1px solid transparent; background: transparent; color: var(--text-secondary); font: 400 14px var(--font); cursor: pointer; }
.editor-tabs button[aria-selected="true"] { background: var(--surface); border-color: var(--border); color: var(--text); font-weight: 500; }

.preview { min-height: 420px; padding: 6px 20px 16px; }
.preview[hidden] { display: none; }
.preview h1, .preview h2, .preview h3 { margin: 18px 0 8px; }
.preview h1 { font-size: 19px; }
.preview h2 { font-size: 17px; }
.preview p, .preview ul, .preview ol, .preview blockquote, .preview pre, .preview table { margin: 0 0 10px; }
.preview ul, .preview ol { padding-left: 22px; }
.preview blockquote { border-left: 3px solid var(--border-strong); padding-left: 12px; color: var(--text-secondary); }
.preview pre { background: var(--surface-sunken); padding: 10px 12px; border-radius: var(--radius); overflow-x: auto; }
.preview table { border-collapse: collapse; }
.preview th, .preview td { border: 1px solid var(--border); padding: 4px 8px; }

.save-bar { display: flex; gap: 8px; align-items: flex-end; margin-top: 12px; }
.save-bar .grow { flex: 1; }
.save-bar label.field { margin-top: 0; }

/* --- diff --- */
.diff { font: 400 13px/1.6 var(--mono); background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.diff div { padding: 1px 14px 1px 30px; white-space: pre-wrap; word-break: break-word; position: relative; min-height: 1.6em; }
.diff div::before { position: absolute; left: 12px; color: var(--text-muted); }
.diff .add { background: var(--add-bg); color: var(--add-text); }
.diff .add::before { content: "+"; color: inherit; }
.diff .del { background: var(--del-bg); color: var(--del-text); }
.diff .del::before { content: "−"; color: inherit; }
.diff .fold { color: var(--text-muted); font-family: var(--font); font-size: 12px; background: var(--surface-sunken); padding-top: 3px; padding-bottom: 3px; }

/* --- rubrics --- */
.rubric { padding: 16px 18px; border-top: 1px solid var(--border); }
.rubric:first-child { border-top: 0; }
.rubric-head { display: grid; grid-template-columns: 72px minmax(0, 1fr) auto; gap: 10px; align-items: end; }
.rubric-head label.field { margin-top: 0; }
.rubric-flags { display: flex; flex-wrap: wrap; gap: 8px 20px; align-items: center; margin-top: 12px; font-size: 14px; }
.rubric-flags label { display: inline-flex; gap: 6px; align-items: center; cursor: pointer; }
.rubric-flags select { width: auto; height: 32px; font-size: 14px; }
.rubric-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; }
.rubric.saved { background: color-mix(in srgb, var(--ok-bg) 55%, transparent); }

/* --- pager --- */
.pager { display: flex; gap: 8px; justify-content: center; margin-top: 16px; align-items: center; }

/* --- login --- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; text-align: center; padding: 28px 24px; }
.login-card .logo { width: 52px; height: 52px; border-radius: 12px; margin: 0 auto 14px; display: block; }
.login-card h1 { margin-bottom: 4px; }
.login-card .lead { color: var(--text-secondary); margin-bottom: 22px; }
.login-code { font: 500 17px var(--mono); margin-top: 4px; user-select: all; }
.login-status { margin-top: 18px; font-size: 13px; color: var(--text-muted); }
.login-status.err { color: var(--err-text); }

.spinner { display: inline-block; width: 10px; height: 10px; border: 2px solid var(--border-strong); border-top-color: var(--text-secondary); border-radius: 50%; animation: spin 0.8s linear infinite; vertical-align: -1px; margin-right: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* --- phones --- */
@media (max-width: 767px) {
  .layout { grid-template-columns: minmax(0, 1fr); align-content: start; }
  .topbar { display: flex; align-items: center; justify-content: space-between; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--bg); position: sticky; top: 0; z-index: 2; }
  .topbar .brand { padding: 0; }
  .sidebar { display: none; position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--border); }
  .layout.menu-open .sidebar { display: flex; }
  .sidebar > .brand { display: none; }
  main { padding: 20px 16px 48px; }
  .page-head { flex-direction: column; }
  .save-bar { flex-direction: column; align-items: stretch; }
  .rubric-head { grid-template-columns: 64px minmax(0, 1fr); }
  .rubric-head .row-actions { grid-column: 1 / -1; }
  textarea.editor { min-height: 320px; }
}
.btn-flush { padding: 0; }
.login-card .hint { margin-top: 18px; }
.below { margin-top: 10px; }
textarea.editor[hidden] { display: none; }

/* --- document view --- */
.doc-view { padding: 6px 24px 18px; }
.doc-view h1, .doc-view h2, .doc-view h3 { margin: 18px 0 8px; }
.doc-view h1 { font-size: 19px; }
.doc-view h2 { font-size: 17px; }
.doc-view p, .doc-view ul, .doc-view ol, .doc-view blockquote, .doc-view pre, .doc-view table { margin: 0 0 10px; }
.doc-view ul, .doc-view ol { padding-left: 22px; }
.doc-view blockquote { border-left: 3px solid var(--border-strong); padding-left: 12px; color: var(--text-secondary); }
.doc-view pre { background: var(--surface-sunken); padding: 10px 12px; border-radius: var(--radius); overflow-x: auto; }
.doc-view table { border-collapse: collapse; }
.doc-view th, .doc-view td { border: 1px solid var(--border); padding: 4px 8px; }
.issues { margin: 6px 0 8px; padding-left: 20px; }
.notice .btn { margin-top: 10px; }
.btn[disabled] { opacity: 0.45; cursor: default; transform: none; }
.btn-primary[disabled]:hover { background: var(--accent); border-color: var(--accent); }

/* --- spending --- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 12px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 18px; }
.stat-label { font-size: 13px; color: var(--text-secondary); }
.stat-value { font-size: 26px; font-weight: 500; margin: 2px 0; font-variant-numeric: tabular-nums; }
.limit { margin-bottom: 4px; }
.limit-head { display: flex; justify-content: space-between; gap: 12px; font-size: 14px; margin-bottom: 8px; }
.bar { height: 6px; background: var(--surface-sunken); border-radius: 3px; overflow: hidden; margin-bottom: 8px; }
.bar-fill { height: 100%; background: var(--text-secondary); }
.amount { font-weight: 500; font-variant-numeric: tabular-nums; }
.w-0 { width: 0%; }
.w-5 { width: 5%; }
.w-10 { width: 10%; }
.w-15 { width: 15%; }
.w-20 { width: 20%; }
.w-25 { width: 25%; }
.w-30 { width: 30%; }
.w-35 { width: 35%; }
.w-40 { width: 40%; }
.w-45 { width: 45%; }
.w-50 { width: 50%; }
.w-55 { width: 55%; }
.w-60 { width: 60%; }
.w-65 { width: 65%; }
.w-70 { width: 70%; }
.w-75 { width: 75%; }
.w-80 { width: 80%; }
.w-85 { width: 85%; }
.w-90 { width: 90%; }
.w-95 { width: 95%; }
.w-100 { width: 100%; }

/* --- rubric cards --- */
.rubric-list { display: flex; flex-direction: column; gap: 12px; }
.rubric-card { padding: 16px 18px; scroll-margin-top: 16px; }
.rubric-card h2 { font-size: 16px; }
.rubric-card.is-editing { border-color: var(--border-strong); box-shadow: inset 0 0 0 1px var(--border-strong); }
.rubric-card.is-flashed { border-color: var(--ok-text); }
.rubric-card .notice { margin: 12px 0 0; }
.rubric-card form + form, .rubric-card .draft-idea { margin-top: 12px; }
.rubric-head { display: grid; grid-template-columns: 40px minmax(0, 1fr) auto; gap: 12px; align-items: center; }
.rubric-icon { width: 40px; height: 40px; border-radius: 10px; background: var(--surface-sunken); display: flex; align-items: center; justify-content: center; font-size: 21px; }
.rubric-title h2 { overflow-wrap: anywhere; }
.rubric-tools { display: flex; align-items: center; gap: 4px; }
.rubric-tools form { display: flex; }
.rubric-tools .btn-ghost { width: 32px; padding: 0; font-size: 16px; }
.rubric-desc { margin-top: 12px; font-size: 15px; position: relative; }
.rubric-desc > :first-child { margin-top: 0; }
.rubric-desc h1, .rubric-desc h2, .rubric-desc h3 { font-size: 15px; margin: 12px 0 6px; }
.rubric-desc p, .rubric-desc ul, .rubric-desc ol { margin: 0 0 8px; }
.rubric-desc ul, .rubric-desc ol { padding-left: 22px; }
.rubric-desc.is-collapsed { max-height: 9.6em; overflow: hidden; }
.rubric-desc.is-collapsed::after { content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 3em; background: linear-gradient(to bottom, transparent, var(--surface)); }
.rubric-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; min-height: 24px; }
.rubric-foot a.hint { text-decoration: none; margin-left: auto; }
.rubric-foot a.hint:hover { text-decoration: underline; }
.rubric-edit-head { display: grid; grid-template-columns: 64px minmax(0, 1fr); gap: 12px; align-items: end; margin-top: 12px; }
.field-label { display: block; font-size: 13px; color: var(--text-secondary); margin: 14px 0 6px; }
.rubric-edit-head .field-label { margin-top: 0; }
.icon-button { width: 56px; height: 38px; border-radius: var(--radius); border: 1px solid var(--border-strong); background: var(--surface); font-size: 21px; cursor: pointer; }
.icon-button:hover { background: var(--surface-sunken); }
textarea.editor-compact { min-height: 200px; }
.card-actions { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 14px; }
.btn-danger { color: var(--err-text); border-color: color-mix(in srgb, var(--err-text) 35%, transparent); }
.btn-danger:hover { background: var(--err-bg); }
.inline-form { display: flex; gap: 8px; }
.inline-form input { flex: 1; min-width: 0; }

/* --- icon picker --- */
.icon-dialog { border: 1px solid var(--border); border-radius: 12px; padding: 14px; background: var(--surface); color: var(--text); width: min(420px, calc(100vw - 32px)); }
.icon-dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
.icon-grid { display: grid; grid-template-columns: repeat(10, 1fr); gap: 2px; margin-bottom: 12px; }
.icon-grid button { aspect-ratio: 1; border: 0; border-radius: 8px; background: transparent; font-size: 22px; cursor: pointer; padding: 0; }
.icon-grid button:hover, .icon-grid button[aria-pressed="true"] { background: var(--surface-sunken); }

@media (max-width: 767px) {
  .rubric-head { grid-template-columns: 40px minmax(0, 1fr); }
  .rubric-tools { grid-column: 1 / -1; justify-content: flex-end; }
  .icon-dialog { margin: auto 0 0; width: 100vw; max-width: 100vw; border-radius: 16px 16px 0 0; }
  .icon-grid { grid-template-columns: repeat(8, 1fr); }
  .card-actions { flex-wrap: wrap; }
}
.version-note { font-size: 13px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* --- texts --- */
.filters { display: grid; grid-template-columns: minmax(0, 1fr) 200px 170px auto auto; gap: 8px; margin-bottom: 14px; }
.filters .btn { height: 38px; }
.list-count { margin-bottom: 6px; }
.post-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; margin-top: 2px; }
.tag-new { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); border-color: transparent; }
.tag-ok { background: var(--ok-bg); color: var(--ok-text); border-color: transparent; }
.tag-err { background: var(--err-bg); color: var(--err-text); border-color: transparent; }
.tag-muted { color: var(--text-muted); }
.grow-min { min-width: 0; }
.grow { flex: 1; }
.post-title { overflow-wrap: anywhere; }
.label-above { margin-bottom: 6px; }
.post-text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15px; line-height: 1.6; }
.post-text a { color: var(--accent); }
.fold-block { margin-top: 10px; }
.fold-block summary, .stack summary { cursor: pointer; color: var(--text-secondary); font-size: 14px; }
.fold-block summary { margin-bottom: 8px; }
.fold-block .post-text { color: var(--text-secondary); }
.post-stats { margin-top: 16px; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
.post-stats .stat-value { font-size: 22px; }
.stat-unit { font-size: 14px; color: var(--text-muted); font-weight: 400; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.stack .field-label { margin-top: 0; margin-bottom: 2px; }
.pre { white-space: pre-wrap; overflow-wrap: anywhere; }
.research { margin-top: 8px; font-size: 14px; color: var(--text-secondary); max-height: 480px; overflow-y: auto; }
.row-top { align-items: flex-start; justify-content: flex-start; }
.row a { color: inherit; text-decoration-color: var(--border-strong); }
.row a:hover { text-decoration-color: currentColor; }
.fact-icon { flex-shrink: 0; width: 22px; height: 22px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 600; margin-top: 1px; }
.fact-icon.ok { background: var(--ok-bg); color: var(--ok-text); }
.fact-icon.warn { background: var(--warn-bg); color: var(--warn-text); }
.warn-text { color: var(--warn-text); }
.quote { margin-top: 4px; font-size: 13px; color: var(--text-secondary); border-left: 2px solid var(--border-strong); padding-left: 10px; overflow-wrap: anywhere; }

@media (max-width: 767px) {
  .filters { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .filters input[type="text"] { grid-column: 1 / -1; }
}
