:root {
  --bg: #f6f1e7;
  --surface: #fffdf8;
  --ink: #1f1a14;
  --ink-2: #5b5247;
  --ink-3: #8c8274;
  --rule: #e4dccd;
  --accent: #8b2e24;
  --accent-soft: #f1e1d6;
  --mark: rgba(139, 46, 36, 0.10);
  --shadow: 0 1px 2px rgba(60, 40, 20, .06), 0 8px 24px rgba(60, 40, 20, .06);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "New York", ui-serif, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", system-ui, sans-serif;
  --poem-size: 18px;
  --tabbar-h: 58px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16130f; --surface: #1f1b16; --ink: #ece4d6; --ink-2: #bdb2a1; --ink-3: #8a8072;
    --rule: #332d25; --accent: #e0957a; --accent-soft: #3a2621; --mark: rgba(224, 149, 122, .14);
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.25);
  }
}
:root[data-theme="dark"] {
  --bg: #16130f; --surface: #1f1b16; --ink: #ece4d6; --ink-2: #bdb2a1; --ink-3: #8a8072;
  --rule: #332d25; --accent: #e0957a; --accent-soft: #3a2621; --mark: rgba(224, 149, 122, .14);
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.25);
}
:root[data-size="s"] { --poem-size: 16px; }
:root[data-size="l"] { --poem-size: 20px; }
:root[data-size="xl"] { --poem-size: 22px; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--accent); }
button { font: inherit; color: inherit; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- login ---------- */
.login {
  min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 24px; text-align: center;
}
.login-mark { font-size: 54px; color: var(--accent); line-height: 1; }
.login h1 { font: 600 34px/1.1 var(--serif); margin: 12px 0 4px; letter-spacing: -.01em; }
.login-sub { color: var(--ink-2); margin: 0 0 28px; font-family: var(--serif); font-style: italic; }
.login form { width: 100%; max-width: 320px; display: grid; gap: 10px; }
.login input {
  font: 17px var(--sans); padding: 14px 16px; border-radius: 12px; border: 1px solid var(--rule);
  background: var(--surface); color: var(--ink); outline: none;
}
.login input:focus { border-color: var(--accent); }
.login button, .btn {
  font: 600 16px var(--sans); padding: 13px 18px; border-radius: 12px; border: 0;
  background: var(--accent); color: #fff; cursor: pointer;
}
:root[data-theme="dark"] .btn, :root[data-theme="dark"] .login button { color: #1a120e; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .btn, :root:not([data-theme="light"]) .login button { color: #1a120e; } }
.btn.secondary { background: var(--accent-soft); color: var(--accent); }
.btn.small { padding: 8px 12px; font-size: 14px; border-radius: 10px; }
.btn:disabled { opacity: .5; }
.login-error { color: var(--accent); margin: 4px 0 0; }

/* ---------- layout ---------- */
.view {
  max-width: 680px; margin: 0 auto; outline: none;
  padding: calc(env(safe-area-inset-top) + 18px) 20px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 32px);
}
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around;
  padding: 6px 8px calc(env(safe-area-inset-bottom) + 4px);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: saturate(1.6) blur(18px); backdrop-filter: saturate(1.6) blur(18px);
  border-top: 0.5px solid var(--rule);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 1px;
  text-decoration: none; color: var(--ink-3); font-size: 10.5px; font-weight: 500; padding: 4px 0;
}
.tabbar a .ti { font-size: 20px; line-height: 26px; }
.tabbar a.active { color: var(--accent); }

.page-head { margin: 4px 0 18px; }
.eyebrow { font: 600 12px/1 var(--sans); letter-spacing: .09em; text-transform: uppercase; color: var(--ink-3); }
.page-title { font: 600 32px/1.1 var(--serif); margin: 6px 0 0; letter-spacing: -.01em; }

