:root {
  --bg: #f4ede0;
  --fg: #2b2620;
  --fg-dim: #7a7263;
  --accent: #a8763e;
  --card: #fbf7ef;
  --border: #e2d7c2;
  --zh: #6b5b3f;
  /* the "cloth-bound book" palette shared with the app icon */
  --green: #2c3f36;
  --crimson: #9a3a2e;
  --gilt: #b8955a;
  --on-green: #f4ecdc;
  --shadow: 60, 40, 20;
  --serif: "Iowan Old Style", Baskerville, Georgia, "Songti SC", "Noto Serif SC", serif;
  --serif-zh: "Songti SC", "Noto Serif SC", STSong, serif;
  --sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", sans-serif;
  --font-size: 19px;
  --line-height: 180%;
}
[data-theme="dark"] {
  --bg: #17181a;
  --fg: #e7e3da;
  --fg-dim: #9a9488;
  --accent: #d7a86e;
  --card: #222325;
  --border: #34363a;
  --zh: #c9b98f;
  --green: #3f5a4d;
  --crimson: #d0705f;
  --gilt: #c9a96a;
  --shadow: 0, 0, 0;
}
[data-theme="sepia"] {
  --bg: #ece0c8;
  --fg: #3a2f22;
  --fg-dim: #82735a;
  --accent: #8a5a2e;
  --card: #f4ead2;
  --border: #d9c9a3;
  --zh: #6e4e26;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--fg);
  font-family: -apple-system, "PingFang SC", "Noto Serif SC", Georgia, serif;
  -webkit-tap-highlight-color: transparent;
}

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

#topbar {
  display: flex; align-items: center; gap: 8px;
  padding: env(safe-area-inset-top,0) 10px 8px;
  padding-top: calc(env(safe-area-inset-top,0) + 8px);
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  z-index: 20;
}
#chapterTitle {
  flex: 1; font-size: 14px; color: var(--fg-dim);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#topbarActions { display: flex; gap: 2px; }
.iconbtn {
  background: none; border: none; color: var(--fg);
  font-size: 15px; padding: 8px 9px; border-radius: 8px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; min-width: 36px; height: 36px;
}
.iconbtn:active { background: var(--border); }
.iconbtn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.textbtn { font-family: var(--serif); font-size: 15px; }

#readerView { flex: 1; min-height: 0; display: flex; flex-direction: column; }

#reader { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
#readerInner {
  max-width: 640px; margin: 0 auto; padding: 22px 20px 60px;
  font-size: var(--font-size); line-height: var(--line-height);
}
.chapter-heading { margin: 4px 0 28px; text-align: center; }
.chapter-heading .num { color: var(--fg-dim); font-size: 13px; letter-spacing: 2px; }
.chapter-heading .title-en { font-size: 22px; font-weight: 600; margin-top: 4px; }
.chapter-heading .title-zh { color: var(--fg-dim); font-size: 15px; margin-top: 2px; }

p.para { margin: 0 0 20px; }
p.para .zh-inline {
  display: block; margin-top: 6px; color: var(--zh);
  font-size: 0.86em; line-height: 165%;
  font-family: "PingFang SC", "Noto Serif SC", serif;
}
.immersive p.para .zh-inline { display: none; }

.w { cursor: pointer; border-radius: 3px; padding: 0 1px; }
.w:hover { background: rgba(168,118,62,0.12); }
.w.hl { box-shadow: inset 0 -0.5em 0 var(--hlcolor, #ffe58a); }
.w.flash { animation: flash 1.1s ease; }
@keyframes flash { 0% { background: var(--accent); color:#fff;} 100% { background: transparent; } }

#progressBar { height: 2px; background: var(--border); }
#progressFill { height: 100%; width: 0%; background: var(--accent); transition: width .15s; }

#chapterNav {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 14px calc(env(safe-area-inset-bottom,0) + 8px);
  border-top: 1px solid var(--border); background: var(--bg);
}
.navbtn { background: none; border: none; color: var(--accent); font-size: 14px; padding: 6px 4px; }
#chapterIndicator { font-size: 12px; color: var(--fg-dim); }

/* drawer / TOC */
#scrim { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 30; }
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0; width: 82%; max-width: 320px;
  background: var(--card); z-index: 31; box-shadow: 2px 0 18px rgba(0,0,0,.25);
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top,0);
}
.drawer-head { display:flex; align-items:center; justify-content:space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); font-weight: 600; }
#chapterList { list-style: none; margin: 0; padding: 6px 0; overflow-y: auto; }
#chapterList li { padding: 11px 16px; font-size: 14px; cursor: pointer; border-bottom: 1px solid var(--border); }
#chapterList li.active { color: var(--accent); font-weight: 600; }
#chapterList li .zh { color: var(--fg-dim); font-size: 12px; }
.closebtn { background:none;border:none;font-size:16px;color:var(--fg-dim); }
.linkbtn { background:none; border:none; color:var(--accent); font-size:13px; padding:4px 0; cursor:pointer; }

