/* Colour Your Own Ramayana — morning courtyard on the Sarayu */
:root {
  --cream: #fdf7ea;
  --sand: #f6e7c9;
  --sand-deep: #efdcb6;
  --ink: #8a4b12;
  --ink-soft: rgba(138, 75, 18, 0.72);
  --saffron: #f08a24;
  --marigold: #f7b733;
  --leaf: #6c9a3a;
  --paper-shadow: 0 2px 0 rgba(138, 75, 18, 0.06), 0 14px 30px rgba(138, 75, 18, 0.2);
  --tray-shadow: 0 4px 14px rgba(138, 75, 18, 0.16);
  --toran-h: clamp(64px, 15vh, 118px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
html { background: var(--sand-deep); }
body {
  position: relative;
  overflow: hidden;
  height: 100dvh;
  display: grid;
  grid-template-rows: var(--toran-h) auto 1fr auto;
  background: linear-gradient(180deg, var(--cream) 0%, var(--sand) 62%, var(--sand-deep) 100%);
  color: var(--ink);
  font-family: "Noto Sans", system-ui, -apple-system, sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
.hi { font-family: "Noto Serif Devanagari", serif; }

/* Faint Ayodhya ghat (Ram ki Paidi) behind everything */
.ghat {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  width: 100%;
  height: 46%;
  opacity: 0.45;
  pointer-events: none;
  z-index: 0;
}

.home-link {
  position: absolute;
  z-index: 3;
  top: calc(env(safe-area-inset-top) + 6px);
  left: calc(env(safe-area-inset-left) + 10px);
  font-size: 12px;
  color: var(--ink-soft);
  text-decoration: none;
  padding: 4px 6px;
  border-radius: 6px;
  background: rgba(253, 247, 234, 0.7);
}

/* Toran */
.top { position: relative; z-index: 2; }
.toran { display: block; width: 100%; height: 100%; overflow: visible; }
.toran-rope { fill: none; stroke: var(--leaf); stroke-width: 3; }
.toran-thread { stroke: var(--leaf); stroke-width: 1.2; }
.toran-hit { fill: transparent; }
.toran-string { cursor: pointer; outline: none; }
.toran-string .marigold.a { fill: #f3d9ad; }
.toran-string .marigold.b { fill: #f6e2bd; }
.toran-string .marigold-heart { fill: rgba(255, 255, 255, 0.55); }
.toran-string.done .marigold.a, .toran-string.current .marigold.a { fill: var(--saffron); }
.toran-string.done .marigold.b, .toran-string.current .marigold.b { fill: var(--marigold); }
.toran-string.gap { opacity: 0.35; cursor: default; }
.toran-current { fill: var(--ink); }
.toran-string:focus-visible .marigold { stroke: var(--ink); stroke-width: 1.5; }
@media (hover: hover) { .toran-string:not(.gap):hover .marigold { filter: brightness(1.06); } }

/* Title */
.title-block { position: relative; z-index: 2; text-align: center; padding: 2px 64px 4px; }
.chapter-title { border: 0; background: none; padding: 2px 8px; line-height: 1.25; }
.chapter-title .site {
  display: block;
  font-family: "Cormorant Garamond", Georgia, serif;
  font-weight: 600;
  font-size: 15px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}
.chapter-title .sub { font-family: "Cormorant Garamond", Georgia, serif; font-size: 16px; color: var(--ink-soft); }
.chapter-title .sub .hi { font-size: 13px; }

/* Sound pills (filled by sound.js) */
.sounds {
  position: absolute;
  z-index: 4;
  right: calc(env(safe-area-inset-right) + 10px);
  top: calc(env(safe-area-inset-top) + var(--toran-h) + 4px);
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: flex-end;
}

/* Stage + paper */
.stage { position: relative; z-index: 1; overflow: hidden; touch-action: none; margin: 0 8px; }
.paper {
  position: absolute;
  left: 0; top: 0;
  transform-origin: 0 0;
  background: #fff;
  border-radius: 4px;
  box-shadow: var(--paper-shadow);
  will-change: transform;
}
.paper canvas, .paper img { position: absolute; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; border-radius: 4px; }
.paper .ref { opacity: 0; transition: opacity 0.35s ease; }
body.peek .paper .ref { opacity: 1; }
.loading { position: absolute; inset: 0; display: grid; place-items: center; color: var(--ink-soft); font-size: 14px; pointer-events: none; }
.loading[hidden] { display: none; }

/* Tray */
.tray {
  position: relative;
  z-index: 3;
  justify-self: center;
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 6px 0 calc(env(safe-area-inset-bottom) + 10px);
  padding: 6px 8px;
  border-radius: 999px;
  background: #fff;
  box-shadow: var(--tray-shadow);
  max-width: calc(100vw - 16px);
}
.dots { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; justify-content: center; }
.dot {
  width: 34px; height: 34px;
  border-radius: 50%;
  border: 0;
  padding: 0;
  display: grid; place-items: center;
  background: transparent;
}
.dot span { width: 22px; height: 22px; border-radius: 50%; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.14); }
.dot[aria-pressed="true"] span { outline: 2px solid var(--ink); outline-offset: 3px; }
.tool {
  width: 36px; height: 36px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  font-size: 19px;
  line-height: 1;
  color: var(--ink);
  display: grid; place-items: center;
}
.tool svg { width: 20px; height: 20px; fill: currentColor; }
.tool[aria-pressed="true"], .tool[aria-expanded="true"] { background: rgba(240, 138, 36, 0.16); }
.tool[hidden] { display: none; }
.tool:disabled { opacity: 0.35; }
button:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

.sheet {
  position: absolute;
  z-index: 6;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 70px);
  transform: translateX(-50%);
  background: #fff;
  border-radius: 18px;
  box-shadow: var(--tray-shadow);
  padding: 10px;
}
.sheet[hidden] { display: none; }
.palette-sheet { width: min(360px, calc(100vw - 24px)); }
.menu { display: flex; flex-direction: column; min-width: 220px; padding: 6px; }
.menu button { border: 0; background: none; text-align: left; padding: 12px 14px; border-radius: 12px; font-size: 15px; }
.menu button:hover, .menu button:focus-visible { background: rgba(240, 138, 36, 0.12); }

/* Story card */
.story {
  position: absolute;
  inset: 0;
  z-index: 8;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(253, 247, 234, 0.55);
}
.story[hidden] { display: none; }
.story-card {
  max-width: 520px;
  max-height: calc(100dvh - 32px);
  overflow: auto;
  background: var(--cream);
  border-radius: 18px;
  padding: 20px 24px;
  box-shadow: 0 14px 40px rgba(138, 75, 18, 0.22);
  text-align: center;
  line-height: 1.55;
}
.story-card h2 { font-family: "Cormorant Garamond", Georgia, serif; font-weight: 600; font-size: 28px; margin: 0; }
.story-num { margin: 0; font-size: 12px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--ink-soft); }
.story-title-hi { margin: 0 0 10px; color: var(--ink-soft); }
.story-card p { margin: 0 0 10px; font-size: 15px; }
.story-card p.hi { font-size: 15px; color: var(--ink-soft); }
.story-hint { font-size: 12px !important; color: var(--ink-soft); margin: 6px 0 0 !important; }
.zoom-hint { margin-top: 2px !important; }
.zoom-hint .mouse-only { display: none; }
@media (hover: hover) and (pointer: fine) {
  .zoom-hint .touch-only { display: none; }
  .zoom-hint .mouse-only { display: inline; }
}

/* Viewing someone else's Ramayana */
.viewing {
  position: absolute;
  z-index: 5;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 12px);
  transform: translateX(-50%);
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: center;
  background: #fff;
  border-radius: 18px;
  box-shadow: var(--tray-shadow);
  padding: 8px 12px;
  font-size: 14px;
  width: max-content;
  max-width: calc(100vw - 24px);
}
.viewing[hidden], .gallery[hidden] { display: none; }
.viewing button, .gallery-actions button, .dialog button.primary, .dialog button.secondary {
  border: 1px solid rgba(138, 75, 18, 0.35);
  background: #fff;
  border-radius: 999px;
  padding: 8px 14px;
}
.viewing button#continueBtn, .gallery-actions button#galleryContinue, .dialog button.primary {
  background: linear-gradient(180deg, #f7a24a, #d9701a);
  color: #2a1408;
  border-color: transparent;
}
body.view-mode .tray { visibility: hidden; }

.gallery {
  position: absolute;
  inset: 0;
  z-index: 7;
  overflow: auto;
  padding: calc(var(--toran-h) + 8px) 16px 24px;
  background: rgba(253, 247, 234, 0.92);
  touch-action: pan-y;
}
.gallery-inner { max-width: 980px; margin: 0 auto; text-align: center; }
.gallery h1 { font-family: "Cormorant Garamond", Georgia, serif; font-weight: 600; font-size: clamp(24px, 4vw, 34px); margin: 0 0 4px; }
.gallery p { margin: 0 0 16px; color: var(--ink-soft); }
.gallery-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(220px, 44vw), 1fr)); gap: 14px; }
.gallery-grid button { border: 0; padding: 0; background: none; text-align: left; }
.gallery-grid canvas { width: 100%; height: auto; display: block; border-radius: 6px; background: #fff; box-shadow: var(--paper-shadow); }
.gallery-grid span { display: block; font-size: 13px; margin-top: 6px; }
.gallery-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 20px; }

