:root {
  --bg: #1f2a1b;
  --panel: #101638;
  --card: #1f2a5c;
  --border: #f4f0e0;
  --text: #f3f0e2;
  --muted: #b8bfa6;
  --accent: #f2c94c;
  --accent-dark: #c99a1e;
  --pos: #8be07a;
  --neg: #ff8a7a;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overflow: hidden; }
.hidden { display: none !important; }

.screen { position: fixed; inset: 0; }
canvas { display: block; width: 100%; height: 100%; }

#hud { position: absolute; top: 10px; left: 10px; right: 10px; display: flex; align-items: center; gap: 8px;
  flex-wrap: wrap; pointer-events: none; }
#hud > * { pointer-events: auto; }
.hud-title { font-weight: 800; font-size: 18px; background: rgba(20,28,18,0.78); padding: 8px 12px; border-radius: 10px; }
.hud-stat { background: rgba(20,28,18,0.78); border-radius: 10px; padding: 4px 12px; text-align: center; min-width: 70px; }
.hud-stat small { display: block; color: var(--muted); font-size: 11px; }
.hud-stat b { font-size: 16px; }
#stat-money { color: var(--accent); }
.hud-btns { margin-left: auto; background: rgba(20,28,18,0.78); border-radius: 10px; padding: 2px; display: flex; }
.icon-btn { background: none; border: none; font-size: 18px; cursor: pointer; padding: 6px; border-radius: 6px; }
.icon-btn:hover { background: rgba(255,255,255,0.1); }

