:root {
  --font-display: 'Cinzel', serif;
  --font-body: 'Satoshi', sans-serif;
  --color-bg: #0c0a08;
  --color-surface: rgba(18, 14, 10, 0.78);
  --color-border: rgba(232, 176, 75, 0.28);
  --color-text: #efe6d5;
  --color-text-muted: #a99e88;
  --color-primary: #e8b04b;
  --color-danger: #d94040;
  --color-ally: #5fa8ff;
  --color-enemy: #e05545;
  --text-xs: 12px;
  --text-sm: 14px;
  --text-base: 16px;
  --text-lg: 20px;
  --text-xl: 28px;
  --text-2xl: 44px;
  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px; --space-6: 24px;
  --panel-blur: 10px;
  --panel-radius: 6px;
  --transition-ui: 180ms cubic-bezier(0.16, 1, 0.3, 1);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; background: var(--color-bg); overscroll-behavior: none; }
body { font-family: var(--font-body); color: var(--color-text); -webkit-tap-highlight-color: transparent; }

#game { position: fixed; inset: 0; display: block; cursor: default; touch-action: none; -webkit-user-select: none; user-select: none; }

.touch-hint { display: none; }
html.has-touch .desktop-hint { display: none; }
html.has-touch span.touch-hint { display: inline; }
html.has-touch div.touch-hint { display: block; }
html.has-touch button.touch-hint { display: inline-flex; }

.hidden { display: none !important; }

.panel {
  background: var(--color-surface);
  backdrop-filter: blur(var(--panel-blur));
  border: 1px solid var(--color-border);
  border-radius: var(--panel-radius);
}

/* ---------- in-game UI ---------- */
#ui { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
#ui button, #ui .mbtn, #ui .tbtn { pointer-events: auto; }

