:root {
  --bg: #0b0d17;
  --bg-2: #12152a;
  --card: #171b33;
  --line: #2a2f4d;
  --ink: #ece8f4;
  --muted: #9b98b3;
  --faint: #6c6a85;
  --neon: #ff4f8b;
  --cyan: #4fd8ff;
  --gold: #f2c14e;
  --good: #6fdba4;
  --bad: #ff7070;
  --magic: #b9a1ff;

  --serif: 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', system-ui, sans-serif;
  --jp: 'Hiragino Mincho ProN', 'Yu Mincho', 'Noto Serif JP', serif;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; background: var(--bg); }

body {
  margin: 0;
  min-height: 100dvh;
  background:
    radial-gradient(120% 60% at 50% -10%, #1c1f45 0%, transparent 60%),
    var(--bg);
  background-attachment: fixed;
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}

#app { max-width: 640px; margin: 0 auto; }

.loading {
  padding: 40vh 24px 0;
  text-align: center;
  color: var(--muted);
  font-family: var(--serif);
  font-style: italic;
}

.kicker {
  margin: 0 0 6px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--neon);
}

/* ---------- buttons ---------- */

.menu button, .create > button, .end button, .actions button, .dice-modal button, .fight-actions button {
  display: block;
  width: 100%;
  min-height: 52px;
  margin: 10px 0 0;
  padding: 12px 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  font-size: 17px;
  font-weight: 600;
  transition: transform .08s ease, background .15s ease;
}

button:active:not(:disabled) { transform: scale(.98); }

button.primary {
  border-color: transparent;
  background: linear-gradient(135deg, var(--neon), #d93a8f);
  color: #fff;
  box-shadow: 0 6px 24px -8px rgba(255, 79, 139, .6);
}

button.ghost { background: transparent; border-color: transparent; color: var(--muted); }

button:disabled { opacity: .4; cursor: default; }

button small { display: block; font-size: 13px; font-weight: 400; opacity: .75; margin-top: 2px; }

/* ---------- title ---------- */

.title-screen {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: calc(var(--safe-top) + 24px) 28px calc(var(--safe-bottom) + 24px);
}

.logo { text-align: center; margin-bottom: 48px; }

.loop {
  width: 140px;
  height: 140px;
  margin: 0 auto 28px;
  border-radius: 50%;
  border: 6px solid var(--gold);
  border-right-color: transparent;
  box-shadow: 0 0 40px -4px rgba(242, 193, 78, .55), inset 0 0 30px -10px rgba(242, 193, 78, .5);
  transform: rotate(-30deg);
  animation: loop 24s linear infinite;
}

@keyframes loop { to { transform: rotate(330deg); } }

.logo .jp { margin: 0; font-family: var(--jp); font-size: 18px; letter-spacing: .3em; color: var(--gold); }

.logo h1 {
  margin: 6px 0 4px;
  font-family: var(--serif);
  font-size: 44px;
  font-weight: 400;
  letter-spacing: .02em;
  text-shadow: 0 0 24px rgba(79, 216, 255, .35);
}

.logo .tagline { margin: 0; color: var(--muted); font-family: var(--serif); font-style: italic; }

.foot { text-align: center; color: var(--faint); font-size: 13px; margin-top: 28px; }

/* ---------- character creation ---------- */

.create { padding: calc(var(--safe-top) + 32px) 22px calc(var(--safe-bottom) + 28px); }

.create h2 { margin: 0 0 20px; font-family: var(--serif); font-size: 30px; font-weight: 400; }

/* .create > .path outranks the generic .create > button rule above, which
   would otherwise stack the badge on top of the text. */
.create > .path {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  width: 100%;
  min-height: 0;
  margin: 0 0 14px;
  padding: 18px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
}

.path .kanji {
  flex: none;
  width: 56px;
  height: 56px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: var(--bg);
  font-family: var(--jp);
  font-size: 32px;
  color: var(--gold);
}

.path > span:last-child { flex: 1; min-width: 0; padding-top: 4px; }
.path b { display: block; font-size: 19px; margin-bottom: 6px; }
.path small { display: block; color: var(--muted); font-size: 15px; font-weight: 400; line-height: 1.5; }

.rolls { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 16px; }

.roll-stat {
  padding: 14px 8px;
  text-align: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
}

.roll-stat > small { display: block; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .08em; }
.roll-stat b { display: block; font-family: var(--serif); font-size: 38px; font-weight: 400; line-height: 1.1; }
.roll-stat .dice { justify-content: center; margin: 10px 0 6px; }

.note { color: var(--muted); font-size: 15px; line-height: 1.5; }
.note.warn { color: var(--gold); }

/* ---------- dice ---------- */

.dice { display: flex; gap: 6px; }

.die {
  display: inline-grid;
  grid-template-columns: repeat(3, 1fr);
  width: 28px;
  height: 28px;
  padding: 4px;
  gap: 1px;
  border-radius: 7px;
  background: #f4efe3;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, .18);
  vertical-align: middle;
}

.die i { border-radius: 50%; }
.die i.on { background: #1a1a22; }
.die[data-v="1"] i.on { background: var(--neon); }

.dice.big .die { width: 64px; height: 64px; padding: 9px; gap: 3px; border-radius: 14px; }

.die.roll { animation: settle .35s ease-out; }
.die.tumble { transform: rotate(var(--r, 12deg)); }
.die.tumble:nth-child(2) { --r: -16deg; }
.die.land { animation: land .3s ease-out; }

@keyframes settle { from { transform: rotate(-25deg) scale(.7); opacity: .3; } }
@keyframes land { 0% { transform: scale(1.25); } 100% { transform: scale(1); } }

/* ---------- stat bar ---------- */

.bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr)) 24px;
  align-items: start;
  width: 100%;
  padding: calc(var(--safe-top) + 9px) 14px 7px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: rgba(11, 13, 23, .86);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  text-align: center;
}

