:root {
  --bg: #f7f8fa;
  --panel: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #2563eb;
  --accent-soft: #eef2ff;
  --danger: #b91c1c;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f19;
    --panel: #121826;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --border: #1f2937;
    --accent: #60a5fa;
    --accent-soft: #172033;
    --danger: #f87171;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 1.5rem; margin: 24px 0 16px; font-weight: 650; letter-spacing: -0.01em; }
h2 { font-size: 1.1rem; margin: 0 0 12px; font-weight: 600; }
h3 { font-size: 0.95rem; margin: 18px 0 8px; color: var(--muted); font-weight: 600; }

.nav {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 18px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.nav .brand { font-weight: 650; color: var(--text); letter-spacing: -0.01em; }
.nav .links { display: flex; gap: 14px; flex: 1; flex-wrap: wrap; }
.nav .links a { color: var(--muted); font-size: 0.92rem; }
.nav .links a.is-active { color: var(--text); font-weight: 600; }
.nav .logout { margin-left: auto; }

.wrap { max-width: 900px; margin: 0 auto; padding: 0 18px 60px; }
.wide { max-width: 1000px; margin: 0 auto; padding: 0 18px 60px; }
.reading { max-width: 780px; margin: 0 auto; padding: 0 18px 80px; }

.chat-wrap { max-width: 820px; margin: 0 auto; padding: 0 16px 140px; }
.chat-shell { display: flex; flex-direction: column; min-height: calc(100dvh - 60px); }

.thread { display: flex; flex-direction: column; gap: 14px; padding: 22px 0; flex: 1; }

.msg { display: flex; }
.msg-user { justify-content: flex-end; }
.msg-assistant { justify-content: flex-start; }

.bubble {
  max-width: 88%;
  padding: 12px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--panel);
  overflow-wrap: anywhere;
}

.msg-user .bubble {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg-assistant .bubble { border-bottom-left-radius: 4px; }
.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 0 0 8px; padding-left: 22px; }
.bubble code {
  background: var(--accent-soft);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.9em;
}
.bubble pre {
  background: var(--accent-soft);
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
}
.bubble a { color: inherit; text-decoration: underline; }

.blocks { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }

.block {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  background: color-mix(in srgb, var(--panel) 70%, var(--accent-soft));
}

.block-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.block-kind {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 7px;
}
.block-title { font-weight: 600; }
.block-meta { color: var(--muted); font-size: 0.86rem; margin-top: 2px; }
.block-actions { margin-top: 6px; display: flex; gap: 12px; }

.composer {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: min(820px, 100%);
  display: flex;
  gap: 8px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}

.composer textarea {
  flex: 1;
  resize: none;
  min-height: 46px;
  max-height: 180px;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font: inherit;
}

.composer textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 40%, transparent); }

.typing {
  position: fixed;
  bottom: 82px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.82rem;
  color: var(--muted);
}

.htmx-indicator { opacity: 0; transition: opacity 120ms ease; }
.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator { opacity: 1; }

.empty { text-align: center; color: var(--muted); padding: 60px 20px; }

.notifications { display: flex; flex-direction: column; gap: 8px; padding-top: 18px; }
.notice {
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  padding: 10px 14px;
  background: var(--panel);
  font-size: 0.9rem;
}
.notice p { margin: 0 0 6px; }

button, .button {
  font: inherit;
  cursor: pointer;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 9px 14px;
}

button:hover { border-color: color-mix(in srgb, var(--accent) 40%, var(--border)); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.ghost { background: transparent; padding: 6px 10px; font-size: 0.86rem; color: var(--muted); }
button.link {
  background: none;
  border: none;
  padding: 0;
  color: var(--accent);
  font-size: 0.86rem;
}
button.link:hover { text-decoration: underline; }

input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  width: 100%;
}

label { display: block; margin-bottom: 12px; font-size: 0.9rem; color: var(--muted); }
label input, label select { margin-top: 4px; color: var(--text); }

.inline-form { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0 18px; }
.inline-form input, .inline-form select { flex: 1 1 140px; width: auto; }
.grid-form {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 8px;
  margin: 12px 0 0;
}
.grid-form button { grid-column: 1 / -1; justify-self: start; }

.auth-wrap { display: flex; align-items: center; justify-content: center; min-height: 100dvh; padding: 20px; }
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
}
.auth-card h1 { margin-top: 0; }
.auth-card button { width: 100%; margin-top: 6px; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; }
.card {
  display: block;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  background: var(--panel);
  color: var(--text);
}
.card:hover { border-color: var(--accent); text-decoration: none; }
.card-title { font-weight: 600; }
.card-meta { color: var(--muted); font-size: 0.82rem; margin-top: 4px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 20px;
  margin-bottom: 16px;
}

.row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.row:last-of-type { border-bottom: none; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 550; overflow-wrap: anywhere; }
.row-meta { color: var(--muted); font-size: 0.82rem; }

.table { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 0.9rem; }
.table th, .table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.table th { color: var(--muted); font-weight: 500; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; }

.stats { display: flex; gap: 20px; flex-wrap: wrap; margin: 8px 0 20px; }
.stat { display: flex; flex-direction: column; }
.stat-value { font-size: 1.25rem; font-weight: 650; }
.stat .muted { font-size: 0.78rem; }

.tabs { display: flex; gap: 14px; margin: 8px 0 16px; font-size: 0.9rem; }
.tabs a { color: var(--muted); }
.tabs a.is-active { color: var(--text); font-weight: 600; }

.note-body { font-size: 1.02rem; }
.note-body h1 { font-size: 1.6rem; margin-top: 8px; }
.note-body h2 { font-size: 1.25rem; margin-top: 28px; }
.note-body h3 { font-size: 1.05rem; margin-top: 22px; color: var(--text); }
.note-body p { margin: 0 0 14px; }
.note-body ul, .note-body ol { margin: 0 0 14px; padding-left: 24px; }
.note-body blockquote {
  margin: 0 0 14px;
  padding: 8px 16px;
  border-left: 3px solid var(--border);
  color: var(--muted);
}
.note-body code { background: var(--accent-soft); padding: 1px 5px; border-radius: 4px; font-size: 0.9em; }
.note-body pre { background: var(--accent-soft); padding: 14px; border-radius: 8px; overflow-x: auto; }
.note-body table { width: 100%; border-collapse: collapse; margin-bottom: 14px; }
.note-body th, .note-body td { border: 1px solid var(--border); padding: 6px 10px; }
.note-body img { max-width: 100%; height: auto; border-radius: 8px; }

.toc {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
  margin: 0 0 22px;
  background: var(--panel);
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.88rem;
}
.toc-title { color: var(--muted); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em; }
.toc-level-3 { padding-left: 14px; color: var(--muted); }

.back { display: inline-block; margin: 16px 0 0; font-size: 0.88rem; }
.note-meta { margin-top: 30px; padding-top: 14px; border-top: 1px solid var(--border); }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { color: var(--danger); font-size: 0.9rem; margin-bottom: 10px; }
.ok { color: #15803d; font-size: 0.9rem; }
.secret {
  display: block;
  background: var(--accent-soft);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 0.9rem;
  overflow-wrap: anywhere;
  margin-bottom: 8px;
}

@media (max-width: 640px) {
  .nav { gap: 10px; padding: 8px 12px; }
  .nav .links { gap: 10px; order: 3; flex-basis: 100%; overflow-x: auto; }
  .bubble { max-width: 94%; }
  h1 { font-size: 1.3rem; }
  .wide, .wrap { padding: 0 12px 60px; }
}
