:root {
  --ink: #1e211a;
  --paper: #e9e1c8;
  --paper-light: #f7f0dd;
  --moss: #67725e;
  --moss-dark: #3e493d;
  --red: #a63d2f;
  --gold: #d9a62e;
  --path: #d6cfb4;
  --grass: #788474;
  --cell-border: rgba(30, 33, 26, 0.25);
  --cols: 8;
  --rows: 5;
  --reserve-size: 5;
  --projectile-duration: 170ms;
  font-family: "Noto Serif TC", "Songti TC", serif;
}

* { box-sizing: border-box; }

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(rgba(239, 231, 204, 0.74), rgba(219, 211, 183, 0.82)),
    repeating-linear-gradient(2deg, transparent 0 12px, rgba(44, 54, 43, 0.08) 13px 14px),
    #9aa492;
}

button { font: inherit; }

.game-shell {
  width: min(100%, 1080px);
  min-height: 100vh;
  margin: 0 auto;
  padding: 20px clamp(14px, 4vw, 42px) 48px;
  background: rgba(229, 224, 199, 0.5);
  box-shadow: 0 0 70px rgba(24, 31, 23, 0.2);
}

.battle-header,
.status-bar,
.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.battle-header { padding: 8px 0 18px; }

.kicker {
  margin: 0 0 4px;
  color: var(--red);
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.18em;
}

h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(1.8rem, 5vw, 3.2rem); letter-spacing: 0.08em; }

.header-actions { display: flex; gap: 8px; }

.gm-button {
  min-width: 52px;
  height: 46px;
  border: 2px solid var(--ink);
  border-radius: 12px;
  color: var(--ink);
  background: var(--gold);
  box-shadow: 3px 4px 0 var(--ink);
  cursor: pointer;
  font-weight: 900;
}

.icon-button {
  width: 46px;
  height: 46px;
  border: 2px solid var(--ink);
  border-radius: 12px;
  color: var(--paper-light);
  background: var(--moss-dark);
  box-shadow: 3px 4px 0 var(--ink);
  cursor: pointer;
  font-size: 1.25rem;
  font-weight: 900;
}

.icon-button:active { transform: translate(2px, 2px); box-shadow: 1px 2px 0 var(--ink); }

.status-bar {
  padding: 14px clamp(12px, 3vw, 26px);
  border: 3px solid var(--ink);
  border-radius: 16px 16px 4px 4px;
  color: var(--paper-light);
  background: var(--moss-dark);
  box-shadow: 0 7px 0 rgba(30, 33, 26, 0.85);
}