/* slot switcher */
.slots {
  position: sticky; top: env(safe-area-inset-top); z-index: 10;
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 4px;
  margin: 0 -4px 26px; border-radius: 12px;
  background: color-mix(in srgb, var(--rule) 70%, var(--bg));
}
.slots button {
  border: 0; background: transparent; padding: 8px 4px; border-radius: 9px; cursor: pointer;
  font: 600 13px/1.2 var(--sans); color: var(--ink-2);
}
.slots button[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

/* ---------- poem ---------- */
.poem-head { margin-bottom: 22px; }
.poem-head .eyebrow { color: var(--accent); }
.poem-title-row { display: flex; align-items: flex-start; gap: 12px; }
.poem-title { font: 600 30px/1.15 var(--serif); margin: 8px 0 6px; flex: 1; letter-spacing: -.01em; text-wrap: balance; }
.poem-author { font: italic 19px/1.3 var(--serif); color: var(--ink-2); margin: 0; }
.poem-author .year { font-style: normal; color: var(--ink-3); font-size: 15px; }
.fav {
  border: 0; background: none; padding: 12px 2px 8px 8px; font-size: 26px; line-height: 1; cursor: pointer; color: var(--ink-3);
}
.fav[aria-pressed="true"] { color: var(--accent); }

.poem {
  font: var(--poem-size)/1.62 var(--serif); margin: 0 0 10px; padding: 4px 0 4px 0;
  counter-reset: none; position: relative;
}
.poem .ln {
  position: relative; white-space: pre-wrap; padding-left: 1.6em; text-indent: -1.6em;
  padding-right: 1.5em; min-height: 1em;
}
.poem .ln.gap { height: .85em; }
.poem .ln .num {
  position: absolute; right: 0; top: .32em; text-indent: 0;
  font: 11px var(--sans); color: var(--ink-3); font-variant-numeric: tabular-nums;
}
.poem .fn {
  background: var(--mark); border-radius: 3px; cursor: pointer; padding: 0 1px;
  box-decoration-break: clone; -webkit-box-decoration-break: clone;
}
.poem .fn sup, .poem .fn-tail sup {
  font: 600 .55em var(--sans); color: var(--accent); margin-left: 1px; vertical-align: .6em; line-height: 0;
}
.poem .fn-tail { cursor: pointer; }
.excerpt-note { font-size: 14px; color: var(--ink-3); font-style: italic; }
.source { font-size: 13px; color: var(--ink-3); margin: 14px 0 0; }
.source a { color: var(--ink-2); }

/* ---------- commentary ---------- */
.commentary { margin-top: 30px; border-top: 1px solid var(--rule); padding-top: 22px; }
.glance {
  font: 18px/1.55 var(--serif); color: var(--ink); margin: 0 0 18px;
  padding: 16px 18px; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow);
}
.glance .eyebrow { display: block; margin-bottom: 8px; color: var(--accent); }
details.sec { border-bottom: 1px solid var(--rule); }
details.sec > summary {
  list-style: none; cursor: pointer; padding: 15px 2px; display: flex; align-items: center; justify-content: space-between;
  font: 600 17px var(--serif);
}
details.sec > summary::-webkit-details-marker { display: none; }
details.sec > summary::after { content: "+"; font: 400 22px var(--sans); color: var(--ink-3); transition: transform .2s; }
details.sec[open] > summary::after { transform: rotate(45deg); }
.sec-body { padding: 0 2px 18px; font: 17px/1.6 var(--serif); color: var(--ink); }
.sec-body p { margin: 0 0 .9em; }
.sec-body h4 { font: 600 15px var(--sans); margin: 1.1em 0 .3em; color: var(--ink); }
.sec-body h4:first-child { margin-top: 0; }
.sec-body blockquote {
  margin: .4em 0 .5em; padding: 2px 0 2px 14px; border-left: 2px solid var(--accent);
  font-style: italic; color: var(--ink-2); white-space: pre-line;
}
.sec-body .meta { font: 13px var(--sans); color: var(--ink-3); }
.notes-list { list-style: none; padding: 0; margin: 0; }
.notes-list li { display: grid; grid-template-columns: 2em 1fr; gap: 4px; margin-bottom: .8em; }
.notes-list .n { font: 600 13px/1.9 var(--sans); color: var(--accent); }
.notes-list .ph { font-style: italic; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.tag { font: 500 12px var(--sans); padding: 4px 10px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); }
.readnext li { margin-bottom: .7em; }

