/* ============================================================
   300 Poems · Tang & Song Ci Reader — styles for the static app
   ============================================================ */

:root {
  --ui-font: -apple-system, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --read-font: "Songti SC", "STSong", "Noto Serif SC", "Source Han Serif SC", "SimSun", serif;

  --radius: 16px;
  --bar-h: 52px;
  --read-size: 18px;   /* overridden by JS */
  --read-lh: 2.05;
}

/* ---------- Themes ---------- */
body[data-theme="paper"] {
  --bg: #f5efe0;
  --bg-soft: #ede5cf;
  --surface: #faf6ea;
  --text: #3b3226;
  --text-dim: #8a7a63;
  --accent: #a0672f;
  --accent-soft: #e5d3b3;
  --line: #e2d6ba;
  --card-shadow: 0 2px 10px rgba(90, 70, 40, 0.08);
  color-scheme: light;
}
body[data-theme="white"] {
  --bg: #fbfaf7;
  --bg-soft: #f1efe9;
  --surface: #ffffff;
  --text: #262626;
  --text-dim: #8f8f8f;
  --accent: #7a5c3e;
  --accent-soft: #e8e0d2;
  --line: #e6e2d8;
  --card-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
  color-scheme: light;
}
body[data-theme="dark"] {
  --bg: #191a1d;
  --bg-soft: #22242a;
  --surface: #202228;
  --text: #c9c3b4;
  --text-dim: #75705f;
  --accent: #c9a15f;
  --accent-soft: #3a3529;
  --line: #2e3038;
  --card-shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

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

html, body { height: 100%; }

body {
  font-family: var(--ui-font);
  background: var(--bg);
  color: var(--text);
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-font-smoothing: antialiased;
  transition: background 0.25s, color 0.25s;
}

#app { height: 100%; display: flex; flex-direction: column; overflow: hidden; }

/* Note: do NOT put overflow:hidden on .view — on WKWebView (iOS) it breaks
   background painting of descendant elements (e.g. the .card-mark block).
   Containment happens at #app instead. */
.view {
  height: 100%;
  display: flex;
  flex-direction: column;
  animation: fadein 0.22s ease;
}
/* .view { display: flex } would override the UA's [hidden] { display: none },
   so hidden views stayed visible (stacked) and navigation never repainted.
   This rule restores hiding with higher priority. */
.view[hidden] { display: none !important; }
@keyframes fadein { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
}

/* ---------- Top bar ---------- */
.bar {
  display: flex;
  align-items: center;
  gap: 6px;
  height: calc(var(--bar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 10px 0;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
  z-index: 5;
}
.bar-title {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.25;
}
.bar-title > span:first-child {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 1px;
}
.bar-sub { font-size: 11px; color: var(--text-dim); }
.bar-actions { display: flex; gap: 2px; }

.icon-btn {
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 22px;
  line-height: 1;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.icon-btn:active { background: var(--accent-soft); }

/* ---------- Home ---------- */
.book-cards {
  padding: calc(24px + env(safe-area-inset-top)) 18px 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.card {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  padding: 18px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  color: inherit;
  transition: transform 0.12s;
}
.card:active { transform: scale(0.985); }

/* Single-book apps: make the lone card a bit more prominent */
body[data-single="1"] .card { padding: 26px 22px; }
body[data-single="1"] .card-title { font-size: 26px; }
body[data-single="1"] .card-mark { width: 62px; height: 62px; font-size: 28px; border-radius: 16px; }

.card-mark {
  flex-shrink: 0;
  width: 52px;
  height: 52px;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--read-font);
  font-size: 24px;
  font-weight: 700;
  color: var(--surface);
  background: linear-gradient(150deg, #b57a3a, #8a5426);
}
.card[data-book="song"] .card-mark {
  background: linear-gradient(150deg, #4f6f8f, #2f465e);
}
.card[data-book="nalan"] .card-mark {
  background: linear-gradient(150deg, #6b5b95, #463a63);
}
.card-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.card-title { font-family: var(--read-font); font-size: 21px; font-weight: 700; letter-spacing: 1px; }
.card-sub { font-size: 12px; color: var(--text-dim); }
.card-arrow { font-size: 26px; color: var(--text-dim); }

.quick-row {
  display: flex;
  gap: 12px;
  padding: 18px;
}
.quick {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 14px 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  cursor: pointer;
  font-family: inherit;
  color: inherit;
  box-shadow: var(--card-shadow);
}
.quick:active { background: var(--accent-soft); }
.quick-icon { font-size: 20px; color: var(--accent); }
.quick-name { font-size: 12px; color: var(--text-dim); }

.home-foot {
  margin-top: auto;
  padding: 10px 24px calc(20px + env(safe-area-inset-bottom));
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.8;
}

/* ---------- Search ---------- */
.search-wrap { padding: 10px 14px; flex-shrink: 0; }
#search-input {
  width: 100%;
  height: 40px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  outline: none;
}
#search-input:focus { border-color: var(--accent); }

/* ---------- List ---------- */
.group-head {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 8px 18px 6px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 2px;
  color: var(--accent);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid transparent;
}
.group-head .cnt { font-weight: 400; color: var(--text-dim); font-size: 11px; letter-spacing: 0; }

.poem-item {
  display: flex;
  align-items: baseline;
  gap: 10px;
  width: 100%;
  padding: 12px 18px;
  border: none;
  background: transparent;
  color: inherit;
  font-family: inherit;
  cursor: pointer;
  text-align: left;
  border-bottom: 1px solid color-mix(in srgb, var(--line) 55%, transparent);
}
.poem-item:active { background: var(--accent-soft); }
.poem-item .t {
  font-family: var(--read-font);
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.5px;
  flex-shrink: 0;
  max-width: 52%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.poem-item .p {
  flex: 1;
  min-width: 0;
  font-size: 12.5px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.poem-item .fav-mark { color: var(--accent); font-size: 13px; }

/* Search result items */
.result-item {
  display: block;
  width: 100%;
  padding: 13px 18px;
  border: none;
  background: transparent;
  color: inherit;
  font-family: inherit;
  cursor: pointer;
  text-align: left;
  border-bottom: 1px solid color-mix(in srgb, var(--line) 55%, transparent);
}
.result-item:active { background: var(--accent-soft); }
.result-item .rt {
  font-family: var(--read-font);
  font-size: 17px;
  font-weight: 600;
}
.result-item .ra { font-size: 12px; color: var(--text-dim); margin-left: 8px; }
.result-item .rp {
  margin-top: 3px;
  font-size: 12.5px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.result-item .book-tag {
  float: right;
  font-size: 10.5px;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 1px 7px;
  margin-top: 2px;
}

.empty {
  padding: 60px 30px;
  text-align: center;
  color: var(--text-dim);
  font-size: 14px;
  line-height: 2;
}

/* ---------- Reading view ---------- */
#read-body {
  padding: 26px 26px 30px;
  max-width: 680px;
  margin: 0 auto;
}
.read-head {
  text-align: center;
  margin-bottom: 26px;
}
.read-title {
  font-family: var(--read-font);
  font-size: calc(var(--read-size) * 1.5);
  font-weight: 700;
  letter-spacing: 3px;
  color: var(--accent);
}
.read-rhyme {
  margin-top: 10px;
  font-family: var(--read-font);
  font-size: 12.5px;
  letter-spacing: 1px;
  color: var(--text-dim);
}
.read-author {
  margin-top: 8px;
  font-size: 13.5px;
  color: var(--text-dim);
  letter-spacing: 2px;
}
.read-divider {
  width: 56px;
  height: 2px;
  margin: 20px auto;
  background: var(--accent-soft);
  border-radius: 2px;
}

.poem-body {
  font-family: var(--read-font);
  font-size: var(--read-size);
  line-height: var(--read-lh);
  letter-spacing: 1px;
  text-align: center;
}
.poem-body p { margin-bottom: 0.45em; }
.poem-body .ci-line { text-align: left; }
.poem-body .ci-group { margin-bottom: 0.9em; }
.poem-body .ci-group:last-child { margin-bottom: 0; }

.read-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-shrink: 0;
  padding: 8px 14px calc(8px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.nav-btn {
  flex: 1;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface);
  color: var(--text);
  font-size: 13.5px;
  font-family: inherit;
  cursor: pointer;
}
.nav-btn:active { background: var(--accent-soft); }
.read-nav .swap { flex: 1.4; font-size: 15px; letter-spacing: 2px; }
.nav-fonts { display: flex; gap: 4px; }
.font-btn {
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.font-btn:active { background: var(--accent-soft); }

/* ---------- Favorites page ---------- */
.fav-book-head {
  padding: 14px 18px 6px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 2px;
  color: var(--accent);
}

/* ---------- Settings sheet ---------- */
#sheet-mask {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 40;
}
#sheet-settings {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 41;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding: 10px 22px calc(22px + env(safe-area-inset-bottom));
  max-height: 78vh;
  overflow-y: auto;
  animation: sheetup 0.24s ease;
}
@keyframes sheetup { from { transform: translateY(40px); opacity: 0.4; } to { transform: none; opacity: 1; } }

.sheet-grab {
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--line);
  margin: 0 auto 12px;
}
#sheet-settings h2 {
  font-size: 16px;
  text-align: center;
  margin-bottom: 16px;
  letter-spacing: 3px;
}
.setting { margin-bottom: 18px; }
.setting-label {
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 8px;
  letter-spacing: 1px;
}
.setting-row {
  display: flex;
  align-items: center;
  gap: 12px;
}
#font-range { flex: 1; accent-color: var(--accent); }

.theme-row { display: flex; gap: 10px; }
.theme-opt {
  flex: 1;
  height: 44px;
  border-radius: 12px;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
}
.theme-opt.active {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

.danger {
  width: 100%;
  height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: transparent;
  color: #b3543f;
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
}
.danger:active { background: var(--accent-soft); }

.about {
  border-top: 1px dashed var(--line);
  padding-top: 14px;
  font-size: 11.5px;
  color: var(--text-dim);
  line-height: 1.9;
  text-align: center;
}
.about-dim { opacity: 0.75; }
.about-link {
  color: var(--accent);
  font-size: 13px;
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(80px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: rgba(30, 28, 22, 0.88);
  color: #f5f0e3;
  padding: 9px 20px;
  border-radius: 20px;
  font-size: 13px;
  z-index: 60;
  max-width: 80vw;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  animation: toastin 0.2s ease;
}
@keyframes toastin { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ---------- Immersive: hide bars while reading ---------- */
body[data-immersive="1"] .bar { display: none; }
body[data-immersive="1"] #read-body { padding-top: calc(18px + env(safe-area-inset-top)); }
body[data-immersive="1"] .read-nav { display: none; }
body[data-immersive="1"] #toast { bottom: calc(24px + env(safe-area-inset-bottom)); }
body[data-immersive="1"] .poem-body { cursor: pointer; }

@media (min-width: 720px) {
  body { background: var(--bg-soft); }
  #app { max-width: 720px; margin: 0 auto; box-shadow: 0 0 40px rgba(0,0,0,0.06); }
}