.stat { min-width: 0; }
.stat small { display: block; padding-left: .1em; font-size: 10px; line-height: 14px; letter-spacing: .1em; text-transform: uppercase; color: var(--muted); }
.stat b { display: block; font-size: 19px; line-height: 24px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat b span { font-size: 13px; color: var(--muted); font-weight: 400; }
.stat.coins b { color: var(--gold); }

.meter { display: block; height: 4px; margin-top: 4px; border-radius: 2px; background: var(--line); overflow: hidden; }
.meter > span { display: block; height: 100%; background: var(--good); border-radius: 2px; transition: width .3s ease; }
.stat .meter { width: 56px; margin: 3px auto 0; }
.stamina.low b { color: var(--bad); }
.stamina.low .meter > span { background: var(--bad); }

.bar-menu { width: 20px; height: 20px; margin: 16px 0 0 4px; fill: none; stroke: var(--muted); stroke-width: 1.6; stroke-linecap: round; }

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

.page { padding: 0 0 calc(var(--safe-bottom) + 40px); }
.page.fresh { animation: fade .45s ease-out; }

@keyframes fade { from { opacity: 0; transform: translateY(6px); } }

.art {
  position: relative;
  margin: 0;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  background: var(--bg-2);
}

.art .ph {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px 20px 30%;
  text-align: center;
  background:
    radial-gradient(80% 70% at 75% 25%, rgba(255, 79, 139, .28), transparent 60%),
    radial-gradient(70% 60% at 15% 85%, rgba(79, 216, 255, .22), transparent 60%),
    linear-gradient(160deg, #1b1f45, #0d0f1f);
}

.art .ph span {
  font-family: var(--serif);
  font-size: 28px;
  line-height: 1.15;
  color: rgba(236, 232, 244, .16);
}

.art img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity .5s ease;
}

.art img.loaded { opacity: 1; }

.art::after {
  content: '';
  position: absolute;
  inset: auto 0 0;
  height: 40%;
  background: linear-gradient(transparent, var(--bg));
  pointer-events: none;
}

.place {
  position: relative;
  margin: -34px 20px 18px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--cyan);
}

.place code { font-size: 11px; color: var(--faint); letter-spacing: 0; text-transform: none; }

.prose {
  padding: 0 20px;
  font-family: var(--serif);
  font-size: 19px;
  line-height: 1.58;
  color: #e9e5f2;
}

