:root {
  color-scheme: light;
  --paper: #fbf7f0;
  --paper-strong: #fffdfa;
  --ink: #1f1b18;
  --vermilion: #c4392b;
  --vermilion-soft: #f5ddd7;
  --rule: #c9bfb2;
  --muted: #6f665e;
  --page-gutter: clamp(16px, 5vw, 32px);
  --measure: 40em;
  --controls-height: 126px;
  --min-touch: 44px;
  --font-mincho: "Hiragino Mincho ProN", "Yu Mincho", serif;
  --font-gothic: -apple-system, "Hiragino Sans", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  overscroll-behavior: none;
}

body {
  overflow: hidden;
  color: var(--ink);
  background: var(--paper);
  font-family: var(--font-gothic);
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
  text-rendering: optimizeLegibility;
}

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

button {
  border-radius: 0;
  cursor: pointer;
}

button:focus-visible,
input:focus-visible,
.ladder-view__start-hit:focus-visible {
  outline: 3px solid var(--ink);
  outline-offset: 3px;
}

.game-shell {
  position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) var(--controls-height);
  width: min(100%, 48rem);
  min-height: 100vh;
  min-height: 100dvh;
  margin-inline: auto;
  background-color: var(--paper);
  border-inline: 1px solid var(--rule);
}

.masthead {
  padding: calc(18px + env(safe-area-inset-top)) var(--page-gutter) 16px;
  text-align: center;
  border-bottom: 1px solid var(--ink);
}

.masthead__kicker,
.masthead__subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.75rem;
  letter-spacing: 0.16em;
}

.masthead h1 {
  margin: 2px 0 0;
  font-family: var(--font-mincho);
  font-size: clamp(1.75rem, 8vw, 2.5rem);
  font-weight: 700;
  letter-spacing: 0.08em;
  line-height: 1.35;
  text-wrap: balance;
  word-break: auto-phrase;
}

.masthead h1::after {
  display: block;
  width: 2.5rem;
  height: 3px;
  margin: 10px auto 8px;
  background: var(--vermilion);
  content: "";
}

#log {
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 24px var(--page-gutter) 32px;
  overscroll-behavior-y: contain;
  scrollbar-gutter: stable;
}

#log:empty {
  display: grid;
  place-items: center;
}

#log:empty::before {
  width: min(100%, 28em);
  padding-block: 24px;
  color: var(--muted);
  border-block: 1px solid var(--rule);
  content: "先生の言葉に続けて、ひらがなで答えましょう。";
  font-family: var(--font-mincho);
  line-height: 1.7;
  text-align: center;
  text-wrap: balance;
  word-break: auto-phrase;
}

.turn {
  position: relative;
  width: min(88%, 32rem);
  margin: 0 0 18px;
  padding: 16px 18px 14px;
  border: 1px solid var(--rule);
  background: var(--paper-strong);
}

.turn::before {
  position: absolute;
  top: -0.72em;
  padding-inline: 0.45em;
  background: var(--paper);
  color: var(--muted);
  content: "";
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.12em;
}

.turn--ai {
  margin-right: auto;
  border-left: 3px solid var(--ink);
}

.turn--ai::before {
  left: 12px;
  content: "先生";
}

.turn--you {
  margin-left: auto;
  border-color: var(--vermilion);
  border-right-width: 3px;
  background: var(--vermilion-soft);
  text-align: right;
}

.turn--you::before {
  right: 12px;
  color: var(--vermilion);
  content: "あなた";
}

.turn--say {
  width: min(100%, var(--measure));
  margin: 10px auto 24px;
  padding: 0 0 0 1em;
  border: 0;
  border-left: 2px solid var(--rule);
  background: transparent;
  color: var(--muted);
}

.turn--say::before {
  display: none;
}

.turn__word,
.turn__gloss {
  margin: 0;
}

.turn__word {
  overflow-wrap: anywhere;
  font-family: var(--font-mincho);
  font-size: clamp(1.25rem, 5vw, 1.65rem);
  font-weight: 700;
  letter-spacing: 0.05em;
  line-height: 1.55;
}