.btn { font: inherit; background: var(--accent); color: #2a2000; border: none; border-radius: 8px;
  padding: 7px 12px; font-weight: 700; cursor: pointer; box-shadow: 0 3px 0 var(--accent-dark);
  display: inline-flex; align-items: center; justify-content: space-between; gap: 8px; }
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--accent-dark); }
.btn.cant, .btn:disabled { background: #5d6455; color: #b9bdb0; box-shadow: 0 3px 0 #444a3e; cursor: not-allowed; }
.btn.big { font-size: 18px; padding: 12px 16px; justify-content: center; width: 100%; }
.btn.wide { width: 100%; margin-top: 6px; }
.btn.small { padding: 3px 8px; font-size: 12px; background: #d9d2b8; box-shadow: 0 2px 0 #a59e85; }
.btn .cost { background: rgba(0,0,0,0.15); border-radius: 5px; padding: 1px 6px; font-size: 13px; }
.safari-btn { background: #ef8a3a; box-shadow: 0 4px 0 #b55f1d; color: #fff; }
.link { background: none; border: none; color: var(--muted); cursor: pointer; text-decoration: underline; font: inherit; font-size: 12px; margin-top: 8px; padding: 0; }

.card { background: var(--card); border-radius: 10px; padding: 10px 12px; }
.card h3 { margin: 0 0 6px; font-size: 15px; }
.card p { margin: 4px 0; font-size: 14px; }
.selected-card { outline: 2px solid var(--accent); }
.muted { color: var(--muted); font-weight: normal; }
.small { font-size: 12px; }
.row { display: flex; justify-content: space-between; font-size: 14px; padding: 2px 0; }
.row.total { border-top: 1px solid rgba(255,255,255,0.2); margin-top: 6px; padding-top: 6px; font-size: 16px; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }

.animal-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.animal-list li { display: flex; align-items: center; gap: 8px; background: rgba(0,0,0,0.15); border-radius: 8px; padding: 4px 8px; }
.animal-list .em { font-size: 24px; }
.animal-list .nm { flex: 1; font-size: 14px; }
.animal-list .nm small { display: block; color: var(--muted); font-size: 11px; }
.animal-list .sellv { color: var(--accent); font-size: 12px; min-width: 36px; }

.upgrade { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.up-head { display: flex; align-items: center; gap: 10px; }
.up-head .em { font-size: 26px; }
.up-head small { display: block; color: var(--muted); font-size: 11px; margin-top: 2px; }
.pips { display: flex; gap: 3px; margin-top: 3px; }
.pip { width: 14px; height: 6px; border-radius: 3px; background: rgba(255,255,255,0.15); }
.pip.on { background: var(--accent); }
.maxed { color: var(--accent); font-weight: 700; font-size: 13px; }

.guide-row { display: flex; align-items: center; gap: 4px; margin: 4px 0; flex-wrap: wrap; }
.guide-biome { width: 26px; font-size: 16px; }
.guide-sp { font-size: 22px; cursor: help; }
.guide-sp.unknown { filter: brightness(0) opacity(0.35); }

#modal { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; z-index: 10; padding: 16px; }
#modal-box { background: var(--panel); border-radius: 14px; padding: 22px 26px; max-width: 520px; width: 100%;
  max-height: 90vh; overflow-y: auto; box-shadow: 0 10px 40px rgba(0,0,0,0.5); border: 3px solid #1a2316; }
#modal-box h2 { margin-top: 0; }
#modal-box h3 { margin: 14px 0 6px; font-size: 16px; }
#modal-box ol { padding-left: 20px; line-height: 1.5; }
#modal-box .btn.big { margin-top: 16px; }
.chip { display: inline-block; background: var(--card); border-radius: 12px; padding: 2px 10px; margin: 2px; font-size: 14px; }

.wallet { color: var(--accent); font-weight: 700; margin: -6px 0 10px; }
.close-x { display: block; margin: 14px auto 0; }
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 8px; }
.stats-grid div { text-align: center; }
.stats-grid small { display: block; color: var(--muted); font-size: 11px; }
#modal-box .card { margin-bottom: 8px; }
#modal-box .btn.wide { margin-top: 8px; }
.guide-region { margin: 8px 0; }
.guide-name { font-weight: 700; font-size: 14px; }

/* ---- Touch controls ---- */
#touch-ui { display: none; }
body.touch #touch-ui { display: block; }
/* Only the game canvases and touch buttons swallow gestures; menus must still scroll. */
body.touch { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
body.touch canvas { touch-action: none; }
#modal-box { touch-action: pan-y; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
#stick-base { display: none; position: fixed; width: 120px; height: 120px; margin: -60px 0 0 -60px; border-radius: 50%;
  background: rgba(255,255,255,0.12); border: 2px solid rgba(255,255,255,0.35); z-index: 5; pointer-events: none; }
#stick-knob { position: absolute; left: 35px; top: 35px; width: 50px; height: 50px; border-radius: 50%;
  background: rgba(255,255,255,0.55); }
.tbtn { position: fixed; z-index: 5; width: 72px; height: 72px; border-radius: 50%; border: 3px solid rgba(255,255,255,0.6);
  background: rgba(20,28,18,0.6); font-size: 32px; color: #fff; padding: 0; touch-action: none; -webkit-tap-highlight-color: transparent; }
.tbtn.down { background: rgba(242,201,76,0.7); }
.tbtn-big { width: 92px; height: 92px; font-size: 42px; }
.tbtn-small { width: 48px; height: 48px; font-size: 20px; }
#tbtn-use { right: calc(24px + env(safe-area-inset-right)); bottom: calc(60px + env(safe-area-inset-bottom)); width: 84px; height: 84px; }
#tbtn-fire { right: calc(24px + env(safe-area-inset-right)); bottom: calc(40px + env(safe-area-inset-bottom)); }
#tbtn-home { right: calc(130px + env(safe-area-inset-right)); bottom: calc(50px + env(safe-area-inset-bottom)); }
#tbtn-pause { left: 50%; margin-left: -24px; bottom: calc(52px + env(safe-area-inset-bottom)); }
#touch-pause { position: fixed; z-index: 6; left: 50%; top: 62%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 12px; width: 260px; }
#touch-pause .tow { background: #d9d2b8; box-shadow: 0 3px 0 #a59e85; }

/* Phones: compact top bar */
@media (max-width: 700px), (max-height: 500px) {
  .hud-title { display: none; }
  .hud-stat { min-width: 0; padding: 3px 8px; }
  .hud-stat small { font-size: 9px; }
  .hud-stat b { font-size: 13px; }
  #hud { gap: 4px; top: 6px; left: 6px; right: 6px; }
  #modal-box { padding: 16px; }
}

/* Very narrow screens (e.g. a folded phone's cover screen): stack upgrade buttons under their text. */
@media (max-width: 480px) {
  .upgrade { flex-wrap: wrap; }
  .upgrade .btn, .upgrade .maxed { width: 100%; justify-content: space-between; }
  .animal-list li { flex-wrap: wrap; }
}

#tbtn-auto { right: calc(130px + env(safe-area-inset-right)); bottom: calc(150px + env(safe-area-inset-bottom));
  width: 64px; height: 40px; border-radius: 12px; font-size: 12px; font-weight: 800; }
#tbtn-auto.on { background: rgba(80,170,70,0.75); }

.locked { color: var(--muted); font-weight: 700; font-size: 13px; white-space: nowrap; }
.progress { height: 10px; background: rgba(255,255,255,0.12); border-radius: 5px; overflow: hidden; margin: 6px 0; }
.progress div { height: 100%; background: var(--accent); }
.guide-sp.shown { outline: 2px solid var(--accent); border-radius: 50%; }

.up-info { flex: 1; min-width: 0; }
.up-btns { display: flex; flex-direction: column; gap: 6px; align-items: stretch; }
.lvlbar { position: relative; height: 8px; background: rgba(255,255,255,0.12); border-radius: 4px; margin: 4px 0 3px; }
.lvlbar .fill { height: 100%; background: var(--accent); border-radius: 4px; }
.lvlbar .cap { position: absolute; top: -3px; width: 2px; height: 14px; background: #fff; opacity: 0.7; }

.cost .short { color: #b3261e; }
.btn.cant .cost .short { color: #ffb4a8; }
#stat-res span { margin: 0 2px; white-space: nowrap; }

.btn .cost { white-space: nowrap; }
.up-btns { flex-shrink: 0; }
.up-btns .btn { justify-content: space-between; }

/* Purchase notifications */
#toasts { position: fixed; top: 64px; left: 50%; transform: translateX(-50%); z-index: 20; display: flex; flex-direction: column;
  gap: 8px; align-items: center; pointer-events: none; width: min(92vw, 440px); }
.toast { display: flex; align-items: center; gap: 12px; width: 100%; background: #f2c94c; color: #2a2000; border-radius: 12px;
  padding: 10px 14px; box-shadow: 0 6px 20px rgba(0,0,0,0.4); animation: toast-in 0.3s ease-out; transition: opacity 0.5s, transform 0.5s; }
.toast.out { opacity: 0; transform: translateY(-10px); }
.toast .t-icon { font-size: 28px; }
.toast b { display: block; font-size: 15px; }
.toast small { display: block; font-size: 12px; margin-top: 2px; }
.toast .was { opacity: 0.65; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-16px) scale(0.95); } to { opacity: 1; transform: none; } }

/* Animal artwork in menus */
.animal-img { width: 1.4em; height: 1.4em; vertical-align: middle; object-fit: contain; }
.animal-list .em .animal-img { width: 36px; height: 36px; }
.guide-sp .animal-img { width: 30px; height: 30px; }
.guide-sp.unknown .animal-img { filter: brightness(0) opacity(0.35); }
.chip .animal-img { width: 20px; height: 20px; }

a.icon-btn { text-decoration: none; display: inline-flex; align-items: center; }

/* ---- Pixel-art look: dialog-box panels with a light border, square corners ---- */
canvas { image-rendering: pixelated; }
.hud-title, .hud-stat, .hud-btns { background: rgba(16,22,56,0.88); border: 2px solid var(--border); border-radius: 4px; }
#modal-box { border: 3px solid var(--border); border-radius: 6px; box-shadow: 0 0 0 3px #101638, 0 10px 40px rgba(0,0,0,0.5); }
#modal-box h2, #modal-box h3 { color: #f8e8b0; letter-spacing: 0.5px; }
.card { border-radius: 4px; border: 1px solid rgba(244,240,224,0.25); }
.btn { border-radius: 4px; border: 2px solid #3a2a00; }
.btn.cant, .btn:disabled { border-color: #2a2e26; }
.chip { border-radius: 4px; border: 1px solid rgba(244,240,224,0.25); }
.animal-list li { border-radius: 4px; }
.animal-img { image-rendering: pixelated; }
.toast { border-radius: 4px; border: 3px solid #3a2a00; }
.pip, .lvlbar, .lvlbar .fill, .progress { border-radius: 0; }
.guide-sp.legendary { background: radial-gradient(circle, rgba(248,216,72,0.45), transparent 70%); border-radius: 50%; color: #f8d848; }

/* Save files */
.save-slot { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.save-info { flex: 1; min-width: 0; }
.save-info small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
@media (max-width: 480px) { .save-slot { flex-wrap: wrap; } .save-slot .up-btns { width: 100%; } }

/* Phones (iOS Safari especially): menus fit the visible screen, and taps on buttons register right away
   instead of waiting to see if they're a double-tap zoom. */
#modal-box { max-height: 90dvh; }
#modal .btn, #modal .link, .icon-btn { touch-action: manipulation; }
/* Short landscape phones: one row of jeep stats so the upgrades show up sooner in the Garage. */
@media (max-height: 500px) { .stats-grid { grid-template-columns: repeat(6, 1fr); } }

/* ---- Garage skill tree ---- */
#modal-box.wide { max-width: 980px; }
.tiers { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-top: 6px; }
.tier { background: rgba(0,0,0,0.2); border: 2px solid rgba(244,240,224,0.2); border-radius: 4px; padding: 6px; text-align: center; display: flex; flex-direction: column; gap: 4px; align-items: center; }
.tier small { color: var(--muted); font-size: 11px; }
.tier.owned { border-color: var(--pos); }
.tier.next { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(242,201,76,0.25); }
.tier-num { font-weight: 800; font-size: 13px; }
.tier .btn { padding: 4px 8px; font-size: 12px; }
.tree { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; align-items: start; }
.branch-head { font-weight: 800; font-size: 15px; text-align: center; color: #f8e8b0; margin-bottom: 4px; }
.node { position: relative; background: var(--card); border: 2px solid rgba(244,240,224,0.25); border-radius: 4px; padding: 8px; margin-top: 16px;
  display: flex; flex-direction: column; gap: 6px; }
.node:first-of-type, .branch-head + .node { margin-top: 0; }
.node + .node::before { content: ''; position: absolute; left: 50%; top: -18px; width: 4px; height: 16px; margin-left: -2px; background: #f4f0e0; opacity: 0.6; }
.node.owned { border-color: var(--pos); }
.node.locked-node { opacity: 0.55; }
.node .up-head .em { font-size: 22px; }
.node .up-btns { flex-direction: row; justify-content: flex-end; }
.node > .maxed, .node > .locked, .node > .btn { align-self: flex-end; }
.node .locked { text-align: right; }
@media (max-width: 760px) {
  .tree { grid-template-columns: 1fr; }
  .tiers { grid-template-columns: repeat(3, 1fr); }
}

/* Purchase pop: the node or permit you just bought bounces and glows */
@keyframes node-pop { 0% { transform: scale(1); } 30% { transform: scale(1.06); box-shadow: 0 0 0 4px rgba(242,201,76,0.8); } 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(242,201,76,0); } }
.node.pop, .tier.pop { animation: node-pop 0.5s ease-out; }
@keyframes fill-grow { from { filter: brightness(1.8); } to { filter: none; } }
.node.pop .lvlbar .fill { animation: fill-grow 0.6s ease-out; }

/* ---- Skill tree graph ---- */
.sk-wrap { overflow-x: auto; margin: 4px -4px 0; padding: 0 4px; }
.sk-tree { position: relative; min-width: 290px; margin-top: 46px; }
.sk-lines { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.sk-lines line { stroke: rgba(244,240,224,0.18); stroke-width: 4; stroke-linecap: round; }
.sk-lines line.lit { stroke: #f2c94c; stroke-width: 5; }
.sk-branch { position: absolute; transform: translateX(-50%); white-space: nowrap; font-weight: 800; font-size: 12px; color: #f8e8b0;
  background: rgba(16,22,56,0.9); border: 1px solid rgba(244,240,224,0.3); border-radius: 4px; padding: 1px 6px; }
.sk-node { position: absolute; transform: translate(-50%, -50%); width: 58px; height: 58px; border-radius: 50%; padding: 0; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; font: inherit; color: #fff;
  border: 5px solid transparent; box-shadow: 0 0 0 2px rgba(244,240,224,0.25);
  /* the face, plus a progress ring in the border showing how many levels are bought */
  background: radial-gradient(circle at 35% 30%, #34407a, #1a2150 70%) padding-box,
              conic-gradient(#8be07a var(--p, 0%), rgba(255,255,255,0.12) 0) border-box; }
.sk-node .ic { font-size: 22px; line-height: 1; }
.sk-node small { font-size: 10px; font-weight: 800; margin-top: 2px; color: #f4f0e0; }
.sk-node.root { width: 50px; height: 50px; cursor: default; background: radial-gradient(circle at 35% 30%, #f8e070, #c99a1e); box-shadow: 0 0 0 3px #f4f0e0; }
.sk-node.root span { font-size: 24px; }
.sk-node.locked { filter: grayscale(1) brightness(0.6); cursor: pointer; }
.sk-node.ready { box-shadow: 0 0 0 2px #f2c94c, 0 0 14px rgba(242,201,76,0.8); animation: sk-glow 1.4s ease-in-out infinite; }
.sk-node.owned { box-shadow: 0 0 0 2px #8be07a; }
.sk-node.max { box-shadow: 0 0 0 2px #f2c94c; background: radial-gradient(circle at 35% 30%, #6a5a20, #3a3010 70%) padding-box, linear-gradient(#f2c94c, #f2c94c) border-box; }
.sk-node.sel { outline: 3px solid #fff; outline-offset: 5px; }
.sk-node:hover { transform: translate(-50%, -50%) scale(1.08); }
@keyframes sk-glow { 50% { box-shadow: 0 0 0 2px #f2c94c, 0 0 22px rgba(242,201,76,1); } }
.sk-node.pop { animation: sk-pop 0.5s ease-out; }
@keyframes sk-pop { 30% { transform: translate(-50%, -50%) scale(1.25); } }
.sk-detail { margin-top: 14px; background: rgba(0,0,0,0.2); border: 2px solid rgba(244,240,224,0.25); border-radius: 4px; padding: 10px;
  display: flex; flex-direction: column; gap: 8px; }
.sk-detail > .maxed, .sk-detail > .locked, .sk-detail > .btn, .sk-detail .up-btns { align-self: flex-end; }
.sk-detail .up-btns { flex-direction: row; }
.sk-legend { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; margin-top: 8px; font-size: 11px; color: var(--muted); }
.sk-legend .dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 4px; vertical-align: -1px; }
.dot.ready { background: #f2c94c; box-shadow: 0 0 6px #f2c94c; } .dot.owned { background: #8be07a; } .dot.max { background: #c99a1e; } .dot.locked { background: #555; }
@media (max-width: 520px) {
  .sk-node { width: 46px; height: 46px; border-width: 4px; }
  .sk-node .ic { font-size: 17px; }
  .sk-node small { font-size: 9px; }
  .sk-node.root { width: 40px; height: 40px; }
  .sk-branch { font-size: 10px; padding: 0 4px; }
}