.rotate-hint {
  position: absolute;
  z-index: 9;
  left: 50%;
  top: calc(var(--toran-h) + 50px);
  transform: translateX(-50%);
  background: #fff;
  border-radius: 999px;
  box-shadow: var(--tray-shadow);
  padding: 8px 8px 8px 16px;
  font-size: 13px;
  white-space: nowrap;
}
.rotate-hint[hidden] { display: none; }
.rotate-hint button { border: 0; background: none; font-size: 18px; padding: 0 8px; }

.toast {
  position: absolute;
  z-index: 20;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 80px);
  transform: translateX(-50%);
  background: #5a3a1e;
  color: #fdf7ea;
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 14px;
}
.toast[hidden] { display: none; }

#print-sheet { display: none; }

@media (max-height: 460px) {
  :root { --toran-h: clamp(52px, 14vh, 70px); }
  .chapter-title .site { font-size: 12px; }
  .chapter-title .sub { font-size: 13px; }
  .dot { width: 30px; height: 30px; }
  .dot span { width: 19px; height: 19px; }
  .tool { width: 32px; height: 32px; font-size: 17px; }
  .tray { margin-top: 4px; margin-bottom: calc(env(safe-area-inset-bottom) + 6px); padding: 4px 6px; }
}
@media (max-width: 520px) {
  .title-block { padding: 2px 8px 4px; }
  .home-link { font-size: 11px; }
}