.turn--say .turn__word {
  white-space: pre-line;
  font-family: var(--font-gothic);
  font-size: 0.95rem;
  font-weight: 400;
  letter-spacing: 0;
  line-height: 1.65;
  text-wrap: pretty;
  word-break: auto-phrase;
}

.turn__gloss {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.5;
}

.turn__gloss:empty {
  display: none;
}

.play-controls {
  z-index: 2;
  align-self: end;
  width: 100%;
  min-height: var(--controls-height);
  padding: 12px var(--page-gutter) calc(10px + env(safe-area-inset-bottom));
  background: var(--paper);
  border-top: 1px solid var(--ink);
}

#form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  width: 100%;
}

#word {
  width: 100%;
  min-width: 0;
  min-height: 48px;
  padding: 10px 12px;
  border: 1px solid var(--ink);
  border-radius: 0;
  background: var(--paper-strong);
  font-size: 16px;
  line-height: 1.5;
}

#word::placeholder {
  color: var(--muted);
  opacity: 1;
}

#send,
#giveup,
#share,
#again {
  min-height: 44px;
}

#send {
  min-width: 72px;
  padding-inline: 18px;
  border: 1px solid var(--vermilion);
  background: var(--vermilion);
  color: var(--paper-strong);
  font-weight: 700;
}

.control-note {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 44px;
}

#input-note {
  max-width: var(--measure);
  margin: 0;
  color: var(--muted);
  font-size: 0.7rem;
  line-height: 1.5;
  text-wrap: pretty;
  word-break: auto-phrase;
}

#giveup {
  flex: 0 0 auto;
  padding-inline: 8px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
  text-decoration: underline;
  text-underline-offset: 0.25em;
}

#ending[hidden] {
  display: none;
}

#ending {
  position: absolute;
  z-index: 10;
  inset: 0;
  display: grid;
  place-items: center;
  padding: calc(20px + env(safe-area-inset-top)) var(--page-gutter)
    calc(20px + env(safe-area-inset-bottom));
  overflow: hidden;
  background: var(--paper);
}

#ending:not([hidden]) {
  animation: ending-reveal 420ms ease-out both;
}

.ending__sheet {
  width: min(100%, 36rem);
  max-height: 100%;
  padding: clamp(20px, 6vw, 40px);
  overflow-y: auto;
  border-block: 3px double var(--ink);
  text-align: center;
}

#ending-verdict {
  margin: 0;
  font-family: var(--font-mincho);
  font-size: clamp(1.5rem, 6vw, 2rem);
  line-height: 1.4;
  text-wrap: balance;
  word-break: auto-phrase;
}

#ending-answer {
  min-height: 1.45em;
  margin: 10px 0 18px;
  color: var(--vermilion);
  font-family: var(--font-mincho);
  font-size: clamp(2.25rem, 12vw, 4rem);
  font-weight: 700;
  letter-spacing: 0.08em;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.sealed {
  padding-block: 12px;
  border-block: 1px solid var(--rule);
}

.sealed h3 {
  margin: 0 0 8px;
  font-family: var(--font-mincho);
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-wrap: balance;
  word-break: auto-phrase;
}

.sealed__explanation {
  max-width: var(--measure);
  margin: 0 auto 8px;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.6;
  text-align: left;
  text-wrap: pretty;
  word-break: auto-phrase;
}

#sealed-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 2px 14px;
  max-width: var(--measure);
  min-height: 1lh;
  margin: 0 auto;
  padding: 0;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.6;
  list-style: none;
}

.sealed__word--answer {
  color: var(--vermilion);
  font-weight: 700;
}

.sealed__word--answer::after {
  content: "（答え）";
}

#ending-question {
  margin: 18px 0 12px;
  font-family: var(--font-mincho);
  font-size: clamp(1rem, 4vw, 1.25rem);
  font-weight: 700;
  line-height: 1.6;
  text-wrap: balance;
  word-break: auto-phrase;
}

.ending__actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

#share,
#again {
  padding: 9px 12px;
  border: 1px solid var(--ink);
  background: var(--paper);
  font-weight: 700;
}