.prose p { margin: 0 0 1em; }
.prose em { color: #fff; }

.events {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 20px 20px;
  padding: 0;
  list-style: none;
}

.events li {
  padding: 5px 11px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  background: var(--card);
  border: 1px solid var(--line);
}

.events .good { color: var(--good); border-color: rgba(111, 219, 164, .35); }
.events .bad { color: var(--bad); border-color: rgba(255, 112, 112, .35); }
.events .magic { color: var(--magic); border-color: rgba(185, 161, 255, .35); }
.events .info { color: var(--muted); }

/* ---------- choices ---------- */

.choices { display: flex; flex-direction: column; gap: 10px; padding: 4px 16px 0; }

.choice {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 56px;
  padding: 12px 16px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  font-family: var(--serif);
  font-size: 18px;
  line-height: 1.35;
}

.choice .arrow { flex: none; width: 22px; text-align: center; font-size: 26px; line-height: 1; color: var(--neon); font-family: var(--sans); }

.choice .kanji {
  flex: none;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: rgba(185, 161, 255, .12);
  font-family: var(--jp);
  font-size: 24px;
  color: var(--magic);
}

.choice.magic { border-color: rgba(185, 161, 255, .35); }
.choice .label { flex: 1; }

.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.tags:empty { display: none; }

.tag {
  font-family: var(--sans);
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
}

.tag.luck, .tag.roll { color: var(--gold); }
.tag.skill { color: var(--cyan); }
.tag.spell { color: var(--magic); }
.tag.cost { color: var(--gold); }
.tag.why { color: var(--bad); }

/* ---------- shop ---------- */

.shop, .fight, .end {
  margin: 0 16px 20px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-2);
}

.shop h3, .fight h3 {
  margin: 0 0 10px;
  font-size: 12px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--muted);
}

.fight h3 small { color: var(--good); letter-spacing: 0; text-transform: none; font-size: 12px; margin-left: 6px; }

.ware { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-top: 1px solid var(--line); }
.ware:first-of-type { border-top: 0; }
.ware div { flex: 1; }
.ware b { display: block; font-size: 16px; }
.ware small { display: block; color: var(--muted); font-size: 13px; line-height: 1.4; margin-top: 2px; }

.ware button {
  flex: none;
  min-width: 84px;
  min-height: 40px;
  border: 1px solid rgba(242, 193, 78, .4);
  border-radius: 10px;
  background: transparent;
  color: var(--gold);
  font-weight: 600;
  font-size: 14px;
}

/* ---------- fight ---------- */

.fight { border-color: rgba(255, 79, 139, .35); }

.foes { display: grid; gap: 8px; }

.foe { padding: 10px 12px; border-radius: 10px; background: var(--card); }
.foe.waiting { opacity: .55; }
.foe.dead { opacity: .35; text-decoration: line-through; }
.foe-name { font-weight: 600; }
.foe-stats { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); margin-top: 2px; }
.foe .meter > span { background: var(--neon); }