/* ================= home ================= */
.home {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  background:
    radial-gradient(120% 60% at 50% -10%, rgba(var(--shadow), .06), transparent 60%),
    var(--bg);
}
.home-inner {
  max-width: 560px; margin: 0 auto;
  padding: calc(env(safe-area-inset-top, 0px) + 14px) 18px calc(env(safe-area-inset-bottom, 0px) + 40px);
}

/* lock screen */
.lock {
  flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px;
  background: #1a2645 url("art/scene.svg") center 70% / cover no-repeat;
}
.lock-card {
  width: 100%; max-width: 340px; text-align: center; color: #f7ecd9;
  padding: 30px 26px 24px; border-radius: 24px;
  background: rgba(10, 16, 34, .55); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  box-shadow: 0 0 0 1px rgba(232, 204, 148, .22), 0 30px 60px -30px rgba(0,0,0,.8);
}
.lock-card .brand-mark { width: 52px; height: 52px; border-radius: 13px; }
.lock-card .brand-over { margin-top: 14px; }
.lock-title { font-family: var(--serif-zh); font-weight: 600; font-size: 24px; letter-spacing: .14em; margin: 6px 0 0; }
.lock-sub { font-family: var(--serif-zh); font-size: 14px; line-height: 1.8; color: rgba(247,236,217,.8); margin: 10px 0 20px; }
.lock-sub em { font-family: var(--serif); font-size: 14px; color: rgba(247,236,217,.6); }
#lockInput {
  width: 100%; padding: 12px 16px; border-radius: 12px; font-size: 16px; text-align: center; letter-spacing: .2em;
  border: 1px solid rgba(232,204,148,.4); background: rgba(255,255,255,.08); color: #fff; outline: none;
}
#lockInput:focus { border-color: rgba(232,204,148,.85); }
.lock-card .lock-btn { margin-top: 14px; width: 100%; padding: 12px; background: #e9d6ae; color: #1f2a26; }
.lock-card.busy .lock-btn { opacity: .6; }
.lock-error { min-height: 20px; margin-top: 10px; font-size: 13px; color: #ffb4a6; }
.lock-note { font-size: 11px; color: rgba(247,236,217,.55); margin: 4px 0 0; letter-spacing: .04em; }

/* the dusk-sea poster: a banner on phones, a tall standing panel on desktop */
.poster {
  position: relative; overflow: hidden; border-radius: 22px;
  min-height: 236px; padding: 18px 20px 20px;
  display: flex; flex-direction: column; justify-content: space-between;
  color: #f7ecd9;
  background: #1a2645 url("art/scene.svg") center 60% / cover no-repeat;
  box-shadow: 0 22px 44px -26px rgba(12, 20, 40, .8);
  margin-bottom: 22px;
}
.poster::after {  /* keep text legible over the water */
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(180deg, rgba(8,14,30,.35) 0%, transparent 32%, transparent 55%, rgba(8,14,30,.55) 100%);
}
.poster > * { position: relative; z-index: 1; }
.poster-head { display: flex; align-items: center; justify-content: space-between; }
.poster .iconbtn { color: #f7ecd9; }
.poster .iconbtn:active { background: rgba(255,255,255,.12); }
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { width: 38px; height: 38px; border-radius: 10px; box-shadow: 0 0 0 1px rgba(255,240,215,.25), 0 6px 14px -6px rgba(0,0,0,.6); }
.brand-over {
  font-family: var(--sans); font-size: 10px; letter-spacing: .26em; text-transform: uppercase;
  color: rgba(247,236,217,.72);
}
.brand-name { font-family: var(--serif-zh); font-size: 20px; font-weight: 600; letter-spacing: .14em; margin-top: 1px; }

.home-greet {
  font-family: var(--serif-zh); font-size: 30px; font-weight: 600; letter-spacing: .06em;
  text-shadow: 0 2px 18px rgba(0,0,0,.35);
}
.home-date { font-family: var(--sans); font-size: 13px; color: rgba(247,236,217,.78); letter-spacing: .06em; margin-top: 4px; }
.poster-motto { display: none; }

@media (min-width: 920px) {
  .home-inner {
    max-width: 1320px;
    display: grid; grid-template-columns: minmax(360px, 5fr) 7fr; gap: 64px;
    padding: 32px 48px 48px;
  }
  .poster {
    position: sticky; top: 32px; align-self: start;
    height: calc(100dvh - 64px); min-height: 560px; margin: 0;
    padding: 28px 30px 30px; justify-content: flex-start;
    background-position: center bottom;
    border-radius: 28px;
  }
  .poster::after {
    background: linear-gradient(180deg, rgba(8,14,30,.45) 0%, rgba(8,14,30,.1) 38%, transparent 55%);
  }
  .poster-text { margin-top: 9vh; }
  .home-greet { font-size: 46px; }
  .home-date { font-size: 14px; margin-top: 8px; }
  .poster-motto {
    display: block; margin: 28px 0 0; padding-top: 22px; max-width: 18em;
    border-top: 1px solid rgba(232, 204, 148, .45);
    font-family: var(--serif-zh); font-size: 17px; line-height: 1.9; letter-spacing: .08em;
    color: rgba(247,236,217,.9);
  }
  .poster-motto em { font-family: var(--serif); font-size: 16px; letter-spacing: .01em; color: rgba(247,236,217,.7); }
  .home-main { padding-top: 6px; min-width: 0; }
  .continue { padding: 24px; gap: 26px; }
  .continue .cover { width: 132px; }
  .continue-title { font-size: 28px; }
  .quote-en { font-size: 23px; }
  .shelf-grid { grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); }
}

/* small caps labels used throughout */
.eyebrow {
  font-family: var(--sans); font-size: 11px; letter-spacing: .2em; text-transform: uppercase;
  color: var(--crimson); font-weight: 600;
}

/* continue reading */
.continue {
  display: flex; gap: 18px; padding: 18px; border-radius: 16px; background: var(--card);
  box-shadow: 0 1px 0 var(--border), 0 18px 40px -24px rgba(var(--shadow), .55);
  cursor: pointer;
}
.continue .cover { width: 96px; flex-shrink: 0; }
.continue-body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.continue-title { font-family: var(--serif-zh); font-size: 22px; font-weight: 600; margin-top: 8px; }
.continue-sub { font-family: var(--serif); font-style: italic; color: var(--fg-dim); font-size: 15px; margin-top: 1px; }
.continue-author { font-family: var(--sans); font-size: 10px; letter-spacing: .16em; text-transform: uppercase; color: var(--fg-dim); margin-top: 3px; }
.continue-chapter {
  font-size: 13px; color: var(--fg); margin-top: 12px; line-height: 1.5;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.bar { height: 3px; border-radius: 3px; background: var(--border); overflow: hidden; margin-top: 10px; }
.bar > i { display: block; height: 100%; background: var(--crimson); border-radius: 3px; }
.continue-meta { display: flex; justify-content: space-between; font-size: 12px; color: var(--fg-dim); margin-top: 6px; font-variant-numeric: tabular-nums; }
.btn-primary {
  margin-top: auto; align-self: flex-start; border: none; cursor: pointer;
  background: var(--green); color: var(--on-green);
  font-family: var(--sans); font-size: 14px; letter-spacing: .06em;
  padding: 9px 18px; border-radius: 999px;
  box-shadow: 0 6px 14px -8px rgba(var(--shadow), .8);
}
.continue-body .btn-primary { margin-top: 14px; }

/* line of the day */
.quote {
  margin: 34px 0 6px; padding: 26px 8px 24px; text-align: center; cursor: pointer;
  border-top: 1px solid var(--gilt); border-bottom: 1px solid var(--gilt);
  position: relative;
}
.quote::before, .quote::after {
  content: ""; position: absolute; left: 0; right: 0; height: 1px; background: var(--gilt); opacity: .45;
}
.quote::before { top: 3px; }
.quote::after { bottom: 3px; }
.quote-en { font-family: var(--serif); font-style: italic; font-size: 20px; line-height: 1.55; margin: 14px auto 0; max-width: 30em; text-wrap: balance; }
.quote-zh { font-family: var(--serif-zh); font-size: 15px; line-height: 1.8; color: var(--zh); margin: 12px auto 0; max-width: 32em; word-break: keep-all; overflow-wrap: anywhere; }
.quote-src { font-size: 12px; color: var(--fg-dim); margin-top: 14px; letter-spacing: .04em; }

/* stats */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); margin: 26px 0 8px; }
.stat { text-align: center; padding: 4px 0; }
.stat + .stat { border-left: 1px solid var(--border); }
.stat b { display: block; font-family: var(--serif); font-size: 28px; font-weight: 500; font-variant-numeric: lining-nums; }
.stat span { font-size: 12px; color: var(--fg-dim); letter-spacing: .12em; }

.home-sec { margin-top: 36px; }
.sec-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 16px; }
.sec-head h2 { margin: 0; font-family: var(--serif-zh); font-size: 19px; font-weight: 600; letter-spacing: .08em; }
.sec-sub { font-family: var(--serif); font-style: italic; color: var(--fg-dim); font-size: 14px; }