.working {
  display: flex; gap: 12px; align-items: center; padding: 16px 18px; border-radius: 14px; background: var(--surface);
  box-shadow: var(--shadow); color: var(--ink-2); font: italic 16px var(--serif);
}
.spinner {
  width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--rule); border-top-color: var(--accent);
  animation: spin 0.9s linear infinite; flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }
.error-box { padding: 14px 16px; border-radius: 12px; background: var(--accent-soft); color: var(--ink); font-size: 15px; }
.error-box .btn { margin-top: 10px; }

.card {
  background: var(--surface); border-radius: 16px; padding: 20px; box-shadow: var(--shadow); margin-bottom: 14px;
}
.card h3 { font: 600 21px/1.25 var(--serif); margin: 0 0 8px; }
.card p { margin: 0 0 12px; color: var(--ink-2); }
.card .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.card .btn { text-decoration: none; display: inline-block; }

/* ---------- lists ---------- */
.month { font: 600 13px var(--sans); letter-spacing: .08em; text-transform: uppercase; color: var(--ink-3); margin: 26px 0 8px; }
.list { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); overflow: hidden; }
.list > li + li { border-top: 1px solid var(--rule); }
.list a { display: block; padding: 14px 16px; text-decoration: none; color: var(--ink); }
.list .d { font: 600 14px var(--sans); color: var(--accent); margin-bottom: 4px; }
.list .t { font: 17px/1.35 var(--serif); }
.list .a { font: italic 15px var(--serif); color: var(--ink-2); }
.list .row-sub { display: block; font: 14px/1.45 var(--serif); color: var(--ink-2); }
.empty { text-align: center; color: var(--ink-3); padding: 50px 20px; font-family: var(--serif); font-style: italic; }

/* settings */
.set-group { background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); margin-bottom: 22px; overflow: hidden; }
.set-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px; min-height: 52px; }
.set-row + .set-row { border-top: 1px solid var(--rule); }
.set-row .lbl { font-weight: 500; }
.set-row .sub { display: block; font-size: 13px; color: var(--ink-3); font-weight: 400; }
.set-note { font-size: 13px; color: var(--ink-3); margin: -14px 4px 22px; }
.seg { display: inline-flex; background: color-mix(in srgb, var(--rule) 70%, var(--bg)); border-radius: 9px; padding: 2px; }
.seg button { border: 0; background: none; padding: 5px 10px; border-radius: 7px; font-size: 14px; cursor: pointer; color: var(--ink-2); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.switch { position: relative; width: 51px; height: 31px; flex: none; }
.switch input { opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; position: relative; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 99px; background: var(--rule); transition: background .2s; }
.switch span::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.2); transition: transform .2s;
}
.switch input:checked + span { background: #34c759; }
.switch input:checked + span::after { transform: translateX(20px); }
.switch input:disabled + span { opacity: .5; }

/* ---------- sheet (footnote popover) ---------- */
.sheet { position: fixed; inset: 0; z-index: 50; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(20, 14, 8, .35); animation: fade .2s; }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 70dvh; overflow: auto;
  background: var(--surface); border-radius: 18px 18px 0 0; box-shadow: 0 -8px 30px rgba(0,0,0,.15);
  padding: 10px 22px calc(env(safe-area-inset-bottom) + 18px); animation: rise .25s cubic-bezier(.2,.8,.2,1);
  max-width: 680px; margin: 0 auto;
}
.sheet-grip { width: 38px; height: 5px; border-radius: 3px; background: var(--rule); margin: 0 auto 14px; }
.sheet-close { display: block; width: 100%; margin-top: 16px; padding: 12px; border: 0; border-radius: 12px; background: var(--accent-soft); color: var(--accent); font-weight: 600; cursor: pointer; }
#sheet-body .ph { font: italic 19px/1.4 var(--serif); margin: 0 0 4px; }
#sheet-body .meta { font: 13px var(--sans); color: var(--ink-3); margin-bottom: 10px; }
#sheet-body p { font: 17px/1.6 var(--serif); margin: 0; }
@keyframes rise { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }

.loading { display: flex; justify-content: center; padding: 80px 0; }
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 99px; font-size: 14px; z-index: 60;
  animation: fade .2s; max-width: calc(100% - 32px);
}
@media (min-width: 700px) {
  .poem-title { font-size: 36px; }
}
