:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece5;
  --text: #1d1d1f;
  --muted: #6b6b70;
  --border: #e2ded5;
  --accent: #c2410c;
  --accent-soft: #fbe6d9;
  --accent-text: #9a3412;
  --danger: #b91c1c;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.05);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171a; --surface: #1f2024; --surface-2: #2a2b30; --text: #ececee; --muted: #9c9ca3;
    --border: #33343a; --accent: #fb923c; --accent-soft: #3a2618; --accent-text: #fdba74;
    --danger: #f87171; --shadow: 0 1px 2px rgba(0,0,0,.3); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16171a; --surface: #1f2024; --surface-2: #2a2b30; --text: #ececee; --muted: #9c9ca3;
  --border: #33343a; --accent: #fb923c; --accent-soft: #3a2618; --accent-text: #fdba74;
  --danger: #f87171; --shadow: 0 1px 2px rgba(0,0,0,.3); 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 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--accent-text); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; }
.muted { color: var(--muted); }
.hide-sm { }
@media (max-width: 560px) { .hide-sm { display: none; } }

/* header */
.top {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 16px;
  padding: 10px max(16px, env(safe-area-inset-left)); padding-top: max(10px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; color: var(--text); }
.brand:hover { text-decoration: none; }
.top nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.top nav a { padding: 6px 12px; border-radius: 999px; color: var(--muted); font-weight: 500; white-space: nowrap; }
.top nav a.active { background: var(--surface-2); color: var(--text); }
.top nav a:hover { text-decoration: none; color: var(--text); }
@media (max-width: 560px) { .brand span:last-child { display: none; } .top { gap: 8px; } }

main { max-width: 1100px; margin: 0 auto; padding: 20px 16px 80px; }

/* controls */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 10px; font: inherit; font-weight: 500; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .6; cursor: default; }
input, textarea, select {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px; width: 100%;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }

.search { position: relative; margin-bottom: 14px; }
.search input { font-size: 18px; padding: 14px 16px 14px 44px; border-radius: var(--radius); box-shadow: var(--shadow); }
.search::before { content: "⌕"; position: absolute; left: 16px; top: 50%; transform: translateY(-52%); font-size: 22px; color: var(--muted); }

.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.chip {
  border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px;
  padding: 4px 12px; font-size: 14px; cursor: pointer; white-space: nowrap;
}
.chip:hover { background: var(--surface-2); text-decoration: none; }
.chip.on { background: var(--accent-soft); border-color: transparent; color: var(--accent-text); font-weight: 600; }
.tag { font-size: 13px; color: var(--accent-text); }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 18px; }
.filters select { width: auto; max-width: 100%; padding: 6px 10px; font-size: 14px; }
.count { color: var(--muted); font-size: 14px; margin: 0 0 10px; }

/* cards */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
@media (max-width: 400px) { .grid { grid-template-columns: 1fr; } }
.card {
  display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); color: var(--text);
  transition: transform .12s ease;
}
.card:hover { text-decoration: none; transform: translateY(-2px); }
.thumb { aspect-ratio: 16/9; background: var(--surface-2); display: grid; place-items: center; font-size: 40px; overflow: hidden; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.card .body { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.card h3 { margin: 0; font-size: 16px; line-height: 1.35; }
.meta { font-size: 13px; color: var(--muted); }
.clamp { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; font-size: 14px; }
.snippet { font-size: 13px; color: var(--muted); font-style: italic; }
.snippet mark { background: var(--accent-soft); color: var(--accent-text); font-style: normal; border-radius: 3px; padding: 0 2px; }
.badge { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.badge.warn { background: var(--accent-soft); color: var(--accent-text); }

.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 28px 0 10px; font-weight: 600; }

/* item / topic pages */
.page { max-width: 760px; margin: 0 auto; }
.page h1 { font-size: 28px; line-height: 1.2; margin: 8px 0 6px; }
.page .hero { border-radius: var(--radius); overflow: hidden; margin: 16px 0; background: var(--surface-2); }
.page .hero img { width: 100%; display: block; max-height: 420px; object-fit: cover; }
.page video { width: 100%; border-radius: var(--radius); margin: 16px 0; background: #000; max-height: 70vh; }
.lead { font-size: 18px; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; margin: 16px 0; }
.panel h2 { font-size: 15px; margin: 0 0 8px; }
.points { margin: 0; padding-left: 20px; }
.md h1, .md h2, .md h3 { line-height: 1.3; margin: 1.4em 0 .5em; }
.md h3 { font-size: 18px; }
.md p, .md ul, .md ol { margin: .6em 0; }
.md code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: .92em; }
.md pre { background: var(--surface-2); padding: 12px; border-radius: 10px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md blockquote { margin: .6em 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
.md table { border-collapse: collapse; display: block; overflow-x: auto; }
.md td, .md th { border: 1px solid var(--border); padding: 4px 8px; }
.md a.ref { background: var(--accent-soft); padding: 0 5px; border-radius: 5px; font-size: .88em; font-weight: 600; }
details summary { cursor: pointer; color: var(--muted); }
details pre { white-space: pre-wrap; font: 14px/1.5 inherit; color: var(--text); }
.edit-grid { display: grid; gap: 10px; }
.topbar { display: flex; justify-content: space-between; gap: 8px; align-items: center; flex-wrap: wrap; }

.topic-group { margin-bottom: 18px; }
.topic-group h2 { font-size: 15px; margin: 0 0 8px; color: var(--muted); }
.topic-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; }
.topic-link { display: block; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; color: var(--text); }
.topic-link:hover { text-decoration: none; border-color: var(--accent); }
.topic-link small { display: block; color: var(--muted); font-size: 13px; }

.mini-list { display: flex; flex-direction: column; gap: 6px; }
.mini { display: flex; gap: 10px; align-items: baseline; }
.mini .id { font-size: 12px; color: var(--muted); min-width: 34px; }

.empty { text-align: center; padding: 60px 16px; color: var(--muted); }
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -3px; }
@keyframes spin { to { transform: rotate(360deg); } }

dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 20px; width: min(520px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog h2 { margin: 0 0 14px; font-size: 20px; }
dialog label { display: block; margin-bottom: 12px; font-weight: 500; font-size: 14px; }
dialog label input, dialog label textarea { margin-top: 4px; font-weight: 400; }
.status { min-height: 1.4em; font-size: 14px; color: var(--muted); }

.login { max-width: 360px; margin: 12vh auto; text-align: center; }
.login form { display: grid; gap: 10px; margin-top: 16px; }

body.logged-out .top nav, body.logged-out #addBtn { visibility: hidden; }
