:root {
  --ink: #241a13;
  --ink-soft: #5f4a36;
  --paper: #f2ddb0;
  --paper-deep: #d8b77a;
  --paper-light: #fff0c9;
  --gold: #efb63e;
  --gold-deep: #c68125;
  --blue: #4e91d8;
  --violet: #9b65c7;
  --red: #d5533f;
  --green: #3f9b65;
  --panel-shadow: 4px 4px 0 rgba(30, 21, 15, 0.94);
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #17130f;
  color: var(--ink);
  font-family: "Microsoft YaHei UI", "Noto Sans SC", "PingFang SC", system-ui, sans-serif;
}

body {
  display: grid;
  place-items: center;
  background: #17130f;
  user-select: none;
}

button { color: inherit; font: inherit; }
button:focus-visible { outline: 3px solid #fff1be; outline-offset: 2px; }

#app {
  position: relative;
  width: min(100vw, 177.777vh);
  height: min(56.25vw, 100vh);
  aspect-ratio: 16 / 9;
  overflow: hidden;
  background: #9fcdd4;
  box-shadow: 0 0 0 5px #241a13, 0 20px 70px rgba(0, 0, 0, 0.65);
}

#gameCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

#hud { position: absolute; inset: 0; pointer-events: none; }
#hud button { pointer-events: auto; }
.overlay button { pointer-events: none; }
.overlay.visible button { pointer-events: auto; }

.status-card {
  position: absolute;
  top: 18px;
  width: 302px;
  padding: 11px 14px 10px;
  border: 3px solid var(--ink);
  border-radius: 9px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: var(--panel-shadow);
}

.player-status { left: 18px; }
.enemy-status { right: 18px; text-align: right; }

.status-heading, .status-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.status-heading { margin-bottom: 7px; }
.status-line { margin-top: 6px; color: var(--ink-soft); font-size: 11px; font-weight: 700; }
.faction-name { font-size: 17px; font-weight: 900; letter-spacing: 0.03em; }
.hp-label { color: var(--ink-soft); font-size: 10px; font-weight: 900; letter-spacing: 0.13em; }

.bar-track {
  height: 11px;
  overflow: hidden;
  border: 2px solid var(--ink);
  border-radius: 4px;
  background: #c8a96d;
}
.bar-fill { display: block; width: 100%; height: 100%; transition: width 120ms linear; }
.player-fill { background: var(--blue); }
.enemy-fill { background: var(--violet); }

.center-status {
  position: absolute;
  top: 14px;
  left: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  transform: translateX(-50%);
}

.match-clock {
  min-width: 118px;
  padding: 3px 14px 4px;
  border: 3px solid var(--gold);
  border-radius: 7px;
  background: #2a2018;
  color: #ffe39b;
  font-family: "Arial Black", "Microsoft YaHei UI", sans-serif;
  font-size: 22px;
  font-weight: 900;
  letter-spacing: 0.12em;
  text-align: center;
  box-shadow: 4px 4px 0 rgba(30, 21, 15, 0.9);
}

