:root {
  color-scheme: light dark;

  --mobile-topbar-h: 52px;
  --bottom-nav-h: 62px;
  --bg: #f7f6f2;
  --surface: #ffffff;
  --surface-2: #f0eee8;
  --sidebar: #fbfaf7;
  --border: #ded9cd;
  --ink: #171717;
  --ink-soft: #4f4b45;
  --muted: #807a70;
  --accent: #6f5524;
  --accent-bg: #efe5cd;
  --green: #276c45;
  --red: #a13e31;
  --shadow: 0 1px 2px rgba(20, 18, 14, 0.08);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #080808;
    --surface-2: #111111;
    --sidebar: #050505;
    --border: #242424;
    --ink: #f2f2f2;
    --ink-soft: #c7c7c7;
    --muted: #868686;
    --accent: #d2b06a;
    --accent-bg: #241f14;
    --green: #78c696;
    --red: #e58576;
    --shadow: none;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.55;
  text-rendering: optimizeLegibility;
}

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

a:hover {
  text-decoration: underline;
}

a:focus-visible,
button:focus-visible,
.card:focus-visible,
input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button,
input {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 22px 18px;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.brand {
  display: flex;
  align-items: center;
  color: var(--ink);
}

.brand:hover {
  text-decoration: none;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.15;
}

.brand-text strong {
  font-family: "Cinzel", Georgia, serif;
  font-size: 15px;
  letter-spacing: .04em;
}

.brand-text small,
.sidebar-note,
.muted {
  color: var(--muted);
}

.brand-text small {
  font-size: 12px;
}

.sidebar-label {
  margin-top: 4px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.main-nav {
  display: grid;
  gap: 4px;
}

.main-nav a {
  color: var(--ink-soft);
  border-radius: 9px;
  padding: 9px 10px;
  font-size: 14px;
  font-weight: 600;
}

.main-nav a:hover {
  background: var(--surface-2);
  color: var(--ink);
  text-decoration: none;
}

.main-nav a.active {
  background: var(--accent-bg);
  color: var(--accent);
}

.sidebar-folder {
  border-top: 1px solid var(--border);
  padding-top: 12px;
}

.sidebar-folder summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .08em;
  list-style: none;
  text-transform: uppercase;
}

.sidebar-folder summary::-webkit-details-marker {
  display: none;
}

.sidebar-folder summary::after {
  content: "+";
  float: right;
  color: var(--muted);
}

.sidebar-folder[open] summary::after {
  content: "-";
}

.sidebar-filters {
  display: grid;
  gap: 6px;
  margin-top: 10px;
}

.sidebar-filters .chip {
  width: 100%;
  border-radius: 9px;
  padding: 8px 10px;
  text-align: left;
}

.sidebar-numbers {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
  margin-top: 10px;
}

.sidebar-numbers a {
  min-width: 0;
  padding: 7px 0;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--ink-soft);
  font-size: 12px;
  font-weight: 800;
  line-height: 1;
  text-align: center;
}

.sidebar-numbers a:hover {
  border-color: var(--accent);
  color: var(--ink);
  text-decoration: none;
}

.sidebar-numbers a.active {
  border-color: var(--accent-bg);
  background: var(--accent-bg);
  color: var(--accent);
}

.sidebar-note {
  margin: 4px 0 0;
  border-top: 1px solid var(--border);
  padding-top: 16px;
  font-size: 13px;
}

.mobile-topbar,
.bottom-nav,
.sidebar-backdrop,
.mobile-filters {
  display: none;
}

.nav-toggle {
  display: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.nav-toggle-icon {
  display: block;
  width: 18px;
  height: 2px;
  margin: 0 auto;
  background: currentColor;
  border-radius: 1px;
  box-shadow: 0 -6px 0 currentColor, 0 6px 0 currentColor;
}

.content-shell {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.content {
  width: 100%;
  max-width: 1060px;
  margin: 0 auto;
  padding: 40px 36px 72px;
  flex: 1;
}

.hero {
  max-width: 760px;
  margin-bottom: 30px;
}

.hero h1 {
  margin: 0 0 10px;
  font-family: "Cinzel", Georgia, serif;
  font-size: clamp(32px, 5vw, 54px);
  line-height: 1.06;
  letter-spacing: -0.03em;
}

.hero h1 .accent {
  color: var(--accent);
}

.hero p {
  margin: 0;
  color: var(--ink-soft);
  font-size: 17px;
  max-width: 700px;
}

.toolbar {
  margin: 24px 0 16px;
}

.search {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 11px 13px;
  box-shadow: var(--shadow);
}

.search .icon {
  color: var(--muted);
}

.search input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
}

.search input::placeholder {
  color: var(--muted);
}

.filters,
.scope-row,
.tagrow,
.study-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.scope-row,
.study-controls {
  justify-content: center;
}

.chip,
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink-soft);
  border-radius: 999px;
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease, color .12s ease;
}

