:root {
  color-scheme: light dark;
  --bg: #f7f5f0;
  --surface: #ffffff;
  --text: #1c1a16;
  --muted: #6b6558;
  --border: #e4ddd0;
  --accent: #8a3324;
  --accent-contrast: #ffffff;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(30, 24, 12, 0.06), 0 8px 24px rgba(30, 24, 12, 0.05);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16140f;
    --surface: #1f1c16;
    --text: #f1ece0;
    --muted: #a89e8a;
    --border: #332e24;
    --accent: #e08a6d;
    --accent-contrast: #1c1a16;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 0.75rem; }

.mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.5rem;
  height: 2.5rem;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 1.25rem;
  font-weight: 700;
  flex-shrink: 0;
}

.brand h1 { font-size: 1.1rem; margin: 0; }
.brand p { font-size: 0.85rem; color: var(--muted); margin: 0.15rem 0 0; }

.ghlink {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.9rem;
  white-space: nowrap;
}
.ghlink:hover { text-decoration: underline; }

.stats {
  display: flex;
  gap: 2rem;
  padding: 1rem 1.5rem;
  flex-wrap: wrap;
}

.stat { display: flex; flex-direction: column; }
.statValue { font-size: 1.4rem; font-weight: 700; }
.statLabel { font-size: 0.8rem; color: var(--muted); }

.controls {
  display: flex;
  gap: 0.75rem;
  padding: 0 1.5rem 1rem;
  flex-wrap: wrap;
  align-items: center;
}

#searchInput, #sortSelect {
  font: inherit;
  padding: 0.6rem 0.9rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

#searchInput { flex: 1 1 320px; min-width: 200px; }
#sortSelect { flex: 0 0 auto; }

.resultCount { font-size: 0.85rem; color: var(--muted); margin-left: auto; }

main { padding: 0 1.5rem 2rem; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  text-decoration: none;
  color: inherit;
  transition: transform 0.12s ease, border-color 0.12s ease;
}

.card:hover, .card:focus-visible {
  transform: translateY(-2px);
  border-color: var(--accent);
}

.cardTitle { font-weight: 700; font-size: 1rem; }
.cardHanja { font-size: 0.85rem; color: var(--muted); }

.cardMeta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.3rem;
  font-size: 0.78rem;
}

.chip {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
  color: var(--muted);
}

.emptyState { text-align: center; color: var(--muted); padding: 3rem 1rem; }

.sentinel { height: 1px; }

footer {
  padding: 1.5rem;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.8rem;
}

/* Reader view */
.reader {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.backBtn {
  align-self: flex-start;
  color: var(--accent);
  text-decoration: none;
  font-size: 0.9rem;
}
.backBtn:hover { text-decoration: underline; }

.readerHeader {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.2rem 1.4rem;
}

.readerTitle { margin: 0 0 0.2rem; font-size: 1.3rem; }
.readerHanja { margin: 0 0 0.5rem; color: var(--muted); }

.partTabs {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.tab {
  font: inherit;
  padding: 0.4rem 0.85rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
}

.readerContent {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.4rem 1.6rem;
  overflow-wrap: break-word;
}

.readerContent h1 { font-size: 1.25rem; margin-top: 0; }
.readerContent h2 { font-size: 1.1rem; margin-top: 1.6rem; padding-top: 1.2rem; border-top: 1px solid var(--border); }
.readerContent h2:first-child { margin-top: 0; padding-top: 0; border-top: none; }
.readerContent h3 { font-size: 0.95rem; color: var(--accent); margin-bottom: 0.3rem; }
.readerContent p { margin: 0.3rem 0 0.8rem; }
.readerContent ul { margin: 0 0 0.8rem; padding-left: 1.3rem; }
.readerContent li { margin-bottom: 0.2rem; font-size: 0.9rem; }
.readerContent hr { border: none; border-top: 1px dashed var(--border); margin: 1.4rem 0; }
.readerContent code { background: var(--bg); padding: 0.1rem 0.35rem; border-radius: 4px; font-size: 0.85em; }
.readerContent a { color: var(--accent); }

.codeBlock {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.8rem 1rem;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.85rem;
}
.codeBlock code { background: none; padding: 0; }

.tableWrap { overflow-x: auto; margin-bottom: 0.8rem; }
.readerContent table { border-collapse: collapse; width: 100%; font-size: 0.85rem; }
.readerContent th, .readerContent td {
  border: 1px solid var(--border);
  padding: 0.4rem 0.6rem;
  text-align: left;
}
.readerContent th { background: var(--bg); }

.readerSource { font-size: 0.78rem; color: var(--muted); }
.readerSource a { color: var(--muted); }

.loading { color: var(--muted); padding: 1rem 0; }