/* library */
.shelf-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 22px 18px; }
.shelf-item { cursor: pointer; }
.shelf-item-title { font-family: var(--serif-zh); font-size: 14px; margin-top: 10px; }
.shelf-item-meta { font-size: 11px; color: var(--fg-dim); margin-top: 2px; }
.shelf-item .bar { margin-top: 6px; height: 2px; }

/* typographic cloth-bound cover, generated from the book's color */
.cover {
  position: relative; aspect-ratio: 2 / 3; container-type: inline-size;
  background-color: var(--c, #7b2d26);
  background-image:
    linear-gradient(90deg, rgba(0,0,0,.35) 0, rgba(255,255,255,.10) 3.5%, rgba(0,0,0,.08) 7%, transparent 11%),
    repeating-linear-gradient(45deg, rgba(255,255,255,.025) 0 2px, transparent 2px 4px),
    linear-gradient(160deg, rgba(255,255,255,.10), rgba(0,0,0,.18));
  border-radius: 3px 7px 7px 3px;
  box-shadow: 0 1px 2px rgba(0,0,0,.25), 0 14px 22px -12px rgba(var(--shadow), .7);
  color: #ecd8ad;
}
.cover-frame {
  position: absolute; inset: 7% 7% 7% 12%;
  border: 1px solid rgba(232, 204, 148, .6);
  outline: 1px solid rgba(232, 204, 148, .25); outline-offset: 2.5cqw;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 0 6%;
}
.cover-title { font-family: var(--serif); font-size: 14cqw; line-height: 1.05; letter-spacing: .02em; }
.cover-rule { width: 30%; height: 1px; background: rgba(232, 204, 148, .7); margin: 8cqw 0 7cqw; }
.cover-zh { font-family: var(--serif-zh); font-size: 12cqw; letter-spacing: .25em; padding-left: .25em; }
.cover-author {
  position: absolute; bottom: 7%; left: 0; right: 0; text-align: center;
  font-family: var(--sans); font-size: 5.6cqw; letter-spacing: .3em; opacity: .8;
}

/* recent words */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: baseline; gap: 6px; max-width: 100%;
  padding: 6px 12px; border-radius: 999px; background: var(--card); border: 1px solid var(--border);
  font-size: 13px;
}
.chip b { font-family: var(--serif); font-weight: 600; font-size: 14px; }
.chip span { color: var(--fg-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 9em; }

.home-foot { margin-top: 48px; text-align: center; color: var(--fg-dim); font-family: var(--serif); font-style: italic; font-size: 14px; }
.home-foot .fleuron { display: block; color: var(--gilt); font-style: normal; font-size: 20px; margin-bottom: 8px; }
.home-foot .zh { font-family: var(--serif-zh); font-style: normal; font-size: 13px; margin-top: 4px; letter-spacing: .1em; }

/* floating popups */
.floatpop {
  position: fixed; z-index: 40; background: var(--card); color: var(--fg);
  border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0,0,0,.22); padding: 10px 12px;
  max-width: min(320px, 88vw); font-size: 14px;
}
.wp-head { display:flex; align-items:baseline; gap:8px; font-size: 16px; font-weight:700; margin-bottom: 4px;}
#wpPhon { font-size: 12px; color: var(--fg-dim); font-weight: 400; }
#wpZh { color: var(--zh); line-height: 150%; white-space: pre-wrap; }
#selectToolbar { display: flex; gap: 4px; padding: 6px; align-items: center; }
#selectToolbar button { border:none; background: transparent; color: var(--fg); font-size: 13px; padding: 6px 8px; border-radius: 6px; cursor:pointer; }
#selectToolbar button:active { background: var(--border); }
#selectToolbar button[data-color] { font-size: 18px; padding: 4px; }

#sheetScrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 40; }

/* bottom sheets */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  background: var(--card); border-radius: 16px 16px 0 0;
  box-shadow: 0 -4px 24px rgba(0,0,0,.25);
  max-height: 70vh; display: flex; flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom,0);
  transform: translateY(0); transition: transform .2s ease;
}
.sheet.hidden { transform: translateY(110%); pointer-events: none; }
.sheet-handle { width: 100%; padding: 14px 0 10px; margin: 0; cursor: pointer; }
.sheet-handle::before { content: ""; display: block; width: 40px; height: 4px; background: var(--border); border-radius: 2px; margin: 0 auto; }
.sheet-body { padding: 4px 20px 20px; overflow-y: auto; }
#zhSheetBody { font-size: 17px; line-height: 175%; color: var(--zh); }

textarea#noteInput {
  width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 10px;
  background: var(--bg); color: var(--fg); font-size: 15px; resize: vertical;
}
.sheet-actions { display: flex; gap: 10px; margin-top: 12px; justify-content: flex-end; }
.sheet-actions button, .filebtn {
  border: 1px solid var(--border); background: var(--bg); color: var(--fg);
  padding: 8px 16px; border-radius: 8px; font-size: 14px; cursor: pointer;
}
.filebtn { position: relative; overflow: hidden; display:inline-block; }
.filebtn input { position:absolute; inset:0; opacity:0; cursor:pointer; }

