/* UI tokens mirror PALETTE.ui in public/render/art.js (see docs/art-direction.md). */
:root {
  --bg: #2B2340;
  --panel: #FFF8E7;
  --panel-edge: #2B2340;
  --text: #2B2340;
  --text-on-dark: #FFF8E7;
  --muted: #7A7190;
  --accent: #FFD84D;
  --accent-deep: #F2B705;
  --accent-text: #2B2340;
  --coffee: #5C3A21;
  --danger: #E5484D;
  --success: #3FB950;
  --warn: #F5A524;
  --enemy: #5B7CFA;
  --mint: #7FD1AE;
  --font: 'Fredoka', 'Nunito', 'Segoe UI', system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); overflow: hidden; overscroll-behavior: none; }
body { position: fixed; inset: 0; width: 100%; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; touch-action: none; }
button { font-family: inherit; }
kbd { font-family: inherit; background: var(--panel); border: 2px solid var(--panel-edge); border-radius: 6px; padding: 0 6px; font-weight: 600; }

#viewport { position: fixed; inset: 0; background: var(--bg); touch-action: none; }
#stage { position: absolute; left: 0; top: 0; width: 1280px; height: 720px; transform-origin: 0 0; overflow: hidden; }
#game { position: absolute; left: 0; top: 0; width: 1280px; height: 720px; display: block; background: var(--bg); touch-action: none; }

.layer { position: absolute; inset: 0; }
.hidden { display: none !important; }
#play { pointer-events: none; }
#play > * { pointer-events: auto; }
#play #banner, #play #paused, #play #tooltip, #play #toasts { pointer-events: none; }

