:root {
  --wood-dark: #3a2417;
  --wood: #5a3a22;
  --wood-light: #8a5a34;
  --wood-edge: #b3884f;
  --parch: #f2dcae;
  --gold: #ffd34d;
  --hp-green: #6ed44a;
  --hp-red: #e6473b;
  --ink: #2a1c10;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #1a2a3a;
  font-family: "Trebuchet MS", "PingFang SC", "Microsoft YaHei", sans-serif;
  color: var(--parch);
  user-select: none;
  -webkit-user-select: none;
}

img { image-rendering: pixelated; image-rendering: crisp-edges; }

#game-root { position: relative; width: 100vw; height: 100vh; }

#world {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: default;
  background: #2a5a7a;
}

.hidden { display: none !important; }

/* ============ Wood panel base ============ */
.wood {
  background:
    repeating-linear-gradient(90deg, rgba(0,0,0,0.06) 0 2px, rgba(255,255,255,0.02) 2px 4px),
    linear-gradient(180deg, var(--wood-light), var(--wood) 55%, var(--wood-dark));
  border: 3px solid var(--wood-edge);
  border-radius: 10px;
  box-shadow: inset 0 0 0 2px rgba(0,0,0,0.25), 0 6px 14px rgba(0,0,0,0.5);
}

/* ============ Top bar ============ */
#topbar {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 22px;
  padding: 6px 18px;
  z-index: 20;
  background: linear-gradient(180deg, rgba(58,36,23,0.92), rgba(36,22,14,0.92));
  border: 2px solid var(--wood-edge);
  border-radius: 30px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.5);
}
.res-group { display: flex; gap: 16px; }
.res {
  display: flex; align-items: center; gap: 6px;
  font-size: 20px; font-weight: bold;
  color: var(--parch);
  text-shadow: 1px 1px 0 #000;
  min-width: 72px;
}
.res-icon { width: 30px; height: 30px; }
#objective {
  display: flex; flex-direction: column; align-items: center;
  border-left: 2px solid var(--wood-edge);
  border-right: 2px solid var(--wood-edge);
  padding: 0 18px;
}
#obj-text { font-size: 14px; color: var(--gold); }
#clock { font-size: 18px; font-weight: bold; }
.top-right { display: flex; gap: 6px; align-items: center; }
.mini-btn {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--wood-light); color: var(--parch);
  border: 2px solid var(--wood-edge); cursor: pointer;
  font-weight: bold; font-size: 16px;
}
.mini-btn:hover { background: var(--wood-edge); }

/* ============ Toast ============ */
#toast {
  position: absolute;
  top: 76px; left: 50%; transform: translateX(-50%);
  z-index: 30; display: flex; flex-direction: column; gap: 6px;
  align-items: center; pointer-events: none;
}
.toast-msg {
  background: rgba(36,22,14,0.95);
  border: 2px solid var(--wood-edge);
  color: var(--parch);
  padding: 6px 16px; border-radius: 16px; font-size: 15px;
  animation: toastIn 0.2s ease, toastOut 0.4s ease 1.6s forwards;
  text-shadow: 1px 1px 0 #000;
}
.toast-msg.warn { color: #ffb3a0; border-color: #c0503f; }
.toast-msg.good { color: #b9f0a0; border-color: #5aa845; }
@keyframes toastIn { from { opacity:0; transform: translateY(-8px);} to {opacity:1;} }
@keyframes toastOut { to { opacity:0; transform: translateY(-8px);} }

/* ============ Bottom bar ============ */
#bottombar {
  position: absolute;
  bottom: 10px; left: 50%; transform: translateX(-50%);
  display: flex; gap: 12px; z-index: 20;
  align-items: stretch;
}
#portrait-panel { width: 240px; padding: 10px; }
#command-panel { width: 320px; padding: 10px; display: flex; flex-direction: column; }
#minimap-panel { padding: 8px; }

.hint { font-size: 13px; color: var(--wood-edge); text-align: center; margin-top: 28px; }

#portrait-top { display: flex; gap: 10px; }
#portrait-img {
  width: 72px; height: 72px;
  background: rgba(0,0,0,0.3);
  border: 2px solid var(--wood-edge); border-radius: 8px;
  object-fit: none; object-position: center;
}
#portrait-info { flex: 1; min-width: 0; }
#portrait-name { font-size: 16px; font-weight: bold; color: var(--gold); text-shadow: 1px 1px 0 #000; }
#portrait-hpbar {
  position: relative; height: 16px; margin: 5px 0;
  background: #2a1c10; border: 1px solid #000; border-radius: 8px; overflow: hidden;
}
#portrait-hpfill { height: 100%; width: 100%; background: linear-gradient(180deg, #8ef06a, var(--hp-green)); transition: width 0.15s; }
#portrait-hptext { position: absolute; inset: 0; text-align: center; font-size: 11px; line-height: 16px; color: #fff; text-shadow: 1px 1px 0 #000; }
#portrait-stats { font-size: 12px; color: var(--parch); line-height: 1.5; }
#portrait-extra { margin-top: 8px; font-size: 12px; }
.multi-grid { display: flex; flex-wrap: wrap; gap: 3px; margin-top: 4px; max-height: 60px; overflow: hidden; }
.multi-chip {
  width: 26px; height: 26px; border: 1px solid var(--wood-edge); border-radius: 5px;
  background: rgba(0,0,0,0.3); position: relative; cursor: pointer;
}
.multi-chip img { width: 100%; height: 100%; object-fit: none; object-position: center; transform: scale(0.45); transform-origin: center; }

