:root {
  --gold: #e8c86a;
  --gold-dark: #9a7a2e;
  --ink: #1a120a;
  --parchment: #f3e2b4;
  --stone-1: #4a3f33;
  --stone-2: #2b241c;
  --font: Georgia, 'Times New Roman', 'Palatino Linotype', serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: #120c06;
  color: var(--parchment);
  font-family: var(--font);
  overflow: hidden;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

#app { position: fixed; inset: 0; }

.screen { position: absolute; inset: 0; display: flex; flex-direction: column; }
.hidden { display: none !important; }

.panel {
  background:
    radial-gradient(ellipse at top, rgba(255, 230, 160, 0.08), transparent 60%),
    repeating-linear-gradient(45deg, rgba(0,0,0,0.06) 0 2px, transparent 2px 6px),
    linear-gradient(180deg, var(--stone-1), var(--stone-2));
  border: 3px solid var(--gold-dark);
  box-shadow: inset 0 0 0 1px var(--gold), inset 0 0 24px rgba(0,0,0,0.7), 0 6px 30px rgba(0,0,0,0.8);
  border-radius: 6px;
}

h1, h2 {
  font-family: var(--font);
  color: var(--gold);
  text-shadow: 0 2px 0 #000, 0 0 12px rgba(232, 200, 106, 0.4);
  letter-spacing: 2px;
  margin: 0;
  text-align: center;
}

h1 { font-size: clamp(28px, 5vw, 48px); font-variant: small-caps; }

button { font-family: var(--font); cursor: pointer; }

.btn-gold {
  background: linear-gradient(180deg, #f6dc8a, #c39a3c 50%, #8a6a22);
  color: #2a1a06;
  border: 2px solid #4a3410;
  border-radius: 4px;
  padding: 10px 28px;
  font-size: 20px;
  font-weight: bold;
  box-shadow: inset 0 1px 0 #fff6cc, 0 3px 6px rgba(0,0,0,0.6);
  text-shadow: 0 1px 0 rgba(255,255,255,0.4);
}
.btn-gold:active { transform: translateY(1px); }
.btn-gold:disabled { filter: grayscale(1) brightness(0.7); cursor: default; }

.btn-stone {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-width: 64px; height: 100%;
  background: linear-gradient(180deg, #6a5a44, #3a3024);
  color: var(--gold);
  border: 2px solid var(--gold-dark);
  border-radius: 4px;
  font-size: 22px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.2), 0 2px 4px rgba(0,0,0,0.6);
  padding: 4px 8px;
}
.btn-stone span { font-size: 12px; margin-top: 2px; }
.btn-stone:hover:not(:disabled) { filter: brightness(1.2); }
.btn-stone:active:not(:disabled) { transform: translateY(1px); }
.btn-stone:disabled { opacity: 0.4; cursor: default; }
.btn-stone.on { background: linear-gradient(180deg, #a08040, #5a4418); color: #fff3c0; }

/* ---------------- setup ---------------- */

#setup {
  flex-direction: row;
  align-items: stretch;
  justify-content: center;
  background: radial-gradient(ellipse at center, #2a3a1a, #0c0804 80%);
  padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
  gap: 8px;
}

.setup-panel {
  flex: 1;
  max-width: 1200px;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 18px;
}

.subtitle { text-align: center; color: #d8c79a; margin: 4px 0 10px; font-style: italic; }

#faction-tabs {
  display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; margin-bottom: 12px;
}
.tab {
  background: linear-gradient(180deg, #3a3024, #241c14);
  color: #c8b890; border: 1px solid #5a4a30; padding: 7px 14px; border-radius: 4px; font-size: 15px;
}
.tab.active { background: linear-gradient(180deg, #8a6a2a, #5a4418); color: #fff3c0; border-color: var(--gold); }

#unit-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
}

.unit-card {
  position: relative;
  background: linear-gradient(180deg, #3a2f22, #221a12);
  border: 2px solid #5a4a30;
  border-radius: 5px;
  padding: 7px;
  cursor: pointer;
  transition: border-color 0.15s, opacity 0.15s;
}
.unit-card:hover { border-color: var(--gold-dark); }
.unit-card.selected { border-color: var(--gold); box-shadow: 0 0 12px rgba(232, 200, 106, 0.45); }
.unit-card.full { opacity: 0.55; }
.unit-card.selected::after {
  content: '\2714';
  position: absolute; top: 6px; right: 8px;
  color: #7fe07f; font-size: 22px; text-shadow: 0 1px 2px #000;
}
.unit-card .portrait { width: 100%; aspect-ratio: 8 / 5; display: block; border: 1px solid #000; border-radius: 3px; }
.unit-card h3 { margin: 5px 0 0; color: var(--gold); font-size: 16px; }
.faction-tag { font-size: 11px; color: #a89870; text-transform: uppercase; letter-spacing: 1px; }
.unit-card .desc { font-size: 12px; color: #c8b890; margin: 3px 0 4px; }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; font-size: 12.5px; }
.stats div { display: flex; justify-content: space-between; border-bottom: 1px dotted rgba(232,200,106,0.15); }
.stats b { color: #fff3c0; font-weight: normal; }

.perks { list-style: none; margin: 5px 0 0; padding: 0; font-size: 11.5px; color: #d8c8a0; }
.perks li { margin-top: 2px; padding-left: 10px; text-indent: -10px; }
.perks li::before { content: '\2726 '; color: var(--gold); }
.perks b { color: #ffd98a; font-weight: normal; }
.perk-chips { display: flex; flex-wrap: wrap; gap: 3px; margin-top: 3px; }
.perk {
  font-size: 10.5px; padding: 0 5px; border-radius: 7px; background: #5a4418; color: #fff3c0; border: 1px solid var(--gold-dark);
}
.status { font-size: 11.5px; color: #e0c8ff; margin-top: 3px; }

.badge {
  display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 8px; margin-left: 6px;
  vertical-align: middle; color: #fff; border: 1px solid rgba(0,0,0,0.5);
}
.badge.melee { background: #8a3a22; }
.badge.ranged { background: #2a6a3a; }
.badge.splash { background: #b05a10; }

@media (max-width: 760px) {
  .btn-gold { padding: 8px 18px; font-size: 18px; }
}

/* ---------------- battle ---------------- */

#battle { background: #0c0804; }
#field-wrap { position: relative; flex: 1; min-height: 0; }
#field { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }

#hud {
  flex: none;
  border-top: 3px solid var(--gold-dark);
  background:
    repeating-linear-gradient(90deg, rgba(0,0,0,0.08) 0 3px, transparent 3px 9px),
    linear-gradient(180deg, #4a3f33, #241d15);
  box-shadow: inset 0 1px 0 var(--gold);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}

#queue { display: flex; gap: 4px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: none; }
#queue::-webkit-scrollbar { display: none; }
.q-item { position: relative; flex: none; width: 46px; height: 46px; border: 2px solid; border-radius: 3px; }
.q-item canvas { width: 100%; height: 100%; display: block; }
.q-item.player { border-color: #c8403a; }
.q-item.ai { border-color: #3a6ad0; }
.q-item.active { width: 56px; height: 56px; border-color: var(--gold); box-shadow: 0 0 10px var(--gold); margin-top: -10px; }
.q-item .n {
  position: absolute; right: 0; bottom: 0; font-size: 11px; padding: 0 3px; color: #fff; background: rgba(0,0,0,0.65);
}
.q-item.next-round { opacity: 0.6; }
.q-sep { flex: none; width: 2px; background: var(--gold-dark); margin: 4px 4px; }

#controls { display: flex; gap: 8px; height: 92px; }
.btn-group { display: flex; gap: 6px; }

#info {
  flex: 0 0 230px;
  font-size: 12.5px;
  background: rgba(0,0,0,0.35);
  border: 1px solid var(--gold-dark);
  border-radius: 3px;
  padding: 4px 8px;
  overflow-y: auto;
}
#info h4 { margin: 0 0 2px; color: var(--gold); font-size: 14px; }
#info .stats { font-size: 12px; }

#log {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  background: rgba(0,0,0,0.35);
  border: 1px solid var(--gold-dark);
  border-radius: 3px;
  padding: 4px 8px;
  font-size: 13px;
  line-height: 1.35;
}
#log .round { color: var(--gold); font-style: italic; }
#log .player { color: #ffb0a0; }
#log .ai { color: #a8c4ff; }
#log .death { color: #ff6a5a; font-weight: bold; }
#log .status { color: #d8b8ff; font-style: italic; }

@media (max-width: 900px) {
  #info { display: none; }
  .btn-stone { min-width: 54px; }
}
@media (max-height: 560px) {
  #controls { height: 64px; }
  .q-item { width: 36px; height: 36px; }
  .q-item.active { width: 42px; height: 42px; margin-top: -6px; }
}

/* ---------------- modal & unit card ---------------- */

#modal {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.6); z-index: 10; padding: 12px;
}
#modal-body { padding: 20px 26px; min-width: min(460px, 100%); max-height: 100%; overflow-y: auto; text-align: center; }
#modal-body h2 { font-size: 40px; margin-bottom: 10px; }
.casualties { display: flex; gap: 18px; justify-content: center; margin: 12px 0 18px; text-align: left; flex-wrap: wrap; }
.casualties h3 { margin: 0 0 6px; color: var(--gold); font-size: 16px; }
.cas-row { display: flex; align-items: center; gap: 8px; font-size: 14px; margin-bottom: 4px; }
.cas-row canvas { width: 34px; height: 34px; border: 1px solid #000; }
.modal-buttons { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }

#unit-card {
  position: fixed; z-index: 9; width: 260px; padding: 10px 12px; pointer-events: none; font-size: 13px;
}
#unit-card h3 { margin: 0 0 4px; color: var(--gold); font-size: 16px; }
#unit-card canvas { width: 100%; aspect-ratio: 4 / 3; border: 1px solid #000; margin-bottom: 4px; }

/* ---------------- audio toggles ---------------- */

.audio-toggles { display: flex; gap: 4px; }
.audio-toggles.stacked { flex-direction: column; }
.audio-toggles.stacked .btn-stone { min-width: 44px; height: auto; flex: 1; font-size: 18px; padding: 0 6px; }
.audio-toggles .off { opacity: 0.45; text-decoration: line-through; filter: grayscale(1); }

/* ---------------- army panel (budget) ---------------- */

#army-panel {
  flex: 0 0 360px;
  display: flex; flex-direction: column; gap: 8px;
  padding: 12px 14px;
  min-height: 0;
}
#army-panel h2 { font-size: 24px; }
.budget-label { color: #d8c79a; margin-bottom: 4px; }
#budget-presets { display: flex; flex-wrap: wrap; gap: 4px; }
.preset { padding: 5px 9px; font-size: 14px; }
.budget-custom { display: block; margin-top: 6px; color: #c8b890; font-size: 14px; }
.budget-custom input, .row-controls .count {
  background: #1a140c; color: #fff3c0; border: 1px solid var(--gold-dark); border-radius: 3px;
  font-family: var(--font); font-size: 15px; padding: 4px 6px;
}
.budget-custom input { width: 120px; }
.gold-bar { height: 10px; background: #1a140c; border: 1px solid var(--gold-dark); border-radius: 5px; overflow: hidden; }
#gold-fill { height: 100%; width: 0; background: linear-gradient(90deg, #8a6a22, #f6dc8a); transition: width 0.15s; }
#gold-text { font-size: 13px; color: #d8c79a; }
#gold-text b { color: #fff3c0; }

#army-list { flex: 1; min-height: 120px; overflow-y: auto; -webkit-overflow-scrolling: touch; display: flex; flex-direction: column; gap: 6px; }
.army-empty { color: #a89870; font-style: italic; text-align: center; padding: 20px 6px; }
.army-row {
  display: flex; gap: 8px; align-items: flex-start;
  background: rgba(0,0,0,0.3); border: 1px solid #5a4a30; border-radius: 4px; padding: 6px;
}
.row-portrait { width: 52px; height: 52px; border: 2px solid #c8403a; border-radius: 3px; flex: none; }
.row-main { flex: 1; min-width: 0; }
.row-head { display: flex; align-items: baseline; gap: 6px; }
.row-head b { color: var(--gold); }
.row-each { font-size: 12px; color: #a89870; flex: 1; }
.row-remove { background: none; border: none; color: #e08070; font-size: 16px; padding: 0 4px; }
.row-controls { display: flex; align-items: center; gap: 4px; margin: 4px 0 2px; }
.row-controls .step { padding: 2px 10px; font-size: 16px; }
.row-controls .max { padding: 3px 8px; font-size: 13px; }
.row-controls .count { width: 64px; text-align: center; }
.subtotal { margin-left: auto; font-size: 13px; color: #fff3c0; white-space: nowrap; }
.slider { width: 100%; accent-color: #d8b050; height: 22px; }

.army-actions, .enemy-choice { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.panel-bottom { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.panel-bottom .btn-gold { flex: 1; }

.price-tag {
  position: absolute; top: 12px; left: 12px;
  background: rgba(20, 14, 6, 0.85); color: #ffe08a; border: 1px solid var(--gold-dark);
  border-radius: 10px; padding: 1px 8px; font-size: 14px; font-weight: bold;
}
.afford { margin-top: 5px; font-size: 12px; color: #9fd08a; }
.unit-card.unaffordable { opacity: 0.45; }
.unit-card.unaffordable .afford { color: #e08070; }
.gold-compare { color: #ffe08a; }

@media (max-width: 1000px) {
  #setup { flex-direction: column; }
  .setup-panel { max-width: none; }
  #army-panel { flex: 0 0 auto; max-height: 48%; }
  #army-list { min-height: 60px; }
}

/* ---------------- hot-seat ---------------- */

#army-panel.p2 { border-color: #3a6ad0; box-shadow: inset 0 0 0 1px #6a9af0, inset 0 0 24px rgba(0,0,0,0.7), 0 6px 30px rgba(0,0,0,0.8); }
#army-panel.p2 h2 { color: #9cc0ff; }
#army-panel.p2 .row-portrait { border-color: #3a6ad0; }
#army-panel.p2 #gold-fill { background: linear-gradient(90deg, #22408a, #8cb4ff); }
#budget-fixed { color: #d8c79a; font-size: 14px; }
.p2-name { color: #9cc0ff; }

#turn-banner {
  position: absolute; top: 8px; left: 50%; transform: translateX(-50%);
  padding: 6px 18px; border-radius: 6px; font-size: 18px; font-weight: bold; white-space: nowrap;
  color: #fff; text-shadow: 0 1px 2px #000; pointer-events: none;
  border: 2px solid var(--gold); box-shadow: 0 3px 10px rgba(0,0,0,0.6);
}
#turn-banner.turn-player { background: linear-gradient(180deg, #c8403a, #6a1410); }
#turn-banner.turn-ai { background: linear-gradient(180deg, #3a6ad0, #12306a); }
#turn-banner.flash { animation: banner-flash 0.9s ease-out; }
@keyframes banner-flash {
  0% { transform: translateX(-50%) scale(1.35); box-shadow: 0 0 30px var(--gold); }
  100% { transform: translateX(-50%) scale(1); }
}
#modal.opaque { background: radial-gradient(ellipse at center, #2a3a1a, #0c0804 80%); }