/* ---------- cards and buttons */
.card {
  background: var(--panel);
  color: var(--text);
  border: 3px solid var(--panel-edge);
  border-radius: 12px;
}
.btn {
  background: var(--accent);
  color: var(--accent-text);
  border: 3px solid var(--panel-edge);
  border-radius: 10px;
  padding: 8px 18px;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  line-height: 1.1;
}
.btn:hover:not(:disabled) { background: var(--accent-deep); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary { background: var(--success); color: var(--text-on-dark); }
.btn.primary:hover:not(:disabled) { background: #33a043; }
.btn.ghost { background: var(--panel); }
.btn.ghost:hover:not(:disabled) { background: #f4e9cd; }
.btn.danger { background: var(--danger); color: var(--text-on-dark); }
.btn.danger:hover:not(:disabled) { background: #cf3a3f; }
.btn.big { font-size: 28px; padding: 12px 44px; }

/* ---------- HUD */
#hud {
  position: absolute; height: 54px;
  left: calc(8px + env(safe-area-inset-left, 0px)); right: calc(8px + env(safe-area-inset-right, 0px)); top: calc(3px + env(safe-area-inset-top, 0px));
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.hud-group { display: flex; align-items: center; gap: 6px; }
.pill {
  display: flex; align-items: center; gap: 6px;
  height: 54px; padding: 0 12px 0 8px;
  background: var(--panel); border: 3px solid var(--panel-edge); border-radius: 12px;
  font-weight: 700; font-size: 20px; white-space: nowrap;
}
.pill small { font-size: 13px; font-weight: 600; color: var(--muted); margin-left: -2px; }
.pill .ico { width: 24px; height: 24px; }
.pill.low { background: #ffd9da; color: var(--danger); border-color: var(--danger); animation: pulse 0.9s ease-in-out infinite; }
.pill.shake { animation: shake 0.35s ease-in-out 1; }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.05); } }
@keyframes shake { 0% { transform: translateX(0); } 25% { transform: translateX(-4px); } 50% { transform: translateX(4px); } 75% { transform: translateX(-3px); } 100% { transform: translateX(0); } }
.hud-btn {
  display: flex; align-items: center; gap: 4px; height: 54px; min-width: 54px; justify-content: center; padding: 0 12px;
  background: var(--panel); border: 3px solid var(--panel-edge); border-radius: 12px;
  font-weight: 700; font-size: 16px; cursor: pointer; white-space: nowrap;
}
.hud-btn:hover { background: var(--accent); }
.hud-btn.on { background: var(--accent); }
.hud-btn .ico { width: 24px; height: 24px; }
.hud-btn svg { width: 22px; height: 22px; display: block; }

.next-wave {
  display: flex; align-items: center; gap: 10px; height: 54px; padding: 0 14px;
  background: var(--accent); border: 3px solid var(--panel-edge); border-radius: 12px;
  font-weight: 700; font-size: 16px; cursor: pointer; white-space: nowrap; color: var(--accent-text);
  max-width: 560px;
}
.next-wave:hover:not(:disabled) { background: var(--accent-deep); }
.next-wave:disabled { background: var(--panel); cursor: default; opacity: 0.9; }
.next-wave .nw-label { color: var(--muted); font-size: 14px; font-weight: 600; }
.next-wave:not(:disabled) .nw-label { color: var(--accent-text); }
.nw-icons { display: flex; align-items: center; gap: 6px; }
.nw-icons .nw-e { display: flex; align-items: center; gap: 1px; font-size: 15px; }
.nw-icons canvas { width: 24px; height: 24px; }
.nw-icons .boss canvas { width: 30px; height: 30px; }
.nw-timer { font-size: 15px; font-weight: 600; min-width: 34px; text-align: right; }
.nw-bonus { font-size: 14px; font-weight: 600; color: var(--coffee); }
.next-wave.final .nw-bonus { display: none; }

/* ---------- banners, toasts, paused */
.banner {
  position: absolute; left: 50%; top: 96px; transform: translateX(-50%);
  background: var(--panel); border: 3px solid var(--panel-edge); border-radius: 12px;
  padding: 10px 26px; text-align: center; max-width: 720px;
  animation: bannerIn 0.35s ease-out;
}
.banner.out { animation: bannerOut 0.4s ease-in forwards; }
.banner-title { font-size: 22px; font-weight: 700; }
.banner-line { font-size: 17px; font-weight: 500; color: var(--text); margin-top: 2px; }
.banner.boss { border-color: var(--enemy); background: #E7ECFF; }
.banner.boss .banner-title { color: #3D55C4; font-size: 26px; }
@keyframes bannerIn { from { opacity: 0; transform: translate(-50%, -14px); } to { opacity: 1; transform: translate(-50%, 0); } }
@keyframes bannerOut { to { opacity: 0; transform: translate(-50%, -10px); } }

.paused {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  background: var(--panel); border: 3px solid var(--panel-edge); border-radius: 14px;
  padding: 16px 40px; font-size: 34px; font-weight: 700; text-align: center; cursor: pointer;
}
.paused small { display: block; font-size: 15px; font-weight: 500; color: var(--muted); }

#toasts { position: absolute; left: 50%; bottom: 94px; transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 6px; }
.toast {
  background: var(--panel); border: 3px solid var(--panel-edge); border-radius: 10px;
  padding: 6px 16px; font-size: 16px; font-weight: 600; white-space: nowrap;
  animation: toastIn 0.2s ease-out;
}
.toast.bad { border-color: var(--danger); color: var(--danger); }
.toast.good { border-color: var(--success); }
.toast.out { animation: toastOut 0.3s ease-in forwards; }
@keyframes toastIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes toastOut { to { opacity: 0; transform: translateY(6px); } }

/* ---------- tower bar */
#towerbar {
  position: absolute; left: 50%; bottom: calc(6px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%);
  display: flex; gap: 8px; padding: 6px;
  background: var(--bg); border-radius: 14px;
}
.tcard {
  display: flex; align-items: center; gap: 8px; width: 150px; height: 62px; padding: 0 8px 0 6px;
  background: var(--panel); border: 3px solid var(--panel-edge); border-radius: 12px;
  cursor: pointer; text-align: left; position: relative; color: var(--text);
}
.tcard:hover:not(:disabled) { background: #fff1c2; }
.tcard.selected { background: var(--accent); border-color: var(--panel-edge); box-shadow: inset 0 0 0 2px var(--panel); }
.tcard:disabled { opacity: 0.5; cursor: not-allowed; }
.tcard canvas { width: 44px; height: 44px; flex: none; }
.tcard .tname { font-size: 13px; font-weight: 700; line-height: 1.05; }
.tcard .tcost { font-size: 15px; font-weight: 700; color: var(--coffee); display: flex; align-items: center; gap: 3px; margin-top: 2px; }
.tcard .tcost canvas { width: 16px; height: 16px; }
.tcard .tkey {
  position: absolute; right: -4px; top: -8px; width: 20px; height: 20px; border-radius: 6px;
  background: var(--accent); border: 2px solid var(--panel-edge); font-size: 12px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
.tcard.cant .tcost { color: var(--danger); }

/* ---------- tooltip */
.tooltip { position: absolute; padding: 8px 12px; width: 280px; font-size: 14px; z-index: 5; }
.tooltip .tt-name { font-size: 17px; font-weight: 700; }
.tooltip .tt-role { font-weight: 500; margin: 2px 0; }
.tooltip .tt-flavor { color: var(--muted); font-style: italic; margin: 2px 0 6px; }
.tooltip .tt-stats { display: flex; flex-wrap: wrap; gap: 4px 10px; font-weight: 600; }
.tooltip .tt-stats span b { color: var(--coffee); }
.tooltip .tt-hp { font-weight: 600; margin-top: 4px; }
.tooltip .tt-hp i { display: inline-block; width: 120px; height: 8px; border: 2px solid var(--panel-edge); border-radius: 5px; vertical-align: middle; margin-left: 6px; position: relative; overflow: hidden; }
.tooltip .tt-hp i b { position: absolute; left: 0; top: 0; bottom: 0; background: var(--success); }
.tooltip .tt-special { color: var(--enemy); font-weight: 600; margin-top: 2px; }

/* ---------- selection panel */
.panel { position: absolute; width: 268px; padding: 10px 12px; z-index: 4; }
.panel .p-head { display: flex; align-items: center; gap: 8px; }
.panel .p-head canvas { width: 40px; height: 40px; }
.panel .p-name { font-size: 17px; font-weight: 700; line-height: 1.05; }
.panel .p-level { font-size: 13px; color: var(--muted); font-weight: 600; }
.panel .p-stats { display: flex; flex-wrap: wrap; gap: 3px 10px; font-size: 14px; font-weight: 600; margin: 6px 0; }
.panel .p-stats span { color: var(--muted); }
.panel .p-stats span b { color: var(--text); }
.panel .p-close { position: absolute; right: 6px; top: 4px; border: 0; background: none; font-size: 20px; cursor: pointer; color: var(--muted); }
.panel .p-close:hover { color: var(--text); }
.panel .upg {
  display: block; width: 100%; text-align: left; padding: 6px 10px; margin: 4px 0;
  background: var(--accent); border: 3px solid var(--panel-edge); border-radius: 10px; cursor: pointer; color: var(--text);
}
.panel .upg:hover:not(:disabled) { background: var(--accent-deep); }
.panel .upg:disabled { background: var(--panel); opacity: 0.6; cursor: not-allowed; }
.panel .upg .u-title { display: flex; justify-content: space-between; font-weight: 700; font-size: 15px; }
.panel .upg .u-cost { color: var(--coffee); }
.panel .upg .u-desc { font-size: 12.5px; font-weight: 500; color: var(--text); margin-top: 1px; line-height: 1.15; }
.panel .upg .u-delta { font-size: 12.5px; font-weight: 600; color: var(--success); margin-top: 2px; }
.panel .upg.maxed { background: var(--mint); }
.panel .p-row { display: flex; gap: 6px; margin-top: 4px; }
.panel .p-row .btn { font-size: 14px; padding: 5px 10px; flex: 1; }
.panel .p-row .btn.sell { background: var(--panel); }
.panel .p-row .btn.sell:hover { background: #ffd9da; }
.panel .p-target { font-size: 13px; color: var(--muted); font-weight: 600; margin-top: 6px; display: flex; align-items: center; gap: 6px; }
.panel .p-target .tg { display: flex; gap: 2px; }
.panel .p-target button { border: 2px solid var(--panel-edge); background: var(--panel); border-radius: 7px; padding: 2px 7px; font-size: 12px; font-weight: 700; cursor: pointer; color: var(--text); }
.panel .p-target button.on { background: var(--accent); }
.panel .p-keys { font-size: 11px; color: var(--muted); margin-top: 6px; }

/* ---------- screens */
.screen {
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  background-image:
    radial-gradient(circle at 12% 18%, rgba(255,216,77,0.12) 0, transparent 30%),
    radial-gradient(circle at 88% 82%, rgba(91,124,250,0.16) 0, transparent 34%),
    repeating-linear-gradient(0deg, transparent 0 39px, rgba(255,248,231,0.05) 39px 40px),
    repeating-linear-gradient(90deg, transparent 0 39px, rgba(255,248,231,0.05) 39px 40px);
  color: var(--text-on-dark);
}
.title-card { text-align: center; position: relative; }
.title-sticky {
  position: absolute; left: 50%; top: -58px; transform: translate(-180px, 0) rotate(-8deg);
  background: var(--accent); color: var(--accent-text); border: 3px solid var(--panel-edge); border-radius: 4px;
  padding: 10px 18px; font-size: 26px; font-weight: 700;
}
.title-card h1 { font-size: 72px; line-height: 0.98; margin: 0 0 14px; font-weight: 700; color: var(--panel); text-shadow: 6px 6px 0 #5B7CFA; }
.title-card .premise { font-size: 22px; margin: 0 0 26px; color: #C9C2DA; font-weight: 500; }
.title-foot { margin-top: 26px; font-size: 13px; color: var(--muted); min-height: 16px; }

.levels-wrap { width: 1100px; text-align: center; }
.levels-wrap h2 { font-size: 36px; margin: 0 0 18px; color: var(--panel); }
.map-cards { display: flex; gap: 22px; justify-content: center; margin-bottom: 18px; }
.mcard {
  width: 330px; padding: 0 0 12px; text-align: left; cursor: pointer; overflow: hidden;
  background: var(--panel); border: 3px solid var(--panel-edge); border-radius: 14px; color: var(--text);
  position: relative;
}
.mcard:hover:not(:disabled) { transform: translateY(-3px); }
.mcard:disabled { cursor: not-allowed; }
.mcard:disabled .m-thumb { filter: grayscale(0.9) brightness(0.6); }
.mcard .m-thumb { display: block; width: 100%; height: 180px; border-bottom: 3px solid var(--panel-edge); }
.mcard .m-body { padding: 8px 14px 0; }
.mcard .m-name { font-size: 21px; font-weight: 700; display: flex; justify-content: space-between; align-items: center; }
.mcard .m-name small { font-size: 13px; color: var(--muted); font-weight: 600; }
.mcard .m-blurb { font-size: 14px; color: var(--text); margin: 4px 0 6px; min-height: 36px; line-height: 1.2; }
.mcard .m-stars { display: flex; gap: 2px; align-items: center; height: 26px; }
.mcard .m-stars canvas { width: 24px; height: 24px; }
.mcard .m-stars span { font-size: 13px; color: var(--muted); font-weight: 600; margin-left: 6px; }
.mcard .m-lock {
  position: absolute; left: 50%; top: 72px; transform: translate(-50%, -50%);
  background: var(--panel); border: 3px solid var(--panel-edge); border-radius: 12px; padding: 6px 14px;
  display: flex; align-items: center; gap: 6px; font-weight: 700; font-size: 15px;
}
.mcard .m-lock canvas { width: 22px; height: 22px; }
.howto { display: inline-block; text-align: left; padding: 10px 18px; max-width: 900px; margin-bottom: 14px; }
.howto h3 { margin: 0 0 4px; font-size: 18px; }
.howto p { margin: 0 0 6px; font-size: 14.5px; line-height: 1.25; }
.howto .keys { color: var(--muted); font-size: 13px; }

/* ---------- modals */
.modal { display: flex; align-items: center; justify-content: center; background: rgba(43,35,64,0.62); z-index: 10; }
.result-card { width: 560px; padding: 22px 28px 24px; text-align: center; animation: bannerIn 0.35s ease-out; transform: none; }
.result-kicker { font-size: 40px; font-weight: 700; line-height: 1; }
.result-card.won .result-kicker { color: var(--success); }
.result-card.lost .result-kicker { color: var(--danger); }
.stars { display: flex; justify-content: center; gap: 10px; margin: 12px 0 8px; height: 60px; }
.stars canvas { width: 56px; height: 56px; }
.stars canvas.in { animation: starIn 0.45s cubic-bezier(.2,1.6,.4,1); }
@keyframes starIn { from { opacity: 0; transform: scale(0.2) rotate(-30deg); } to { opacity: 1; transform: scale(1) rotate(0); } }
.result-line { font-size: 19px; margin: 0 0 6px; }
.result-stats { font-size: 15px; color: var(--muted); font-weight: 600; margin: 0 0 16px; }
.row { display: flex; gap: 10px; justify-content: center; }
.confirm-card { width: 420px; padding: 20px 24px; text-align: center; }
.confirm-card p { font-size: 18px; margin: 0 0 14px; }

.range-note { position: absolute; font-size: 12px; color: var(--muted); }

/* ---------- touch placement controls */
.build-confirm { position: absolute; display: flex; gap: 6px; align-items: center; z-index: 6; }
.build-confirm .btn { font-size: 17px; padding: 8px 16px; white-space: nowrap; }
.build-confirm .btn.x { padding: 6px 12px; font-size: 20px; line-height: 1; }
.place-cancel {
  position: absolute; left: 50%; bottom: 88px; transform: translateX(-50%);
  background: var(--panel); color: var(--text); border: 3px solid var(--panel-edge); border-radius: 10px;
  padding: 7px 16px; font-size: 15px; font-weight: 600; cursor: pointer; white-space: nowrap; z-index: 3;
}
.place-cancel:hover { background: #ffd9da; }
.place-cancel span { color: var(--danger); font-weight: 700; }

/* ---------- portrait */
.rotate { position: fixed; inset: 0; transform: none; z-index: 50; }
.rotate-card { width: min(88vw, 420px); padding: 26px 24px; text-align: center; }
.rotate-card p { font-size: 26px; font-weight: 700; margin: 12px 0 4px; }
.rotate-card small { font-size: 15px; color: var(--muted); font-weight: 500; }
.rotate-device { display: inline-block; width: 92px; height: 64px; border: 4px solid var(--panel-edge); border-radius: 10px; position: relative; animation: rotateHint 2.2s ease-in-out infinite; }
.rotate-device i { position: absolute; right: 6px; top: 50%; width: 6px; height: 6px; margin-top: -3px; border-radius: 50%; background: var(--panel-edge); }
@keyframes rotateHint { 0%, 20% { transform: rotate(-90deg); } 50%, 100% { transform: rotate(0); } }

/* new-enemy intro card */
.intro-card { width: 640px; padding: 20px 26px 22px; text-align: left; animation: bannerIn 0.35s ease-out; }
.intro-kicker { font-size: 15px; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; color: var(--enemy); text-align: center; margin-bottom: 8px; }
.intro-body { display: flex; gap: 18px; align-items: center; }
.intro-art { flex: none; background: #f4e9cd; border: 3px solid var(--panel-edge); border-radius: 14px; }
.intro-name { font-size: 30px; font-weight: 700; line-height: 1.1; }
.intro-flavor { margin: 6px 0 8px; font-style: italic; color: var(--muted); font-size: 16px; }
.intro-specials { margin: 0; padding-left: 20px; font-size: 16px; }
.intro-specials li { margin: 2px 0; }
.intro-counter { margin: 14px 0 16px; padding: 10px 14px; background: #fff1c2; border: 2px solid var(--accent-deep); border-radius: 10px; font-size: 16px; }
#btn-intro { display: block; margin: 0 auto; min-width: 180px; font-size: 20px; min-height: 52px; }