.chip {
  padding: 6px 11px;
  font-size: 13px;
}

.chip:hover,
.btn:hover {
  background: var(--surface-2);
  color: var(--ink);
}

.chip.active,
.btn.primary {
  background: var(--accent-bg);
  border-color: var(--accent-bg);
  color: var(--accent);
  font-weight: 700;
}

.count {
  margin: 18px 0 12px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}

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

.card,
.letter-card,
.detail,
.quiz-card,
.flash-face {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card {
  min-height: 126px;
  padding: 15px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease;
}

.card:hover {
  background: var(--surface-2);
  border-color: var(--accent);
}

.card .num,
.detail-num,
.letter-card .lc-val,
.flash-front .big,
.quiz-result .score {
  font-family: "Cinzel", Georgia, serif;
  color: var(--accent);
}

.card .num {
  font-size: 30px;
  line-height: 1;
}

.card .mean {
  color: var(--ink);
  font-size: 14px;
  font-weight: 700;
  line-height: 1.35;
}

.card .ltr {
  margin-top: auto;
  color: var(--muted);
  font-size: 12px;
}

.detail,
.quiz-card {
  padding: clamp(22px, 4vw, 34px);
}

.back {
  display: inline-flex;
  margin-bottom: 22px;
  color: var(--muted);
  font-size: 14px;
}

.detail-head {
  display: flex;
  align-items: flex-start;
  gap: 18px;
  flex-wrap: wrap;
}

.detail-num {
  width: 78px;
  height: 78px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  background: var(--accent-bg);
  font-size: 34px;
}

.detail-head h1 {
  margin: 0 0 6px;
  font-family: "Cinzel", Georgia, serif;
  font-size: clamp(30px, 5vw, 46px);
  line-height: 1.1;
}

.detail-head .sub {
  color: var(--muted);
}

.letterbox {
  margin-top: 22px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  display: flex;
  align-items: center;
  gap: 13px;
}

.letterbox .glyph {
  min-width: 28px;
  color: var(--accent);
  font-size: 25px;
  font-weight: 700;
  text-align: center;
}

.letterbox .lt-text,
.refs .note {
  color: var(--ink-soft);
  font-size: 14px;
}

.letterbox .lt-text strong {
  color: var(--ink);
}

.detail h3 {
  margin: 30px 0 10px;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
}

.detail p.body {
  margin: 0 0 14px;
  color: var(--ink);
  font-size: 16px;
}

.quote {
  margin: 14px 0;
  padding: 16px 18px;
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  background: var(--surface-2);
}

.quote p {
  margin: 0 0 8px;
  color: var(--ink);
  font-style: italic;
}

.quote cite {
  color: var(--muted);
  font-size: 13px;
  font-style: normal;
  font-weight: 700;
}

.refs {
  list-style: none;
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 0;
}

.refs li {
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
}

.refs .ref {
  color: var(--accent);
  font-weight: 700;
}

.tag {
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.detail-nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 28px;
}

.detail-nav a {
  padding: 13px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--ink);
}

.detail-nav a:hover {
  border-color: var(--accent);
  text-decoration: none;
}

.detail-nav a.next {
  text-align: right;
}

.detail-nav a small {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

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

.letter-card {
  padding: 16px;
}

.letter-card .lc-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
}

.letter-card .lc-val {
  font-size: 25px;
}

.letter-card .lc-name {
  font-weight: 800;
}

.letter-card .lc-pic {
  margin-top: 8px;
  color: var(--ink-soft);
  font-size: 14px;
}

.letter-card .lc-mean {
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.section-intro {
  max-width: 760px;
  margin: -8px 0 24px;
  color: var(--ink-soft);
  font-size: 16px;
}

.study-panel {
  max-width: 580px;
  margin: 0 auto;
}

.flashcard {
  height: 320px;
  margin: 18px 0;
  perspective: 1400px;
}

.flashcard-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform .45s ease;
  transform-style: preserve-3d;
  cursor: pointer;
}

