:root {
  --forest: #1d2a1b;
  --moss: #3d5a2a;
  --paper: #f3ead3;
  --paper-2: #e8dcbc;
  --ink: #2b2318;
  --ink-2: #6b5d48;
  --accent: #c8642a;
  --accent-2: #a84e1c;
  --good: #3f7d2c;
  --bad: #b3362a;
  --warn: #b8862a;
  --serif: Georgia, 'Times New Roman', serif;
  --sans: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--forest); color: var(--ink); font-family: var(--sans); }
body { overscroll-behavior: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: manipulation; }
#game { position: fixed; inset: 0; display: block; touch-action: none; }
.hidden { display: none !important; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; }
.hud-top {
  position: absolute; top: calc(10px + var(--safe-t)); left: calc(10px + var(--safe-l)); right: calc(10px + var(--safe-r));
  display: flex; justify-content: space-between; align-items: flex-start; gap: 8px;
}
.hud-left, .hud-right { display: flex; flex-direction: column; gap: 6px; }
.hud-right { align-items: flex-end; }
.pill {
  background: rgba(20, 26, 16, 0.72); color: var(--paper); border-radius: 999px; padding: 6px 12px;
  font-size: 14px; font-weight: 600; backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
  font-variant-numeric: tabular-nums; white-space: nowrap;
}
#hud-clock.late { color: #ffc27a; }
.coins { color: #ffe08a; }
.basket-pill { display: flex; align-items: center; gap: 8px; }
.bar { width: 74px; height: 8px; border-radius: 4px; background: rgba(255, 255, 255, 0.15); overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #d9a441, #e4c35a); transition: width .25s; }
.bar i.full { background: #d0533e; }
.vitals { display: flex; align-items: center; gap: 8px; color: #ff6f5e; letter-spacing: 1px; }
.bar.stamina { width: 60px; height: 6px; }
.bar.stamina i { background: linear-gradient(90deg, #6fb7d9, #9ad4ec); transition: width .1s; }
.bar.stamina i.low { background: #d98a3a; }
.joystick, .touch-pad { display: none; }
body.touch .joystick, body.touch .touch-pad { display: flex; }
.joystick {
  position: absolute; left: calc(22px + var(--safe-l)); bottom: calc(22px + var(--safe-b));
  width: 132px; height: 132px; border-radius: 50%; pointer-events: auto; touch-action: none;
  align-items: center; justify-content: center;
  background: radial-gradient(circle, rgba(20,26,16,0.25) 0 55%, rgba(20,26,16,0.45) 56% 100%);
  box-shadow: inset 0 0 0 2px rgba(243,234,211,0.35);
}
.joystick.on { box-shadow: inset 0 0 0 2px rgba(243,234,211,0.7); }
.joy-knob {
  width: 58px; height: 58px; border-radius: 50%; pointer-events: none;
  background: rgba(243,234,211,0.85); box-shadow: 0 4px 12px rgba(0,0,0,0.35);
}
.touch-pad {
  position: absolute; right: calc(18px + var(--safe-r)); bottom: calc(96px + var(--safe-b));
  flex-direction: column; align-items: flex-end; gap: 10px; pointer-events: none;
}
.round-btn {
  pointer-events: auto; width: 66px; height: 66px; border-radius: 50%; border: 0; cursor: pointer; touch-action: none;
  background: rgba(20, 26, 16, 0.72); color: var(--paper); line-height: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.round-btn span { font-size: 24px; }
.round-btn small { font-size: 9.5px; font-weight: 700; margin-top: 3px; }
.round-btn.hidden { display: none; }
.sprint-btn.on { background: #3f7da0; transform: scale(0.95); }
.hide-btn { background: rgba(40, 80, 40, 0.8); }
body.touch .action-btn {
  max-width: calc(100vw - 190px - var(--safe-l) - var(--safe-r)); padding: 17px 20px; font-size: 16px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
/* phones: walking joystick on the right, run / hide / action on the left */
body.touch .joystick { left: auto; right: calc(22px + var(--safe-r)); }
body.touch .action-btn { right: auto; left: calc(18px + var(--safe-l)); }
body.touch .touch-pad { right: auto; left: calc(18px + var(--safe-l)); align-items: flex-start; }
.hud-buttons { display: flex; gap: 6px; pointer-events: auto; }
.icon-btn {
  width: 42px; height: 42px; border-radius: 50%; border: 0; font-size: 19px; cursor: pointer;
  background: rgba(20, 26, 16, 0.72); color: var(--paper); backdrop-filter: blur(4px);
}
.icon-btn:active { transform: scale(0.94); }

.action-btn {
  position: absolute; right: calc(18px + var(--safe-r)); bottom: calc(22px + var(--safe-b));
  pointer-events: auto; border: 0; border-radius: 999px; padding: 18px 26px; font-size: 18px; font-weight: 700;
  background: var(--accent); color: #fff; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35); cursor: pointer;
  animation: pop .2s ease-out;
}
.action-btn:active { transform: scale(0.96); background: var(--accent-2); }
@keyframes pop { from { transform: scale(0.7); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.hint {
  position: absolute; left: 50%; bottom: calc(176px + var(--safe-b)); transform: translateX(-50%);
  max-width: min(92vw, 520px); text-align: center; padding: 10px 16px; border-radius: 14px;
  background: rgba(20, 26, 16, 0.78); color: var(--paper); font-size: 14px; line-height: 1.35;
  opacity: 0; transition: opacity .4s;
}
.hint.show { opacity: 1; }

#toasts {
  position: fixed; top: calc(100px + var(--safe-t)); left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; z-index: 30; width: max-content; max-width: 92vw;
}
.toast {
  background: rgba(243, 234, 211, 0.95); color: var(--ink); padding: 8px 14px; border-radius: 12px; font-size: 14px; font-weight: 600;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3); transition: opacity .5s, transform .5s; animation: pop .2s ease-out; text-align: center;
}
.toast.bad { background: #f5d6cf; color: #6b1a12; }
.toast.warn { background: #f7e2b5; }
.toast.good { background: #dcecc9; }
.toast.out { opacity: 0; transform: translateY(-8px); }

/* ---------- overlays & panels ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center;
  padding: calc(12px + var(--safe-t)) calc(12px + var(--safe-r)) calc(12px + var(--safe-b)) calc(12px + var(--safe-l));
  background: rgba(10, 14, 8, 0.55);
}
#modal { z-index: 25; }
.panel {
  position: relative; background: var(--paper); color: var(--ink); border-radius: 18px; padding: 22px;
  width: min(460px, 100%); max-height: 100%; overflow-y: auto; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  animation: rise .22s ease-out; user-select: text; -webkit-user-select: text;
}
.panel.wide { width: min(760px, 100%); }
@keyframes rise { from { transform: translateY(16px); opacity: 0; } to { transform: none; opacity: 1; } }
.panel h1, .panel h2, .panel h3 { font-family: var(--serif); margin: 0 0 8px; font-weight: 700; }
.panel h2 { font-size: 24px; padding-right: 28px; }
.panel h3 { font-size: 17px; }
.panel p { margin: 6px 0 10px; line-height: 1.45; }
.close {
  position: sticky; top: 0; float: right; margin: -10px -8px 0 0; z-index: 2; width: 36px; height: 36px; border-radius: 50%; border: 0;
  background: var(--paper); font-size: 26px; color: var(--ink-2); cursor: pointer; line-height: 1;
}
.muted { color: var(--ink-2); }
.note { background: var(--paper-2); padding: 10px 12px; border-radius: 10px; }
.empty { text-align: center; color: var(--ink-2); padding: 18px 0; }
.col { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
b.g { color: var(--good); }
b.r { color: var(--bad); }

.btn {
  border: 0; border-radius: 12px; padding: 13px 16px; font-size: 16px; font-weight: 650; cursor: pointer;
  background: var(--paper-2); color: var(--ink); font-family: var(--sans);
}
.btn:hover:not(:disabled) { filter: brightness(0.97); }
.btn:active:not(:disabled) { transform: scale(0.98); }
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: transparent; box-shadow: inset 0 0 0 1.5px rgba(43, 35, 24, 0.2); }
.btn.big { padding: 16px 18px; font-size: 18px; }
.btn.small { padding: 8px 12px; font-size: 14px; }
.btn:disabled { opacity: 0.45; cursor: default; }

.tag {
  display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 999px;
  background: rgba(43, 35, 24, 0.1); color: var(--ink-2); vertical-align: middle; font-family: var(--sans); font-style: normal;
}
.tag.ok { background: #dcecc9; color: #2d5a1e; }
.tag.bad { background: #f5d0c8; color: #8a1f14; }

/* title */
.panel.title { text-align: center; background: linear-gradient(180deg, #f6eed9, #ead9b4); }
.title-card h1 { font-size: 42px; letter-spacing: 0.5px; color: #3a2a18; margin-top: 4px; }
.title-mush { width: 120px; height: 110px; display: block; margin: 0 auto; }
.lead { font-size: 16px; color: #4a3d2a; }
.controls { margin-top: 16px; font-size: 13px; color: var(--ink-2); text-align: left; display: grid; gap: 6px; }

/* pick */
.pick-panel { padding: 0; overflow: hidden; display: grid; grid-template-columns: 1fr; align-content: start; width: min(560px, 100%); }
#pick-canvas { width: 100%; height: min(46vh, 330px); display: block; touch-action: none; cursor: pointer; }
.pick-info { padding: 14px 18px 0; }
.pick-info h2 { font-size: 22px; margin-bottom: 2px; }
.pick-info p { margin: 4px 0; }
#pick-msg { min-height: 44px; }
.pick-actions { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 18px 18px; }
.pick-actions .btn { flex: 1 1 auto; }
.pick-actions .btn.primary { flex: 2 1 60%; }

.install {
  margin: 12px 0 0; padding: 10px 12px; border-radius: 12px; text-align: left; font-size: 14px; line-height: 1.4;
  background: #fff8e6; box-shadow: inset 0 0 0 1.5px rgba(200, 100, 42, 0.35);
}
.install b { display: block; margin-bottom: 2px; }
.install .btn { margin-top: 8px; width: 100%; }
.share-ico {
  display: inline-flex; align-items: center; justify-content: center; width: 20px; height: 20px; border-radius: 5px;
  background: #2f7cf6; color: #fff; font-size: 12px; vertical-align: -3px;
}

/* lists */
.list { list-style: none; margin: 10px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.list li { display: flex; align-items: center; gap: 10px; background: rgba(255, 255, 255, 0.5); border-radius: 12px; padding: 8px 10px; }
.list canvas { width: 48px; height: 48px; flex: none; }
.list small { color: var(--ink-2); }
.grow { flex: 1; min-width: 0; }
.big-ico { font-size: 28px; width: 44px; text-align: center; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 10px; margin-top: 10px; }
.card { display: flex; gap: 12px; background: rgba(255, 255, 255, 0.55); border-radius: 14px; padding: 12px; }
.card.focus { box-shadow: 0 0 0 2.5px var(--accent); }
.card canvas { width: 84px; height: 96px; flex: none; }
.card p { font-size: 14px; margin: 6px 0; }
.card small { font-size: 12px; }

.receipt { width: 100%; border-collapse: collapse; margin: 8px 0; font-size: 15px; }
.receipt td { padding: 7px 4px; border-bottom: 1px dashed rgba(43, 35, 24, 0.2); }
.receipt td { white-space: nowrap; }
.receipt td:first-child { max-width: 1px; width: 100%; overflow: hidden; text-overflow: ellipsis; }
.receipt td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.receipt tr.bad td { color: var(--bad); }
.receipt tr.muted td { color: var(--ink-2); }
.receipt tr.total td { font-weight: 800; border-bottom: 0; font-size: 17px; }
.says { background: #fff8e6; border-left: 4px solid var(--warn); padding: 8px 12px; border-radius: 8px; margin-top: 8px; }
.says p { margin: 4px 0; font-family: var(--serif); font-style: italic; }
.says small { color: var(--ink-2); }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 12px 0; }
.stats div { background: rgba(255, 255, 255, 0.55); border-radius: 12px; padding: 10px 6px; text-align: center; }
.stats b { display: block; font-size: 20px; }
.stats small { color: var(--ink-2); font-size: 12px; }

@media (max-width: 520px) {
  .pill { font-size: 13px; padding: 5px 10px; }
  .bar { width: 52px; }
  .icon-btn { width: 40px; height: 40px; font-size: 18px; }
  .panel { padding: 18px 16px; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .cards { grid-template-columns: 1fr; }
  .title-card h1 { font-size: 36px; }
}


/* ---------- compact popups (fit one screen, no scrolling) ---------- */
.panel h2 small { font-family: var(--sans); font-size: 14px; font-weight: 600; margin-left: 6px; }
.fine { font-size: 12.5px; margin: 8px 0 0; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 6px; margin-top: 8px; }
.tile { display: flex; align-items: center; gap: 8px; min-width: 0; background: rgba(255, 255, 255, 0.55); border-radius: 10px; padding: 4px 6px 4px 4px; }
.tile canvas { width: 34px; height: 36px; flex: none; }
.tile b { display: block; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile small { display: block; font-size: 11.5px; color: var(--ink-2); white-space: nowrap; }
.tile .r { color: var(--bad); font-weight: 700; }
.x { flex: none; width: 30px; height: 30px; border-radius: 50%; border: 0; background: rgba(43, 35, 24, 0.08); font-size: 18px; color: var(--ink-2); cursor: pointer; }

.gtabs { display: grid; grid-template-columns: repeat(12, 1fr); gap: 4px; margin: 6px 0 10px; }
.gtab {
  aspect-ratio: 1; border: 0; border-radius: 10px; background: rgba(255, 255, 255, 0.5); padding: 2px; cursor: pointer;
  font-size: 22px; display: flex; align-items: center; justify-content: center; min-width: 0;
}
.gtab canvas { width: 100%; height: 100%; pointer-events: none; }
.gtab.on { background: #fff; box-shadow: 0 0 0 2px var(--accent); }
.gdetail { display: grid; grid-template-columns: 120px 1fr; gap: 14px; align-items: start; }
.gdetail h3 { margin-bottom: 2px; }
.gdetail p { margin: 6px 0; font-size: 14.5px; }
.gpic { width: 120px; height: 130px; }
.gpic.beast { font-size: 80px; display: flex; align-items: center; justify-content: center; }

.split { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
.split .side { display: flex; flex-direction: column; gap: 8px; }
.split .side .col, .split .side .note, .split .side .says { margin: 0; }
.split .stats { grid-template-columns: repeat(2, 1fr); margin: 0; }

.shop-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 8px; }
.shop-tile { display: flex; align-items: center; gap: 10px; background: rgba(255, 255, 255, 0.55); border-radius: 12px; padding: 8px 10px; }
.shop-tile .big-ico { width: 34px; font-size: 26px; }
.shop-tile small { display: block; font-size: 12px; color: var(--ink-2); line-height: 1.3; }
.shop-tile small.next { color: var(--ink); }

@media (max-width: 560px) {
  .split, .shop-grid { grid-template-columns: 1fr; }
  .gtabs { grid-template-columns: repeat(6, 1fr); }
  .gdetail { grid-template-columns: 90px 1fr; }
  .gpic { width: 90px; height: 100px; }
}

/* ---------- rotate-to-landscape screen (touch devices held upright) ---------- */
#rotate {
  display: none; position: fixed; inset: 0; z-index: 60; background: var(--forest); color: var(--paper);
  flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 24px;
}
#rotate h2 { font-family: var(--serif); font-size: 26px; margin: 18px 0 6px; }
#rotate p { margin: 0 0 18px; opacity: 0.8; }
.rotate-phone { font-size: 64px; animation: turn 2.2s ease-in-out infinite; }
@keyframes turn { 0%, 30% { transform: rotate(0); } 60%, 100% { transform: rotate(-90deg); } }
@media (orientation: portrait) { body.touch #rotate { display: flex; } }

/* ---------- landscape phones: low screens ---------- */
@media (orientation: landscape) and (max-height: 520px) {
  .hud-top { top: calc(6px + var(--safe-t)); }
  .hud-left, .hud-right { gap: 4px; }
  .pill { font-size: 12px; padding: 4px 10px; }
  .icon-btn { width: 36px; height: 36px; font-size: 16px; }
  .bar { width: 52px; }
  .joystick { width: 116px; height: 116px; bottom: calc(14px + var(--safe-b)); }
  body.touch .joystick { right: calc(20px + var(--safe-r)); }
  .joy-knob { width: 50px; height: 50px; }
  .action-btn { bottom: calc(14px + var(--safe-b)); right: calc(14px + var(--safe-r)); }
  body.touch .action-btn { left: calc(14px + var(--safe-l)); }
  body.touch .action-btn { max-width: 240px; padding: 15px 20px; }
  .touch-pad { bottom: calc(76px + var(--safe-b)); gap: 8px; }
  body.touch .touch-pad { left: calc(14px + var(--safe-l)); }
  .round-btn { width: 58px; height: 58px; }
  .round-btn span { font-size: 21px; }
  .hint { bottom: calc(14px + var(--safe-b)); max-width: min(46vw, 420px); font-size: 13px; padding: 8px 12px; }
  #toasts { top: calc(8px + var(--safe-t)); }
  .toast { font-size: 13px; padding: 6px 12px; }
  .overlay { padding: 8px calc(8px + var(--safe-r)) 8px calc(8px + var(--safe-l)); }
  .panel { padding: 16px 18px; }
  .panel h2 { font-size: 20px; }
  .pick-panel { grid-template-columns: 1.1fr 1fr; width: min(820px, 100%); max-height: 100%; padding: 0; }
  #pick-canvas { grid-row: 1 / span 2; height: calc(100vh - 16px); max-height: 380px; }
  .pick-info { padding: 12px 14px 0; }
  .pick-info h2 { font-size: 19px; }
  .pick-info p { font-size: 14px; }
  .pick-actions { padding: 8px 14px 12px; align-content: start; }
  .panel.title { width: min(780px, 100%); }
  .title-card { display: grid; grid-template-columns: 1fr 1.15fr; column-gap: 20px; align-items: center; }
  .title-card .title-mush { grid-column: 1; grid-row: 1; }
  .title-card h1 { grid-column: 1; grid-row: 2; }
  .title-card .lead { grid-column: 1; grid-row: 3; align-self: start; }
  .title-card .col { grid-column: 2; grid-row: 1 / span 2; margin-top: 0; }
  .title-card .install { grid-column: 2; grid-row: 3; margin-top: 8px; font-size: 13px; }
  .title-card .controls { grid-column: 1 / span 2; grid-row: 4; }
  body.touch .title-card .controls { display: none; }
  .title-mush { width: 80px; height: 70px; }
  .title-card h1 { font-size: 30px; }
  .lead { font-size: 14px; margin: 4px 0; }
  .controls { margin-top: 8px; font-size: 12px; }
  .col { margin-top: 8px; }
  .btn.big { padding: 12px 16px; font-size: 16px; }
  .split .stats { grid-template-columns: repeat(2, 1fr); }
  .stats div { padding: 6px 4px; }
  .stats b { font-size: 17px; }
  .receipt { font-size: 13px; margin: 4px 0; }
  .receipt td { padding: 3px 4px; }
  .receipt tr.total td { font-size: 15px; }
  .says { padding: 6px 10px; }
  .says p { font-size: 13.5px; margin: 2px 0; }
  .note { padding: 6px 10px; font-size: 13.5px; }
  .panel p { margin: 4px 0 6px; }
  .gdetail p { font-size: 13.5px; }
  .gpic { width: 100px; height: 110px; }
  .gdetail { grid-template-columns: 100px 1fr; }
  .pick-info p { font-size: 13px; margin: 3px 0; }
  #pick-msg { min-height: 0; }
  .pick-actions .btn { padding: 10px 12px; font-size: 15px; }
  .pick-actions .btn.big { padding: 12px 14px; }
  .cards { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
}
