:root {
  --bg: #17110f;
  --bg-raised: #241b18;
  --bg-sunk: #120d0b;
  --line: #3a2d27;
  --text: #f3e9dc;
  --muted: #9a8a7c;
  --dim: #5f524a;
  --accent: #e3a64a;
  --accent-ink: #1b1209;
  --danger: #d9776a;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --text-scale: 1;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 17px;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}

input, select { font: inherit; color: inherit; }

#view { min-height: 100%; }

/* ---------- Generic screen chrome ---------- */

.screen {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(var(--safe-top) + 20px) 20px calc(var(--safe-bottom) + 40px);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 44px;
  margin-bottom: 12px;
}
.topbar h1 { flex: 1; margin: 0; font: 600 20px/1.2 var(--sans); }
.topbar .link { color: var(--accent); min-height: 44px; padding: 0 4px; }

.brand {
  font: 400 34px/1.1 var(--serif);
  margin: 8px 0 4px;
  letter-spacing: 0.01em;
}
.subtle { color: var(--muted); font-size: 15px; }
.home-head { display: flex; align-items: flex-start; gap: 12px; }
.home-head > div { flex: 1; min-width: 0; }
.home-head .icon-btn { margin-top: 6px; background: var(--bg-raised); color: var(--accent); text-decoration: none; }
.home-head .icon-btn svg { width: 22px; height: 22px; }

.section-title {
  margin: 28px 0 10px;
  font: 600 13px/1 var(--sans);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.card {
  background: var(--bg-raised);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 12px;
}

.routine-card { display: flex; align-items: center; gap: 12px; }
.routine-card .info { flex: 1; min-width: 0; }
.routine-card h2 { margin: 0 0 4px; font: 400 22px/1.2 var(--serif); }
.routine-card p { margin: 0; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.icon-btn {
  width: 44px; height: 44px;
  display: inline-grid; place-items: center;
  border-radius: 50%;
  flex: none;
}
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn.filled { background: var(--accent); color: var(--accent-ink); width: 52px; height: 52px; }
.icon-btn.filled svg { width: 26px; height: 26px; }
.icon-btn.ghost { color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 20px;
  border-radius: 14px;
  background: var(--bg-raised);
  font-weight: 600;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.block { width: 100%; }
.btn.danger { color: var(--danger); }
.btn-row { display: flex; gap: 10px; margin-top: 16px; }
.btn-row .btn { flex: 1; }

.toggle-row {
  display: flex; align-items: center; justify-content: space-between;
  min-height: 52px; gap: 12px;
}
.switch { position: relative; width: 51px; height: 31px; flex: none; }
.switch input { opacity: 0; width: 100%; height: 100%; margin: 0; position: absolute; cursor: pointer; }
.switch span {
  position: absolute; inset: 0; border-radius: 31px; background: var(--line);
  transition: background .2s; pointer-events: none;
}
.switch span::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px;
  border-radius: 50%; background: #fff; transition: transform .2s;
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(20px); }

/* ---------- Editor ---------- */

.name-input {
  width: 100%;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--line);
  padding: 8px 0;
  font: 400 28px/1.2 var(--serif);
  outline: none;
}
.name-input:focus { border-color: var(--accent); }

.item-list { list-style: none; margin: 0; padding: 0; }
.item {
  display: flex; align-items: flex-start; gap: 6px;
  background: var(--bg-raised);
  border-radius: 14px;
  padding: 12px 6px 6px 14px;
  margin-bottom: 8px;
}
.item .num { color: var(--dim); width: 20px; padding-top: 2px; font-variant-numeric: tabular-nums; }
.item .body { flex: 1; min-width: 0; }
.item .title { display: block; font: 400 19px/1.25 var(--serif); }
.item .controls { display: flex; align-items: center; gap: 2px; margin-top: 4px; }
.item .controls .stepper { margin-right: auto; }
.item .controls .stepper[hidden] + .icon-btn { margin-left: auto; }
.item .title small { display: block; font: 13px var(--sans); color: var(--muted); margin-top: 2px; }
.item .icon-btn { width: 36px; height: 44px; color: var(--muted); }
.item .icon-btn svg { width: 20px; height: 20px; }
.item .icon-btn:disabled { opacity: .25; }

.stepper { display: inline-flex; align-items: center; gap: 2px; }
.stepper button { width: 30px; height: 36px; border-radius: 8px; background: var(--bg-sunk); font-size: 18px; }
.stepper output { min-width: 30px; text-align: center; font-variant-numeric: tabular-nums; font-size: 15px; }

.empty { color: var(--muted); text-align: center; padding: 28px 0; }

/* Bottom sheet (library picker, queue) */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 40;
  animation: fade .2s ease;
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  max-width: 560px; margin: 0 auto;
  max-height: 80vh; overflow: auto;
  background: var(--bg-raised);
  border-radius: 20px 20px 0 0;
  padding: 8px 16px calc(var(--safe-bottom) + 20px);
  animation: rise .25s ease;
}
.sheet::before {
  content: ""; display: block; width: 36px; height: 5px; border-radius: 3px;
  background: var(--line); margin: 0 auto 12px;
}
.sheet h3 { margin: 4px 0 12px; font: 600 17px var(--sans); }
.sheet-row {
  display: flex; align-items: center; gap: 12px; width: 100%;
  min-height: 56px; padding: 8px 4px; text-align: left;
  border-bottom: 1px solid var(--line);
  font: 400 18px/1.25 var(--serif);
}
.sheet-row:last-child { border-bottom: 0; }
.sheet-row small { display: block; font: 13px var(--sans); color: var(--muted); }
.sheet-row.current { color: var(--accent); }
@keyframes rise { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }

/* ---------- Player ---------- */

.player {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  background: radial-gradient(120% 60% at 50% 0%, #2a1c15 0%, var(--bg) 60%);
}

.player-top {
  display: flex; align-items: center; gap: 4px;
  padding: calc(var(--safe-top) + 6px) 8px 6px;
}
.player-top .heading { flex: 1; min-width: 0; text-align: center; }
.player-top .heading strong {
  display: block; font: 600 15px/1.3 var(--sans);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.player-top .heading span { font-size: 13px; color: var(--muted); }

.lyrics {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  position: relative;
  min-height: 0;
  padding: 0 24px;
  mask-image: linear-gradient(to bottom, transparent 0, #000 12%, #000 80%, transparent 100%);
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 12%, #000 80%, transparent 100%);
}

.lyrics::-webkit-scrollbar { display: none; }
/* Spacers (not padding) let the first and last lines reach the reading position without forcing a min height. */
.lyrics::before { content: ""; display: block; height: 30vh; }
.lyrics::after { content: ""; display: block; height: 45vh; }

.stanza { margin-bottom: 30px; }

/* Image view: the deity image fills the space, with the current line (karaoke included) beneath it. */
.art {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 4px 24px 8px;
}
.art img { min-height: 0; max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 12px; flex: 0 1 auto; }
.art-line { flex: none; width: 100%; margin-top: 14px; text-align: center; }
.art-line .lyric { opacity: 1; padding: 0; cursor: default; }
.art-line .lyric .text { font-size: calc(22px * var(--text-scale)); }
.player-top .spacer { visibility: hidden; }

.lyric {
  padding: 8px 0;
  cursor: pointer;
  transition: opacity .35s ease;
  opacity: .42;
}
.lyric .text {
  font: 600 calc(25px * var(--text-scale))/1.28 var(--serif);
  letter-spacing: 0.005em;
  color: var(--text);
}
.lyric .word { transition: color .18s ease; }
.lyric .tr {
  margin-top: 4px;
  font: italic 400 calc(16px * var(--text-scale))/1.35 var(--serif);
  color: var(--muted);
}
.hide-tr .tr, .hide-tr .stanza-tr { display: none; }
.tr, .stanza-tr { white-space: pre-line; }   /* keep the source's line breaks in translations */

.lyric.past { opacity: .28; }
.lyric.active { opacity: 1; }
.lyric.active .text { color: var(--dim); }
.lyric.active .word.sung { color: var(--text); }
.lyric.active.no-words .text { color: var(--text); }
.lyric.active .tr { color: #cdbca9; }

.stanza-tr {
  margin: 6px 0 0;
  padding-left: 12px;
  border-left: 2px solid var(--line);
  font: italic 400 calc(16px * var(--text-scale))/1.4 var(--serif);
  color: var(--muted);
}

.instruction {
  margin: 10px 0 30px;
  padding: 14px 16px;
  border-radius: 14px;
  background: rgba(255,255,255,.04);
  font: 400 calc(16px * var(--text-scale))/1.4 var(--sans);
  color: var(--muted);
  opacity: .5;
  transition: opacity .35s ease, background .35s ease;
}
.instruction::before {
  content: "Instruction";
  display: block; margin-bottom: 4px;
  font: 600 11px/1 var(--sans); letter-spacing: .08em; text-transform: uppercase;
  color: var(--dim);
}
.instruction.active { opacity: 1; background: rgba(227,166,74,.12); color: var(--text); }
.instruction.active::before { color: var(--accent); }

.meaning {
  margin-top: 40px; padding-top: 16px;
  border-top: 1px solid var(--line);
  font: 400 calc(15px * var(--text-scale))/1.5 var(--serif);
  color: var(--muted);
}
.meaning h4 { margin: 0 0 6px; font: 600 12px var(--sans); letter-spacing: .08em; text-transform: uppercase; }
.meaning p { margin: 0 0 8px; }

.player-bottom {
  padding: 8px 20px calc(var(--safe-bottom) + 14px);
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}

.progress { display: flex; align-items: center; gap: 10px; font: 12px var(--sans); color: var(--muted); font-variant-numeric: tabular-nums; }
.progress input[type=range] { flex: 1; accent-color: var(--accent); height: 28px; }

.counter {
  text-align: center; min-height: 20px;
  font: 600 12px var(--sans); letter-spacing: .06em; text-transform: uppercase; color: var(--accent);
}

.transport {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 6px;
}
.transport .main { display: flex; align-items: center; gap: 22px; }
.transport .icon-btn.filled { width: 68px; height: 68px; }
.transport .icon-btn.filled svg { width: 32px; height: 32px; }
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 36px; padding: 0 12px; border-radius: 18px;
  font: 600 12px var(--sans); color: var(--muted);
  border: 1px solid var(--line);
}
.pill svg { width: 16px; height: 16px; }
.pill.on { color: var(--accent); border-color: rgba(227,166,74,.5); }

.upnext {
  position: absolute; left: 16px; right: 16px; bottom: calc(var(--safe-bottom) + 170px);
  max-width: 528px; margin: 0 auto;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 16px;
  box-shadow: 0 10px 40px rgba(0,0,0,.5);
  animation: rise-small .3s ease;
}
.upnext small { color: var(--muted); font-size: 12px; letter-spacing: .06em; text-transform: uppercase; font-weight: 600; }
.upnext p { margin: 4px 0 12px; font: 400 22px/1.2 var(--serif); }
@keyframes rise-small { from { transform: translateY(12px); opacity: 0; } }

/* Sync editor */
.editbar {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;
  margin-top: 10px;
}
.editbar .btn { min-height: 44px; padding: 0 8px; font-size: 14px; }
.editbar .stamp { grid-column: 1 / -1; min-height: 56px; font-size: 17px; }
.edit-mode .lyric.target { outline: 1px dashed var(--accent); outline-offset: 4px; border-radius: 6px; }
.edit-flag {
  text-align: center; font: 600 12px var(--sans); color: var(--accent);
  letter-spacing: .06em; text-transform: uppercase; margin-bottom: 4px;
}

#toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 24px); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #f3e9dc; color: #1b1209; padding: 10px 16px; border-radius: 12px;
  font-weight: 600; font-size: 14px; transition: all .25s ease;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 700px) {
  .lyric .text { font-size: calc(32px * var(--text-scale)); }
  .lyrics { padding-left: max(24px, calc(50% - 300px)); padding-right: max(24px, calc(50% - 300px)); }
  .player-bottom > * { max-width: 560px; margin-left: auto; margin-right: auto; }
}

/* Untimed chants: each stanza is one block; the block containing the current audio is lit. */
.block { margin-bottom: 36px; }
.block .block-head {
  margin-bottom: 8px;
  font: 600 12px/1 var(--sans); letter-spacing: .08em; text-transform: uppercase;
  color: var(--dim);
}
.block.active .block-head { color: var(--accent); }
.block .text > div { padding-left: 1em; text-indent: -1em; }
.block .text > div + div { margin-top: 2px; }

/* Headings and notes inside a chant (shown, never timed). */
.lyric-heading {
  margin: 28px 0 10px;
  font: 400 calc(21px * var(--text-scale))/1.25 var(--serif);
  color: var(--accent);
}
h2.lyric-heading { font-size: calc(23px * var(--text-scale)); }
.lyric-heading.byline { margin-top: -4px; font-size: calc(15px * var(--text-scale)); font-style: italic; color: var(--muted); }
.note {
  margin: 8px 0 22px;
  font: 400 calc(15px * var(--text-scale))/1.45 var(--sans);
  color: var(--muted);
}
.note.comment { font-style: italic; font-family: var(--serif); font-size: calc(16px * var(--text-scale)); }
.note.advice {
  padding: 12px 14px;
  border-radius: 12px;
  background: rgba(255,255,255,.04);
}
.note.advice::before {
  content: "Rinpoche’s advice";
  display: block; margin-bottom: 4px;
  font: 600 11px/1 var(--sans); letter-spacing: .08em; text-transform: uppercase; color: var(--dim);
}
.times {
  display: inline-block; vertical-align: middle;
  margin-left: 6px; padding: 2px 7px; border-radius: 8px;
  font: 600 11px/1.2 var(--sans); letter-spacing: .04em; color: var(--accent);
  border: 1px solid rgba(227,166,74,.45);
}
.lyric-image { margin: 8px auto 24px; max-width: 300px; }
.lyric-image img { display: block; width: 100%; height: auto; border-radius: 12px; }
.lyric-image figcaption { margin-top: 6px; text-align: center; font: 400 11px/1.4 var(--sans); color: var(--dim); }
.lyric em, .tr em, .note em { font-style: italic; }
.lyric .text em { color: inherit; }

/* Reading mode: no audio; the whole text is lit and scrolls freely; Next sits at the bottom. */
.readnext { display: none; }
.reading .readnext {
  display: flex; flex-direction: column; gap: 2px; min-height: 60px; margin-bottom: 6px;
  font-size: 16px; text-align: center;
}
.readnext small { font-size: 11px; letter-spacing: .08em; text-transform: uppercase; opacity: .75; }
.reading .progress, .reading .toggle, .reading .counter, .reading .auto { display: none; }
.reading .transport { margin-top: 0; }
.reading .lyrics { mask-image: linear-gradient(to bottom, transparent 0, #000 4%, #000 92%, transparent 100%); -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 4%, #000 92%, transparent 100%); }
.reading .lyrics::before { height: 3vh; }
.reading .lyrics::after { height: 20vh; }
.read-title { margin: 8px 0 22px; }
.read-title h1 { margin: 0; font: 400 calc(30px * var(--text-scale))/1.15 var(--serif); }
.read-title p { margin: 8px 0 0; font: italic 400 calc(17px * var(--text-scale))/1.35 var(--serif); color: var(--muted); }
.read-stanza { margin-bottom: 22px; }
.reading .lyric { padding: 2px 0; cursor: default; }
.reading .lyric .text {
  font-weight: 400; font-size: calc(20px * var(--text-scale)); line-height: 1.4;
  padding-left: 1.2em; text-indent: -1.2em;   /* wrapped verse lines hang under the first word */
}
.reading .lyric .tr { margin: 0 0 6px 1.2em; }
.reading .instruction { opacity: 1; }