#command-title { font-size: 13px; color: var(--wood-edge); margin-bottom: 6px; text-align:center; letter-spacing: 2px; }
#command-grid {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px;
  flex: 1;
}
.cmd-btn {
  position: relative;
  aspect-ratio: 1;
  background: linear-gradient(180deg, #6a8fb8, #3f5d82);
  border: 2px solid var(--wood-edge);
  border-radius: 8px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.05s, filter 0.1s;
  overflow: visible;
}
.cmd-btn:hover { filter: brightness(1.15); }
.cmd-btn:active { transform: translateY(2px); }
.cmd-btn.disabled { filter: grayscale(0.8) brightness(0.6); cursor: not-allowed; }
.cmd-btn img { width: 80%; height: 80%; object-fit: contain; pointer-events: none; }
.cmd-btn .cost {
  position: absolute; bottom: -2px; right: 1px;
  font-size: 10px; font-weight: bold; display: flex; gap: 3px;
  background: rgba(0,0,0,0.6); padding: 0 3px; border-radius: 4px;
}
.cmd-btn .cost .g { color: var(--gold); }
.cmd-btn .cost .w { color: #d6a06a; }
.cmd-btn .hotkey {
  position: absolute; top: -2px; left: 1px; font-size: 10px; font-weight: bold;
  color: #fff; background: rgba(0,0,0,0.55); padding: 0 3px; border-radius: 4px;
}

/* Command tooltip */
#cmd-tooltip {
  position: fixed; z-index: 60; pointer-events: none;
  background: rgba(28,18,10,0.97); border: 2px solid var(--wood-edge);
  border-radius: 8px; padding: 8px 10px; max-width: 220px;
  font-size: 12px; color: var(--parch); box-shadow: 0 4px 12px rgba(0,0,0,0.6);
}
#cmd-tooltip .tt-name { font-size: 14px; font-weight: bold; color: var(--gold); margin-bottom: 3px; }
#cmd-tooltip .tt-cost { color: #d6a06a; margin-top: 4px; }

#minimap { display: block; image-rendering: pixelated; border: 2px solid #000; border-radius: 4px; }

/* ============ Menus ============ */
#menu, #endscreen, #help {
  position: absolute; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 50% 35%, rgba(40,70,90,0.85), rgba(10,18,28,0.96));
  backdrop-filter: blur(2px);
}
.menu-card, .end-card, .help-card {
  width: min(640px, 94vw);
  padding: 28px 32px;
  text-align: center;
  background: linear-gradient(180deg, var(--wood-light), var(--wood) 55%, var(--wood-dark));
  border: 4px solid var(--wood-edge);
  border-radius: 18px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.7), inset 0 0 0 3px rgba(0,0,0,0.25);
  max-height: 94vh;
  overflow-y: auto;
}
.menu-card h1, .end-card h2 {
  font-size: 48px; color: var(--gold);
  text-shadow: 3px 3px 0 #000, 0 0 18px rgba(255,211,77,0.4);
  letter-spacing: 4px;
}
.subtitle { color: var(--wood-edge); letter-spacing: 3px; margin-bottom: 14px; font-size: 14px; }
.menu-body { text-align: left; background: rgba(0,0,0,0.22); padding: 14px 18px; border-radius: 12px; margin-bottom: 14px; line-height: 1.7; font-size: 13px; }
.menu-body ul { margin: 8px 0 0 18px; }
.menu-body b { color: var(--gold); }