.flashcard.flipped .flashcard-inner {
  transform: rotateY(180deg);
}

.flash-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  padding: 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.flash-front .big {
  font-size: 76px;
  line-height: 1;
}

.flash-front .hint,
.progress {
  color: var(--muted);
  font-size: 13px;
}

.flash-back {
  transform: rotateY(180deg);
}

.flash-back .mean {
  margin-bottom: 12px;
  color: var(--accent);
  font-family: "Cinzel", Georgia, serif;
  font-size: 25px;
}

.flash-back .body {
  max-height: 220px;
  overflow-y: auto;
  color: var(--ink-soft);
  font-size: 14px;
}

.btn {
  padding: 10px 17px;
  font-size: 14px;
  font-weight: 700;
}

.btn.ghost {
  background: transparent;
}

.progress {
  margin-top: 14px;
  text-align: center;
}

.quiz-q {
  margin: 0;
  font-size: 20px;
}

.quiz-q .big {
  color: var(--accent);
  font-family: "Cinzel", Georgia, serif;
}

.quiz-meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.quiz-options {
  display: grid;
  gap: 9px;
  margin-top: 18px;
}

.quiz-opt {
  width: 100%;
  padding: 13px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--ink);
  cursor: pointer;
  text-align: left;
}

.quiz-opt:hover:not(:disabled) {
  border-color: var(--accent);
}

.quiz-opt.correct {
  border-color: var(--green);
  background: color-mix(in srgb, var(--green) 14%, var(--surface));
}

.quiz-opt.wrong {
  border-color: var(--red);
  background: color-mix(in srgb, var(--red) 13%, var(--surface));
}

.quiz-opt:disabled {
  cursor: default;
}

.quiz-feedback {
  min-height: 24px;
  margin-top: 16px;
  font-weight: 700;
}

.quiz-feedback.ok {
  color: var(--green);
}

.quiz-feedback.no {
  color: var(--red);
}

.quiz-result {
  text-align: center;
}

.quiz-result .score {
  font-size: 56px;
}

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

.site-footer {
  max-width: 1060px;
  width: 100%;
  margin: 0 auto;
  padding: 24px 36px 34px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
}

.site-footer p {
  margin: 0 0 8px;
}

