:root {
  --bg: #f2f2f7;
  --card: #ffffff;
  --text: #000000;
  --secondary: rgba(60, 60, 67, 0.6);
  --tertiary: rgba(60, 60, 67, 0.36);
  --hairline: rgba(60, 60, 67, 0.12);
  --accent: #007aff;
  --accent-soft: rgba(0, 122, 255, 0.1);
  --green: #34c759;
  --orange: #ff9f0a;
  --red: #ff3b30;
  --serif: "New York", ui-serif, Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", "PingFang SC", "Segoe UI", sans-serif;
  --tabbar-h: 84px;
  --radius: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --text: #ffffff;
    --secondary: rgba(235, 235, 245, 0.6);
    --tertiary: rgba(235, 235, 245, 0.32);
    --hairline: rgba(235, 235, 245, 0.14);
    --accent: #0a84ff;
    --accent-soft: rgba(10, 132, 255, 0.16);
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html { height: 100%; }

body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  height: 100%;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--bg);
}

/* ---------- Views ---------- */
.view {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + 16px);
  display: none;
}
.view.active { display: block; }

/* ---------- Large title header ---------- */
.view-header {
  padding: 20px 20px 8px;
  position: sticky;
  top: 0;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  z-index: 10;
}
.view-header .eyebrow {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 4px;
}
.view-header h1 {
  font-size: 34px;
  font-weight: 700;
  letter-spacing: 0.01em;
  line-height: 1.1;
}
.view-header .sub {
  margin-top: 6px;
  font-size: 15px;
  color: var(--secondary);
}

