:root {
  --paper: #f7f2ea; --card: #fffdf9; --ink: #26221f; --soft: #7c7268; --line: #e4dbcf;
  --pink: #d9798f; --pink-soft: #f6dbe2; --blue-soft: #d9e7fb; --green: #6f8f6a; --red: #b5473f;
  --serif: "EB Garamond", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --st-planned: #cfc6ba; --st-writing: #e8b86b; --st-draft: #8fb3d9; --st-polished: #7fae84;
  --safe-t: env(safe-area-inset-top, 0px); --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #17191f; --card: #1f222a; --ink: #ece4d6; --soft: #9a948b; --line: #2f333d;
    --pink-soft: #4a2a33; --blue-soft: #233650; --red: #e0776e;
  }
}
:root[data-theme="dark"] {
  --paper: #17191f; --card: #1f222a; --ink: #ece4d6; --soft: #9a948b; --line: #2f333d;
  --pink-soft: #4a2a33; --blue-soft: #233650; --red: #e0776e;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
html { -webkit-text-size-adjust: 100%; }
body { background: var(--paper); color: var(--ink); font-family: var(--sans); font-size: 16px; line-height: 1.5; overflow-x: hidden; }
a { color: inherit; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { font-family: var(--serif); font-weight: 500; }
h1 { font-size: 34px; margin: 0; font-style: italic; line-height: 1.12; }
h2 { font-size: 22px; margin: 28px 0 10px; }
.muted { color: var(--soft); }
.small { font-size: 13px; }

/* ---------- chrome ---------- */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 6px;
  padding: calc(8px + var(--safe-t)) 10px 8px;
  background: color-mix(in srgb, var(--paper) 90%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.back { width: 40px; height: 40px; border-radius: 50%; border: 0; background: none; font-size: 30px; line-height: 1; flex: none; display: grid; place-items: center; text-decoration: none; padding-bottom: 4px; }
.top-t { flex: 1; min-width: 0; font-family: var(--serif); font-size: 19px; font-style: italic; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; border: 1px solid var(--line); background: var(--card); flex: none; }
body.at-home .back { visibility: hidden; width: 0; }
body.chapter .top, body.login .top { display: none; }

main { max-width: 760px; margin: 0 auto; padding: 18px 16px calc(40px + var(--safe-b)); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 8px 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--card); text-decoration: none; font-size: 15px;
}
.btn.primary { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn.sm { min-height: 34px; padding: 4px 12px; font-size: 13.5px; border-radius: 10px; }
.btn.block { width: 100%; }
.btn.danger { color: var(--red); }
.btn:disabled { opacity: .5; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 16px; }
.sp { flex: 1; }
.empty { text-align: center; color: var(--soft); padding: 40px 20px; }
.empty .btn { margin-top: 14px; }

/* ---------- login ---------- */
.login-box { min-height: 86vh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 4px; max-width: 340px; margin: 0 auto; }
.login-box svg { width: 26px; height: 66px; margin-bottom: 8px; }
.login-box form { width: 100%; margin-top: 22px; display: grid; gap: 10px; }
.login-box input { width: 100%; padding: 13px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); font-size: 16px; }
.login-box .err { color: var(--red); min-height: 1.5em; font-size: 14px; }

/* ---------- library ---------- */
.hello { margin: 8px 2px 18px; }
.hello .muted { margin-top: 4px; }
.books { display: grid; gap: 14px; }
.book { display: grid; grid-template-columns: 84px 1fr; gap: 16px; align-items: center; text-decoration: none; }
.cover {
  aspect-ratio: 2 / 3; border-radius: 4px 10px 10px 4px; background: var(--paper);
  border: 1px solid var(--line); box-shadow: inset 6px 0 0 color-mix(in srgb, var(--line) 70%, transparent), 0 6px 16px rgba(0,0,0,.08);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; padding: 8px 6px 8px 12px; text-align: center;
}
.cover svg { width: 10px; height: 26px; }
.cover b { font-family: var(--serif); font-style: italic; font-weight: 500; font-size: 12.5px; line-height: 1.15; }
.book .t { font-family: var(--serif); font-size: 23px; line-height: 1.15; }
.book .meta { color: var(--soft); font-size: 13px; margin-top: 2px; }
.bar { height: 6px; background: var(--line); border-radius: 99px; overflow: hidden; margin-top: 10px; }
.bar > i { display: block; height: 100%; background: var(--pink); border-radius: 99px; }
.foot { margin-top: 34px; display: flex; justify-content: center; }

/* ---------- book ---------- */
.book-head { text-align: center; padding: 6px 0 4px; }
.book-head svg { width: 16px; height: 42px; }
.book-head .muted { margin-top: 4px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 18px; }
.stat { padding: 10px 6px; text-align: center; }
.stat .v { font-family: var(--serif); font-size: 24px; line-height: 1.1; }
.stat .k { font-size: 10.5px; text-transform: uppercase; letter-spacing: .07em; color: var(--soft); margin-top: 2px; }
.next { margin-top: 12px; display: flex; align-items: center; gap: 12px; text-decoration: none; }
.next .t { flex: 1; min-width: 0; }
.next .t b { display: block; font-family: var(--serif); font-weight: 500; font-size: 21px; line-height: 1.2; }
.next .go { font-size: 22px; color: var(--pink); }
.days { display: flex; align-items: flex-end; gap: 4px; height: 70px; margin-top: 10px; }
.days .d { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px; height: 100%; justify-content: flex-end; }
.days .d i { width: 100%; background: var(--pink); border-radius: 4px 4px 2px 2px; min-height: 2px; opacity: .85; }
.days .d span { font-size: 10px; color: var(--soft); }

.pills { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; margin: 22px -16px 4px; padding: 0 16px; }
.pills::-webkit-scrollbar { display: none; }
.pills a, .pills button { flex: none; text-decoration: none; padding: 7px 14px; border-radius: 999px; border: 1px solid var(--line); color: var(--soft); font-size: 14px; background: var(--card); }
.pills .on { background: var(--ink); color: var(--paper); border-color: var(--ink); }

.s { display: inline-block; width: 9px; height: 9px; border-radius: 50%; flex: none; }
.s-planned { background: var(--st-planned); } .s-writing { background: var(--st-writing); }
.s-draft { background: var(--st-draft); } .s-polished { background: var(--st-polished); }
.legend { display: flex; gap: 12px; font-size: 12px; color: var(--soft); flex-wrap: wrap; margin: 12px 2px 0; }
.legend .s { margin-right: 5px; }

.part-h { margin: 22px 2px 8px; font-family: var(--serif); font-size: 18px; }
.part-h em { color: var(--soft); }
.list { background: var(--card); border: 1px solid var(--line); border-radius: 16px; overflow: hidden; }
.list > a, .list > .item { display: flex; align-items: center; gap: 12px; padding: 11px 14px; text-decoration: none; border-top: 1px solid var(--line); min-height: 56px; }
.list > :first-child { border-top: 0; }
.list .n { font-family: var(--serif); font-size: 20px; color: var(--soft); width: 26px; text-align: right; flex: none; }
.list .t { flex: 1; min-width: 0; }
.list .t b { display: block; font-weight: 500; font-family: var(--serif); font-size: 18px; line-height: 1.25; }
.list .t small { color: var(--soft); font-size: 12.5px; }
.list .go { color: var(--soft); font-size: 20px; }
.tag { display: inline-block; font-size: 11px; letter-spacing: .03em; padding: 0 7px; border-radius: 99px; background: var(--paper); border: 1px solid var(--line); color: var(--soft); margin-left: 4px; font-family: var(--sans); vertical-align: 2px; line-height: 1.6; }
.tag.past { background: var(--blue-soft); }

/* ---------- markdown notes ---------- */
.md { font-size: 15px; }
.md h1 { font-size: 26px; } .md h2 { font-size: 21px; } .md h3 { font-size: 18px; margin: 18px 0 6px; }
.md p, .md li { overflow-wrap: anywhere; }
.md ul, .md ol { padding-left: 20px; }
.md .tbl { overflow-x: auto; margin: 10px -16px; padding: 0 16px; }
.md table { border-collapse: collapse; font-size: 13.5px; min-width: 100%; }
.md th, .md td { border-bottom: 1px solid var(--line); padding: 6px 8px; text-align: left; vertical-align: top; }
.md blockquote { margin: 8px 0; padding-left: 12px; border-left: 2px solid var(--pink); color: var(--soft); }
.md code { font-size: 12.5px; background: var(--card); padding: 1px 5px; border-radius: 5px; overflow-wrap: anywhere; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 20px 0; }

/* ---------- chapter: four screens in a loop ---------- */
/* Pinned to the visible viewport (set from JS) so the format bar sits right above the keyboard. */
.wbox { position: fixed; left: 0; right: 0; top: var(--vvt, 0px); height: var(--vvh, 100dvh); z-index: 15; display: flex; flex-direction: column; background: var(--paper); }
body.chapter { overflow: hidden; }
.w-head { flex: none; display: flex; align-items: center; gap: 6px; padding: calc(6px + var(--safe-t)) 8px 4px; }
body.kb .w-head { padding-top: 6px; }
.w-head .ttl { flex: 1; min-width: 0; line-height: 1.2; }
.w-head .ttl b { display: block; font-family: var(--serif); font-weight: 500; font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.w-head .ttl small { color: var(--soft); font-size: 12px; }
.w-head .ttl small.warn { color: var(--red); }
.w-head .more { width: 40px; height: 40px; border-radius: 50%; border: 0; background: none; font-size: 22px; flex: none; }

.seg { flex: none; display: grid; grid-template-columns: repeat(4, 1fr); margin: 2px 12px 8px; padding: 3px; border-radius: 12px; background: color-mix(in srgb, var(--line) 55%, transparent); position: relative; }
.seg button { position: relative; z-index: 1; border: 0; background: none; padding: 7px 0; font-size: 13.5px; color: var(--soft); border-radius: 9px; }
.seg button.on { color: var(--ink); }
.seg .knob { position: absolute; z-index: 0; top: 3px; bottom: 3px; left: 3px; width: calc((100% - 6px) / 4); border-radius: 9px; background: var(--card); box-shadow: 0 1px 4px rgba(0,0,0,.08); transition: transform .28s ease; }
body.kb .seg { display: none; }

.track { flex: 1; min-height: 0; position: relative; overflow: hidden; touch-action: pan-y; border-top: 1px solid var(--line); }
.panel { position: absolute; inset: 0; touch-action: pan-y; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; will-change: transform; }
.panel.anim { transition: transform .3s cubic-bezier(.2, .8, .2, 1); }
.panel .inner { max-width: 760px; margin: 0 auto; padding: 18px 16px 48px; }
.panel.write { display: flex; flex-direction: column; overflow: hidden; }
.panel-label { font-size: 11.5px; letter-spacing: .14em; text-transform: uppercase; color: var(--soft); text-align: center; margin: 0 0 14px; }

.editor {
  flex: 1; min-height: 0; width: 100%; max-width: 760px; margin: 0 auto; display: block;
  resize: none; overflow-y: auto; -webkit-overflow-scrolling: touch; touch-action: pan-y;
  font-family: var(--serif); font-size: var(--ed-size, 20px); line-height: 1.6;
  background: transparent; color: var(--ink); border: 0; outline: none; border-radius: 0;
  padding: 16px 18px 60px; caret-color: var(--pink);
}
.kbar {
  flex: none; display: flex; gap: 6px; align-items: center; overflow-x: auto; scrollbar-width: none;
  padding: 8px 10px calc(8px + var(--safe-b)); background: var(--card); border-top: 1px solid var(--line);
}
body.kb .kbar { padding-bottom: 8px; }
body.kb .kbar .done { display: block; }
.kbar::-webkit-scrollbar { display: none; }
.kbar button { flex: none; min-width: 42px; height: 40px; padding: 0 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--paper); font-size: 15px; }
.kbar .den { background: var(--blue-soft); }
.kbar .her { background: var(--pink-soft); }
.kbar .done { display: none; margin-left: auto; background: var(--ink); color: var(--paper); border-color: var(--ink); }

.beats { margin: 0; padding-left: 20px; font-size: 16px; }
.ask-row { display: flex; justify-content: center; margin: 0 0 14px; }
.ask-card { margin-bottom: 14px; background: color-mix(in srgb, var(--pink-soft) 45%, var(--card)); }
.ask-card p { margin: 6px 0; white-space: pre-wrap; }
.ask-card a { color: inherit; }
.outline-ed { display: block; width: 100%; min-height: 40vh; resize: none; overflow: hidden; touch-action: pan-y; font: 15px/1.55 var(--sans); color: var(--ink); background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px; outline: none; }
.outline-ed:focus { border-color: var(--soft); }
.panel .row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.choice { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.choice button { border: 1px solid var(--line); background: var(--paper); border-radius: 12px; padding: 10px; text-align: left; font-size: 15px; }
.choice button small { display: block; color: var(--soft); font-size: 12px; }
.choice button.on { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); background: var(--card); }
.beats li { margin: 8px 0; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; margin-bottom: 16px; }

/* ---------- book pages ---------- */
.page { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 34px 22px 40px; box-shadow: 0 8px 26px rgba(0,0,0,.05); }
.page.hani { background: color-mix(in srgb, var(--blue-soft) 22%, var(--card)); }
.opener { text-align: center; margin-bottom: 28px; }
.opener .label { font-size: 11.5px; letter-spacing: .3em; text-transform: uppercase; color: var(--soft); }
.opener h2 { margin: 6px 0 0; font-style: italic; font-size: 28px; }
.prose { font-family: var(--serif); font-size: var(--read-size, 19px); line-height: 1.6; }
.prose p { margin: 0; text-indent: 1.4em; text-align: justify; hyphens: auto; -webkit-hyphens: auto; }
.prose p.first, .prose p.after { text-indent: 0; }
.prose p.first::first-letter { -webkit-initial-letter: 2 1; initial-letter: 2 1; margin-right: .06em; font-weight: 500; }
.prose .verse { text-indent: 0; text-align: left; margin: .6em 1em; font-style: italic; }
.prose .msgs { margin: .7em 0 .7em .8em; padding-left: 12px; border-left: 1.5px solid var(--pink-soft); font-size: .9em; }
.prose .msgs p { text-indent: 0; text-align: left; font-style: italic; }
.prose .brk { text-align: center; color: var(--soft); margin: 1em 0; letter-spacing: .6em; }
mark { color: inherit; padding: 0 .2em; margin: 0 -.06em; border-radius: .6em .25em .7em .3em; -webkit-box-decoration-break: clone; box-decoration-break: clone; }
mark.den { background: linear-gradient(100deg, transparent .6%, rgba(110,170,255,.55) 2.4%, rgba(110,170,255,.32) 8%, rgba(110,170,255,.3) 92%, rgba(110,170,255,.5) 97%, transparent 99%); }
mark.her { background: linear-gradient(100deg, transparent .6%, rgba(255,120,170,.5) 2.4%, rgba(255,120,170,.3) 8%, rgba(255,120,170,.28) 92%, rgba(255,120,170,.46) 97%, transparent 99%); }

/* ---------- sheets ---------- */
.scrim { position: fixed; inset: 0; background: rgba(20, 16, 12, .35); z-index: 40; opacity: 0; pointer-events: none; transition: opacity .2s; }
.scrim.on { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  max-height: 84vh; overflow-y: auto; overscroll-behavior: contain;
  background: var(--card); border-radius: 20px 20px 0 0; border: 1px solid var(--line); border-bottom: 0;
  padding: 10px 18px calc(22px + var(--safe-b));
  transform: translateY(105%); transition: transform .25s ease;
  max-width: 760px; margin: 0 auto;
}
.sheet.on { transform: none; }
.sheet .grab { width: 40px; height: 5px; border-radius: 9px; background: var(--line); margin: 0 auto 12px; }
.sheet h3 { margin: 6px 0 6px; font-size: 21px; }
.sheet h4 { margin: 18px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--soft); font-weight: 600; }
.sheet p { margin: 6px 0; }
.sheet label { display: block; font-size: 12.5px; color: var(--soft); margin: 12px 0 4px; }
.sheet textarea { width: 100%; padding: 11px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--paper); font: 16px/1.45 var(--sans); color: var(--ink); resize: vertical; }
.sheet input, .sheet select { width: 100%; padding: 11px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--paper); font-size: 16px; }
.sheet .actions { display: grid; gap: 8px; margin-top: 16px; }
.sheet .row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.versions { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.versions .item { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; border-top: 1px solid var(--line); font-size: 14px; }
.versions .item:first-child { border-top: 0; }

#toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); z-index: 60;
  transform: translateX(-50%) translateY(16px); opacity: 0; max-width: calc(100vw - 32px);
  background: var(--ink); color: var(--paper); padding: 9px 16px; border-radius: 999px; font-size: 14px;
  transition: .25s; pointer-events: none; text-align: center;
}
#toast.on { opacity: 1; transform: translateX(-50%); }
body.chapter #toast { top: calc(var(--vvt, 0px) + 96px + var(--safe-t)); bottom: auto; }

@media (min-width: 700px) {
  .page { padding: 56px 64px 64px; }
  .book { grid-template-columns: 110px 1fr; }
}