.ending__status {
  min-height: 1.6em;
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.75rem;
}

#again {
  border-color: var(--vermilion);
  background: var(--vermilion);
  color: var(--paper-strong);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.ladder-view {
  display: block;
  width: auto;
  max-width: min(100%, 320px);
  height: auto;
  max-height: 100%;
  margin: auto;
  overflow: visible;
  touch-action: manipulation;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.ladder-view__rail,
.ladder-view__rung {
  fill: none;
  stroke: var(--ink);
  stroke-linecap: square;
  stroke-width: 1.5;
}

.ladder-view__goal {
  fill: none;
  stroke: var(--ink);
  stroke-linecap: square;
  stroke-width: 2.5;
}

.ladder-view__start-knob {
  fill: var(--paper);
  stroke: var(--ink);
  stroke-width: 2;
}

.ladder-view__label {
  fill: var(--ink);
  font-family: var(--font-mincho);
  font-size: 16px;
  font-weight: 600;
}

.ladder-view__start-hit,
.ladder-view__placement-hit {
  fill: var(--paper);
  fill-opacity: 0;
  pointer-events: all;
  touch-action: manipulation;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.ladder-view--start-selectable .ladder-view__start-knob {
  fill: var(--vermilion);
  stroke: var(--vermilion);
  stroke-width: 4;
}

.ladder-view--start-selectable .ladder-view__start-hit {
  cursor: pointer;
}

.ladder-view__start-hit:focus-visible {
  fill: var(--rule);
  fill-opacity: 0.3;
  stroke: var(--ink);
  stroke-width: 2;
}

.ladder-view__start-hit.is-disabled {
  pointer-events: none;
}

.ladder-view__placement-hit {
  cursor: pointer;
}

.ladder-view__trace {
  fill: none;
  stroke: var(--vermilion);
  stroke-linecap: square;
  stroke-linejoin: miter;
  stroke-width: 4;
}

.amida-page {
  height: 100vh;
  height: 100dvh;
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
}

.amida-shell {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  width: min(100%, 48rem);
  height: 100vh;
  height: 100dvh;
  min-height: 100vh;
  min-height: 100dvh;
  margin-inline: auto;
  overflow: hidden;
  background: var(--paper);
  border-inline: 1px solid var(--rule);
}

.amida-masthead {
  padding: calc(4px + env(safe-area-inset-top)) var(--page-gutter) 4px;
}

.amida-masthead h1 {
  margin: 0;
  font-size: clamp(1.35rem, 6.2vw, 1.75rem);
  line-height: 1.2;
}

.amida-masthead h1::after {
  display: none;
}

#amida-app {
  min-height: 0;
  padding: 8px var(--page-gutter) calc(8px + env(safe-area-inset-bottom));
  overflow: hidden;
}

.amida-step {
  display: grid;
  gap: 10px;
  width: min(100%, var(--measure));
  height: 100%;
  margin-inline: auto;
}

.amida-step__lead {
  margin: 0;
  color: var(--muted);
  font-size: 16px;
  line-height: 1.35;
}

.amida-step__title {
  margin: 0;
  font-family: var(--font-mincho);
  font-size: clamp(1.35rem, 6vw, 1.65rem);
  line-height: 1.2;
}

.amida-form {
  display: grid;
  gap: 14px;
}

.amida-step--candidates {
  grid-template-rows: auto auto minmax(0, 1fr);
  overflow: hidden;
}

.amida-step--candidates .amida-form {
  align-content: start;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior-y: contain;
}

.amida-label {
  display: block;
  margin-bottom: 4px;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.35;
}

.amida-input {
  width: 100%;
  min-height: 48px;
  padding: 10px 12px;
  border: 1px solid var(--ink);
  border-radius: 0;
  background: var(--paper-strong);
  font-size: 16px;
  line-height: 1.5;
}

.amida-form__error {
  min-height: 1.6em;
  margin: -4px 0 0;
  color: var(--vermilion);
  font-size: 16px;
  line-height: 1.35;
}

.amida-action {
  min-width: var(--min-touch);
  min-height: var(--min-touch);
  padding: 10px 16px;
  border: 1px solid var(--vermilion);
  background: var(--vermilion);
  color: var(--paper-strong);
  font-weight: 700;
}

.amida-ladder {
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  border-block: 1px solid var(--rule);
}

.amida-result {
  margin: 0;
  padding: 6px 10px;
  border-block: 3px double var(--ink);
  color: var(--vermilion);
  font-family: var(--font-mincho);
  font-size: clamp(1.25rem, 7vw, 1.75rem);
  font-weight: 700;
  line-height: 1.25;
  text-align: center;
  overflow-wrap: anywhere;
}

.amida-step--ladder {
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 6px;
  overflow: hidden;
}

.amida-step--ladder .amida-step__title {
  grid-row: 1;
}

.amida-step--ladder .amida-step__lead {
  grid-row: 2;
}

.amida-step--ladder .amida-ladder {
  grid-row: 3;
}

.amida-step--ladder > .amida-action,
.amida-step--ladder > .amida-result {
  grid-row: 4;
}

.amida-layout-probe {
  visibility: hidden;
  pointer-events: none;
}

.amida-step--result {
  position: relative;
  isolation: isolate;
}

.ladder-view__trace-segment.is-animated,
.ladder-view__fakeout-rung.is-animated,
.ladder-view__breakthrough-rung.is-animated,
.ladder-view__rail--answer.is-animated {
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
}

.ladder-view__trace-segment.is-animated {
  animation: amida-trace var(--trace-duration, 300ms) linear forwards;
}

.ladder-view__fakeout-rung,
.ladder-view__breakthrough-rung {
  fill: none;
  stroke: var(--vermilion);
  stroke-linecap: square;
  stroke-width: 5;
}

.ladder-view__fakeout-rung.is-animated {
  animation: amida-fakeout var(--fakeout-duration, 500ms) ease-in-out forwards;
}

.ladder-view__breakthrough-rung.is-animated {
  animation: amida-breakthrough var(--breakthrough-duration, 400ms) ease-in forwards;
}

.ladder-view__rail--answer {
  stroke: var(--vermilion);
  stroke-width: 4;
}

.ladder-view__rail--answer.is-animated {
  animation: amida-rail-grow var(--rail-grow-duration, 300ms) ease-in forwards;
}

.ladder-view__trace--dragged {
  stroke-width: 6;
}

.ladder-view__trace--dragged.is-animated {
  animation-name: amida-drag;
  animation-timing-function: steps(5, jump-none);
}

.amida-ladder--tremor.is-animated {
  animation: amida-tremor var(--tremor-duration, 200ms) steps(2, jump-none) both;
}

.amida-ladder--quake.is-animated {
  animation: amida-quake var(--quake-duration, 300ms) steps(2, jump-none) both;
}

.amida-result--blinking.is-animated {
  animation: amida-result-blink var(--blink-duration, 300ms) steps(4, jump-none) both;
}

.amida-focus,
.amida-vermilion-flash {
  position: fixed;
  z-index: 20;
  inset: 0;
  pointer-events: none;
}

.amida-focus {
  background: repeating-conic-gradient(
    from 0deg at 50% 50%,
    var(--vermilion) 0deg 1deg,
    transparent 1deg 10deg
  );
  opacity: 0.58;
}

.amida-focus.is-animated {
  animation: amida-focus var(--focus-duration, 400ms) ease-in both;
}

.amida-vermilion-flash {
  z-index: 21;
  background: var(--vermilion);
  opacity: 0.72;
}

.amida-vermilion-flash.is-animated {
  animation: amida-vermilion-flash var(--flash-duration, 300ms) steps(2, jump-none) both;
}

.amida-result--overruled {
  color: var(--ink);
  text-decoration: line-through 3px var(--vermilion);
  opacity: 0.45;
}

.ladder-view__label--answer-slot,
.ladder-view__label--answer {
  font-size: 18px;
}

.ladder-view__label--answer {
  fill: var(--vermilion);
  font-weight: 700;
  transform-box: fill-box;
  transform-origin: center;
}

.ladder-view__label--answer.is-animated {
  animation: amida-answer-open var(--answer-duration, 400ms) steps(2, jump-none) both;
}

.amida-step--reduced-motion .amida-ladder--tremor,
.amida-step--reduced-motion .amida-ladder--quake {
  outline: 3px double var(--vermilion);
}

.amida-step--reduced-motion .amida-result--blinking {
  color: var(--paper);
  background: var(--vermilion);
}

@keyframes amida-trace {
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes amida-fakeout {
  0% {
    stroke-dashoffset: 1;
  }

  58% {
    stroke-dashoffset: 0;
  }

  72% {
    stroke-dashoffset: 0;
  }

  100% {
    stroke-dashoffset: 0.38;
  }
}

@keyframes amida-breakthrough {
  0% {
    stroke-dashoffset: 1;
    stroke-width: 5;
  }

  72% {
    stroke-width: 10;
  }

  100% {
    stroke-dashoffset: 0;
    stroke-width: 5;
  }
}

@keyframes amida-rail-grow {
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes amida-drag {
  0% {
    stroke-dashoffset: 1;
    transform: translateX(-2px);
  }

  50% {
    transform: translateX(3px);
  }

  100% {
    stroke-dashoffset: 0;
    transform: translateX(0);
  }
}

@keyframes amida-tremor {
  0% {
    transform: translate(-1px, 0);
  }

  50% {
    transform: translate(1px, -1px);
  }

  100% {
    transform: translate(0, 0);
  }
}

@keyframes amida-quake {
  0% {
    transform: translate(-5px, 3px) rotate(-0.3deg);
  }

  33% {
    transform: translate(6px, -4px) rotate(0.4deg);
  }

  66% {
    transform: translate(-7px, -2px) rotate(-0.5deg);
  }

  100% {
    transform: translate(0, 0) rotate(0);
  }
}

@keyframes amida-result-blink {
  0%,
  100% {
    color: var(--vermilion);
    background: var(--paper);
  }

  50% {
    color: var(--paper);
    background: var(--vermilion);
  }
}

@keyframes amida-focus {
  from {
    opacity: 0;
    transform: scale(1.45);
  }

  to {
    opacity: 0.58;
    transform: scale(1);
  }
}

@keyframes amida-vermilion-flash {
  0%,
  55% {
    opacity: 0.72;
  }

  100% {
    opacity: 0;
  }
}

@keyframes amida-answer-open {
  0% {
    opacity: 0;
    transform: scaleX(0);
  }

  100% {
    opacity: 1;
    transform: scaleX(1);
  }
}

@keyframes ending-reveal {
  from {
    opacity: 0;
    transform: translateY(12px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-height: 560px) {
  :root {
    --controls-height: 108px;
  }

  .masthead {
    padding-top: calc(8px + env(safe-area-inset-top));
    padding-bottom: 8px;
  }

  .masthead__kicker,
  .masthead h1::after {
    display: none;
  }

  .masthead h1 {
    font-size: 1.4rem;
  }

  #log {
    padding-block: 16px;
  }

  .ending__sheet {
    padding-block: 12px;
  }

  #ending-answer {
    margin-block: 4px 8px;
    font-size: 2rem;
  }

  #ending-question {
    margin-block: 8px;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --paper: #16130f;
    --paper-strong: #211c17;
    --ink: #eee7dd;
    --vermilion: #a94a40;
    --vermilion-soft: #38211d;
    --rule: #574d43;
    --muted: #b8aea3;
  }
}

@media (prefers-reduced-motion: reduce) {
  #ending:not([hidden]) {
    animation: none;
  }

  .ladder-view__trace-segment,
  .ladder-view__fakeout-rung,
  .ladder-view__breakthrough-rung,
  .ladder-view__rail--answer,
  .ladder-view__trace--dragged,
  .amida-ladder--tremor,
  .amida-ladder--quake,
  .amida-result--blinking,
  .amida-focus,
  .amida-vermilion-flash,
  .ladder-view__label--answer {
    animation: none;
  }
}