/* ---------- Home ---------- */
.hero-card {
  margin: 16px 20px;
  background: var(--card);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.hero-card .book-title {
  font-family: var(--serif);
  font-size: 26px;
  line-height: 1.15;
  font-weight: 500;
  letter-spacing: -0.01em;
}
.hero-card .book-meta {
  margin-top: 8px;
  font-size: 14px;
  color: var(--secondary);
}
.progress-track {
  margin-top: 18px;
  height: 6px;
  border-radius: 3px;
  background: var(--accent-soft);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  border-radius: 3px;
  background: var(--accent);
  width: 0%;
  transition: width 0.6s cubic-bezier(0.22, 1, 0.36, 1);
}
.progress-label {
  margin-top: 8px;
  font-size: 13px;
  color: var(--secondary);
  font-variant-numeric: tabular-nums;
}

.stat-row {
  display: flex;
  gap: 12px;
  margin: 0 20px 16px;
}
.stat {
  flex: 1;
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.stat .num { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .lbl { font-size: 12px; color: var(--secondary); margin-top: 2px; letter-spacing: 0.04em; text-transform: uppercase; font-weight: 600; }

.section-label {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--tertiary);
  margin: 24px 20px 10px;
}

/* ---------- Chapter list ---------- */
.chapter-list { margin: 0 20px; background: var(--card); border-radius: var(--radius); overflow: hidden; }
.chapter-row {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  border-bottom: 0.5px solid var(--hairline);
  cursor: pointer;
  user-select: none;
  transition: background 0.15s;
}
.chapter-row:last-child { border-bottom: none; }
.chapter-row:active { background: var(--accent-soft); }
.chapter-num {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  font-variant-numeric: tabular-nums;
}
.chapter-row.done .chapter-num { background: var(--green); color: #fff; }
.chapter-info { flex: 1; min-width: 0; }
.chapter-info .t {
  font-size: 16px;
  font-weight: 500;
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.chapter-info .d {
  font-size: 13px;
  color: var(--secondary);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chapter-chevron { color: var(--tertiary); font-size: 17px; flex-shrink: 0; }

/* region group headers */
.region-header {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--secondary);
  margin: 22px 20px 8px;
}

/* ---------- Search ---------- */
.search-wrap { padding: 12px 20px 4px; }
.search-box {
  width: 100%;
  background: var(--card);
  border: none;
  border-radius: 12px;
  padding: 11px 16px;
  font-size: 16px;
  font-family: var(--sans);
  color: var(--text);
  outline: none;
}
.search-box::placeholder { color: var(--tertiary); }
.search-hint { margin: 20px; color: var(--secondary); font-size: 14px; text-align: center; }

/* ---------- Reader ---------- */
.reader { font-family: var(--serif); }
.reader-topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}
.back-btn {
  border: none;
  background: none;
  color: var(--accent);
  font-size: 17px;
  font-family: var(--sans);
  padding: 6px 10px 6px 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 2px;
}
.reader-topbar .spacer { flex: 1; }
.icon-btn {
  border: none;
  background: none;
  font-size: 19px;
  padding: 6px 10px;
  cursor: pointer;
  color: var(--accent);
}
.reader-body { padding: 4px 26px 40px; font-family: var(--serif); }
.reader-body .kicker {
  font-family: var(--sans);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 10px;
}
.reader-body h2 {
  font-size: 30px;
  font-weight: 600;
  line-height: 1.12;
  letter-spacing: -0.01em;
  margin-bottom: 6px;
}
.reader-body .dx-line {
  font-family: var(--sans);
  font-size: 14px;
  color: var(--secondary);
  margin-bottom: 22px;
}
.reader-body p.lede {
  font-size: 19px;
  line-height: 1.62;
  margin-bottom: 26px;
}
.pearls-label {
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--secondary);
  margin: 26px 0 14px;
  padding-top: 18px;
  border-top: 0.5px solid var(--hairline);
}
.pearl {
  display: flex;
  gap: 12px;
  margin-bottom: 14px;
  font-size: 17px;
  line-height: 1.55;
}
.pearl::before {
  content: "";
  flex-shrink: 0;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  margin-top: 10px;
}
.reader-actions {
  margin: 8px 26px 30px;
  display: flex;
  gap: 10px;
  font-family: var(--sans);
}
.action-btn {
  flex: 1;
  border: none;
  border-radius: 12px;
  padding: 13px;
  font-size: 16px;
  font-weight: 600;
  font-family: var(--sans);
  cursor: pointer;
  background: var(--accent);
  color: #fff;
  transition: transform 0.1s, opacity 0.15s;
}
.action-btn:active { transform: scale(0.97); opacity: 0.85; }
.action-btn.secondary { background: var(--accent-soft); color: var(--accent); }
.action-btn.done { background: var(--green); }

/* notes */
.notes-box {
  margin: 0 26px 30px;
  font-family: var(--sans);
}
.notes-box textarea {
  width: 100%;
  min-height: 110px;
  border: none;
  background: var(--card);
  border-radius: 12px;
  padding: 14px;
  font-size: 15px;
  font-family: var(--sans);
  color: var(--text);
  resize: vertical;
  outline: none;
  line-height: 1.5;
}
.notes-box .saved-tag { font-size: 12px; color: var(--tertiary); margin-top: 6px; text-align: right; }

/* ---------- Flashcards ---------- */
.deck-area { padding: 16px 24px; }
.deck-meta { text-align: center; color: var(--secondary); font-size: 14px; margin-bottom: 16px; font-variant-numeric: tabular-nums; }
.card-stage { perspective: 1200px; }
.flashcard {
  position: relative;
  width: 100%;
  min-height: 340px;
  transform-style: preserve-3d;
  transition: transform 0.55s cubic-bezier(0.22, 1, 0.36, 1);
  cursor: pointer;
}
.flashcard.flipped { transform: rotateY(180deg); }
.face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: var(--card);
  border-radius: 24px;
  padding: 30px 26px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-shadow: 0 6px 24px rgba(0,0,0,0.07);
}
.face.back { transform: rotateY(180deg); }
.face .face-tag {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--tertiary);
  text-align: center;
  margin-bottom: 16px;
}
.face .ch-tag { font-size: 12px; color: var(--accent); font-weight: 600; text-align: center; margin-bottom: 14px; letter-spacing: 0.06em; }
.face .q {
  font-family: var(--serif);
  font-size: 24px;
  line-height: 1.3;
  text-align: center;
  font-weight: 500;
}
.face .a {
  font-family: var(--serif);
  font-size: 19px;
  line-height: 1.5;
  text-align: center;
}
.face .tap-hint {
  position: absolute;
  bottom: 18px;
  left: 0; right: 0;
  text-align: center;
  font-size: 12px;
  color: var(--tertiary);
  letter-spacing: 0.08em;
}
.deck-buttons { display: flex; gap: 12px; margin-top: 22px; }
.deck-btn {
  flex: 1;
  border: none;
  border-radius: 14px;
  padding: 15px;
  font-size: 16px;
  font-weight: 600;
  font-family: var(--sans);
  cursor: pointer;
  transition: transform 0.1s;
}
.deck-btn:active { transform: scale(0.96); }
.deck-btn.know { background: var(--green); color: #fff; }
.deck-btn.again { background: var(--card); color: var(--orange); }
.deck-empty { text-align: center; padding: 60px 30px; color: var(--secondary); font-size: 16px; line-height: 1.5; }
.deck-empty .big { font-size: 40px; display: block; margin-bottom: 12px; }

/* ---------- Full notes rendering ---------- */
.reader-full p {
  font-size: 17px;
  line-height: 1.62;
  margin-bottom: 16px;
}
.reader-full h3 {
  font-family: var(--sans);
  font-size: 20px;
  font-weight: 700;
  margin: 28px 0 12px;
  letter-spacing: -0.01em;
}
.reader-full ul, .reader-full ol {
  margin: 0 0 16px 22px;
  font-size: 16px;
  line-height: 1.55;
}
.reader-full li { margin-bottom: 6px; }
.reader-full strong { font-weight: 700; }
.full-loading { color: var(--tertiary); font-family: var(--sans); font-size: 15px; padding: 12px 0; }

/* ---------- Tab bar ---------- */
.tabbar {
  position: absolute;
  bottom: 0;
  left: 0; right: 0;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  display: flex;
  background: color-mix(in srgb, var(--card) 78%, transparent);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border-top: 0.5px solid var(--hairline);
  z-index: 50;
}
.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border: none;
  background: none;
  cursor: pointer;
  color: var(--tertiary);
  font-family: var(--sans);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.02em;
  padding-top: 6px;
}
.tab svg { width: 25px; height: 25px; }
.tab.active { color: var(--accent); }

/* ---------- More ---------- */
.more-list { margin: 16px 20px; background: var(--card); border-radius: var(--radius); overflow: hidden; }
.more-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 0.5px solid var(--hairline);
  font-size: 16px;
  cursor: pointer;
  user-select: none;
}
.more-row:last-child { border-bottom: none; }
.more-row .val { color: var(--secondary); font-size: 15px; }
.more-row.destructive { color: var(--red); justify-content: center; font-weight: 500; }
.about-block { margin: 20px; color: var(--secondary); font-size: 14px; line-height: 1.6; }
.about-block b { color: var(--text); }

@media (min-width: 641px) {
  #app { box-shadow: 0 0 40px rgba(0,0,0,0.12); }
}