.round {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.round .side { text-align: center; }
.round .side small { display: block; font-size: 12px; color: var(--muted); }
.round .side small:first-child { margin-bottom: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.round .side .die { margin: 0 2px; }
.round .side b { display: block; font-family: var(--serif); font-size: 30px; font-weight: 400; margin-top: 4px; }
.round .vs { color: var(--faint); font-style: italic; font-family: var(--serif); }

.verdict, .round .luck, .round .other { grid-column: 1 / -1; margin: 6px 0 0; text-align: center; font-size: 15px; }
.round .other { color: var(--muted); }
.round .other.bad { color: var(--bad); }
.round .other .die { width: 20px; height: 20px; padding: 3px; margin-right: 3px; }
.verdict.hit { color: var(--good); }
.verdict.hurt { color: var(--bad); }
.verdict.tie { color: var(--muted); }
.round .luck .die { width: 20px; height: 20px; padding: 3px; margin-right: 3px; }
.round .luck.good { color: var(--good); }
.round .luck.bad { color: var(--bad); }

.fight-actions button small { display: inline; margin-left: 6px; }

/* ---------- endings ---------- */

.end { text-align: center; padding: 22px 18px; }
.end h3 { margin: 0 0 8px; font-family: var(--serif); font-size: 26px; font-weight: 400; }
.end p { color: var(--muted); line-height: 1.5; }
.end.death { border-color: rgba(255, 112, 112, .4); }
.end.death .kicker { color: var(--bad); }
.end.victory { border-color: rgba(242, 193, 78, .5); box-shadow: 0 0 40px -12px rgba(242, 193, 78, .45); }
.end.victory .kicker { color: var(--gold); }

/* ---------- overlays ---------- */

#overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(5, 6, 12, .66);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  animation: fadein .2s ease-out;
}

#overlay[hidden] { display: none; }

@keyframes fadein { from { opacity: 0; } }

.sheet {
  width: 100%;
  max-width: 640px;
  max-height: 92dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 6px 18px calc(var(--safe-bottom) + 24px);
  border-radius: 22px 22px 0 0;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-bottom: 0;
  animation: rise .25s ease-out;
}

@keyframes rise { from { transform: translateY(40px); opacity: .6; } }

.sheet header {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  background: var(--bg-2);
  z-index: 1;
}

.sheet header h2 { margin: 0; font-family: var(--serif); font-weight: 400; font-size: 24px; }
.sheet header button { width: auto; min-height: 40px; margin: 0; padding: 0 4px; border: 0; background: none; color: var(--cyan); font-size: 17px; }

.sheet h3 { margin: 22px 0 8px; font-size: 12px; letter-spacing: .14em; text-transform: uppercase; color: var(--muted); }

.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat-grid div { padding: 12px; text-align: center; border-radius: 12px; background: var(--card); }
.stat-grid small { display: block; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.stat-grid b { display: block; font-family: var(--serif); font-size: 32px; font-weight: 400; }

.supplies { margin-top: 12px; display: grid; gap: 8px; }
.supplies > div { display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: 12px; background: var(--card); }
.supplies span { flex: 1; }
.supplies b { display: block; }
.supplies small { display: block; color: var(--muted); font-size: 13px; margin-top: 2px; }
.supplies button {
  flex: none;
  min-width: 72px;
  min-height: 40px;
  border-radius: 10px;
  border: 1px solid rgba(111, 219, 164, .45);
  background: transparent;
  color: var(--good);
  font-weight: 600;
}

.items, .spells { margin: 0; padding: 0; list-style: none; }
.items li { padding: 10px 0; border-top: 1px solid var(--line); }
.items li:first-child { border-top: 0; }
.items b { display: block; }
.items small { display: block; color: var(--muted); font-size: 14px; line-height: 1.45; margin-top: 2px; }

.spells li { display: flex; gap: 14px; padding: 12px 0; border-top: 1px solid var(--line); }
.spells li:first-child { border-top: 0; }
.spells .kanji {
  flex: none;
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: rgba(185, 161, 255, .12);
  font-family: var(--jp);
  font-size: 28px;
  color: var(--magic);
}
.spells b { display: block; }
.spells small { display: block; color: var(--muted); font-size: 14px; line-height: 1.45; margin-top: 2px; }
.spells small.cost { color: var(--magic); }

.rules summary { cursor: pointer; color: var(--cyan); font-weight: 600; padding: 6px 0; }
.rules p { color: var(--muted); font-size: 15px; line-height: 1.5; }
.rules b { color: var(--ink); }

.dev select { width: 100%; min-height: 44px; font-size: 16px; background: var(--card); color: var(--ink); border: 1px solid var(--line); border-radius: 10px; }

.dice-modal {
  align-self: center;
  width: calc(100% - 48px);
  max-width: 360px;
  padding: 26px 22px 20px;
  text-align: center;
  border-radius: 22px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  box-shadow: 0 30px 80px -20px rgba(0, 0, 0, .8);
}

.dice-modal .dice { justify-content: center; gap: 14px; margin: 18px 0; }
.dice-modal .result { font-family: var(--serif); font-size: 19px; line-height: 1.45; margin: 0 0 8px; }
.dice-modal .result small { display: block; margin-top: 4px; font-family: var(--sans); font-size: 13px; color: var(--muted); }
.dice-modal .good { color: var(--good); }
.dice-modal .bad { color: var(--bad); }

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

/* ---------- memories ---------- */

.events .memory {
  flex-basis: 100%;
  border-radius: 12px;
  color: var(--gold);
  border-color: rgba(242, 193, 78, .4);
  font-family: var(--serif);
  font-size: 16px;
  font-style: italic;
  font-weight: 400;
}

.remembered { margin: 18px 0 6px; text-align: left; }
.remembered .kicker { text-align: center; }
.remembered ul, .memories { margin: 0; padding: 0; list-style: none; }
.remembered li, .memories li {
  padding: 8px 0;
  border-top: 1px solid var(--line);
  font-family: var(--serif);
  font-style: italic;
  color: var(--ink);
  line-height: 1.4;
}

.events .standing {
  flex-basis: 100%;
  border-radius: 12px;
  color: var(--cyan);
  border-color: rgba(79, 216, 255, .35);
  font-family: var(--serif);
  font-size: 16px;
  font-style: italic;
  font-weight: 400;
}

.memories.standing li { color: var(--cyan); }

.tag.locked { color: var(--muted); font-style: italic; }
.tag.locked::before { content: '🔒 '; font-style: normal; }
.choice.locked .label { color: var(--muted); }
.mod-reasons { display: block; margin-top: 4px; font-size: 13px; color: var(--cyan); font-style: italic; }
button.choice.locked:disabled { opacity: .75; }

/* ---------- sound ---------- */

.sound-hint {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 16px);
  transform: translateX(-50%);
  z-index: 20;
  padding: 8px 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(11, 13, 23, .96);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  color: var(--muted);
  font-size: 14px;
}