.level-block { margin-bottom: 14px; text-align: left; }
.level-label { font-size: 13px; color: var(--wood-edge); margin-bottom: 8px; letter-spacing: 2px; text-align: center; }
.level-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}
.level-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; min-height: 58px; padding: 6px 4px;
  border-radius: 10px; cursor: pointer;
  background: rgba(0,0,0,0.32); color: var(--parch);
  border: 2px solid var(--wood-edge); font-size: 12px; line-height: 1.2;
}
.level-btn .lv-num { font-size: 16px; font-weight: bold; color: var(--gold); }
.level-btn .lv-name { font-size: 11px; }
.level-btn:hover:not(:disabled) { filter: brightness(1.12); }
.level-btn.active {
  background: linear-gradient(180deg, #ffe27a, var(--gold));
  color: var(--ink); border-color: #fff3c4;
}
.level-btn.active .lv-num { color: var(--ink); }
.level-btn.cleared:not(.active) { box-shadow: inset 0 0 0 1px #6ed44a; }
.level-btn.locked, .level-btn:disabled {
  opacity: 0.45; cursor: not-allowed; filter: grayscale(0.6);
}
.level-desc {
  margin-top: 8px; font-size: 12px; color: #cdb892;
  background: rgba(0,0,0,0.2); padding: 8px 12px; border-radius: 8px;
  min-height: 2.6em; line-height: 1.5;
}

.diff-row { display: flex; gap: 8px; align-items: center; justify-content: center; margin-bottom: 16px; font-size: 14px; }
.diff-btn {
  padding: 6px 16px; border-radius: 18px; cursor: pointer;
  background: rgba(0,0,0,0.3); color: var(--parch);
  border: 2px solid var(--wood-edge); font-weight: bold;
}
.diff-btn.active { background: var(--gold); color: var(--ink); }

#btn-play, #btn-restart, #btn-help-close, #btn-next, #btn-menu {
  font-size: 20px; font-weight: bold; letter-spacing: 2px;
  padding: 12px 36px; cursor: pointer;
  color: var(--ink);
  background: linear-gradient(180deg, #ffe27a, var(--gold) 60%, #d9a328);
  border: 3px solid #fff3c4;
  border-radius: 14px;
  box-shadow: 0 6px 0 #9c6f10, 0 10px 18px rgba(0,0,0,0.5);
  transition: transform 0.06s, box-shadow 0.06s;
}
#btn-play:hover, #btn-restart:hover, #btn-help-close:hover, #btn-next:hover, #btn-menu:hover { filter: brightness(1.06); }
#btn-play:active, #btn-restart:active, #btn-help-close:active, #btn-next:active, #btn-menu:active {
  transform: translateY(4px); box-shadow: 0 2px 0 #9c6f10;
}
#btn-menu.btn-secondary, .btn-secondary {
  background: linear-gradient(180deg, #8a6a4a, #5a3a22 60%, #3a2417);
  color: var(--parch); border-color: var(--wood-edge);
  box-shadow: 0 6px 0 #2a1810, 0 10px 18px rgba(0,0,0,0.5);
  font-size: 16px; padding: 10px 28px;
}
.end-actions { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; align-items: center; }
#loading { margin-top: 16px; color: var(--gold); font-size: 16px; }

.help-card { text-align: left; }
.help-card h2 { color: var(--gold); text-align: center; margin-bottom: 14px; }
.help-card ul { margin-left: 18px; line-height: 1.9; font-size: 14px; }
.help-card b { color: var(--gold); }
.help-card button { display: block; margin: 18px auto 0; }
#end-sub { font-size: 17px; margin: 12px 0 22px; line-height: 1.5; }

@media (max-width: 640px) {
  .level-row { grid-template-columns: repeat(3, 1fr); }
  .menu-card h1 { font-size: 36px; }
}