#topbar {
  position: absolute; top: 0; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: var(--space-4);
  padding: var(--space-2) var(--space-4);
  border-top: none; border-radius: 0 0 var(--panel-radius) var(--panel-radius);
  font-size: var(--text-sm); font-weight: 700;
  font-variant-numeric: tabular-nums lining-nums;
}
.res { display: flex; align-items: center; gap: 6px; }
.ico { width: 12px; height: 12px; border-radius: 2px; display: inline-block; }
.ico.gold { background: linear-gradient(135deg, #ffd977, #c9901f); }
.ico.wood { background: linear-gradient(135deg, #9c6b3d, #5e3b1e); }
.ico.supply { background: linear-gradient(135deg, #c9bfa8, #8d846e); border-radius: 50%; }
.spacer { width: var(--space-6); }
#game-clock { color: var(--color-text-muted); font-weight: 500; }
.tbtn {
  background: none; border: 1px solid var(--color-border); color: var(--color-text-muted);
  border-radius: 4px; width: 26px; height: 26px; cursor: pointer; font-size: 14px;
  transition: var(--transition-ui);
}
.tbtn:hover { color: var(--color-primary); border-color: var(--color-primary); }
.tbtn.muted { color: var(--color-danger); text-decoration: line-through; }

#bottombar {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; align-items: flex-end; gap: var(--space-3);
  padding: var(--space-3);
}
#minimap-wrap { padding: 6px; line-height: 0; pointer-events: auto; }
#minimap { cursor: pointer; touch-action: none; }

#selection-panel {
  flex: 1; max-width: 620px; min-height: 130px;
  display: flex; gap: var(--space-4); padding: var(--space-3) var(--space-4);
  pointer-events: auto;
}
#sel-info { flex: 0 0 190px; max-width: 190px; display: flex; flex-direction: column; gap: 6px; }
#sel-name { font-family: var(--font-display); font-weight: 700; font-size: var(--text-base); letter-spacing: 0.04em; }
.hpline { display: flex; align-items: center; gap: 8px; font-size: var(--text-xs); color: var(--color-text-muted); }
.hpbar { flex: 1; height: 8px; background: rgba(255,255,255,0.08); border-radius: 4px; overflow: hidden; }
#sel-hpfill { height: 100%; width: 0%; background: linear-gradient(90deg, #5fbf5f, #8fd98f); transition: width 120ms; }
#sel-hptext { font-variant-numeric: tabular-nums; min-width: 62px; text-align: right; }
#sel-detail { font-size: var(--text-xs); color: var(--color-text-muted); line-height: 1.5; }

#sel-actions { flex: 1 1 260px; min-width: 260px; display: grid; grid-template-columns: repeat(3, 78px); gap: 8px; align-content: start; max-height: 190px; overflow-y: auto; }
.action-btn {
  pointer-events: auto; cursor: pointer;
  background: rgba(255,255,255,0.04); border: 1px solid var(--color-border);
  border-radius: var(--panel-radius); color: var(--color-text);
  padding: 8px 4px; font-family: var(--font-body); font-size: var(--text-xs); font-weight: 700;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  text-align: center; line-height: 1.15; overflow-wrap: break-word;
  transition: var(--transition-ui);
}
.action-btn:hover:not(:disabled) { border-color: var(--color-primary); background: rgba(232,176,75,0.1); }
.action-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.action-btn .ab-icon { font-size: 18px; line-height: 1; }
.action-btn .ab-cost { font-weight: 500; color: var(--color-text-muted); font-size: 10px; font-variant-numeric: tabular-nums; }
.action-btn .ab-cost.insufficient { color: var(--color-danger); }

#hint { margin-left: auto; padding: 8px 12px; font-size: var(--text-xs); color: var(--color-text-muted); max-width: 240px; }

#placement-tip {
  position: absolute; top: 52px; left: 50%; transform: translateX(-50%);
  padding: 8px 14px; font-size: var(--text-sm); color: var(--color-primary); font-weight: 700;
  display: flex; align-items: center; gap: var(--space-3); white-space: nowrap;
}
#btn-cancel-placement { width: auto; height: auto; padding: 6px 14px; font-size: var(--text-xs); font-weight: 700; }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(5, 3, 2, 0.6); pointer-events: auto; z-index: 20;
}
.menu-card { padding: var(--space-6) 40px; text-align: center; display: flex; flex-direction: column; gap: var(--space-3); min-width: 300px; }
.menu-card h1 { font-family: var(--font-display); font-size: var(--text-xl); letter-spacing: 0.1em; color: var(--color-primary); }
.menu-card h2 { font-family: var(--font-display); font-size: var(--text-lg); letter-spacing: 0.1em; }
.menu-card p { font-size: var(--text-sm); color: var(--color-text-muted); }
#end-title.defeat { color: var(--color-danger); }

.mbtn {
  font-family: var(--font-body); font-weight: 700; font-size: var(--text-sm);
  background: rgba(232,176,75,0.12); color: var(--color-text);
  border: 1px solid var(--color-border); border-radius: var(--panel-radius);
  padding: 10px 20px; cursor: pointer; transition: var(--transition-ui);
}
.mbtn:hover { border-color: var(--color-primary); background: rgba(232,176,75,0.24); }

/* ---------- title screen ---------- */
#title-screen {
  position: fixed; inset: 0; z-index: 30;
  display: flex; align-items: center; justify-content: center;
  background: #0c0a08;
}
#title-art { position: absolute; inset: 0; background-size: cover; background-position: center; opacity: 0.55; }
#title-art::after { content: ''; position: absolute; inset: 0; background: radial-gradient(ellipse at center, transparent 20%, #0c0a08 90%); }
#title-content {
  position: relative; text-align: center; display: flex; flex-direction: column;
  align-items: center; gap: var(--space-3); padding: var(--space-6); max-width: 560px;
}
#title-logo { filter: drop-shadow(0 2px 12px rgba(232,176,75,0.4)); }
.game-title {
  font-family: var(--font-display); font-weight: 900;
  font-size: clamp(3rem, 9vw, 5.5rem); letter-spacing: 0.12em; line-height: 1;
  color: var(--color-primary);
  text-shadow: 0 2px 24px rgba(0,0,0,0.8), 0 0 60px rgba(232,176,75,0.25);
}
.game-subtitle {
  font-family: var(--font-display); font-size: var(--text-lg); letter-spacing: 0.4em;
  color: var(--color-text-muted); text-transform: uppercase;
}
.title-desc { font-size: var(--text-base); color: var(--color-text); line-height: 1.6; margin: var(--space-3) 0; text-shadow: 0 1px 8px rgba(0,0,0,0.9); }