/* Phones held sideways: tray becomes a slim column on the right so the page gets the full height */
@media (max-height: 460px) and (orientation: landscape) {
  :root { --toran-h: clamp(40px, 11vh, 50px); }
  body { grid-template-rows: var(--toran-h) 1fr; }
  /* the chapter name moves onto the toran line; tap it (or the toran) for the story */
  .title-block { position: absolute; top: 2px; left: 50%; transform: translateX(-50%); padding: 0; z-index: 3; pointer-events: auto; }
  .chapter-title .site { display: none; }
  .chapter-title .sub { font-size: 12px; background: rgba(251, 243, 226, 0.85); border-radius: 10px; padding: 0 8px; }
  .stage { margin: 0 80px 4px calc(env(safe-area-inset-left) + 4px); }
  /* white tray hugs its contents (colours in two columns) so nothing pokes out past it */
  .tray {
    position: absolute;
    right: calc(env(safe-area-inset-right) + 8px);
    top: calc((var(--toran-h) + 100vh) / 2);
    transform: translateY(-50%);
    max-height: calc(100vh - var(--toran-h) - env(safe-area-inset-bottom) - 10px);
    margin: 0;
    flex-direction: column;
    justify-content: center;
    border-radius: 22px;
    padding: 6px 4px;
    gap: 0;
  }
  .tray .dots { display: grid; grid-template-columns: repeat(2, 30px); gap: 0; margin-bottom: 2px; }
  .tray .dot { width: 30px; height: 28px; }
  .tray .dot span { width: 19px; height: 19px; }
  .tray .tool { width: 34px; height: 30px; font-size: 17px; }
  .sounds { right: auto; left: calc(env(safe-area-inset-left) + 8px); top: calc(var(--toran-h) + 2px); }
  .sheet { left: auto; right: calc(env(safe-area-inset-right) + 82px); bottom: 10px; transform: none; }
  .viewing { bottom: 6px; }
  /* story card: tighter, both hints on one line, so it fits without scrolling */
  .story-card { padding: 12px 20px; }
  .story-card h2 { font-size: 24px; }
  .story-card p, .story-card p.hi { font-size: 14px; margin-bottom: 6px; }
  .story-hint { display: inline; }
  .zoom-hint::before { content: " · "; }
}
/* Narrow phones held upright: keep all colours on one line */
@media (max-width: 420px) and (orientation: portrait) {
  .tray { padding: 4px 6px; gap: 0; }
  .tray .dots { flex-wrap: nowrap; gap: 0; }
  .dot { width: 30px; height: 30px; }
  .dot span { width: 19px; height: 19px; }
  .tool { width: 30px; height: 32px; font-size: 17px; }
}

