/* ---------- Theme tokens ---------- */
:root {
  color-scheme: light;
  --bg: #f7f3ea;
  --surface: #fffdf8;
  --surface-2: #efe8d9;
  --text: #1f1a14;
  --muted: #6b6155;
  --border: #ddd3c1;
  --rubric: #a3201b;
  --accent: #7a1f1a;
  --focus: #b8860b;
  --missing-bg: #fbf1dc;
  --missing-border: #e2c58a;
  --lit-violet: #6a3d9a;
  --lit-white: #c9a94b;
  --lit-red: #b3261e;
  --lit-green: #2f7a3a;
  --lit-rose: #d0739a;
  --lit-black: #333;
  --text-size: 19px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Times New Roman", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --tabbar-h: 60px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #16130f;
    --surface: #1f1b16;
    --surface-2: #2a251e;
    --text: #ece4d6;
    --muted: #a99d8c;
    --border: #3a332a;
    --rubric: #ef7f73;
    --accent: #e9a59c;
    --missing-bg: #2b2418;
    --missing-border: #6b5428;
    --lit-violet: #a782d6;
    --lit-white: #e2cc84;
    --lit-red: #ec6d62;
    --lit-green: #6fbf79;
    --lit-rose: #eb9bbd;
    --lit-black: #aaa;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #16130f;
  --surface: #1f1b16;
  --surface-2: #2a251e;
  --text: #ece4d6;
  --muted: #a99d8c;
  --border: #3a332a;
  --rubric: #ef7f73;
  --accent: #e9a59c;
  --missing-bg: #2b2418;
  --missing-border: #6b5428;
  --lit-violet: #a782d6;
  --lit-white: #e2cc84;
  --lit-red: #ec6d62;
  --lit-green: #6fbf79;
  --lit-rose: #eb9bbd;
  --lit-black: #aaa;
}