.economy-lines { display: flex; gap: 6px; font-size: 11px; font-weight: 800; }
.resource-pill, .income-pill {
  padding: 4px 9px;
  border: 2px solid var(--ink);
  border-radius: 5px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 2px 2px 0 rgba(30, 21, 15, 0.85);
}
.resource-pill b { color: #8f5b16; font-size: 13px; }
.income-pill b { color: #277347; }
.center-buttons { display: flex; gap: 5px; }

.mini-btn {
  border: 2px solid var(--ink);
  border-radius: 5px;
  padding: 3px 11px;
  background: var(--paper);
  color: var(--ink);
  font-size: 10px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 2px 2px 0 rgba(30, 21, 15, 0.9);
}
.mini-btn:hover { background: var(--paper-light); transform: translate(-1px, -1px); }
.mini-btn:active { transform: translate(1px, 1px); box-shadow: none; }

.hint-bar {
  position: absolute;
  top: 129px;
  left: 50%;
  max-width: 880px;
  padding: 7px 17px;
  border: 2px solid var(--ink);
  border-radius: 5px;
  background: #fff0c9;
  color: var(--ink);
  font-size: 11px;
  font-weight: 700;
  text-align: center;
  transform: translateX(-50%);
  box-shadow: 3px 3px 0 rgba(30, 21, 15, 0.82);
}

.bottom-dock {
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 14px;
  display: grid;
  grid-template-columns: minmax(300px, 0.9fr) minmax(540px, 1.65fr) minmax(360px, 1.1fr);
  gap: 10px;
  min-height: 174px;
  pointer-events: none;
}

.build-panel,
.army-panel,
.command-panel {
  border: 3px solid var(--ink);
  border-radius: 9px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: var(--panel-shadow);
}

.build-panel { padding: 12px; }
.army-panel { display: grid; grid-template-columns: 0.9fr 1.35fr; gap: 12px; padding: 12px; }
.command-panel { padding: 12px; }

.panel-title {
  margin-bottom: 8px;
  color: #765d42;
  font-size: 9px;
  font-weight: 900;
  letter-spacing: 0.17em;
}

.build-cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; }
.build-card {
  display: flex;
  min-width: 0;
  min-height: 78px;
  padding: 8px 7px;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
  border: 2px solid var(--ink);
  border-radius: 5px;
  background: #e5ca91;
  color: var(--ink);
  cursor: pointer;
  box-shadow: 2px 2px 0 rgba(30, 21, 15, 0.9);
  transition: 80ms ease;
}
.build-card:hover { background: #f7e1a8; transform: translate(-1px, -1px); }
.build-card.active { background: #ffd267; box-shadow: 3px 3px 0 rgba(30, 21, 15, 0.95); }
.build-card:active { transform: translate(1px, 1px); box-shadow: none; }
.build-card.poor { opacity: 0.5; }
.card-icon { color: #8b5916; font-size: 18px; line-height: 1; font-weight: 900; }
.card-copy { display: flex; min-width: 0; flex-direction: column; gap: 1px; text-align: left; }
.card-copy b { overflow: hidden; font-size: 11px; text-overflow: ellipsis; white-space: nowrap; }
.card-copy small { color: var(--ink-soft); font-size: 9px; font-weight: 700; }
.micro-copy { margin-top: 7px; color: #755d42; font-size: 9px; font-weight: 700; line-height: 1.4; }

.selection-panel {
  display: flex;
  min-width: 0;
  padding-right: 11px;
  flex-direction: column;
  border-right: 2px solid rgba(36, 26, 19, 0.25);
}
.selection-title { margin-top: 4px; font-size: 16px; font-weight: 900; }
.selection-sub { margin-top: 4px; color: #6f5840; font-size: 10px; font-weight: 700; line-height: 1.45; }
.selection-actions { display: flex; margin-top: auto; gap: 6px; }

.action-btn {
  border: 2px solid var(--ink);
  border-radius: 5px;
  padding: 6px 10px;
  background: #ffd267;
  color: var(--ink);
  font-size: 10px;
  font-weight: 900;
  cursor: pointer;
  box-shadow: 2px 2px 0 rgba(30, 21, 15, 0.9);
}
.action-btn:hover { background: #ffe091; }
.action-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.production-wrap { min-width: 0; }
.unit-buttons { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; }
.unit-btn {
  position: relative;
  min-width: 0;
  min-height: 51px;
  overflow: hidden;
  padding: 6px 7px;
  border: 2px solid rgba(36, 26, 19, 0.72);
  border-radius: 5px;
  background: #ead2a0;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}
.unit-btn:hover { background: #f7e2b1; }
.unit-btn.active { border-color: var(--ink); background: #ffd267; box-shadow: inset 0 0 0 2px #fff0b3; }
.unit-btn b { display: block; overflow: hidden; font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
.unit-btn small { color: #70573d; font-size: 8px; font-weight: 700; }
.unit-btn.unavailable { opacity: 0.4; cursor: not-allowed; }
.unit-btn .unit-dot {
  position: absolute;
  top: 7px;
  right: 7px;
  width: 7px;
  height: 7px;
  border: 1px solid var(--ink);
  border-radius: 50%;
  background: #c68125;
  opacity: 0;
}
.unit-btn.active .unit-dot { opacity: 1; }

.command-buttons { display: grid; grid-template-columns: 1fr; gap: 5px; }
.command-btn {
  display: grid;
  grid-template-columns: 32px 1fr;
  grid-template-rows: auto auto;
  align-items: center;
  min-height: 43px;
  padding: 3px 9px 3px 5px;
  border: 2px solid var(--ink);
  border-radius: 5px;
  background: #ecd5a6;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  box-shadow: 2px 2px 0 rgba(30, 21, 15, 0.82);
}
.command-btn:hover:not(:disabled) { background: #fae8b9; transform: translateX(-2px); }
.command-btn:active:not(:disabled) { transform: translate(1px, 1px); box-shadow: none; }
.command-btn:disabled { opacity: 0.36; cursor: not-allowed; }
.command-key {
  grid-row: 1 / 3;
  display: grid;
  width: 24px;
  height: 24px;
  place-items: center;
  border: 2px solid var(--ink);
  border-radius: 50%;
  background: #fff0c9;
  font-size: 9px;
  font-weight: 900;
}
.command-btn b { font-size: 11px; line-height: 1.05; }
.command-btn small { color: #70573d; font-size: 8px; font-weight: 700; }
.attack-btn:hover:not(:disabled) { background: #f3a58f; }
.hold-btn:hover:not(:disabled) { background: #a9c8ef; }
.retreat-btn:hover:not(:disabled) { background: #9bd7ad; }

.toast {
  position: absolute;
  top: 176px;
  left: 50%;
  z-index: 20;
  max-width: min(620px, 80%);
  padding: 8px 15px;
  border: 2px solid var(--ink);
  border-radius: 5px;
  background: #fff0c9;
  color: var(--ink);
  font-size: 11px;
  font-weight: 800;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -8px);
  transition: opacity 150ms ease, transform 150ms ease;
  box-shadow: 3px 3px 0 rgba(30, 21, 15, 0.82);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.overlay {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 28px;
  background: rgba(36, 26, 19, 0.72);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 220ms ease, visibility 0s linear 220ms;
}
.overlay.visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transition: opacity 220ms ease, visibility 0s;
}

.start-card,
.end-card {
  width: min(760px, 92vw);
  padding: 34px 42px 31px;
  border: 4px solid var(--ink);
  border-radius: 12px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 10px 10px 0 rgba(25, 17, 12, 0.9);
  text-align: center;
}

.eyebrow { color: #8c5a16; font-size: 9px; font-weight: 900; letter-spacing: 0.28em; }
.start-card h1 {
  margin: 13px 0 7px;
  color: var(--ink);
  font-family: "Arial Black", "Microsoft YaHei UI", sans-serif;
  font-size: clamp(44px, 6vw, 74px);
  font-weight: 900;
  letter-spacing: 0.07em;
  line-height: 1;
  text-shadow: 4px 4px 0 #d6b270;
}
.start-card h1 span {
  display: block;
  margin-top: 8px;
  color: #9c6417;
  font-size: 0.3em;
  letter-spacing: 0.32em;
}
.tagline { margin: 0 0 21px; color: #72583b; font-size: 12px; font-weight: 700; letter-spacing: 0.1em; }

.faction-picker { display: grid; grid-template-columns: 1fr 1fr; gap: 11px; }
.faction-choice {
  display: flex;
  min-height: 112px;
  padding: 14px;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  border: 3px solid var(--ink);
  border-radius: 8px;
  background: #e8cd94;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  box-shadow: 3px 3px 0 rgba(30, 21, 15, 0.9);
  transition: 80ms ease;
}
.faction-choice:hover { background: #f4dfa9; transform: translate(-1px, -1px); }
.faction-choice.selected { background: #ffd267; box-shadow: 4px 4px 0 rgba(30, 21, 15, 0.95); }
.faction-emblem { margin-bottom: 4px; color: #8c5a16; font-size: 27px; }
.faction-choice b { font-size: 18px; }
.faction-choice small { margin-top: 3px; color: #6d5439; font-size: 10px; font-weight: 700; }

.start-controls {
  display: flex;
  margin: 20px 0;
  flex-wrap: wrap;
  justify-content: center;
  gap: 7px 16px;
  color: #6d5439;
  font-size: 10px;
  font-weight: 700;
}
.start-controls b { color: var(--ink); }

.start-btn {
  min-width: 210px;
  padding: 12px 24px;
  border: 3px solid var(--ink);
  border-radius: 6px;
  background: #2c2118;
  color: #ffe39a;
  font-size: 13px;
  font-weight: 900;
  letter-spacing: 0.13em;
  cursor: pointer;
  box-shadow: 4px 4px 0 rgba(30, 21, 15, 0.9);
}
.start-btn:hover { background: #443022; transform: translate(-1px, -1px); }
.start-btn:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 rgba(30, 21, 15, 0.9); }

.end-card h2 { margin: 13px 0 9px; color: #9c6417; font-size: 50px; letter-spacing: 0.08em; }
.end-card p { margin: 0 0 22px; color: #6d5439; font-weight: 700; }

.pause-overlay {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-content: center;
  gap: 7px;
  background: rgba(36, 26, 19, 0.5);
  text-align: center;
  opacity: 0;
  pointer-events: none;
}
.pause-overlay.visible { opacity: 1; }
.pause-label { color: #fff0bd; font-size: 36px; font-weight: 900; letter-spacing: 0.12em; text-shadow: 3px 3px 0 #241a13; }

.error-overlay {
  position: absolute;
  inset: 0;
  z-index: 100;
  display: none;
  padding: 40px;
  overflow: auto;
  background: #301713;
  color: #ffe0ce;
  font: 13px/1.6 Consolas, monospace;
  white-space: pre-wrap;
}
.error-overlay.visible { display: block; }

@media (max-width: 1100px) {
  .status-card { width: 245px; }
  .bottom-dock { grid-template-columns: 0.8fr 1.55fr 1fr; }
  .army-panel { grid-template-columns: 0.85fr 1.2fr; }
  .hint-bar { top: 105px; font-size: 9px; }
}

@media (max-height: 650px) {
  .bottom-dock { min-height: 145px; bottom: 9px; }
  .build-card { min-height: 62px; }
  .status-card { top: 9px; }
  .center-status { top: 7px; }
  .hint-bar { display: none; }
}
/* Hand-painted texture pass */
.status-card,
.build-panel,
.army-panel,
.command-panel,
.start-card,
.end-card {
  background-image:
    radial-gradient(rgba(82, 55, 27, 0.055) 0.7px, transparent 0.8px),
    linear-gradient(180deg, rgba(255, 249, 223, 0.42), rgba(204, 168, 108, 0.08));
  background-size: 7px 7px, 100% 100%;
}

.status-card {
  box-shadow: 4px 4px 0 rgba(30, 21, 15, 0.94), inset 0 0 0 1px rgba(255, 247, 213, 0.35);
}

.faction-name {
  text-shadow: 1px 1px 0 rgba(255, 247, 213, 0.55);
}

.panel-title {
  padding-bottom: 5px;
  border-bottom: 1px dashed rgba(72, 50, 29, 0.28);
}

.build-card,
.unit-btn,
.command-btn {
  background-image: linear-gradient(180deg, rgba(255, 248, 220, 0.22), rgba(104, 75, 42, 0.04));
}

.faction-choice {
  background-image:
    radial-gradient(rgba(82, 55, 27, 0.05) 0.7px, transparent 0.8px),
    linear-gradient(135deg, rgba(255, 250, 226, 0.42), rgba(182, 139, 74, 0.08));
  background-size: 7px 7px, 100% 100%;
}

.start-card h1 {
  text-shadow: 4px 4px 0 #d6b270, 7px 7px 0 rgba(53, 37, 23, 0.18);
}

.faction-emblem {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border: 2px solid #241a13;
  border-radius: 50%;
  background: rgba(255, 244, 204, 0.55);
}

#uiLayer {
  position: absolute;
  top: 0;
  left: 0;
  width: 1600px;
  height: 900px;
  pointer-events: none;
  transform-origin: top left;
  will-change: transform;
}
