:root {
  color-scheme: dark;
  --ink: #110e17;
  --ivory: #f4ead9;
  --gold: #d9b980;
  --muted: #c1b8c9;
  --line: #c0a38146;
  --teal: #80dccc;
  --red: #ef979d;
  --serif: Georgia, 'Times New Roman', 'Yu Mincho', 'Hiragino Mincho ProN', serif;
  --sans: 'Yu Gothic UI', 'Hiragino Kaku Gothic ProN', Meiryo, system-ui, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; background: var(--ink); color: var(--ivory); font: 400 1rem/1.5 var(--sans); }
button { color: inherit; font: inherit; cursor: pointer; touch-action: manipulation; }
button:disabled { cursor: default; }
button:focus-visible { outline: 2px solid var(--teal); outline-offset: 3px; }
h1, h2, p { margin: 0; }

.game-frame {
  min-height: 100svh;
  position: relative;
  isolation: isolate;
  padding: max(1rem, env(safe-area-inset-top)) max(1.75rem, env(safe-area-inset-right)) max(1rem, env(safe-area-inset-bottom)) max(1.75rem, env(safe-area-inset-left));
  background: #140f1c;
}
.game-frame::before {
  position: absolute;
  z-index: -1;
  inset: 0;
  content: '';
  background: linear-gradient(100deg,#140f1cfa 1%,#140f1cea 50%,#140f1cb5), linear-gradient(0deg,#100e17 1%,transparent 80%), url('./assets/ruins.jpg') center 43% / cover;
}

.topbar { display: flex; align-items: center; justify-content: space-between; gap: .65rem; position: relative; z-index: 1; width: min(100%, 90rem); margin: 0 auto; padding-bottom: .70rem; border-bottom: 1px solid #cbb1a030; }
.brand { display: inline-flex; align-items: center; gap: .72rem; border: 0; padding: 0; background: none; font: 700 .96rem/1 var(--serif); letter-spacing: .13em; text-align: left; white-space: nowrap; }
.brand b { color: var(--gold); }
.sigil { width: 2.3rem; height: 2.3rem; display: grid; place-items: center; border: 1px solid #d9b98079; background: #241a25; flex: none; }
.sigil i { width: 1.35rem; height: 1.35rem; background: #dabe8e; clip-path: polygon(0 5%,40% 33%,50% 30%,60% 33%,100% 5%,87% 61%,50% 96%,13% 61%,27% 60%,50% 79%,73% 60%,78% 37%,57% 51%,50% 47%,43% 51%,22% 37%,27% 60%,13% 61%); }
.utility { display: flex; gap: .5rem; align-items: center; }
.icon-button { display: inline-flex; align-items: center; justify-content: center; gap: .42rem; padding: 0 .75rem; min-height: 2.3rem; min-width: 2.3rem; border: 1px solid var(--line); background: #191322d6; font-size: 1rem; color: var(--gold); }
.utility-label { color: var(--ivory); font-size: .83rem; white-space: nowrap; }
.icon-button:hover, .button.secondary:hover { border-color: var(--gold); background-color: #3a2a37; }

.overline { font: 700 .85rem/1.4 var(--sans); letter-spacing: .20em; color: var(--gold); }
.button { min-height: 2.8rem; padding: .55rem .95rem; border: 1px solid transparent; display: inline-flex; gap: .45rem; justify-content: center; align-items: center; font-weight: 700; text-align: center; white-space: nowrap; }
.button.primary { background: linear-gradient(150deg,#f4d79e,#bb8e52); color: #251725; border-color: #ead0a0; box-shadow: inset 0 0 0 2px #5b3e3335, 0 .3rem 1.1rem #130c1490; }
.button.primary:hover { background: linear-gradient(150deg,#ffe6b2,#d0a365); }
.button.secondary { border-color: #c9a77c85; background: #1c1524d9; }
.button:disabled, .text-button:disabled, .icon-button:disabled { opacity: .43; }
.button.compact { min-height: 2.3rem; padding: .3rem .60rem; font-size: .87rem; }
.text-button { color: var(--gold); background: none; padding: .32rem .25rem; border: 0; font-size: .88rem; font-weight: 600; }
.text-button:hover:not(:disabled) { text-decoration: underline; text-underline-offset: .17em; }

.subscreen { width: min(100%, 74rem); margin: 0 auto; }
.subscreen { padding: clamp(1rem,3.4vw,3rem) 0 1rem; }
.screen-head { display: flex; gap: .6rem; justify-content: space-between; align-items: end; margin-bottom: 1rem; }
.screen-head h1 { font: 700 clamp(1.55rem,3.5vw,2.65rem)/1.27 var(--serif); margin-top: .2rem; }
.panel { border: 1px solid var(--line); background: linear-gradient(142deg,#241b2ef7,#170f1fef); box-shadow: inset 0 0 0 3px #c2a58a0b, 0 .7rem 2rem #07060b6d; }
.study-layout { display: grid; grid-template-columns: 1.6fr 1fr; gap: .8rem; }
.study-list { padding: .85rem; }
.list-tools { display: flex; align-items: center; justify-content: space-between; gap: .45rem; margin-bottom: .7rem; }
.segmented { display: flex; gap: .22rem; padding: .15rem; border: 1px solid #c4a78636; }
.segmented button { background: transparent; border: 0; min-height: 2rem; padding: .25rem .65rem; color: var(--muted); font-size: .85rem; }
.segmented button[aria-pressed='true'] { background: #4b3741; color: #ffdfa4; }
.words-grid { display: grid; grid-template-columns: repeat(4,minmax(0,1fr)); gap: .38rem; }
.word-tile { display: flex; flex-direction: column; align-items: flex-start; gap: .02rem; background: #160f20b0; border: 1px solid #b89b7b30; padding: .52rem .60rem; text-align: left; min-width: 0; }
.word-tile:hover, .word-tile.is-selected { border-color: #d9b980b0; background: #352637; }
.word-tile strong { font: 700 1.1rem/1.1 var(--serif); }
.word-tile span { color: var(--muted); font-size: .78rem; }
.word-detail { display: flex; flex-direction: column; align-items: flex-start; padding: clamp(1rem,3vw,2.5rem); }
.word-detail h2 { font: 700 clamp(2.75rem,6vw,5rem)/1.11 var(--serif); margin-top: .32rem; }
.translation { margin-top: .15rem; color: var(--gold); font-size: 1.2rem; }
.example { width: 100%; margin-top: .82rem; padding-top: .65rem; border-top: 1px solid var(--line); color: var(--muted); }
.example p:first-child { font: 500 1.13rem/1.45 var(--serif); color: var(--ivory); }
.example p + p { margin-top: .25rem; }
.word-controls { display: flex; justify-content: flex-end; gap: .4rem; width: 100%; margin: auto 0 0; padding-top: .9rem; }
.word-controls button { font-size: 1.4rem; }
.reveal { margin-top: .7rem; }
.empty { grid-column: 1 / -1; padding: .6rem .2rem; color: var(--muted); }

.collection-count { color: var(--gold); white-space: nowrap; }
.collection-grid { display: grid; grid-template-columns: repeat(8,minmax(0,1fr)); gap: .65rem; }
.theme-teal { --accent: #8cdfcc; --art-bg: #284f52; }
.theme-violet { --accent: #d5b4f0; --art-bg: #4a3764; }
.theme-amber { --accent: #eccb88; --art-bg: #614638; }
.theme-blue { --accent: #a9d7fd; --art-bg: #304861; }
.theme-red { --accent: #f4a999; --art-bg: #653849; }
.card { min-width: 0; position: relative; isolation: isolate; overflow: hidden; border: 1px solid #ba9a766e; background: #1b1421; padding: .29rem; display: flex; flex-direction: column; align-items: center; text-align: center; box-shadow: inset 0 0 0 2px #c6a78715; }
button.card:hover, .card.is-selected { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent), 0 0 1rem #c8a47945; }
.card.is-blocked:not(.is-selected) { opacity: .64; }
.card.is-sealed { filter: grayscale(.7); }
.card-cost { position: absolute; z-index: 1; top: .45rem; left: .45rem; display: grid; place-items: center; width: 1.45rem; height: 1.45rem; background: #16292c; border: 1px solid #80dccc89; font: 700 .95rem/1 var(--serif); color: #c9fff3; }
.card-art { position: relative; display: grid; place-items: center; min-height: 3rem; width: 100%; aspect-ratio: 1.75; background: radial-gradient(ellipse at 50% 48%, var(--art-bg),#19131e 75%); border: 1px solid #ae9a9245; overflow: hidden; }
.card-art::before { content: ''; position: absolute; width: 34%; aspect-ratio: 1; transform: rotate(45deg); border: 1px solid var(--accent); opacity: .3; box-shadow: 0 0 0 4px #d9bfd122; }
.symbol { display: grid; place-items: center; color: var(--accent, var(--gold)); font: 700 1.6rem/1 var(--serif); text-shadow: 0 0 .65rem var(--accent, var(--gold)); }
.card-key { margin-top: .15rem; font: 700 .95rem/1.15 var(--serif); letter-spacing: .03em; }
.card-name { color: var(--muted); font-size: .76rem; line-height: 1.35; }
.card-bottom { display: flex; justify-content: center; align-items: center; gap: .65rem; min-height: 1.25rem; width: 100%; margin-top: auto; color: var(--accent); font-size: .76rem; }
.collection-grid .card { min-height: 8.15rem; }
.collection-grid .card-bottom { padding-top: .24rem; }
.attack-stat { color: #e8be83; }
.hp-stat { color: var(--red); }

.view-battle::before { background: linear-gradient(90deg,#171624e3,#201525c4,#2b1521b8), linear-gradient(0deg,#0d0b16,#120e16ae 54%,#0d0b1690), url('./assets/ruins.jpg') center 46% / cover; }

.charge-panel { width: min(100%, 35rem); padding: clamp(.85rem,2.4vw,1.6rem); }
.charge-head, .charge-foot { display: flex; justify-content: space-between; align-items: center; gap: .35rem; color: var(--gold); }
.charge-head > span + span { font: 700 1rem/1.2 var(--serif); }
.question-prompt { text-align: center; margin: .70rem 0 .85rem; }
.question-prompt p { color: var(--muted); font-size: .9rem; }
.question-prompt h1 { font: 700 clamp(2.6rem,6vw,5rem)/1.09 var(--serif); }
.choices { display: grid; grid-template-columns: 1fr 1fr; gap: .45rem; }
.choice { display: flex; align-items: center; gap: .6rem; min-height: 2.8rem; padding: .4rem .62rem; text-align: left; background: #171020; border: 1px solid #a589725e; font-weight: 650; }
.choice:hover:not(:disabled) { border-color: var(--gold); background: #372738; }
.choice kbd { flex: none; display: grid; place-items: center; width: 1.35rem; height: 1.35rem; border: 1px solid #ad967258; color: var(--gold); font: .80rem/1 var(--sans); }
.choice.correct { color: #c2ffec; border-color: #87dfc8; background: #23463f; }
.choice.incorrect { color: #ffe0e1; border-color: #d1919d; background: #4d2737; }
.choice:disabled:not(.correct,.incorrect) { opacity: .54; }
.charge-foot { min-height: 2.5rem; margin-top: .6rem; }
.feedback { color: var(--teal); font-size: .88rem; }

button.field-unit:hover, .field-unit.is-selected { border-color: var(--accent); }

.result-screen { min-height: min(78svh,45rem); display: grid; place-items: center; padding: 1rem 0; }
.result-panel { width: min(100%,36rem); padding: clamp(.85rem,2.7vw,2rem); }
.result-head { text-align: center; }
.result-head h1 { font: 700 clamp(2rem,4.2vw,3.65rem)/1.15 var(--serif); margin: .2rem 0 .32rem; }
.result-head p:last-child { color: var(--muted); }
.result-stats { display: flex; margin-top: .95rem; border-block: 1px solid var(--line); }
.result-stats div { flex: 1; display: grid; justify-items: center; align-content: center; gap: .1rem; padding: .5rem .2rem; color: var(--muted); font-size: .82rem; text-align: center; }
.result-stats div + div { border-left: 1px solid var(--line); }
.result-stats strong { color: var(--ivory); font: 700 1.8rem/1.05 var(--serif); }
.result-stats strong.reward { font-size: 1.04rem; color: var(--gold); }
.missed, .perfect { margin-top: .75rem; }
.missed h2 { font-size: .9rem; }
.missed div { display: flex; flex-wrap: wrap; gap: .3rem; margin-top: .35rem; max-height: 7rem; overflow: auto; }
.missed span { padding: .13rem .4rem; background: #322434; border: 1px solid var(--line); color: var(--muted); font-size: .8rem; }
.missed b { color: var(--ivory); }
.perfect { text-align: center; color: var(--teal); }
.result-actions { display: flex; gap: .45rem; justify-content: center; flex-wrap: wrap; margin-top: .85rem; }

.dialog { max-width: min(91vw,33rem); max-height: 90svh; padding: 0; color: var(--ivory); background: #241a2c; border: 1px solid #bd9c74a9; box-shadow: 0 0 3rem #09040d; }
.dialog::backdrop { background: #0b0611bd; }
.dialog-inner { position: relative; padding: 1rem 1.2rem; }
.dialog h2 { font: 700 1.55rem/1.4 var(--serif); padding-right: 2.5rem; }
.dialog ol, .dialog ul { padding-left: 1.35rem; }
.dialog li + li { margin-top: .34rem; }
.dialog p { color: var(--muted); }
.close-dialog { position: absolute; right: .6rem; top: .65rem; }

.learning-reminder { color: #29443f; background: linear-gradient(140deg,#f9f2df,#e4f0e9); border: 2px solid #bd9c64; overflow-y: auto; }
.learning-reminder h2 { padding-right: 0; margin: .35rem 0 .8rem; }
.learning-reminder p { color: #3f5750; font-size: 1rem; line-height: 1.65; }
.learning-reminder .learning-reminder-stats { padding: .35rem .65rem; background: #d9e7dd; border-radius: .3rem; font-weight: 700; }
.learning-reminder-actions { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: .6rem; margin-top: 1.15rem; }
.learning-reminder-actions .button { min-height: 2.8rem; white-space: normal; font-size: .95rem; }

@media (orientation: landscape) and (max-height: 560px) {
  .game-frame { padding: max(.35rem, env(safe-area-inset-top)) max(.65rem, env(safe-area-inset-right)) max(.35rem, env(safe-area-inset-bottom)) max(.65rem, env(safe-area-inset-left)); }
  .topbar { padding-bottom: .35rem; }
  .sigil, .icon-button { min-height: 1.95rem; min-width: 1.95rem; }
  .sigil { width: 1.95rem; height: 1.95rem; }
  .subscreen { padding: .55rem 0 0; }
  .screen-head { margin-bottom: .48rem; }
  .screen-head h1 { font-size: 1.55rem; }
  .study-list { padding: .4rem; }
  .list-tools { margin-bottom: .35rem; }
  .word-tile { padding: .20rem .36rem; }
  .word-tile strong { font-size: .98rem; }
  .word-tile span { font-size: .71rem; }
  .word-detail { padding: .8rem; }
  .word-detail h2 { font-size: 2.6rem; }
  .word-controls { padding-top: .42rem; }
  .charge-panel { padding: .55rem .82rem; }
  .question-prompt { margin: .26rem 0 .36rem; }
  .question-prompt p { font-size: .8rem; }
  .question-prompt h1 { font-size: min(3.15rem, 12svh); }
  .choice { min-height: 2.3rem; padding-block: .25rem; }
  .charge-foot { min-height: 2.05rem; margin-top: .36rem; }
  .card-cost { top: .35rem; left: .35rem; width: 1.25rem; height: 1.25rem; }
  .result-screen { min-height: 0; padding: .6rem 0 0; }
  .result-panel { padding: .75rem; }
}

@media (max-width: 660px) and (orientation: portrait) {
  .game-frame { padding: max(.65rem, env(safe-area-inset-top)) max(.75rem, env(safe-area-inset-right)) max(.75rem, env(safe-area-inset-bottom)) max(.75rem, env(safe-area-inset-left)); }
  .utility { gap: .3rem; }
  .utility-label { display: none; }
  .brand { font-size: .8rem; gap: .38rem; }
  .sigil { width: 2rem; height: 2rem; }
  .icon-button { padding-inline: .45rem; }
  .screen-head .button { padding-inline: .45rem; font-size: .84rem; }
  .study-layout { grid-template-columns: 1fr; }
  .word-detail { grid-row: 1; }
  .study-list { padding: .52rem; }
  .word-tile { padding: .42rem .28rem; }
  .word-tile strong { font-size: .96rem; }
  .collection-grid { grid-template-columns: repeat(3,minmax(0,1fr)); gap: .38rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; }
}
