:root {
  --ink: #eee7d5;
  --muted: #918a9e;
  --black: #0a0710;
  --panel: #15101f;
  --panel-soft: #1e172b;
  --purple: #8e54c7;
  --purple-deep: #321a4d;
  --gold: #d5ad58;
  --ivory: #f3e9c9;
  --danger: #d34d56;
  --heal: #3f9b75;
  --move: #7d78b7;
  --cell-light: #746b78;
  --cell-dark: #393044;
  --border: #3b2c4e;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--black);
}

* { box-sizing: border-box; }

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background:
    radial-gradient(circle at 50% -10%, #382151 0, transparent 34rem),
    linear-gradient(145deg, #09060e, #110b19 55%, #09060e);
}

button, input { font: inherit; }
button { color: inherit; }
button:not(:disabled) { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .38; }

[hidden] { display: none !important; }

.loading-screen {
  min-height: 100vh;
  display: grid;
  place-content: center;
  justify-items: center;
  padding: 1rem 0;
  overflow: hidden;
  text-align: center;
}

.loading-screen h1 {
  margin: 1.2rem 0 0;
  color: var(--gold);
  font-family: Georgia, serif;
  font-size: clamp(2rem, 7vw, 4.5rem);
  letter-spacing: .18em;
}

.loading-game { margin: .25rem 0 2rem; letter-spacing: .35em; }
.loading-game span { color: var(--purple); }
.loading-status { min-height: 1.4em; color: var(--ivory); font-family: "Cascadia Mono", Consolas, monospace; }
.loading-status.typing::after { content: "█"; margin-left: .12em; color: var(--gold); animation: terminal-cursor .7s steps(1) infinite; }
.loading-status.error { color: var(--danger); }
@keyframes terminal-cursor { 0%, 49% { opacity: 1; } 50%, 100% { opacity: 0; } }
.cold-start { max-width: 38rem; color: var(--muted); font-size: .85rem; }
.splash-help { color: #caa7ef; font-size: .82rem; }
.splash-help:hover { color: var(--gold); }

.ascii-logo {
  width: 100vw;
  max-width: 100vw;
  overflow: hidden;
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: max(1.5px, .4vw);
  line-height: 1;
  filter: drop-shadow(0 0 18px rgba(142, 84, 199, .5));
}
.ascii-line { height: 1em; white-space: pre; }
.ascii-canvas { display: block; width: 100%; height: auto; }
.living-emblem { --ascii-unit: max(1.5px, .4vw); position: relative; width: 100vw; max-width: 100vw; }
.emblem-title-arc { position: absolute; z-index: 2; top: 8%; left: 30%; width: 41.25%; height: 24%; overflow: visible; pointer-events: none; opacity: .4; filter: drop-shadow(0 0 2px rgba(255, 211, 106, .8)) drop-shadow(0 0 7px rgba(145, 76, 255, .45)); transform: translateY(calc(var(--ascii-unit) * 4)); }
.emblem-title-arc text { fill: #ffd36a; font: 800 82px Georgia, serif; letter-spacing: 20px; paint-order: stroke; stroke: #2b1208; stroke-width: 5px; }
.legacy-motto { position: absolute; z-index: 2; left: 50%; bottom: 25%; transform: translateX(-50%) translateY(calc(var(--ascii-unit) * 5)); opacity: .65; color: #baf5ff; font: 700 clamp(.65rem, 1.2vw, 1.3rem) Georgia, serif; letter-spacing: .42em; white-space: nowrap; text-shadow: 0 0 2px rgba(186, 245, 255, .9), 0 0 7px rgba(49, 145, 255, .75); }

.app-shell { width: min(1440px, 100%); margin: 0 auto; padding: .7rem 1rem; }
.app-header, .phase-panel, .board-panel, .army-panel, .terminal-panel, .result-modal, .confirm-modal {
  border: 1px solid var(--border);
  background: rgba(21, 16, 31, .94);
  box-shadow: 0 16px 40px rgba(0, 0, 0, .24);
}

.app-header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 1.25rem;
  align-items: center;
  min-height: 4.6rem;
  padding: .55rem 1.1rem;
  border-top: 3px solid var(--gold);
}
.brand-mark { justify-self: start; }
.brand-mark p { margin: 0; }
.help-link { display: inline-flex; align-items: center; gap: .42rem; margin-top: .45rem; padding: .38rem .58rem; border: 1px solid #98783b; border-radius: 4px; background: #493719; color: #fff5d9; font-size: .82rem; font-weight: 800; letter-spacing: .06em; text-decoration: none; text-transform: uppercase; box-shadow: 0 0 0 1px rgba(213, 173, 88, .12), 0 4px 14px rgba(0, 0, 0, .28); }
.help-link::before { content: "?"; width: 1.25rem; height: 1.25rem; display: grid; place-items: center; border-radius: 50%; background: var(--gold); color: #1a1005; font: 900 .82rem/1 Georgia, serif; }
.help-link:hover { border-color: #f0ca75; background: #624a20; color: #fff; transform: translateY(-1px); }
.app-header > h1 { justify-self: center; text-align: center; }

h1, h2, p { margin-top: 0; }
h1, h2 { font-family: Georgia, serif; }
.app-header h1 { margin: 0; font-size: clamp(1.9rem, 4vw, 3rem); }
.subtitle { margin: 0; color: var(--muted); }
.eyebrow { margin-bottom: .25rem; color: var(--gold); font-size: .72rem; font-weight: 800; letter-spacing: .22em; }

.status-strip { display: grid; justify-self: end; gap: .15rem; min-width: 17rem; color: var(--muted); text-align: right; font-size: .82rem; }
.status-strip strong { color: var(--ivory); font-family: Georgia, serif; font-size: 1.1rem; }

.phase-panel { margin-top: 1rem; padding: 1rem 1.25rem; }
.panel-heading, .terminal-heading { display: flex; justify-content: space-between; gap: 1rem; align-items: center; }
.panel-heading h2, .terminal-heading h2, .army-panel h2 { margin-bottom: .25rem; }
.hint { margin: 0; color: var(--muted); }
.opponent-choice { display: grid; grid-template-columns: minmax(12rem, 1fr) auto; gap: .35rem 1rem; align-items: center; margin-top: 1rem; padding: .8rem 1rem; border: 1px solid var(--border); background: #100b18; }
.opponent-choice h3 { margin: 0; font: 1.05rem Georgia, serif; }
.difficulty-buttons { display: flex; gap: .45rem; flex-wrap: wrap; justify-content: flex-end; }
.difficulty-buttons button { min-width: 6rem; padding: .5rem .75rem; }
.difficulty-buttons button.active { border-color: var(--gold); background: #57421d; color: #fff5d9; box-shadow: 0 0 0 1px rgba(213, 173, 88, .28); }
.difficulty-hint { grid-column: 1 / -1; margin: .15rem 0 0; color: var(--muted); font-size: .8rem; }
.astrology-ticker { margin: 1rem -1.25rem -1rem; overflow: hidden; border-top: 1px solid var(--border); background: #0b0710; color: #cdbce0; font: .78rem/1.4 "Cascadia Mono", Consolas, monospace; white-space: nowrap; }
.astrology-track { width: max-content; display: flex; padding: .6rem 0; animation: astrology-scroll 48s linear infinite; }
.astrology-track span { padding-right: 5rem; }
@keyframes astrology-scroll { from { transform: translateX(0); } to { transform: translateX(-50%); } }

button {
  border: 1px solid #514064;
  border-radius: 4px;
  padding: .65rem .85rem;
  background: #241a31;
  transition: border-color .15s, background .15s, transform .1s;
}
button:not(:disabled):hover { border-color: var(--gold); background: #30213f; }
button:not(:disabled):active { transform: translateY(1px); }
.primary-action { border-color: #98783b; background: #57421d; color: #fff5d9; }
.danger-action { border-color: #77323b; color: #ffb7bd; }
.button-row, .budget-buttons { display: flex; gap: .55rem; flex-wrap: wrap; }

.shop-grid { display: grid; grid-template-columns: repeat(5, minmax(105px, 1fr)); gap: .65rem; margin: 1rem 0; }
.unit-card { min-height: 8rem; display: grid; place-items: center; align-content: center; gap: .4rem; }
.unit-symbol { color: var(--ivory); font-family: Georgia, serif; font-size: 2.1rem; text-shadow: 0 2px 8px #000; }

.game-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(270px, 360px); gap: .7rem; margin-top: .7rem; }
.main-column { min-width: 0; }
.main-column > .phase-panel { margin-top: 0; margin-bottom: .7rem; }
.board-panel { min-width: 0; padding: .65rem; overflow: auto; }
.board-frame { width: min(62vh, 650px, 100%); margin: 0 auto; display: grid; grid-template-columns: 1.35rem minmax(0, 1fr); grid-template-rows: auto auto; align-items: stretch; }
.board-stack { position: relative; aspect-ratio: 1; }
.board { width: 100%; height: 100%; display: grid; grid-template-columns: repeat(var(--board-size), minmax(0, 1fr)); grid-template-rows: repeat(var(--board-size), minmax(0, 1fr)); border: 5px solid #281d31; outline: 1px solid #604b73; }
.board-cell { position: relative; width: 100%; height: 100%; min-width: 0; min-height: 0; overflow: hidden; border: 0; border-radius: 0; padding: 0; font-family: Georgia, "Times New Roman", serif; font-size: clamp(1.5rem, 5.5vw, 4.2rem); line-height: 1; text-shadow: 0 2px 4px #000; }
.board-cell.light { background: var(--cell-light); }
.board-cell.dark { background: var(--cell-dark); }
.board-cell:hover { box-shadow: inset 0 0 0 3px rgba(255,255,255,.2); }
.board-cell.side-white { color: var(--ivory); }
.board-cell.side-black { color: #bd79f0; }
.board-cell.selected { box-shadow: inset 0 0 0 4px var(--gold), inset 0 0 25px rgba(213,173,88,.45); z-index: 1; }
.board-cell.exposed { color: #ff5964; text-shadow: 0 0 10px #5f0911; }
.board-cell.target-move { background: color-mix(in srgb, var(--move) 72%, var(--cell-dark)); box-shadow: inset 0 0 0 3px #bbb5ff; }
.board-cell.target-attack { background: color-mix(in srgb, var(--danger) 72%, var(--cell-dark)); box-shadow: inset 0 0 0 3px #ff9a9f; }
.board-cell.target-heal { background: color-mix(in srgb, var(--heal) 75%, var(--cell-dark)); box-shadow: inset 0 0 0 3px #a7f0cf; }
.board-cell.setup-cell:not(.side-white):not(.side-black) { box-shadow: inset 0 0 0 2px rgba(213, 173, 88, .5); }

.board-ranks, .board-files { margin: 0; color: #77717e; font: 600 .72rem/1 "Cascadia Mono", Consolas, monospace; }
.board-ranks { display: grid; grid-template-rows: repeat(var(--board-size), minmax(0, 1fr)); padding: 5px .3rem 5px 0; }
.board-ranks span { display: grid; place-items: center end; }
.board-files { display: grid; grid-template-columns: repeat(var(--board-size), minmax(0, 1fr)); padding-top: .35rem; }
.board-files span { text-align: center; }
.board-legend { display: flex; justify-content: center; gap: 1rem; flex-wrap: wrap; margin-top: 1.1rem; color: var(--muted); font-size: .76rem; }
.move-arrow { position: absolute; inset: 5px; width: calc(100% - 10px); height: calc(100% - 10px); overflow: visible; pointer-events: none; opacity: 0; filter: drop-shadow(0 0 3px #000); }
.move-arrow line { stroke: #ffd86b; stroke-width: .075; stroke-linecap: round; marker-end: url(#arrowHead); }
.move-arrow path { fill: #ffd86b; }
.move-arrow.active { animation: move-arrow-fade 3.2s ease-out forwards; }
@keyframes move-arrow-fade { 0%, 55% { opacity: .95; } 100% { opacity: 0; } }
.board-legend span { display: flex; align-items: center; gap: .35rem; }
.board-legend i { width: .8rem; height: .8rem; border-radius: 2px; display: inline-block; }
.legend-move { background: var(--move); } .legend-attack { background: var(--danger); } .legend-heal { background: var(--heal); } .legend-exposed { background: #ff5964; }

.army-panel { display: flex; flex-direction: column; min-height: 100%; padding: .8rem; }
.army-list { display: grid; gap: .4rem; }
.setup-controls { display: grid; gap: .55rem; margin-top: 2rem; padding-top: 1rem; border-top: 1px solid var(--border); }
.battle-controls { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .55rem; margin-top: auto; padding-top: 1rem; border-top: 1px solid var(--border); }
.army-row { width: 100%; display: flex; justify-content: space-between; gap: .75rem; align-items: center; text-align: left; }
.army-row span:last-child { color: var(--muted); font: .76rem/1.35 "Cascadia Mono", Consolas, monospace; text-align: right; }
.army-row.selected { border-color: var(--gold); opacity: 1; }
.army-row.placed { opacity: .48; }
.army-row.placed:hover, .army-row.placed.selected { opacity: 1; }
.setup-unit-identity { display: inline-flex; align-items: center; gap: .55rem; }
.setup-checkbox { width: 1.15rem; height: 1.15rem; display: inline-grid; flex: 0 0 auto; place-items: center; border: 1px solid #756486; border-radius: 3px; color: #17101f; background: transparent; font-size: .82rem; font-weight: 900; }
.army-row.placed .setup-checkbox { border-color: var(--gold); background: var(--gold); box-shadow: 0 0 8px rgba(213, 173, 88, .35); }
.army-row.unplaced { animation: recruit-attention 1.4s ease-in-out infinite; }
.army-row.unplaced:nth-child(2n) { animation-delay: -.35s; }
.army-row.unplaced:nth-child(3n) { animation-delay: -.7s; }
.army-row.exposed { border-color: var(--danger); background: #3a1721; }
@keyframes recruit-attention { 0%, 100% { border-color: #514064; box-shadow: none; } 50% { border-color: var(--gold); box-shadow: 0 0 12px rgba(213, 173, 88, .42), inset 0 0 10px rgba(213, 173, 88, .1); } }

.terminal-panel { margin-top: .7rem; padding: .7rem .9rem; }
.terminal-heading span { color: var(--muted); font-size: .75rem; }
.console-log { height: 9rem; overflow-y: auto; margin-top: .5rem; padding: .65rem; border: 1px solid #2f253a; background: #08060c; color: #bdb5c8; font: .8rem/1.45 "Cascadia Mono", Consolas, monospace; }
.console-command { color: var(--gold); }
.console-opponent { color: #c487ff; font-weight: 700; }
.console-roll-success strong { color: #fff07c; text-shadow: 0 0 8px rgba(255, 240, 124, .45); }
.console-roll-success span { color: #bdb5c8; font-weight: 400; text-shadow: none; }
.console-death { color: #ff737d; font-weight: 900; letter-spacing: .04em; text-shadow: 0 0 10px rgba(255, 71, 84, .55); }
.console-check { margin: .15rem 0; }
.console-check summary { cursor: pointer; list-style-position: outside; }
.console-check-success summary strong { color: #fff07c; text-shadow: 0 0 8px rgba(255, 240, 124, .45); }
.console-check-missed summary strong { color: #bfaecb; }
.console-check summary span { color: #bdb5c8; }
.check-breakdown { display: grid; gap: .12rem; margin: .35rem 0 .45rem 1.2rem; padding: .45rem .6rem; border-left: 2px solid #5b4470; color: #8f8799; }
.console-error { color: #ff7780; }
.suggestions { min-height: 2.4rem; display: flex; gap: .35rem; padding: .45rem 0; overflow-x: auto; }
.suggestions button { flex: 0 0 auto; padding: .35rem .55rem; color: #c0b6cc; font: .72rem "Cascadia Mono", Consolas, monospace; }
.command-line { display: flex; align-items: center; gap: .6rem; padding: .65rem .8rem; border: 1px solid #514064; background: #0d0912; color: var(--gold); font: .9rem "Cascadia Mono", Consolas, monospace; }
.command-line input { width: 100%; border: 0; outline: 0; background: transparent; color: var(--ivory); font: inherit; }

.modal-overlay { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; padding: 1rem; background: rgba(5, 3, 8, .72); backdrop-filter: blur(4px); }
.result-modal, .confirm-modal, .turn-report-modal { min-width: min(26rem, 100%); display: grid; justify-items: center; gap: 1.2rem; padding: 2.2rem; border: 2px solid var(--gold); background: rgba(21, 16, 31, .98); box-shadow: 0 18px 55px rgba(0, 0, 0, .55); text-align: center; }
.turn-report-modal { width: min(38rem, 100%); }
.turn-report-modal > strong { font: 1.8rem Georgia, serif; }
.turn-report-content { width: 100%; max-height: min(56vh, 32rem); display: grid; gap: .8rem; overflow-y: auto; padding: .85rem; border: 1px solid var(--border); background: #09060e; color: #c9c1d2; font: .86rem/1.45 "Cascadia Mono", Consolas, monospace; text-align: left; }
.turn-report-segment { display: grid; gap: .4rem; }
.turn-report-segment + .turn-report-segment { padding-top: .8rem; border-top: 1px solid #574267; }
.turn-report-segment h3 { margin: 0 0 .2rem; color: var(--gold); font: 700 .78rem "Cascadia Mono", Consolas, monospace; letter-spacing: .12em; text-transform: uppercase; }
.turn-report-segment.report-opponent h3 { color: #c487ff; }
.turn-report-content strong { color: #fff07c; }
.report-check summary { cursor: pointer; }
.report-check .check-breakdown { color: #91889c; }
.turn-report-content .report-death { color: #ff737d; font-weight: 900; text-shadow: 0 0 10px rgba(255, 71, 84, .55); }
.turn-thinking { width: min(18rem, 100%); display: grid; justify-items: center; gap: .75rem; color: var(--ivory); }
.turn-thinking img { width: 10rem; max-width: 50vw; aspect-ratio: 1; border: 1px solid #725d83; border-radius: 6px; object-fit: cover; box-shadow: 0 0 28px rgba(142, 84, 199, .3); }
.turn-thinking strong { font: 1rem/1.4 Georgia, serif; }
.thinking-dots { width: 7.5rem; height: 2.5rem; display: inline-flex; align-items: center; justify-content: center; gap: .28rem; font: 700 2rem/1 "Cascadia Mono", Consolas, monospace; }
.thinking-dots i { font-style: normal; animation: thinking-dot 1.05s ease-in-out infinite; animation-delay: calc(var(--dot-index) * .12s); }
.queue-status { min-height: 2.8em; color: #c9c1d2; font: .78rem/1.4 "Cascadia Mono", Consolas, monospace; }
@keyframes thinking-dot {
  0%, 100% { opacity: .2; transform: translateY(0); }
  45% { opacity: 1; transform: translateY(-.18rem); }
}
.result-modal strong { font: 2.6rem Georgia, serif; }
.confirm-modal { border-color: var(--danger); }
.confirm-modal strong { font: 1.65rem Georgia, serif; }
.confirm-modal p { margin: 0; color: var(--muted); }
.modal-overlay[data-kind="victory"] .result-modal { border-color: var(--heal); }
.modal-overlay[data-kind="defeat"] .result-modal { border-color: var(--danger); }

@media (min-width: 701px) {
  .emblem-title-arc { left: 30.5%; }
  .emblem-title-arc text { transform-box: fill-box; transform-origin: left center; transform: rotate(1deg) scaleX(1.02); }
  .legacy-motto { left: calc(50% + 7px); bottom: 25.8%; font-size: 1.45rem; letter-spacing: .5em; }
}

@media (max-width: 900px) {
  .app-header { grid-template-columns: 1fr auto; }
  .app-header > h1 { grid-column: 1 / -1; grid-row: 1; }
  .brand-mark { grid-column: 1; grid-row: 1; }
  .status-strip { grid-column: 2; grid-row: 1; min-width: 0; }
  .panel-heading, .terminal-heading { align-items: flex-start; flex-direction: column; }
  .game-layout { grid-template-columns: 1fr; }
  .shop-grid { grid-template-columns: repeat(auto-fit, minmax(105px, 1fr)); }
  .army-panel { order: -1; }
}

@media (max-width: 700px) {
  .emblem-title-arc text { transform-box: fill-box; transform-origin: left center; transform: scaleX(1.05); }
  .legacy-motto { font-size: clamp(.6rem, 1.6vw, .72rem); letter-spacing: .38em; }
  .opponent-choice { grid-template-columns: 1fr; }
  .difficulty-buttons { justify-content: flex-start; }
}

@media (max-width: 540px) {
  .app-shell { padding: .5rem; }
  .app-header { grid-template-columns: 1fr 1fr; gap: .45rem .75rem; }
  .app-header > h1 { grid-column: 1 / -1; grid-row: 1; }
  .brand-mark { grid-column: 1; grid-row: 2; }
  .status-strip { grid-column: 2; grid-row: 2; }
  .phase-panel, .board-panel, .army-panel, .terminal-panel { padding: .7rem; }
  .board-frame { grid-template-columns: 1rem minmax(0, 1fr); }
  .board { border-width: 3px; }
  .board-cell { font-size: clamp(1.5rem, 11vw, 3.5rem); }
  .army-row { align-items: flex-start; flex-direction: column; }
  .army-row span:last-child { text-align: left; }
  .terminal-heading span { display: none; }
  .ascii-logo { width: 100vw; max-width: 100vw; font-size: max(1.5px, .4vw); }
  .ascii-line { height: 1em; }
  .emblem-title-arc text { font-size: 68px; letter-spacing: 14px; }
  .legacy-motto { bottom: 25%; }
}

@media (prefers-reduced-motion: reduce) {
  .army-row.unplaced { animation: none; border-color: var(--gold); }
  .astrology-track { animation: none; }
  .astrology-track span[aria-hidden="true"] { display: none; }
}
