:root {
  color-scheme: dark;
  --ink: #111c1c;
  --panel: #192524;
  --paper: #f0e8d1;
  --muted: #b6b8a5;
  --brass: #d8b579;
  --line: #596054;
  --ally: #82cfaa;
  --enemy: #ef9277;
  --serif: "STIX Two Text", "Songti TC", "Songti SC", "Noto Serif CJK TC", "PMingLiU", serif;
  --sans: "Atkinson Hyperlegible Next", "PingFang TC", "Microsoft JhengHei", "Noto Sans CJK TC", sans-serif;
  --mono: "IBM Plex Mono", "Courier New", monospace;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { min-height: 100%; background: #101918; }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--paper);
  font: 15px/1.55 var(--sans);
  background: radial-gradient(ellipse at 50% 15%, #25362f 0%, #18231f 47%, #111a18 84%);
}
button { color: inherit; font: inherit; cursor: pointer; }
button:disabled { cursor: default; }
button:focus-visible, input:focus-visible {
  outline: 2px solid #ffe3a5;
  outline-offset: 2px;
}
h1, h2, p, ol { margin: 0; }
.game-shell { width: min(1160px, calc(100% - 72px)); margin: 0 auto; padding-bottom: 40px; }
.masthead {
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #586152;
}
.brand { display: flex; gap: 10px; align-items: center; }
.crest {
  width: 38px; height: 42px;
  font-size: 0;
  background: url("../assets/crest.png") center / contain no-repeat;
  image-rendering: pixelated;
}
.brand-name {
  display: block; font: 700 21px/1.15 var(--serif);
  color: #f0dfac; letter-spacing: .13em;
}
.brand-en {
  display: block; margin-top: 5px;
  font: 9px/1.15 var(--mono); letter-spacing: .16em;
  color: #b9b39a; text-transform: uppercase;
}
#help-toggle, #help-close, .duel-controls button {
  border: 1px solid #666c58; padding: 6px 13px;
  color: #d9dbc6; background: #1d2a26; font-size: 13px;
}
#help-toggle::before {
  content: "?"; display: inline-grid; place-items: center;
  width: 14px; height: 14px; border: 1px solid #9b9e82;
  border-radius: 50%; margin-right: 7px; color: #c9c5a3;
  font: bold 10px/1 var(--mono); vertical-align: 1px;
}
#help-toggle:hover, #help-close:hover, .duel-controls button:hover {
  border-color: var(--brass); color: #fff0c4;
}
.battle-heading {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 24px 0 21px;
}
.chapter {
  font-size: 11px; font-weight: 650; line-height: 1.3;
  color: var(--brass); letter-spacing: .2em; margin-bottom: 5px;
}
h1 { font: 700 clamp(30px, 3.2vw, 44px)/1.12 var(--serif); letter-spacing: .085em; }
.turn-indicator {
  display: flex; align-items: center; gap: 11px; white-space: nowrap;
  align-self: flex-end; margin-bottom: 3px; padding: 7px 12px;
  border: 1px solid #576150; background: #1c2b25; color: #d3d0b6;
  font-size: 13px; font-variant-numeric: tabular-nums;
}
.status-dot {
  width: 7px; height: 7px; background: var(--ally);
  box-shadow: 0 0 0 2px #364d3b; transform: rotate(45deg);
}
#turn-side { padding-left: 11px; border-left: 1px solid #626652; color: #a1d6ad; }
.turn-indicator[data-side="enemy"] #turn-side { color: var(--enemy); }
.turn-indicator[data-side="enemy"] .status-dot { background: var(--enemy); }
.battle-layout { display: grid; grid-template-columns: minmax(0, 1fr) clamp(252px, 25vw, 324px); gap: 18px; align-items: start; }
.map-panel, .command-panel > section, .battle-log {
  border: 1px solid var(--line);
  box-shadow: inset 0 0 0 2px #192220, 0 3px 7px #080f0d48;
  background: linear-gradient(120deg, #23312b, #1c2825 74%);
}
.map-panel { min-width: 0; }
.map-heading {
  display: flex; align-items: center; justify-content: space-between; padding: 8px 15px;
}
.map-heading h2, .command-panel h2, .battle-log h2, .duel-heading h2 {
  font: 700 16px/1.4 var(--serif); letter-spacing: .12em; color: #e6d7b0;
}
.map-heading h2::before, .objective-panel h2::before, .roster-panel h2::before, .order-panel h2::before, .battle-log h2::before {
  content: ""; display: inline-block; width: 6px; height: 6px;
  margin-right: 9px; margin-bottom: 2px;
  border: 1px solid #b99d68; transform: rotate(45deg);
}
#position { color: #b7bdab; font: 12px/1.4 var(--mono); }
.map-stage {
  position: relative; margin: 0 6px;
  border: 1px solid #101d17;
  box-shadow: 0 0 0 1px #9a9c81, inset 0 0 0 1px #202b22;
}
#map-viewport { overflow: auto; overscroll-behavior: contain; scrollbar-color: #7e856b #28352c; }
.board {
  display: grid; grid-template-columns: repeat(var(--cols,12), minmax(0,1fr));
  grid-template-rows: repeat(var(--rows,9), minmax(0,1fr));
  aspect-ratio: 4 / 3; width: 100%;
  background: #6a8050 url("../assets/oak-forest.png") center / 100% 100% no-repeat;
  image-rendering: pixelated; isolation: isolate; user-select: none; -webkit-user-select: none;
}
.tile {
  position: relative; appearance: none; border: 0; border-radius: 0;
  border-right: 1px solid #e3e4b52b; border-bottom: 1px solid #e3e4b52b;
  min-width: 0; min-height: 0; padding: 0; margin: 0;
  background: transparent; color: #fff6d8; -webkit-tap-highlight-color: transparent;
}
.tile::before, .tile::after { content: ""; position: absolute; pointer-events: none; }
.tile::before { inset: 0; z-index: 1; }
.tile[data-unit]::before {
  inset: 60% 15% 12%; border-radius: 50%;
  background: #11231abd; box-shadow: 0 1px 1px #17221a90;
}
@media (hover: hover) {
  .tile:hover { background-color: #f9e8a842; }
  .tile:hover::after { inset: 1px; border: 1px solid #fff0bcbb; z-index: 4; }
}
.tile:focus-visible { z-index: 5; outline: 2px dashed #fff6c9; outline-offset: -3px; }
.tile.reachable { background-color: #90e3d15c; box-shadow: inset 0 0 0 1px #b3f2dc98; }
.tile.in-range { background-color: #f4975c33; box-shadow: inset 0 0 0 1px #ffe3a163; }
.tile.legal-target { background-color: #d1513860; box-shadow: inset 0 0 0 2px #ffc59a; }
.tile.route:not([data-unit]):not(.destination)::before {
  inset: 44%; background: #fff2a7; border: 1px solid #6c582f; transform: rotate(45deg);
}
.tile.selected::after, .tile.destination::after {
  --edge: #fff5cd;
  inset: 1px; z-index: 4; border: 0;
  background:
    linear-gradient(var(--edge),var(--edge)) left top / 28% 3px,
    linear-gradient(var(--edge),var(--edge)) right top / 28% 3px,
    linear-gradient(var(--edge),var(--edge)) left bottom / 28% 3px,
    linear-gradient(var(--edge),var(--edge)) right bottom / 28% 3px,
    linear-gradient(var(--edge),var(--edge)) left top / 3px 28%,
    linear-gradient(var(--edge),var(--edge)) right top / 3px 28%,
    linear-gradient(var(--edge),var(--edge)) left bottom / 3px 28%,
    linear-gradient(var(--edge),var(--edge)) right bottom / 3px 28%;
  background-repeat: no-repeat; filter: drop-shadow(0 1px 0 #292d1c);
}
.tile.destination::after { --edge: #ffdf69; }
.tile.destination:not([data-unit]) { background-color: #f3d66b88; }
.tile .unit-sprite {
  position: absolute; z-index: 2; width: 100%; height: 99%; bottom: 5%; left: 0;
  object-fit: contain; image-rendering: pixelated; pointer-events: none;
  filter: drop-shadow(0 1px 1px #1d2419);
}
.unit-hp {
  position: absolute; z-index: 3; bottom: 3%; left: 21%; width: 58%; height: max(4px, 8%);
  background: #192c25; border: 1px solid #13241f; pointer-events: none;
}
.unit-hp i { display: block; height: 100%; background: #8be5b7; }
.tile[data-side="enemy"] .unit-hp i { background: #ffa084; }
.tile.spent .unit-sprite { filter: saturate(.23) brightness(.74) drop-shadow(0 1px 1px #222c21); }
.map-legend { display: flex; gap: 21px; padding: 9px 15px 10px; color: #c4c8b0; font-size: 12px; }
.legend-item { display: inline-flex; align-items: center; gap: 7px; }
.legend-item i { width: 9px; height: 9px; border: 1px solid #aaafa1; }
.legend-item .ally { background: var(--ally); border-color: #78b892; }
.legend-item .enemy { background: var(--enemy); border-color: #cb7963; }
.legend-item .range { background: #548777; border-color: #a9e2bd; }
.command-panel { display: grid; gap: 12px; min-width: 0; }
.command-panel > section { padding: 14px 16px; min-width: 0; }
.command-panel h2 { font-size: 15px; }
.objective-line {
  display: flex; justify-content: space-between; align-items: center; gap: 6px;
  margin-top: 8px; padding-left: 15px;
}
.objective-line strong { font-size: 15px; font-weight: 580; }
#enemy-count { color: #e2bc7b; font: 700 15px/1 var(--mono); }
#roster { display: grid; margin: 7px -5px -5px; }
.roster-unit {
  display: grid; grid-template-columns: 46px minmax(0, 1fr) auto; align-items: center; gap: 11px;
  width: 100%; border: 1px solid transparent; border-top: 1px solid #475449;
  padding: 8px 5px; background: transparent; text-align: left;
}
.roster-unit:first-child { border-top-color: transparent; }
.roster-unit:hover:not(:disabled), .roster-unit.selected {
  background: #394638;
}
.roster-unit.selected { border-color: #a59d71; }
.roster-unit .portrait {
  width: 46px; height: 49px; object-fit: cover; image-rendering: pixelated;
  border: 1px solid #969479; box-shadow: 0 0 0 1px #111c17;
}
.unit-details { min-width: 0; display: grid; row-gap: 3px; }
.unit-name { font: 700 17px/1.2 var(--serif); letter-spacing: .08em; }
.roster-vital { color: #bbc2ad; font: 10px/1.1 var(--mono); }
.unit-state { align-self: start; color: #add1ab; font-size: 11px; white-space: nowrap; }
progress {
  appearance: none; -webkit-appearance: none; height: 5px; width: 100%;
  border: 0; border-radius: 0; background: #111c19; color: #92b994;
}
progress::-webkit-progress-bar { background: #111c19; }
progress::-webkit-progress-value { background: #93b995; }
progress::-moz-progress-bar { background: #93b995; }
.order-panel { border-top-color: #9a8966 !important; }
#prompt { color: #c5c8b6; font-size: 13px; line-height: 1.7; margin: 8px 0; }
#selection-info { color: #e7d4a5; font-size: 12px; padding: 6px 0; border-top: 1px solid #414b40; }
#actions, .duel-controls { display: flex; flex-wrap: wrap; gap: 7px; }
#actions:empty { display: none; }
.command, #end-turn, #help-close, .duel-controls button {
  padding: 7px 13px; border-radius: 0; font-size: 13px; font-weight: 650; letter-spacing: .06em;
}
.command, #end-turn {
  transition: background-color .12s;
  box-shadow: inset 1px 1px 0 #dfd9b135, inset -1px -1px 0 #14201970;
}
.command.secondary {
  background: #28372e; border: 1px solid #75806a; color: #dddcc5;
}
.command.primary, #end-turn {
  border: 1px solid #c2ad79; background: #d5bd87; color: #232b21;
}
#end-turn { width: 100%; margin-top: 5px; }
.command.primary:hover:not(:disabled), #end-turn:hover { background: #ead19a; }
.command.secondary:hover:not(:disabled) { background: #40513e; }
.command:disabled { opacity: .48; }
.battle-log {
  display: flex; gap: 24px; align-items: baseline;
  margin-top: 16px; padding: 13px 16px 14px;
}
.battle-log h2 { white-space: nowrap; font-size: 14px; }
.battle-log ol { list-style: none; padding: 0; }
.battle-log li { display: flex; align-items: baseline; gap: 12px; font-size: 13px; color: #c8cbb8; }
.battle-log li+li { margin-top: 4px; }
.log-turn { color: #c2ac77; font: 11px var(--mono); white-space: nowrap; }
.duel-overlay {
  position: absolute; inset: 0; z-index: 12; display: grid; place-items: center;
  padding: 14px; background: #0b140ebd;
}
.duel-window, #help {
  border: 1px solid #e1d5ac; outline: 2px solid #242e25;
  box-shadow: 0 0 0 4px #8f937a, inset 0 0 0 2px #5f6757, 0 9px 28px #08100ca1;
  background: #223029; color: var(--paper);
}
.duel-window { width: min(91%, 600px); max-height: 100%; overflow: auto; padding: 10px; }
.duel-heading { display: flex; align-items: center; justify-content: space-between; gap: 6px; padding: 1px 3px 6px; }
#duel-round { font: 13px var(--mono); color: #c8c9ad; }
#duel-arena {
  position: relative; width: 100%; aspect-ratio: 2.65; overflow: hidden;
  border: 2px ridge #b2b299;
  background: #41593b url("../assets/duel-glade.jpg") center 50% / cover;
  image-rendering: pixelated;
}
.duel-fighter { position: absolute; bottom: 6%; height: 54%; width: 28%; display: grid; place-items: end center; }
.duel-fighter.left { left: 19%; transform: scaleX(-1); }
.duel-fighter.right { right: 19%; }
.duel-fighter::before {
  content: ""; position: absolute; bottom: 0; left: 19%; width: 62%; height: 8%;
  background: #313e26ab; border-radius: 50%;
}
.duel-fighter .unit-sprite { position: relative; width: 100%; height: 100%; object-fit: contain; image-rendering: pixelated; }
.duel-fighter.fallen { opacity: .28; }
#duel-stats { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 8px; margin-top: 7px; }
.duel-card {
  display: grid; grid-template-columns: min(27%,56px) minmax(0,1fr); gap: 7px; align-items: center;
  border: 1px solid #7b816e; padding: 4px; background: #19241f;
}
.duel-card .portrait { display: block; width: 100%; aspect-ratio: 1; object-fit: cover; image-rendering: pixelated; }
.duel-card-info { min-width: 0; display: grid; gap: 3px; }
.duel-card-info .unit-name { font-size: clamp(12px, 1.4vw, 18px); }
.duel-hp-value { color: #d3d3b8; font: 11px/1.1 var(--mono); }
#duel-line { font-size: 13px; min-height: 1.5em; margin: 7px 1px; }
.duel-controls { justify-content: end; }
.duel-controls button { padding: 3px 10px; }
#help { width: min(470px, calc(100vw - 40px)); padding: 23px 25px; }
#help::backdrop { background: #0a1411bd; }
#help h2 { color: #ebd9a5; font: 700 24px var(--serif); letter-spacing: .08em; margin-bottom: 14px; }
#help p { margin-bottom: 9px; font-size: 14px; line-height: 1.65; }
.assist-option { display: flex; align-items: center; gap: 5px; color: #f1dfb0; margin-top: 12px; }
.assist-option input { accent-color: #9fc098; }
#help #assist-note { color: var(--muted); font-size: 12px; padding-left: 24px; }
#help-close { margin-top: 8px; }
@media (min-width: 950px) and (max-height: 830px) {
  .game-shell { width: min(1100px, calc(100% - 56px)); }
  .masthead { min-height: 55px; }
  .battle-heading { padding: 14px 0; }
  h1 { font-size: 34px; }
  .battle-layout { grid-template-columns: minmax(0, 1fr) min(28%, 318px); }
  .command-panel { gap: 10px; }
  .roster-unit { padding-block: 6px; }
}
@media (max-width: 900px) {
  .game-shell { width: calc(100% - 32px); }
  .battle-layout { grid-template-columns: minmax(0,1fr) 225px; gap: 12px; }
  .command-panel > section { padding: 10px; }
  .roster-unit { grid-template-columns: 36px minmax(0, 1fr) auto; gap: 7px; }
  .roster-unit .portrait { width: 36px; height: 40px; }
  .map-stage { margin-inline: 4px; }
  .map-heading, .map-legend { padding-inline: 10px; }
}
@media (max-width: 650px) and (orientation: portrait) {
  .game-shell { width: calc(100% - 20px); padding-bottom: 16px; }
  .masthead { min-height: 57px; }
  .crest { width: 29px; height: 33px; }
  .brand-name { font-size: 18px; }
  .brand-en { font-size: 8px; }
  .battle-heading { padding: 16px 0 13px; }
  .turn-indicator { padding: 4px 8px; gap: 6px; font-size: 11px; }
  #turn-side { padding-left: 6px; }
  .battle-layout { grid-template-columns: minmax(0,1fr); }
  .map-stage { margin-inline: 3px; }
  .board { min-width: 350px; }
  .command-panel { grid-template-columns: minmax(0,1fr) minmax(0,1fr); gap: 8px; align-items: start; }
  .objective-panel { grid-column: 1; }
  .roster-panel { grid-column: 2; grid-row: 1 / span 2; }
  .order-panel { grid-column: 1; grid-row: 2; }
  .roster-unit { grid-template-columns: 35px minmax(0, 1fr); }
  .unit-state { display: none; }
  .objective-line { padding-left: 0; flex-wrap: wrap; }
  .battle-log { margin-top: 10px; }
  .duel-overlay { padding: 7px; }
  .duel-window { width: 95%; padding: 7px; }
}
@media (max-height: 530px) and (orientation: landscape) {
  .game-shell { width: calc(100% - 16px); padding-bottom: 12px; }
  .masthead { min-height: 35px; }
  .brand { gap: 6px; }
  .crest { width: 21px; height: 26px; }
  .brand-name { font-size: 15px; }
  .brand-en { display: none; }
  #help-toggle { font-size: 12px; padding: 4px 7px; }
  .battle-heading { padding: 6px 1px; }
  .battle-heading > div:first-child { display: flex; align-items: baseline; gap: 10px; }
  .chapter { font-size: 10px; margin: 0; }
  h1 { font-size: 22px; }
  .turn-indicator { align-self: center; padding: 2px 6px; margin: 0; font-size: 11px; }
  .battle-layout { grid-template-columns: minmax(0,1fr) clamp(176px,31%,260px); gap: 8px; }
  .map-heading, .map-legend { padding: 3px 8px; }
  #map-viewport { max-height: calc(100dvh - 122px); }
  .board { min-width: 420px; }
  .command-panel { gap: 5px; }
  .command-panel > section { padding: 5px 8px; }
  .command-panel h2, .map-heading h2 { font-size: 13px; }
  .objective-line { margin-top: 3px; }
  .objective-line strong, .unit-name { font-size: 13px; }
  .roster-unit { grid-template-columns: 30px minmax(0,1fr) auto; gap: 5px; padding: 3px 5px; }
  .roster-unit .portrait { width: 29px; height: 29px; }
  .unit-details { row-gap: 1px; }
  .unit-state, .roster-vital { font-size: 9px; }
  #prompt { margin: 4px 0; font-size: 12px; line-height: 1.4; }
  .command, #end-turn { min-height: 29px; padding: 4px 7px; }
  .battle-log { margin-top: 6px; padding-block: 5px; }
  .duel-overlay { padding: 4px; }
  .duel-window { width: min(93%,430px); padding: 5px; }
  #duel-arena { aspect-ratio: 4; }
  .duel-card { grid-template-columns: min(23%,36px) minmax(0,1fr); }
  .duel-card .portrait { max-width: 36px; }
  .duel-controls button, #duel-line { font-size: 11px; }
  #duel-line { margin: 3px 1px; }
}
@media (max-width: 420px) {
  .battle-log { display: block; }
  .battle-log h2 { margin-bottom: 5px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0s !important; }
}