@media (max-width: 820px) {
  body {
    font-size: 16px;
    -webkit-tap-highlight-color: transparent;
    overflow-x: hidden;
  }

  body.nav-open {
    overflow: hidden;
  }

  .app-shell {
    display: block;
    min-height: auto;
  }

  .mobile-topbar {
    position: sticky;
    top: 0;
    z-index: 70;
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: var(--mobile-topbar-h);
    padding: calc(env(safe-area-inset-top) + 8px) 14px 8px;
    border-bottom: 1px solid var(--border);
    background: color-mix(in srgb, var(--sidebar) 92%, transparent);
    backdrop-filter: blur(10px);
  }

  .nav-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    padding: 8px;
    border-radius: 10px;
  }

  .nav-toggle:active {
    background: var(--surface-2);
  }

  .mobile-title {
    min-width: 0;
    color: var(--ink);
    font-size: 15px;
    font-weight: 700;
    line-height: 1.2;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 80;
    border: 0;
    background: rgba(8, 8, 8, 0.55);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity .2s ease, visibility .2s ease;
  }

  body.nav-open .sidebar-backdrop {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
  }

  .sidebar {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 90;
    width: min(320px, 88vw);
    height: auto;
    max-height: 100dvh;
    padding: calc(env(safe-area-inset-top) + 18px) 16px calc(env(safe-area-inset-bottom) + 18px);
    overflow-y: auto;
    overscroll-behavior: contain;
    border-right: 1px solid var(--border);
    border-bottom: 0;
    box-shadow: 8px 0 28px rgba(0, 0, 0, 0.18);
  }

  body.nav-open .sidebar {
    display: flex;
  }

  .main-nav {
    display: grid;
  }

  .main-nav a {
    min-height: 44px;
    display: flex;
    align-items: center;
    padding: 10px 12px;
  }

  .sidebar-numbers a {
    min-height: 40px;
    padding: 10px 0;
    font-size: 13px;
  }

  .sidebar-filters .chip {
    min-height: 44px;
    padding: 10px 12px;
  }

  .sidebar-note {
    display: block;
    font-size: 12px;
  }

  .content-shell {
    width: 100%;
    min-height: calc(100dvh - var(--mobile-topbar-h) - var(--bottom-nav-h));
  }

  .content {
    padding: 16px 16px calc(var(--bottom-nav-h) + env(safe-area-inset-bottom) + 16px);
  }

  .toolbar-browse {
    position: sticky;
    top: calc(var(--mobile-topbar-h) - 1px);
    z-index: 40;
    margin: -4px -16px 12px;
    padding: 8px 16px 10px;
    background: color-mix(in srgb, var(--bg) 94%, transparent);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid color-mix(in srgb, var(--border) 70%, transparent);
  }

  .toolbar {
    margin: 0;
  }

  .search {
    padding: 12px 14px;
  }

  .search input {
    font-size: 16px;
  }

  .mobile-filters {
    display: flex;
    margin-top: 10px;
  }

  .filters-scroll {
    display: flex;
    flex-wrap: nowrap;
    gap: 8px;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
  }

  .filters-scroll::-webkit-scrollbar {
    display: none;
  }

  .filters-scroll .chip {
    flex: 0 0 auto;
    min-height: 38px;
    padding: 8px 14px;
    white-space: nowrap;
  }

  .count {
    margin: 4px 0 10px;
  }

  .hero {
    margin-bottom: 20px;
  }

  .hero h1 {
    font-size: clamp(28px, 9vw, 40px);
  }

  .hero p {
    font-size: 16px;
  }

  .grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .card {
    min-height: 118px;
    padding: 14px 12px;
    -webkit-tap-highlight-color: transparent;
  }

  .card .num {
    font-size: 28px;
  }

  .card .mean {
    font-size: 13px;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  .letters-grid {
    grid-template-columns: 1fr;
  }

  .letter-card {
    padding: 18px 16px;
  }

  .detail,
  .quiz-card {
    padding: 20px 16px;
  }

  .detail-head {
    gap: 14px;
  }

  .detail-num {
    width: 64px;
    height: 64px;
    font-size: 28px;
    flex-shrink: 0;
  }

  .detail-head h1 {
    font-size: clamp(24px, 7vw, 34px);
  }

  .letterbox {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .detail-nav {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .detail-nav a {
    min-height: 52px;
    padding: 14px 16px;
  }

  .detail-nav a.next {
    text-align: left;
  }

  .back {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    margin-bottom: 16px;
  }

  .flashcard {
    height: min(58vw, 280px);
    min-height: 220px;
  }

  .flash-front .big {
    font-size: clamp(56px, 18vw, 76px);
  }

  .study-controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .study-controls .btn {
    min-height: 44px;
  }

  .study-controls .btn.ghost:last-child {
    grid-column: 1 / -1;
  }

  .scope-row {
    justify-content: flex-start;
    flex-wrap: nowrap;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 4px;
    margin: 0 -4px;
    padding-left: 4px;
    padding-right: 4px;
  }

  .scope-row::-webkit-scrollbar {
    display: none;
  }

  .scope-row .chip {
    flex: 0 0 auto;
    min-height: 38px;
    white-space: nowrap;
  }

  .quiz-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  .quiz-opt {
    min-height: 48px;
    padding: 14px 16px;
    font-size: 16px;
  }

  .quiz-result .score {
    font-size: 48px;
  }

  .btn,
  .chip {
    touch-action: manipulation;
  }

  .bottom-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 65;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 2px;
    min-height: var(--bottom-nav-h);
    padding: 6px 8px calc(env(safe-area-inset-bottom) + 6px);
    border-top: 1px solid var(--border);
    background: color-mix(in srgb, var(--sidebar) 94%, transparent);
    backdrop-filter: blur(10px);
  }

  .bottom-nav a {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-height: 48px;
    padding: 4px 6px;
    border-radius: 10px;
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
    text-align: center;
    text-decoration: none;
  }

  .bottom-nav a:hover {
    text-decoration: none;
  }

  .bottom-nav a.active {
    color: var(--accent);
    background: var(--accent-bg);
  }

  .bottom-nav-icon {
    font-size: 17px;
    line-height: 1;
  }

  .site-footer {
    padding: 20px 16px calc(var(--bottom-nav-h) + env(safe-area-inset-bottom) + 12px);
    font-size: 12px;
  }

  #filterFolder {
    display: none;
  }
}
