:root {
  color-scheme: light;
  --paper: #f1eadc;
  --sheet: #fffdf7;
  --ink: #1d1c18;
  --muted: #716d63;
  --line: #c9c0ae;
  --signal: #ef5b35;
  --signal-dark: #ba3518;
  --mint: #b9d6bd;
  --blue: #a8c7d8;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: "Avenir Next", "PingFang SC", "Hiragino Sans GB", sans-serif;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; -webkit-text-size-adjust: 100%; }
body {
  min-width: 320px;
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(rgba(80, 69, 48, 0.055) 1px, transparent 1px),
    var(--paper);
  background-size: 100% 28px;
  font-size: 16px;
  line-height: 1.5;
  padding-bottom: calc(38px + var(--safe-bottom));
}
button, input, select { font: inherit; }
button, a, select { -webkit-tap-highlight-color: transparent; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible {
  outline: 3px solid rgba(239, 91, 53, 0.34);
  outline-offset: 2px;
}
[hidden] { display: none !important; }

.masthead {
  position: relative;
  overflow: hidden;
  color: #fffdf7;
  background: var(--ink);
  padding: max(14px, env(safe-area-inset-top)) 18px 25px;
}
.masthead::before {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0.13;
  background-image: radial-gradient(#fff 0.7px, transparent 0.7px);
  background-size: 5px 5px;
  pointer-events: none;
}
.topline, .title-lockup, .headline-stats, main { width: min(100%, 720px); margin-inline: auto; }
.topline { position: relative; z-index: 1; display: flex; align-items: center; justify-content: space-between; min-height: 44px; }
.edition { color: rgba(255, 253, 247, 0.62); font-size: 10px; font-weight: 800; letter-spacing: 0.14em; }
.install-button { min-height: 44px; padding: 0 13px; color: #fff; border: 1px solid rgba(255,255,255,.38); border-radius: 0; background: transparent; font-size: 12px; font-weight: 800; }
.title-lockup { position: relative; z-index: 1; display: flex; align-items: flex-start; gap: 17px; padding-top: 26px; }
.title-lockup p { margin: 0 0 8px; color: rgba(255,253,247,.62); font-size: 12px; letter-spacing: .06em; }
.title-lockup h1 { margin: 0; font-family: "Songti SC", "STSong", serif; font-size: clamp(52px, 16vw, 76px); font-weight: 900; line-height: .87; letter-spacing: -.09em; }
.title-lockup h1 em { color: var(--signal); font-style: normal; }
.stamp { display: grid; flex: 0 0 auto; place-items: center; width: 48px; height: 48px; margin-top: 7px; color: var(--ink); background: var(--mint); border-radius: 50%; font-family: "Songti SC", serif; font-size: 14px; font-weight: 900; transform: rotate(-9deg); box-shadow: 0 0 0 3px var(--ink), 0 0 0 4px var(--mint); }
.headline-stats { position: relative; z-index: 1; display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px; margin-top: 30px; background: rgba(255,255,255,.2); border-block: 1px solid rgba(255,255,255,.2); }
.headline-stats div { padding: 13px 8px 12px; background: var(--ink); text-align: center; }
.headline-stats strong { display: block; font-family: "Songti SC", serif; font-size: 25px; line-height: 1; }
.headline-stats span { display: block; margin-top: 6px; color: rgba(255,253,247,.58); font-size: 10px; letter-spacing: .08em; }

main { padding: 0 16px 30px; }
.study-controls { position: relative; z-index: 1; margin: 0 -16px; padding: 15px 16px 12px; background: rgba(241,234,220,.96); border-bottom: 1px solid var(--line); }
.view-switch { display: grid; grid-template-columns: 1fr 1fr; margin-bottom: 10px; border: 1px solid var(--ink); background: var(--sheet); }
.view-switch button { min-height: 44px; border: 0; color: var(--ink); background: transparent; font-size: 13px; font-weight: 900; }
.view-switch button.is-active { color: #fff; background: var(--ink); }
.tier-switch { display: grid; grid-template-columns: 1.16fr .84fr; gap: 8px; margin-bottom: 10px; }
.tier-switch button { display: flex; align-items: center; justify-content: space-between; min-height: 52px; padding: 0 13px; color: var(--muted); border: 1px solid var(--line); background: rgba(255,253,247,.72); text-align: left; }
.tier-switch span { font-family: "Songti SC", "STSong", serif; font-size: 16px; font-weight: 900; }
.tier-switch strong { display: grid; place-items: center; min-width: 29px; height: 29px; padding: 0 7px; color: var(--ink); background: #e3dbc9; font-size: 11px; }
.tier-switch button.is-active { color: #fffdf7; border-color: var(--ink); background: var(--ink); box-shadow: inset 5px 0 0 var(--signal); }
.tier-switch button.is-active strong { background: var(--signal); color: #fff; }
.tier-switch #tierATab.is-active { box-shadow: inset 5px 0 0 #779cad; }
.tier-switch #tierATab.is-active strong { color: var(--ink); background: var(--blue); }
.age-strip { display: flex; gap: 7px; overflow-x: auto; margin: 0 -16px 10px; padding: 0 16px 2px; scrollbar-width: none; }
.age-strip::-webkit-scrollbar { display: none; }
.age-chip { flex: 0 0 auto; min-height: 44px; padding: 0 13px; color: #514c42; border: 1px solid var(--line); border-radius: 999px; background: rgba(255,253,247,.72); font-size: 11px; font-weight: 800; }
.age-chip.is-active { color: #fff; border-color: var(--ink); background: var(--ink); }
.search-field { display: grid; grid-template-columns: 27px 1fr 44px; align-items: center; min-height: 50px; padding-left: 13px; background: var(--sheet); border: 1px solid var(--ink); }
.search-field > span { font-size: 23px; transform: rotate(-15deg); }
.search-field input { min-width: 0; width: 100%; border: 0; outline: 0; color: var(--ink); background: transparent; font-size: 15px; }
.search-field input::placeholder { color: #8b8578; opacity: 1; }
.search-field button { width: 44px; height: 44px; border: 0; background: transparent; font-size: 20px; }
.select-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding-top: 8px; }
.select-row label { display: grid; grid-template-columns: auto minmax(0,1fr); align-items: center; gap: 6px; min-height: 44px; padding: 0 8px 0 10px; color: var(--muted); border: 1px solid var(--line); background: rgba(255,253,247,.55); font-size: 10px; }
.select-row select { min-width: 0; height: 42px; border: 0; outline: 0; color: var(--ink); background: transparent; font-size: 12px; font-weight: 800; }
.topic-strip { display: flex; gap: 7px; overflow-x: auto; padding: 10px 0 1px; scrollbar-width: none; }
.topic-strip::-webkit-scrollbar { display: none; }
.topic-chip { flex: 0 0 auto; min-height: 44px; padding: 0 14px; color: #4d493f; border: 1px solid var(--line); border-radius: 999px; background: var(--sheet); font-size: 12px; font-weight: 800; }
.topic-chip.is-active { color: var(--ink); border-color: var(--signal); background: #ffd1c2; }

.results { padding-top: 20px; }
.results-header { display: flex; align-items: flex-end; justify-content: space-between; gap: 14px; padding: 0 2px 13px; border-bottom: 3px solid var(--ink); }
.results-header p { margin: 0 0 3px; color: var(--signal-dark); font-size: 9px; font-weight: 900; letter-spacing: .16em; }
.results-header h2 { margin: 0; font-family: "Songti SC", serif; font-size: 24px; }
.results-header > strong { font-size: 12px; }
.study-list { display: grid; gap: 14px; margin: 14px 0 0; padding: 0; list-style: none; }
.study-card { position: relative; overflow: hidden; padding: 17px 15px 14px 18px; background: var(--sheet); border: 1px solid var(--ink); box-shadow: 5px 5px 0 rgba(29,28,24,.12); animation: card-in .34s ease both; }
.study-card::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 5px; background: var(--signal); }
.study-card.tier-a::before { background: #779cad; }
.study-cover { display: block; width: calc(100% + 33px); height: 168px; margin: -17px -15px 16px -18px; object-fit: cover; background: #ddd3c1; border-bottom: 1px solid var(--ink); }
.study-card.is-learning { animation: card-out .28s ease forwards; }
.card-topline { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.rank { color: var(--signal-dark); font-size: 10px; font-weight: 900; letter-spacing: .12em; }
.topic-label { padding: 4px 8px; color: #29412f; background: var(--mint); font-size: 10px; font-weight: 800; transform: rotate(1deg); }
.study-card h3 { margin: 12px 0 9px; font-family: "Songti SC", "STSong", serif; font-size: 21px; line-height: 1.36; letter-spacing: -.01em; }
.byline { display: flex; flex-wrap: wrap; gap: 4px 10px; margin: 0; color: var(--muted); font-size: 11px; }
.metrics { display: grid; grid-template-columns: repeat(3,1fr); gap: 6px; margin: 14px 0 0; padding: 10px 0; border-block: 1px dashed var(--line); }
.metrics div { text-align: center; }
.metrics strong { display: block; font-size: 15px; }
.metrics span { display: block; color: var(--muted); font-size: 9px; }
.card-actions { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 8px; padding-top: 12px; }
.watch-button, .learn-button { min-height: 46px; border: 1px solid var(--ink); font-size: 13px; font-weight: 900; }
.watch-button { display: grid; place-items: center; color: #fff; background: var(--ink); text-decoration: none; }
.learn-button { padding: 0 13px; color: var(--ink); background: transparent; }
.learned-view .study-card::before { background: var(--blue); }
.learned-view .watch-button { background: #374b55; }
.restore-button { min-height: 46px; padding: 0 13px; color: var(--ink); border: 1px solid var(--ink); background: var(--blue); font-size: 13px; font-weight: 900; }

.loading { display: grid; gap: 8px; padding: 38px 8px; }
.loading span { display: block; height: 14px; background: linear-gradient(90deg,#ddd3c1,#f8f3e9,#ddd3c1); background-size: 220% 100%; animation: shimmer 1.2s linear infinite; }
.loading span:nth-child(2) { width: 72%; }
.loading p { color: var(--muted); font-size: 12px; text-align: center; }
.error, .empty { margin-top: 16px; padding: 28px 20px; border: 1px dashed var(--ink); background: rgba(255,253,247,.64); text-align: center; }
.error strong, .empty strong { display: block; font-family: "Songti SC", serif; font-size: 20px; }
.error p, .empty p { color: var(--muted); font-size: 13px; }
.error button, .empty button { min-height: 44px; padding: 0 18px; border: 1px solid var(--ink); background: var(--sheet); font-weight: 800; }
.load-more { display: block; width: 100%; min-height: 48px; margin-top: 16px; color: var(--ink); border: 1px solid var(--ink); background: var(--sheet); font-size: 13px; font-weight: 900; box-shadow: 4px 4px 0 rgba(29,28,24,.12); }
.tier-continue { display: block; width: 100%; min-height: 52px; margin-top: 16px; color: #fff; border: 1px solid var(--ink); background: #374b55; font-size: 13px; font-weight: 900; box-shadow: 5px 5px 0 var(--blue); }
.empty > span { display: grid; place-items: center; width: 44px; height: 44px; margin: 0 auto 12px; color: #fff; border-radius: 50%; background: var(--ink); font-size: 23px; }

.return-dock { position: fixed; z-index: 40; right: 12px; bottom: calc(12px + var(--safe-bottom)); left: 12px; display: grid; grid-template-columns: minmax(0,1fr) auto 36px; align-items: center; gap: 8px; max-width: 680px; margin: auto; padding: 10px 8px 10px 13px; color: #fff; background: var(--ink); box-shadow: 0 10px 32px rgba(0,0,0,.28); }
.return-dock div { min-width: 0; }
.return-dock span { display: block; color: rgba(255,255,255,.57); font-size: 9px; }
.return-dock strong { display: block; overflow: hidden; font-size: 11px; text-overflow: ellipsis; white-space: nowrap; }
.return-dock button { min-height: 42px; color: var(--ink); border: 0; background: var(--mint); font-size: 11px; font-weight: 900; }
.return-dock #dismissReturn { width: 36px; color: #fff; background: transparent; font-size: 20px; }
.toast { position: fixed; z-index: 60; right: 16px; bottom: calc(18px + var(--safe-bottom)); left: 16px; max-width: 420px; margin: auto; padding: 13px 15px; color: #fff; background: var(--signal-dark); font-size: 13px; text-align: center; box-shadow: 4px 4px 0 rgba(29,28,24,.3); }
.toast button { margin-left: 12px; color: #fff; border: 0; border-bottom: 1px solid #fff; background: transparent; font-weight: 900; }

dialog { border: 0; }
dialog::backdrop { background: rgba(29,28,24,.68); backdrop-filter: blur(3px); }
.install-dialog { width: min(calc(100% - 32px), 430px); padding: 0; background: var(--sheet); box-shadow: 8px 8px 0 var(--signal); }
.install-dialog form { position: relative; padding: 28px 24px 22px; }
.dialog-close { position: absolute; top: 8px; right: 8px; width: 44px; height: 44px; border: 0; background: transparent; font-size: 24px; }
.dialog-mark { display: grid; place-items: center; width: 52px; height: 52px; color: #fff; background: var(--signal); font-family: "Songti SC",serif; font-size: 27px; font-weight: 900; transform: rotate(-6deg); }
.install-dialog h2 { margin: 18px 0 12px; font-family: "Songti SC",serif; font-size: 24px; line-height: 1.3; }
.install-dialog p { color: var(--muted); font-size: 14px; }
.dialog-primary { width: 100%; min-height: 48px; margin-top: 12px; color: #fff; border: 0; background: var(--ink); font-weight: 900; }

@keyframes shimmer { to { background-position: -220% 0; } }
@keyframes card-in { from { opacity: 0; transform: translateY(9px); } to { opacity: 1; transform: translateY(0); } }
@keyframes card-out { to { opacity: 0; transform: translateX(24px) rotate(1deg); } }
@media (prefers-reduced-motion: reduce) { *,*::before,*::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; } }
@media (min-width: 620px) { .masthead { padding-inline: 28px; } main { padding-inline: 24px; } .study-controls { margin-inline: -24px; padding-inline: 24px; } .study-list { grid-template-columns: 1fr 1fr; align-items: start; } }