.bigbtn {
  font-family: var(--font-display); font-weight: 700; font-size: var(--text-base); letter-spacing: 0.15em;
  background: linear-gradient(180deg, #f0c168, #c9901f); color: #241a08;
  border: 1px solid #8a6a22; border-radius: 4px; padding: 14px 48px; cursor: pointer;
  box-shadow: 0 4px 24px rgba(232,176,75,0.3), inset 0 1px 0 rgba(255,255,255,0.35);
  transition: var(--transition-ui);
}
.bigbtn:hover { transform: translateY(-1px); box-shadow: 0 6px 32px rgba(232,176,75,0.45), inset 0 1px 0 rgba(255,255,255,0.35); }

.linkbtn { background: none; border: none; color: var(--color-text-muted); font-family: var(--font-body); font-size: var(--text-sm); cursor: pointer; text-decoration: underline; transition: var(--transition-ui); }
.linkbtn:hover { color: var(--color-primary); }

#how-panel { text-align: left; padding: var(--space-4) var(--space-6); margin-top: var(--space-2); }
#how-panel ul { list-style: none; display: flex; flex-direction: column; gap: 6px; font-size: var(--text-sm); color: var(--color-text-muted); }
#how-panel b { color: var(--color-primary); }

/* Phone-first controls. Canvas remains the world; reliable HTML buttons own touch commands. */
#mobile-command-row { display: none; }

@media (max-width: 900px) {
  :root { --space-2: 8px; --space-3: 10px; }
  #hint, #hint-touch, #minimap-wrap { display: none !important; }

  #topbar {
    top: env(safe-area-inset-top, 0px);
    width: calc(100% - 16px);
    min-height: 52px;
    justify-content: center;
    gap: 8px;
    padding: 4px 8px;
    font-size: 14px;
  }
  #topbar .spacer { display: none; }
  #topbar .res { gap: 4px; }
  #game-clock { margin-left: 2px; }
  .tbtn { width: 44px; height: 44px; font-size: 18px; }

  #bottombar {
    padding: 0 8px calc(8px + env(safe-area-inset-bottom, 0px));
    align-items: stretch;
  }
  #selection-panel {
    width: 100%; max-width: none; min-height: 0;
    flex-direction: column; gap: var(--space-2);
    max-height: min(45dvh, 336px); overflow-y: auto;
    overscroll-behavior: contain; -webkit-overflow-scrolling: touch; touch-action: pan-y;
    padding: 10px;
  }
  html.has-touch #mobile-command-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 52px;
    gap: 8px;
  }
  .mobile-command-btn, .mobile-clear-btn {
    min-height: 52px;
    border: 1px solid var(--color-border);
    color: var(--color-text);
    background: rgba(255,255,255,0.06);
    font-family: var(--font-body); font-weight: 800; font-size: 15px;
    touch-action: manipulation;
  }
  .mobile-command-btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 9px;
    border-radius: 8px;
  }
  .mobile-command-icon { color: var(--color-primary); font-size: 22px; line-height: 1; }
  .mobile-command-btn.is-active {
    color: #241a08; border-color: #f0c168;
    background: linear-gradient(180deg, #f0c168, #c9901f);
    box-shadow: 0 0 0 2px rgba(232,176,75,0.22), 0 6px 18px rgba(0,0,0,0.28);
  }
  .mobile-command-btn.is-active .mobile-command-icon { color: #241a08; }
  .mobile-command-btn:disabled, .mobile-clear-btn:disabled { opacity: 0.45; }
  .mobile-clear-btn { border-radius: 8px; font-size: 30px; line-height: 1; }
  #mobile-command-status {
    grid-column: 1 / -1; min-height: 18px;
    color: var(--color-text-muted); font-size: 12px; line-height: 1.3;
  }
  html.command-mode #mobile-command-status { color: var(--color-primary); font-weight: 700; }

  #sel-info { flex: 0 0 auto; max-width: none; gap: 4px; }
  #sel-name { font-size: 16px; }
  #sel-detail { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
  #sel-actions {
    flex: 0 0 auto; width: 100%; min-width: 0;
    grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px;
    max-height: none; overflow-y: visible;
  }
  .action-btn { min-height: 64px; padding: 8px; font-size: 13px; border-radius: 8px; touch-action: manipulation; }
  .action-btn .ab-icon { font-size: 20px; }
  .action-btn .ab-cost { font-size: 11px; }

  #placement-tip {
    top: calc(env(safe-area-inset-top, 0px) + 62px);
    width: calc(100% - 24px); padding: 8px 10px; white-space: normal;
    justify-content: space-between; text-align: center;
  }
  #btn-cancel-placement { min-height: 44px; padding: 7px 14px; }
  .menu-card { width: min(92vw, 380px); min-width: 0; padding: 24px; }
  .mbtn { min-height: 48px; font-size: 16px; }

  #title-content { width: min(100%, 540px); padding: 20px 18px max(28px, env(safe-area-inset-bottom, 0px)); }
  .title-desc { font-size: 15px; line-height: 1.5; }
  .bigbtn { min-height: 52px; padding: 14px 32px; }
  #how-panel { max-height: 42dvh; overflow-y: auto; padding: 14px; }
}

@media (max-width: 390px) {
  #topbar { gap: 5px; font-size: 13px; }
  #topbar .tbtn { width: 40px; height: 40px; }
  #game-clock { display: none; }
}

@media (orientation: landscape) and (max-height: 560px) {
  #selection-panel { max-height: 43dvh; }
  #mobile-command-row { grid-template-columns: minmax(0, 1fr) 48px; }
  .mobile-command-btn, .mobile-clear-btn { min-height: 46px; }
  .action-btn { min-height: 52px; }
}
