/* Manga reader (ui/manga.js). Bubbles and panel art keep their own manga
   colours in both themes; the page and chrome follow the app's tokens. */

.mg-reader {
  --mg-ink: #1b1626;
  --mg-paper: #fffdf7;
  --mg-frame: #1b1626;
  --mg-gutter: 10px;
  --mg-h: clamp(380px, calc(100dvh - var(--mg-chrome, 170px)), 860px);
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  color: var(--ink);
  font-family: var(--font);
  outline: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) .mg-reader { --mg-paper: #0f1016; --mg-frame: #000; }
}
:root[data-theme='dark'] .mg-reader { --mg-paper: #0f1016; --mg-frame: #000; }

/* ---------- top bar ---------- */

.mg-bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas: 'title skip' 'progress progress';
  align-items: center;
  gap: 6px 12px;
  width: min(100%, calc(var(--mg-h) * 1.65));
}
.mg-bar-title {
  grid-area: title;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 1rem;
}
.mg-bar-label {
  display: inline-block;
  padding: 1px 9px;
  margin-right: 4px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  vertical-align: 2px;
}
.mg-bar-progress {
  grid-area: progress;
  height: 5px;
  border-radius: 999px;
  background: var(--line);
  overflow: hidden;
}
.mg-bar-progress span {
  display: block;
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: linear-gradient(90deg, #ff8a2a, #ffc21a);
  transition: width 0.3s ease;
}
.mg-skip {
  grid-area: skip;
  appearance: none;
  border: 2px solid var(--line);
  background: var(--card);
  color: var(--ink);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 700;
  padding: 5px 14px;
  border-radius: 999px;
  cursor: pointer;
  min-height: 36px;
}
.mg-skip:hover { border-color: var(--accent); }
.mg-skip:focus-visible, .mg-done:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- page ---------- */

.mg-stage {
  width: 100%;
  display: flex;
  justify-content: center;
  cursor: pointer;
}
.mg-page {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: var(--mg-gutter);
  padding: var(--mg-gutter);
  height: var(--mg-h);
  width: min(100%, calc(var(--mg-h) * 1.65));
  background: var(--mg-paper);
  border-radius: 6px;
  box-shadow: var(--shadow);
}

.mg-panel {
  position: relative;
  min-width: 0;
  min-height: 0;
  border: 3px solid var(--mg-frame);
  border-radius: 3px;
  overflow: hidden;
  container-type: size;
  background: color-mix(in srgb, var(--mg-paper) 88%, var(--mg-frame));
  background-image: radial-gradient(circle, rgb(0 0 0 / 9%) 1px, transparent 1.4px);
  background-size: 7px 7px;
}
.mg-panel:not(.is-shown) { border-color: color-mix(in srgb, var(--mg-frame) 25%, transparent); }
.mg-panel:not(.is-shown) .mg-art { visibility: hidden; }
.mg-panel.is-shown { animation: mg-panel-in 0.35s ease-out both; }
.mg-page:not(.mg-page--n1) .mg-panel.is-shown:not(.is-current) { filter: saturate(0.85); }

.mg-art { position: absolute; inset: 0; }
.mg-bg, .mg-fx { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.mg-fx-over { pointer-events: none; }

/* ---------- characters ---------- */

.mg-chars { position: absolute; inset: 0; --mg-fit: 1; }
.mg-char {
  --ch: min(64cqh, 50cqw);
  position: absolute;
  bottom: 4%;
  height: calc(var(--ch) * var(--mg-fit));
  aspect-ratio: var(--ar, 4 / 5);
  transition: height 0.2s ease;
}
.mg-char svg { display: block; width: 100%; height: 100%; overflow: visible; }
.mg-side-left { left: 3%; }
.mg-side-right { right: 3%; }
.mg-side-right svg { transform: scaleX(-1); }
.mg-side-center { left: 50%; transform: translateX(-50%); }
.mg-side-left.mg-slot-1 { left: 26%; }
.mg-side-right.mg-slot-1 { right: 26%; }

.mg-crowd .mg-char { --ch: min(58cqh, 34cqw); }
.mg-char--kitsu { --ch: min(42cqh, 36cqw); bottom: 16%; }
.mg-crowd .mg-char--kitsu { --ch: min(38cqh, 28cqw); }
.mg-char--beast { --ch: min(38cqh, 36cqw); }
.mg-crowd .mg-char--beast { --ch: min(34cqh, 28cqw); }
.mg-char--umbra { --ch: min(86cqh, 62cqw); bottom: 1%; }
.mg-crowd .mg-char--umbra { --ch: min(80cqh, 44cqw); }
.mg-char--sovereign { --ch: 104cqh; bottom: -26%; z-index: 0; }
.mg-char--sovereign.mg-side-left { left: -8%; }
.mg-char--sovereign.mg-side-right { right: -8%; }
.mg-char--sovereign.mg-side-right svg { transform: none; }
.mg-chars > .mg-char:not(.mg-char--sovereign) { z-index: 1; }

.mg-char--hero.mg-mood-awakened { --ch: min(80cqh, 66cqw); }
.mg-crowd .mg-char--hero.mg-mood-awakened { --ch: min(66cqh, 40cqw); }

.is-shown .mg-char {
  animation: mg-char-in 0.45s cubic-bezier(0.2, 0.9, 0.3, 1.2) both;
  animation-delay: calc(var(--i, 0) * 90ms + 60ms);
}
.is-shown .mg-side-center { animation-name: mg-char-in-center; }
.is-shown .mg-char--kitsu svg { animation: mg-bob 2.6s ease-in-out infinite; }
.is-shown .mg-char--kitsu.mg-side-right svg { animation-name: mg-bob-flip; }
.is-shown .mg-aura { animation: mg-aura 1.2s ease-in-out infinite; transform-origin: 60px 90px; }
.is-shown .mg-char--sovereign { animation: mg-loom 1.2s ease-out both; }

/* ---------- bubbles ---------- */

.mg-bubbles {
  position: absolute;
  inset: 0;
  z-index: 3;
  font-size: clamp(13px, calc(1.6cqw + 1.6cqh + 4px), 19px);
  line-height: 1.28;
  pointer-events: none;
}
.mg-bubble {
  position: absolute;
  left: 0;
  top: 0;
  width: max-content;
  max-width: 60%;
  color: var(--mg-ink);
}
.mg-bubble-shape {
  position: absolute;
  left: 0;
  top: 0;
  overflow: visible;
  z-index: 0;
}
.mg-bubble-text {
  position: relative;
  z-index: 1;
  margin: 0;
  font-weight: 700;
  text-align: center;
  overflow-wrap: break-word;
  hyphens: manual;
}
/* Readable even before the SVG shape is drawn. */
.mg-bubble--speech .mg-bubble-text { padding: 0.55em 1em; }
.mg-bubble--speech:not(:has(path)) { background: #fff; border: 3px solid var(--mg-ink); border-radius: 1.4em; }
.mg-bubble--thought .mg-bubble-text { padding: 0.5em 0.9em; font-style: italic; font-weight: 600; }
.mg-bubble--thought:not(:has(circle)) { background: #fff; border: 3px dashed var(--mg-ink); border-radius: 2em; }
.mg-bubble--shout .mg-bubble-text {
  padding: 0.3em 0.55em;
  font-weight: 900;
  font-size: 1.12em;
  letter-spacing: 0.01em;
  line-height: 1.12;
}
.mg-bubble--shout:not(:has(path)) { background: #fff; border: 3px solid var(--mg-ink); }
.mg-bubble--narration {
  background: #fff6d6;
  border: 2.5px solid var(--mg-ink);
  box-shadow: 3px 3px 0 var(--mg-ink);
  border-radius: 2px;
}
.mg-bubble--narration .mg-bubble-text {
  padding: 0.35em 0.7em;
  font-size: 0.86em;
  font-weight: 700;
  text-align: left;
}

.is-shown .mg-bubble {
  animation: mg-pop 0.32s cubic-bezier(0.2, 0.9, 0.3, 1.4) both;
  animation-delay: calc(var(--i, 0) * 260ms + 260ms);
}

/* ---------- effects ---------- */

.mg-twinkle { transform-box: fill-box; transform-origin: center; }
.is-shown .mg-twinkle { animation: mg-twinkle 1.6s ease-in-out infinite; }
.mg-fx-impact.is-shown.is-current { animation: mg-panel-in 0.35s ease-out both, mg-shake 0.4s 0.15s ease-in-out; }
.mg-fx-flash.is-current::after {
  content: '';
  position: absolute;
  inset: 0;
  background: #fff;
  pointer-events: none;
  opacity: 0;
  animation: mg-flash 0.6s ease-out;
  z-index: 4;
}

/* ---------- title + teaser cards ---------- */

.mg-card-body {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.4em;
  padding: 6%;
  text-align: center;
  color: #fff;
  font-size: clamp(14px, calc(1.4cqw + 1.4cqh), 22px);
}
.mg-card-arc {
  margin: 0;
  font-weight: 800;
  font-size: 0.85em;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  opacity: 0.85;
}
.mg-card-label {
  margin: 0;
  display: inline-block;
  padding: 0.15em 0.9em;
  background: #ffc21a;
  color: var(--mg-ink);
  border: 3px solid var(--mg-ink);
  box-shadow: 3px 3px 0 var(--mg-ink);
  font-weight: 900;
  font-size: 1em;
  transform: rotate(-3deg);
}
.mg-card-title {
  margin: 0.15em 0 0;
  font-size: 2.6em;
  line-height: 1.05;
  font-weight: 900;
  font-style: italic;
  letter-spacing: 0.01em;
  color: #fff;
  -webkit-text-stroke: 0.08em var(--mg-ink);
  paint-order: stroke fill;
  text-shadow: 0.06em 0.08em 0 var(--mg-ink);
}
.mg-card-kitsu { width: min(34cqh, 40cqw); height: auto; margin-top: 0.3em; }
.is-shown .mg-card-kitsu { animation: mg-bob 2.6s ease-in-out infinite; }
.is-shown .mg-card-title { animation: mg-slam 0.5s cubic-bezier(0.2, 0.9, 0.3, 1.3) both 0.1s; }
.mg-card--teaser .mg-card-body { color: var(--mg-ink); }
.mg-card-teaser {
  margin: 0.3em 0 0;
  max-width: 26em;
  padding: 0.7em 1em;
  background: #fff;
  border: 3px solid var(--mg-ink);
  border-radius: 1.2em;
  box-shadow: 4px 4px 0 var(--mg-ink);
  font-weight: 800;
  font-size: 1.15em;
  line-height: 1.3;
}
.mg-card--teaser .mg-card-label { transform: rotate(2deg); }
.mg-card--teaser .mg-card-kitsu { width: min(26cqh, 30cqw); }
.mg-done {
  appearance: none;
  border: 3px solid var(--mg-ink);
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 900;
  font-size: 0.95em;
  padding: 0.4em 1.4em;
  border-radius: 999px;
  box-shadow: 3px 3px 0 var(--mg-ink);
  cursor: pointer;
  min-height: 44px;
}

/* ---------- hint ---------- */

.mg-hint {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: min(100%, calc(var(--mg-h) * 1.65));
  min-height: 24px;
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
}
.mg-hint-text { animation: mg-nudge 1.6s ease-in-out infinite; }
.mg-count { font-variant-numeric: tabular-nums; }
.mg-live {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  margin: 0;
}

/* ---------- phone: one panel at a time ---------- */

.mg-reader.mg-phone { --mg-gutter: 0px; }
.mg-phone .mg-page {
  display: block;
  width: 100%;
  height: min(calc(100dvh - var(--mg-chrome-phone, 170px)), 128vw, 640px);
  min-height: 360px;
  padding: 0;
  background: transparent;
  box-shadow: none;
}
.mg-phone .mg-panel { position: absolute; inset: 0; border-radius: 6px; }
.mg-phone .mg-page { position: relative; }
.mg-phone .mg-bar, .mg-phone .mg-hint { width: 100%; }
.mg-phone .mg-char { --ch: min(62cqh, 58cqw); }
.mg-phone .mg-crowd .mg-char { --ch: min(50cqh, 36cqw); }
.mg-phone .mg-char--kitsu { --ch: min(40cqh, 40cqw); }
.mg-phone .mg-char--beast { --ch: min(36cqh, 40cqw); }
.mg-phone .mg-crowd .mg-char--beast { --ch: min(30cqh, 30cqw); }
.mg-phone .mg-char--umbra { --ch: min(80cqh, 74cqw); }
.mg-phone .mg-crowd .mg-char--umbra { --ch: min(64cqh, 46cqw); }
.mg-phone .mg-char--hero.mg-mood-awakened { --ch: min(72cqh, 80cqw); }
.mg-phone .mg-char--sovereign { --ch: 100cqh; bottom: -24%; }
.mg-phone .mg-bubbles { font-size: clamp(14px, 4.3cqw, 17px); }
.mg-phone .mg-hint { justify-content: center; gap: 14px; font-size: 0.95rem; }

/* ---------- animation ---------- */

@keyframes mg-panel-in { from { opacity: 0; transform: translateY(6px) scale(0.985); } to { opacity: 1; transform: none; } }
@keyframes mg-char-in { from { opacity: 0; translate: 0 14px; } to { opacity: 1; translate: 0 0; } }
@keyframes mg-char-in-center { from { opacity: 0; translate: 0 14px; } to { opacity: 1; translate: 0 0; } }
@keyframes mg-pop { from { opacity: 0; transform: scale(0.6); } to { opacity: 1; transform: none; } }
@keyframes mg-bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-4%); } }
@keyframes mg-bob-flip { 0%, 100% { transform: scaleX(-1) translateY(0); } 50% { transform: scaleX(-1) translateY(-4%); } }
@keyframes mg-aura { 0%, 100% { opacity: 0.85; transform: scale(1); } 50% { opacity: 1; transform: scale(1.04); } }
@keyframes mg-loom { from { opacity: 0; translate: 0 8%; } to { opacity: 1; translate: 0 0; } }
@keyframes mg-twinkle { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: 0.35; transform: scale(0.6); } }
@keyframes mg-shake {
  0%, 100% { translate: 0 0; }
  20% { translate: -5px 2px; }
  40% { translate: 4px -3px; }
  60% { translate: -3px 1px; }
  80% { translate: 2px -1px; }
}
@keyframes mg-flash { from { opacity: 0.95; } to { opacity: 0; } }
@keyframes mg-slam { from { opacity: 0; transform: scale(1.6); } to { opacity: 1; transform: none; } }
@keyframes mg-nudge { 0%, 100% { translate: 0 0; } 50% { translate: 4px 0; } }

@media (prefers-reduced-motion: reduce) {
  .mg-reader *, .mg-reader *::before, .mg-reader *::after {
    animation: none !important;
    transition: none !important;
  }
}