/* True black: for OLED phones and praying in the dark. */
:root[data-theme="black"] {
  color-scheme: dark;
  --bg: #000;
  --surface: #0c0c0c;
  --surface-2: #1a1a1a;
  --text: #e6e0d6;
  --muted: #958b7e;
  --border: #262626;
  --rubric: #e8776b;
  --accent: #e9a59c;
  --missing-bg: #15110a;
  --missing-border: #4d3d1d;
  --lit-violet: #a782d6;
  --lit-white: #e2cc84;
  --lit-red: #ec6d62;
  --lit-green: #6fbf79;
  --lit-rose: #eb9bbd;
  --lit-black: #aaa;
}
:root[data-theme="black"] .topbar { background: #000; backdrop-filter: none; -webkit-backdrop-filter: none; }

/* ---------- Reading font (Settings → Font) ---------- */
:root[data-font="literata"] { --serif: 'R-literata', Georgia, serif; }
:root[data-font="garamond"] { --serif: 'R-garamond', Garamond, Georgia, serif; --font-scale: 1.1; }
:root[data-font="sourceserif"] { --serif: 'R-sourceserif', Georgia, serif; }
:root[data-font="atkinson"] { --serif: 'R-atkinson', var(--sans); --font-scale: .96; }
:root[data-font="sans"] { --serif: var(--sans); --font-scale: .96; }

/* ---------- Base ---------- */
* { box-sizing: border-box; }
/* The page background also fills any area the phone draws behind its own bars. */
html {
  -webkit-text-size-adjust: 100%;
  background: var(--bg);
  /* Scrollbar: thin and almost transparent, so it doesn't distract from the text. */
  scrollbar-width: thin;
  scrollbar-color: color-mix(in srgb, var(--muted) 14%, transparent) transparent;
}
::-webkit-scrollbar { width: 3px; height: 3px; background: transparent; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: color-mix(in srgb, var(--muted) 14%, transparent); border-radius: 3px; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  line-height: 1.45;
  min-height: 100dvh;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
a { color: inherit; }
button, select, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
main { max-width: 720px; margin: 0 auto; padding: 0 16px 32px; outline: none; }
.loading { color: var(--muted); text-align: center; padding: 48px 0; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar .title { flex: 1; text-align: center; min-width: 0; }
.topbar .title strong { display: block; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .title small { display: block; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  width: 40px; height: 40px; flex: none;
  display: inline-grid; place-items: center;
  border: 0; border-radius: 10px; background: transparent; cursor: pointer; text-decoration: none;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg, .tabbar svg, .chev svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn[hidden] { visibility: hidden; display: inline-grid; }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: center;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.tabbar a {
  flex: 1; max-width: 160px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  text-decoration: none; color: var(--muted); font-size: 11px;
}
.tabbar a[aria-current="page"] { color: var(--accent); }
body.reading .tabbar { display: none; }
body.reading { padding-bottom: env(safe-area-inset-bottom); }

/* ---------- Day view ---------- */
.day-head {
  margin: 20px 0 16px;
  padding: 18px 18px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 6px solid var(--lit, var(--border));
  border-radius: 14px;
}
.lit-violet { --lit: var(--lit-violet); }
.lit-white { --lit: var(--lit-white); }
.lit-red { --lit: var(--lit-red); }
.lit-green { --lit: var(--lit-green); }
.lit-rose { --lit: var(--lit-rose); }
.lit-black { --lit: var(--lit-black); }
.day-head .meta { color: var(--muted); font-size: 13px; margin: 0 0 6px; }
.day-head h1 { font-family: var(--serif); font-weight: 600; font-size: 24px; line-height: 1.2; margin: 0 0 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted);
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--lit, var(--muted)); flex: none; }
.note { font-size: 13px; color: var(--muted); margin: 10px 0 0; }
.choice { margin-top: 14px; }
.choice label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.choice select { width: 100%; padding: 10px; border-radius: 10px; border: 1px solid var(--border); background-color: var(--bg); }

.hours { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.hours a {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px; border-radius: 12px;
  background: var(--surface); border: 1px solid var(--border);
  text-decoration: none;
}
.hours a:hover { border-color: var(--accent); }
.hours .hname { flex: 1; font-family: var(--serif); font-size: 18px; }
.hours .hsub { display: block; font-family: var(--sans); font-size: 12px; color: var(--muted); }
.hours .chev { color: var(--muted); }
.section-title { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 22px 4px 8px; }

/* ---------- Office text ---------- */
.office, .font-preview { font-family: var(--serif); font-size: calc(var(--text-size) * var(--font-scale, 1)); line-height: 1.55; padding-top: 8px; }
.font-preview { margin: 0; padding: 4px 0 14px; border-bottom: 1px solid var(--border); }
.font-preview .mark { color: var(--rubric); }
.office-head { text-align: center; margin: 18px 0 8px; }
.office-head h1 { font-size: 1.35em; margin: 0; color: var(--rubric); font-weight: 600; }
.office-head p { margin: 4px 0 0; color: var(--muted); font-size: .8em; }
.office h3.part {
  font-size: .72em; text-transform: uppercase; letter-spacing: .12em;
  color: var(--rubric); font-weight: 600; margin: 1.6em 0 .5em;
}
.office .stanza { margin: 0 0 .8em; }
.office .line, .office .vr, .office .indent, .office .rubric { display: block; }
.office .indent { padding-left: 1.5em; }
.office .psalm .line, .office .vr { padding-left: 1.5em; text-indent: -1.5em; }
.office .rubric { color: var(--rubric); font-style: italic; font-size: .85em; }
.office p.rubric.block { margin: .6em 0; }
.office .mark { color: var(--rubric); }
.office .antiphon { margin: .6em 0; }
.office .antiphon .label { color: var(--rubric); font-weight: 600; font-size: .85em; }
.office .antiphon.repeat { margin-top: -.2em; }
.office .psalm-title { font-size: .9em; margin: 1em 0 .1em; font-weight: 600; }
.office .psalm-subtitle { font-size: .8em; font-style: italic; color: var(--muted); margin: 0 0 .6em; }
.office .gloria { margin-bottom: .4em; }
.office .reading .ref { color: var(--rubric); font-size: .85em; margin: 0 0 .3em; }
.office .reading .author, .office .reading .reading-title { font-size: .85em; margin: 0 0 .3em; font-style: italic; }
.office .src { font-family: var(--sans); font-size: 11px; color: var(--muted); margin: -.4em 0 .6em; opacity: .8; }
.office .missing {
  font-family: var(--sans); font-size: 14px;
  background: var(--missing-bg); border: 1px dashed var(--missing-border);
  border-radius: 10px; padding: 8px 12px; margin: .5em 0;
}
.office .missing summary { cursor: pointer; color: var(--muted); }
.office .missing-label { color: var(--text); font-weight: 600; }
.office .missing ol { margin: 6px 0 4px; padding-left: 22px; }
.office .missing code { font-size: 12px; word-break: break-all; }
.office { padding-bottom: 48px; }

/* ---------- Calendar ---------- */
.month-nav { display: flex; align-items: center; justify-content: space-between; margin: 16px 0 8px; }
.month-nav h1 { font-family: var(--serif); font-size: 22px; margin: 0; }
.cal-list { list-style: none; padding: 0; margin: 0; }
.cal-list a {
  display: grid; grid-template-columns: 44px 1fr; gap: 10px; align-items: center;
  padding: 10px 6px; border-bottom: 1px solid var(--border); text-decoration: none;
}
.cal-list li.today a { background: var(--surface); border-radius: 10px; }
.cal-num { text-align: center; }
.cal-num b { display: block; font-size: 18px; font-family: var(--serif); }
.cal-num small { color: var(--muted); font-size: 11px; text-transform: uppercase; }
.cal-title { font-size: 15px; display: flex; gap: 8px; align-items: baseline; }
.cal-title .dot { transform: translateY(-1px); }
.cal-rank { display: block; font-size: 12px; color: var(--muted); margin-left: 17px; }
.cal-list .sunday .cal-title { font-weight: 600; }

/* ---------- Settings ---------- */
.settings h1 { font-family: var(--serif); font-size: 24px; margin: 20px 0 8px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 4px 16px; margin: 0 0 14px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.row:last-child { border-bottom: 0; }
.row label, .row .lbl { flex: 1; }
.row small { display: block; color: var(--muted); font-size: 12px; }
.row select, .row input[type="text"], .row input[type="date"] {
  padding: 8px 10px; border-radius: 9px; border: 1px solid var(--border); background-color: var(--bg); max-width: 55%;
}
/* Dropdowns: our own caret, with room on the right, in the theme's colours. */
select {
  -webkit-appearance: none; appearance: none;
  padding-right: 34px !important;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 52%, calc(100% - 13px) 52%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  text-overflow: ellipsis; white-space: nowrap; overflow: hidden;
  cursor: pointer;
}
.row input[type="range"] { width: 140px; accent-color: var(--accent); }
.row input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 7px 10px; cursor: pointer; font-size: 14px; }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--bg); }
.btn {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 10px;
  padding: 9px 14px; cursor: pointer; font-size: 14px;
}
a.btn { text-decoration: none; display: inline-block; }
.btn.primary { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.local-list { list-style: none; margin: 0; padding: 0; }
.local-list li { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.local-list li span { flex: 1; }
.local-form { display: grid; gap: 8px; padding: 12px 0; }
.local-form input, .local-form select { padding: 8px 10px; border-radius: 9px; border: 1px solid var(--border); background-color: var(--bg); width: 100%; }
.local-form .two { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.local-form .field-label { font-size: 13px; color: var(--muted); margin-bottom: -4px; }
.local-form .field-label small { font-size: 12px; }
.local-form input[type="date"] { min-height: 38px; }
.fine { color: var(--muted); font-size: 12px; margin: 6px 4px 18px; }

/* ---------- Praying an Hour: keep the controls out of the way ---------- */
body.reading .topbar {
  border-bottom-color: transparent;
  background: var(--bg);
  backdrop-filter: none; -webkit-backdrop-filter: none;
  padding-top: calc(2px + env(safe-area-inset-top)); padding-bottom: 2px;
  transition: transform .25s ease, opacity .25s ease;
}
body.reading .topbar .icon-btn { color: var(--muted); opacity: .6; width: 36px; height: 36px; }
body.reading .topbar .icon-btn svg { width: 19px; height: 19px; }
body.reading .topbar .title strong { font-size: 13px; font-weight: 500; color: var(--muted); }
body.reading .topbar .title small { display: none; }
/* Slides away while scrolling down through the text; returns when scrolling up. */
body.reading.bar-hidden .topbar { transform: translateY(-100%); opacity: 0; }

/* ---------- Full-screen reading mode ---------- */
body.focus .topbar, body.focus .tabbar { display: none; }
body.focus .missing, body.focus .src, body.focus .missing-note { display: none; }
body.focus main { padding-top: calc(12px + env(safe-area-inset-top)); }
.focus-exit {
  position: fixed; z-index: 30;
  top: calc(10px + env(safe-area-inset-top)); right: 12px;
  display: none; align-items: center; justify-content: center;
  width: 34px; height: 34px; padding: 0; border: 0; border-radius: 50%;
  background: color-mix(in srgb, var(--surface-2) 70%, transparent); color: var(--muted); cursor: pointer;
  opacity: 0; pointer-events: none; transition: opacity .3s;
}
.focus-exit svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
body.focus .focus-exit { display: inline-flex; }
body.focus .focus-exit.visible { opacity: .75; pointer-events: auto; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px;
  transition: opacity .2s, transform .2s; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (min-width: 720px) {
  .day-head h1 { font-size: 28px; }
}