.setting-row { display:flex; align-items:center; justify-content:space-between; padding: 12px 0; border-bottom:1px solid var(--border); }
.setting-row input[type=range] { width: 60%; }
.theme-swatches { display:flex; gap: 8px; }
.theme-swatches button { border:1px solid var(--border); background:var(--bg); color:var(--fg); border-radius: 8px; padding:6px 10px; font-size:13px; }
.theme-swatches button.active { border-color: var(--accent); color: var(--accent); }

#searchInput {
  width: 100%; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); color: var(--fg); font-size: 15px; margin-bottom: 10px;
}
#searchResults .hit { padding: 10px 0; border-bottom: 1px solid var(--border); cursor: pointer; }
#searchResults .hit .meta { font-size: 11px; color: var(--fg-dim); }
#searchResults .hit .snip { font-size: 14px; margin-top: 2px; }
#searchResults .hit mark { background: #ffe58a; color: #222; border-radius: 2px; }

.tabs { display:flex; gap: 6px; margin-bottom: 8px; }
.tab { flex:1; padding:8px; border-radius: 8px; border:1px solid var(--border); background:var(--bg); color:var(--fg-dim); font-size:13px; }
.tab.active { color: var(--fg); border-color: var(--accent); }
#vocabList, #notesList { list-style:none; margin:0; padding:0; max-height: 46vh; overflow-y:auto; }
#vocabList li, #notesList li { padding: 10px 0; border-bottom: 1px solid var(--border); font-size:14px; }
#vocabList li b { font-size:15px; }
#vocabList li .zh, #notesList li .zh { color: var(--zh); font-size: 13px; }
#notesList li .meta { color: var(--fg-dim); font-size: 11px; margin-top:2px;}
#notesList li .del, #vocabList li .del { float:right; color:var(--fg-dim); cursor:pointer; }

.hidden { display: none !important; }

@media (min-width: 860px) {
  #readerInner { padding-top: 40px; }
  .sheet { left: 50%; right: auto; width: 480px; margin-left: -240px; border-radius: 16px; bottom: 24px; box-shadow: 0 10px 40px rgba(0,0,0,.35); }
}