.status-item { display: flex; align-items: center; gap: 10px; min-width: 130px; }
.status-item:last-child { justify-content: flex-end; text-align: right; }
.status-item small, .wave-status small { display: block; color: #ccd3c4; font-size: 0.68rem; letter-spacing: 0.1em; }
.status-item strong { display: block; font-size: 1.2rem; }
.status-icon {
  display: grid;
  width: 46px;
  aspect-ratio: 1;
  place-items: center;
  border: 2px solid #171a16;
  border-radius: 50%;
  color: var(--ink);
  background: var(--paper-light);
  font-weight: 900;
}
.wave-status { text-align: center; }
.wave-status strong { display: block; font-size: clamp(1rem, 3vw, 1.5rem); }
.wave-status span { font-size: 0.72rem; color: #dce2d7; }
.hearts strong { color: #ef6254; letter-spacing: 0.08em; white-space: nowrap; }

.battlefield-wrap { position: relative; margin-top: 42px; }
.battlefield {
  position: relative;
  width: 100%;
  aspect-ratio: var(--cols) / var(--rows);
  overflow: hidden;
  border: 5px solid var(--ink);
  border-radius: 6px;
  background: var(--grass);
  box-shadow: 0 9px 0 rgba(30, 33, 26, 0.8);
}

.grid {
  display: grid;
  width: 100%;
  height: 100%;
  grid-template-columns: repeat(var(--cols), 1fr);
  grid-template-rows: repeat(var(--rows), 1fr);
}

.cell {
  position: relative;
  min-width: 0;
  border: 1px solid var(--cell-border);
  background:
    linear-gradient(135deg, rgba(255,255,255,.07), transparent 55%),
    var(--grass);
  cursor: crosshair;
}

.cell::after {
  content: "";
  position: absolute;
  inset: 14%;
  opacity: 0.12;
  background: repeating-linear-gradient(75deg, transparent 0 8px, #1f2c1e 9px 10px);
}

.cell.path {
  background:
    radial-gradient(circle at 25% 35%, rgba(111, 98, 69, .13) 0 2px, transparent 3px),
    linear-gradient(155deg, rgba(255,255,255,.22), transparent 42%),
    var(--path);
  cursor: not-allowed;
}
.cell.unlocked {
  background:
    linear-gradient(135deg, rgba(255,255,255,.09), transparent 55%),
    var(--grass);
}
.cell.locked {
  background:
    repeating-linear-gradient(135deg, rgba(27,33,27,.12) 0 7px, transparent 7px 14px),
    #4f5a50;
  cursor: not-allowed;
  filter: saturate(.55);
}
.cell.path::after { display: none; }
.cell.start { box-shadow: inset 0 0 0 4px #9a382b; }
.cell.end { box-shadow: inset 0 0 0 4px var(--gold); }
.cell.start::before, .cell.end::before {
  position: absolute;
  z-index: 1;
  inset: auto 4px 3px;
  color: rgba(30, 33, 26, .62);
  font-size: clamp(.5rem, 1.5vw, .75rem);
  font-weight: 900;
  text-align: center;
}
.cell.start::before { content: "始"; }
.cell.end::before { content: "關"; }
.cell.selected-target { box-shadow: inset 0 0 0 4px #e4bd42; }
.cell.occupied { cursor: pointer; }
.cell.drop-ready { box-shadow: inset 0 0 0 4px rgba(228, 189, 66, .85); }

.entity-layer, .projectile-layer { position: absolute; inset: 0; pointer-events: none; }
.unit, .enemy {
  position: absolute;
  z-index: 3;
  display: grid;
  width: calc(76% / var(--cols));
  aspect-ratio: 1;
  place-items: center;
  transform: translate(-50%, -50%);
}
.unit {
  color: #211d16;
  border: 2px solid #322b1e;
  border-radius: 10px;
  background: #f5ecd5;
  box-shadow: 3px 4px 0 rgba(31, 26, 18, .52);
  font-size: clamp(1rem, 4vw, 2.25rem);
  font-weight: 900;
  pointer-events: auto;
  cursor: grab;
}
.unit:active { cursor: grabbing; }
.unit.selected { outline: 4px solid rgba(228, 189, 66, .9); }
.unit.drop-ready { outline: 4px dashed rgba(228, 189, 66, .95); }
.unit::after { content: attr(data-level); position: absolute; top: 2px; right: 5px; font: 800 clamp(.5rem, 1.4vw, .75rem) sans-serif; }
.unit.cavalry { color: #91452b; }
.unit.spear { color: #293c5a; }
.unit.archer { color: #355837; }
.unit.attacking { animation: attack-pulse 180ms ease-out; }

.enemy {
  z-index: 5;
  width: calc(61% / var(--cols));
  color: #151714;
  font-size: clamp(.9rem, 3.5vw, 2rem);
  font-weight: 900;
  text-shadow: 1px 2px 0 rgba(255,255,255,.25);
}
.enemy::after { content: ""; position: absolute; inset: 14% 10%; z-index: -1; border-radius: 50%; background: rgba(43, 50, 40, .22); transform: skew(-10deg); }
.health-track {
  position: absolute;
  top: -5px;
  left: 5%;
  width: 90%;
  height: 6px;
  overflow: hidden;
  border: 1px solid #1b1d18;
  border-radius: 99px;
  background: #2b2c27;
}
.health-fill { display: block; width: 100%; height: 100%; background: #e64e3f; transform-origin: left; }
.enemy.hit { animation: enemy-hit 170ms ease; }

.projectile {
  position: absolute;
  z-index: 8;
  display: grid;
  width: 22px;
  height: 22px;
  place-items: center;
  color: #171913;
  font: 900 18px/1 sans-serif;
  transform: translate(-50%, -50%);
  transition: left var(--projectile-duration) linear, top var(--projectile-duration) linear;
}
.projectile.arrow { font-size: 25px; }
.projectile.slash { color: #9e3027; }
.projectile.charge { width: 28px; height: 28px; border-radius: 50%; background: rgba(166,61,47,.28); }
.projectile.spear { color: #29466c; }

.range-preview {
  position: absolute;
  z-index: 2;
  display: none;
  border: 2px dashed rgba(218, 166, 46, .9);
  border-radius: 50%;
  background: rgba(218, 166, 46, .12);
  pointer-events: none;
  transform: translate(-50%, -50%);
}

.gate-label {
  position: absolute;
  z-index: 10;
  top: -25px;
  padding: 4px 11px;
  border: 2px solid var(--ink);
  border-radius: 5px 5px 0 0;
  color: var(--paper-light);
  background: var(--red);
  font-size: .68rem;
  font-weight: 900;
}
.start-label { left: 0; }
.end-label { right: 0; background: var(--gold); color: var(--ink); }
.battle-message { min-height: 24px; margin: 18px 0 0; color: #454b40; text-align: center; font-size: .82rem; }

.barracks {
  margin-top: 24px;
  padding: clamp(18px, 3vw, 28px);
  border: 3px solid var(--ink);
  border-radius: 6px 6px 18px 18px;
  background: rgba(247, 240, 221, .78);
  box-shadow: 0 7px 0 rgba(30, 33, 26, .74);
}
.section-heading h2 { margin: 0; font-size: clamp(1.3rem, 4vw, 2rem); }
.battle-button {
  min-height: 48px;
  padding: 9px 20px;
  border: 2px solid var(--ink);
  border-radius: 10px;
  color: var(--paper-light);
  background: var(--red);
  box-shadow: 3px 4px 0 var(--ink);
  cursor: pointer;
  font-weight: 900;
}
.battle-button:disabled { opacity: .45; cursor: not-allowed; }
.battle-button:not(:disabled):active { transform: translate(2px,2px); box-shadow: 1px 2px 0 var(--ink); }

.recruitment-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 142px;
  gap: 14px;
  margin-top: 20px;
}
.reserve-slots { display: grid; grid-template-columns: repeat(var(--reserve-size), 1fr); gap: 9px; }
.reserve-slot {
  display: grid;
  min-height: 116px;
  min-width: 0;
  padding: 12px 8px;
  border: 2px solid #514b3c;
  border-radius: 8px;
  color: var(--ink);
  background: var(--paper-light);
  box-shadow: 2px 3px 0 rgba(30,33,26,.45);
  cursor: pointer;
  text-align: center;
  cursor: grab;
}
.reserve-slot.empty {
  place-items: center;
  border-style: dashed;
  color: rgba(58, 57, 48, .4);
  background: rgba(226, 220, 198, .48);
  cursor: default;
  font-size: 1.4rem;
}
.reserve-slot:hover:not(.empty), .reserve-slot.active { border-color: #a66c13; outline: 3px solid rgba(230, 174, 43, .38); transform: translateY(-2px); }
.reserve-slot.tool-slot { background: #dce6cf; }
.reserve-slot.drop-ready { border-color: var(--gold); background: #fff6d7; }
.unit-glyph { font-size: clamp(1.65rem, 5vw, 2.7rem); font-weight: 900; line-height: 1.1; }
.unit-name { margin-top: 4px; font-weight: 900; }
.unit-role { overflow: hidden; color: #6a6659; font-size: .65rem; text-overflow: ellipsis; white-space: nowrap; }
.reserve-slot strong { margin-top: 7px; color: var(--red); font-size: .7rem; }
.recruit-button {
  display: grid;
  align-content: center;
  min-height: 116px;
  padding: 12px;
  border: 3px solid var(--ink);
  border-radius: 10px;
  color: var(--paper-light);
  background: linear-gradient(90deg, #94533e 0 36%, #3e4442 36%);
  box-shadow: 4px 5px 0 var(--ink);
  cursor: pointer;
}
.recruit-button span { font-size: 1.55rem; font-weight: 900; }
.recruit-button strong { color: #f1d276; font-size: 1rem; }
.recruit-button small { color: #d5d8d0; font-size: .64rem; }
.recruit-button:disabled { filter: grayscale(.8); opacity: .55; cursor: not-allowed; }
.recruit-button:not(:disabled):active { transform: translate(2px,2px); box-shadow: 2px 3px 0 var(--ink); }
.recruit-button.drawing { animation: recruit-shake 280ms ease; }
.odds-note { margin: 14px 0 0; color: #676659; font-size: .68rem; text-align: center; }

dialog {
  width: min(calc(100% - 32px), 520px);
  padding: 30px;
  border: 3px solid var(--ink);
  border-radius: 12px;
  color: var(--ink);
  background: var(--paper-light);
  box-shadow: 8px 10px 0 rgba(25,28,23,.6);
}
dialog::backdrop { background: rgba(20,24,20,.72); backdrop-filter: blur(3px); }
dialog h2 { font-size: 2rem; }
dialog li { margin-bottom: 10px; }
.dialog-close { position: absolute; top: 10px; right: 14px; border: 0; background: transparent; cursor: pointer; font-size: 1.7rem; }

.gm-dialog { width: min(calc(100% - 24px), 900px); padding: clamp(20px, 4vw, 34px); }
.gm-intro { margin-bottom: 14px; color: #57594f; }
.gm-toolbar, .gm-actions { display: flex; flex-wrap: wrap; gap: 9px; align-items: center; }
.gm-actions { justify-content: flex-end; margin-top: 16px; }
.small-button, .file-button {
  display: inline-flex;
  min-height: 38px;
  align-items: center;
  padding: 6px 12px;
  border: 2px solid var(--ink);
  border-radius: 7px;
  color: var(--ink);
  background: #e1d7bd;
  cursor: pointer;
  font: 800 .78rem/1.2 "Noto Serif TC", serif;
}
.editor-label { display: block; margin: 16px 0 6px; font-size: .76rem; font-weight: 900; }
.gm-tabs { display: flex; gap: 6px; margin-top: 18px; border-bottom: 2px solid #736f60; }
.gm-tab {
  padding: 9px 17px;
  border: 2px solid transparent;
  border-bottom: 0;
  border-radius: 7px 7px 0 0;
  color: #5b5b51;
  background: transparent;
  cursor: pointer;
  font-weight: 900;
}
.gm-tab.active { border-color: #736f60; color: var(--ink); background: #eee5cf; }
.gm-panel { max-height: min(56vh, 620px); overflow: auto; padding: 14px 3px 3px; }
.form-hint { margin: 0 0 12px; color: #65675e; font-size: .72rem; }
.gm-form-section { margin-bottom: 13px; border: 2px solid #aaa28d; border-radius: 9px; background: rgba(244, 237, 217, .66); }
.gm-form-section > summary { padding: 11px 14px; cursor: pointer; font-weight: 900; }
.gm-form-section-body { padding: 2px 14px 15px; }
.gm-field-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 11px; }
.gm-field { display: grid; gap: 4px; align-content: start; }
.gm-field.wide { grid-column: 1 / -1; }
.gm-field-label { font-size: .75rem; font-weight: 900; }
.gm-field-help { color: #68695f; font: .68rem/1.35 sans-serif; }
.gm-field input, .gm-field textarea {
  width: 100%;
  min-height: 38px;
  padding: 7px 9px;
  border: 1px solid #716e61;
  border-radius: 5px;
  color: #25271f;
  background: #fffaf0;
  font: 14px/1.35 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.gm-field textarea { min-height: 120px; resize: vertical; }
.gm-repeat-list { display: grid; gap: 9px; }
.gm-repeat-card { position: relative; border: 1px solid #aaa28d; border-radius: 7px; background: rgba(255,250,240,.76); }
.gm-repeat-card > summary { padding: 11px 44px 11px 12px; cursor: pointer; font-size: .78rem; font-weight: 900; }
.gm-repeat-body { padding: 4px 12px 12px; }
.remove-row { position: absolute; top: 8px; right: 8px; min-width: 30px; min-height: 30px; padding: 2px; border: 1px solid #8b3c32; border-radius: 5px; color: #8b3027; background: #f7ddd4; cursor: pointer; }
.add-row { margin-top: 9px; }
.gm-map-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 9px; color: #66675e; font-size: .68rem; }
.gm-map-legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: .7rem; }
.gm-map-legend span::before { content: ""; display: inline-block; width: 13px; height: 13px; margin-right: 5px; border: 1px solid #4d5047; vertical-align: -2px; }
.gm-map-legend .locked::before { background: #4f5a50; }
.gm-map-legend .unlocked::before { background: var(--grass); }
.gm-map-legend .path::before { background: var(--path); }
.gm-map-grid {
  display: grid;
  width: min(100%, 620px);
  margin: 4px auto;
  grid-template-columns: repeat(var(--gm-map-cols), minmax(34px, 1fr));
  overflow: auto;
  border: 3px solid var(--ink);
  background: #343b34;
}
.gm-map-cell {
  display: grid;
  min-width: 34px;
  aspect-ratio: 1;
  place-content: center;
  gap: 2px;
  border: 1px solid rgba(31,35,29,.35);
  color: #20231d;
  cursor: pointer;
  font-size: clamp(.48rem, 1.4vw, .66rem);
}
.gm-map-cell small { opacity: .62; font: 9px/1 monospace; }
.gm-map-cell.locked { color: #e2e4dc; background: repeating-linear-gradient(135deg, #4f5a50 0 7px, #465047 7px 14px); }
.gm-map-cell.unlocked { background: var(--grass); }
.gm-map-cell.path { background: var(--path); }
.gm-map-cell:hover { outline: 3px solid var(--gold); outline-offset: -3px; }
#gm-config-editor {
  width: 100%;
  min-height: min(50vh, 520px);
  resize: vertical;
  padding: 14px;
  border: 2px solid #4c4c43;
  border-radius: 7px;
  color: #e7eadf;
  background: #242923;
  font: 13px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace;
  tab-size: 2;
}
.gm-error { min-height: 22px; margin: 7px 0; color: #a52e24; font: 700 .75rem/1.4 sans-serif; }
.gm-error.success { color: #386638; }
.gm-dialog details { margin-top: 8px; color: #53564c; font-size: .78rem; }
.gm-dialog code { color: #8e3228; }

.overlay {
  position: fixed;
  z-index: 30;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(19,24,19,.78);
  backdrop-filter: blur(4px);
}
.overlay[hidden] { display: none; }
.result-card { width: min(100%, 500px); padding: 40px; border: 4px solid var(--ink); border-radius: 14px; background: var(--paper-light); box-shadow: 10px 12px 0 rgba(0,0,0,.45); text-align: center; }
.result-card h2 { margin-bottom: 8px; font-size: clamp(2rem, 7vw, 3.8rem); }

@keyframes attack-pulse { 50% { transform: translate(-50%,-50%) scale(1.16) rotate(-3deg); } }
@keyframes enemy-hit { 50% { color: #d53f30; transform: translate(-50%,-50%) scale(.86); } }
@keyframes recruit-shake { 30% { transform: rotate(-2deg) scale(1.03); } 70% { transform: rotate(2deg) scale(1.03); } }

@media (max-width: 700px) {
  .game-shell { padding-inline: 10px; }
  .status-bar { gap: 7px; }
  .status-item { min-width: 0; gap: 5px; }
  .status-icon { width: 34px; font-size: .75rem; }
  .status-item strong { font-size: .9rem; }
  .status-item small, .wave-status small { font-size: .55rem; }
  .wave-status span { display: none; }
  .battlefield-wrap { margin-top: 34px; }
  .recruitment-row { grid-template-columns: 1fr 92px; gap: 7px; }
  .reserve-slots { gap: 4px; }
  .reserve-slot { min-height: 88px; padding: 7px 2px; }
  .recruit-button { min-height: 88px; padding: 6px; }
  .recruit-button span { font-size: 1.1rem; }
  .unit-role { display: none; }
  .section-heading { align-items: flex-end; }
  .battle-button { min-height: 42px; padding: 7px 12px; font-size: .8rem; }
  .gm-field-grid { grid-template-columns: 1fr; }
  .gm-field.wide { grid-column: auto; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