/* Dialogs (save link, PIN) */
.dialog-back {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(90, 58, 30, 0.25);
  touch-action: auto;
  -webkit-user-select: text;
  user-select: text;
}
.dialog {
  width: min(440px, 100%);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  background: var(--cream);
  border-radius: 20px;
  padding: 20px 22px 16px;
  box-shadow: 0 14px 40px rgba(138, 75, 18, 0.25);
  text-align: center;
}
.dialog h2 { font-family: "Cormorant Garamond", Georgia, serif; font-weight: 600; font-size: 26px; margin: 0 0 6px; }
.dialog p { margin: 0 0 12px; font-size: 14px; line-height: 1.5; color: var(--ink-soft); }
.pins { display: flex; gap: 16px; justify-content: center; margin: 4px 0 6px; }
.pin-label { display: block; font-size: 12px; color: var(--ink-soft); margin-bottom: 4px; }
.pins > label + input { display: block; }
.pin {
  width: 120px;
  font: 500 26px "Noto Sans", system-ui, sans-serif;
  letter-spacing: 0.4em;
  text-align: center;
  padding: 8px 4px 8px 14px;
  border: 1px solid rgba(138, 75, 18, 0.35);
  border-radius: 12px;
  background: #fff;
  color: var(--ink);
}
.pins { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 140px)); justify-content: center; }
.dialog-msg { min-height: 1.5em; color: #b03a1a !important; margin: 4px 0 8px !important; }
.dialog-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.link-box { background: #fff; border: 1px dashed rgba(138, 75, 18, 0.4); border-radius: 12px; padding: 10px; word-break: break-all; color: var(--ink) !important; font-size: 15px !important; }
.text-btn { border: 0; background: none; margin-top: 8px; color: var(--ink-soft); padding: 8px; }

/* Sound pills */
.sound { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 0;
  border-radius: 999px;
  padding: 5px 11px;
  font-size: 12px;
  background: rgba(242, 154, 46, 0.14);
  color: var(--ink);
  opacity: 0.6;
}
.pill[aria-pressed="true"] { opacity: 1; background: rgba(242, 154, 46, 0.3); }
.sound input[type="range"] { width: 92px; accent-color: var(--saffron); touch-action: pan-x; }
.sound input[hidden] { display: none; }
@media (max-height: 460px) and (orientation: landscape) {
  .sound { align-items: flex-start; }
  .pill-label { display: none; }
  .pill { padding: 4px 8px; }
  .sound input[type="range"] { width: 70px; }
}
@media (max-width: 420px) and (orientation: portrait) { .pill-label { display: none; } }

/* Printing: only the coloured page */
@page { size: A4 landscape; margin: 10mm; }
@media print {
  html, body { background: #fff !important; height: auto; overflow: visible; display: block; padding: 0; }
  body > *:not(#print-sheet) { display: none !important; }
  #print-sheet { display: block !important; }
  #print-sheet img { max-width: 100%; max-height: 270mm; width: auto; height: auto; display: block; margin: 0 auto; }
}
.menu .menu-link + .menu-link { border-top: 0; margin-top: 0; }
.menu .menu-link { padding: 10px 14px; font-size: 12px; color: var(--ink-soft); text-decoration: none; border-top: 1px solid rgba(138, 75, 18, 0.12); margin-top: 4px; }
.save-status { display: block; margin: 2px auto 0; border: 0; background: rgba(240, 138, 36, 0.12); color: var(--ink-soft); border-radius: 999px; padding: 3px 12px; font-size: 12px; cursor: default; }
.save-status.locked { background: rgba(224, 59, 59, 0.12); color: #9b2c1a; cursor: pointer; }
.save-status[hidden] { display: none; }

.sound-col { display: flex; flex-direction: column; gap: 6px; align-items: flex-end; }
@media (max-height: 460px) and (orientation: landscape) { .sound-col { align-items: flex-start; } }

/* Portrait pages on phones and iPads held upright: slim header, page edge to edge.
   Sounds stand in two little columns, one each side of the chapter name; a sound's volume slider
   opens right under its button (vertical) and pushes the button below it down. */
@media (orientation: portrait) {
  html[data-layout="mobile"] { --toran-h: 70px; }
  html[data-layout="mobile"] .title-block { display: flex; align-items: center; justify-content: center; min-height: 40px; padding: 0 48px 2px; }
  html[data-layout="mobile"] .chapter-title { padding: 2px 4px; line-height: 1.15; }
  html[data-layout="mobile"] .chapter-title .site { display: none; }
  html[data-layout="mobile"] .chapter-title .sub { font-size: 16px; color: var(--ink); }
  html[data-layout="mobile"] .chapter-title .sub .hi { display: block; font-size: 12px; color: var(--ink-soft); }
  html[data-layout="mobile"] .sounds {
    left: calc(env(safe-area-inset-left) + 4px); right: calc(env(safe-area-inset-right) + 4px);
    top: calc(env(safe-area-inset-top) + var(--toran-h) + 2px);
    flex-direction: row; justify-content: space-between; align-items: flex-start; pointer-events: none;
  }
  html[data-layout="mobile"] .sound-col { gap: 4px; align-items: center; pointer-events: auto; }
  html[data-layout="mobile"] .sound { align-items: center; gap: 4px; }
  html[data-layout="mobile"] .pill-label { display: none; }
  html[data-layout="mobile"] .pill { padding: 4px 7px; font-size: 14px; }
  html[data-layout="mobile"] .sound input[type="range"] {
    writing-mode: vertical-lr; direction: rtl;
    width: 26px; height: 84px; margin: 0; padding: 8px 0;
    background: rgba(253, 247, 234, 0.92); border-radius: 13px; box-shadow: var(--tray-shadow);
  }
  html[data-layout="mobile"] .stage { margin: 0 2px; }
  html[data-layout="mobile"] .tray { margin-top: 4px; margin-bottom: calc(env(safe-area-inset-bottom) + 6px); }
}
#chapterNameHi::before { content: "· "; }
@media (orientation: portrait) { html[data-layout="mobile"] #chapterNameHi::before { content: none; } }
