:root {
  --bg: #e9f4fb; --ink: #1c2541; --muted: #52627f; --card: #ffffff; --line: #d5e2f0;
  --green: #0b8f6a; --gold: #ffb400; --danger: #d6455d; --accent: #12b886;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: #d6e7f3; color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none; touch-action: manipulation; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 3px solid #3b82f6; outline-offset: 2px; }

#stage {
  position: relative; height: 100vh; height: 100dvh; width: 100%; max-width: 560px; margin: 0 auto; overflow: hidden; background: var(--bg);
  display: grid; grid-template-rows: auto var(--shop-h) minmax(0, 1fr); grid-template-areas: "hud" "shop" "panel";
  --shop-h: clamp(220px, 38dvh, 400px);
}

/* ---- HUD ---- */
#hud { grid-area: hud; display: flex; align-items: center; gap: 10px; padding: max(10px, env(safe-area-inset-top)) 14px 8px; background: var(--card); box-shadow: 0 2px 10px rgba(28,37,65,.08); z-index: 9; position: relative; }
#hud .cash { flex: 1; min-width: 0; }
#hud .lbl { font-size: 11px; font-weight: 800; letter-spacing: .1em; color: var(--muted); }
#money { font-size: 34px; font-weight: 900; line-height: 1.05; color: var(--green); font-variant-numeric: tabular-nums; transform-origin: left center; }
#money.bump { animation: bump .18s; }
@keyframes bump { 40% { transform: scale(1.12); } }
#rate { font-size: 13px; font-weight: 800; color: var(--muted); font-variant-numeric: tabular-nums; }
.stagebox { text-align: right; }
#stagenum { font-size: 11px; font-weight: 800; letter-spacing: .1em; color: var(--muted); }
#stagename { font-size: 15px; font-weight: 900; }
#hud .btns { display: flex; gap: 6px; }
#hud .btns button { width: 42px; height: 42px; border-radius: 12px; border: 1px solid var(--line); background: #f3f8fd; font-size: 17px; padding: 0; }
#hud .btns button[aria-pressed="true"] { opacity: .55; }

/* ---- shop ---- */
#shop { grid-area: shop; position: relative; overflow: hidden; background: #cfe6ef; }
#shopcv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: pointer; }
#chips { position: absolute; left: 10px; top: 10px; display: flex; gap: 6px; pointer-events: none; }
.chip { background: rgba(28,37,65,.82); color: #fff; font-size: 12px; font-weight: 800; padding: 5px 10px; border-radius: 999px; font-variant-numeric: tabular-nums; }
.chip { display: inline-flex; align-items: center; gap: 5px; }
.chip .ci svg { width: 15px; height: 15px; display: block; }
.chip.full { background: #b3541e; }
#hint { position: absolute; left: 10px; right: 10px; bottom: 8px; text-align: center; pointer-events: none; }
#hint:not(:empty) { font-size: 12px; font-weight: 700; color: #fff; background: rgba(28,37,65,.82); padding: 6px 12px; border-radius: 999px; width: max-content; max-width: calc(100% - 20px); left: 50%; right: auto; transform: translateX(-50%); }
#toast { position: absolute; left: 50%; top: 44px; transform: translateX(-50%); background: #fff; color: var(--ink); border-radius: 16px; padding: 7px 16px; font-weight: 800; font-size: 14px; box-shadow: 0 6px 18px rgba(28,37,65,.2); opacity: 0; pointer-events: none; text-align: center; width: max-content; max-width: calc(100% - 20px); line-height: 1.25; }
#toast b { display: block; }
#toast span { display: block; font-size: 11.5px; font-weight: 700; color: var(--muted); }
#toast.on { animation: toast 1.7s; }
@keyframes toast { 0% { opacity: 0; transform: translate(-50%, 8px); } 12% { opacity: 1; transform: translate(-50%, 0); } 80% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%, -8px); } }
#banner { position: absolute; left: 0; right: 0; top: 24%; text-align: center; pointer-events: none; opacity: 0; }
#banner b { display: block; font-size: clamp(30px, 9vw, 46px); font-weight: 900; color: #fff; text-shadow: 0 3px 0 rgba(28,37,65,.6), 0 6px 20px rgba(28,37,65,.4); letter-spacing: .02em; }
#banner span { display: inline-block; margin-top: 6px; background: rgba(28,37,65,.82); color: #fff; padding: 5px 14px; border-radius: 999px; font-size: 13px; font-weight: 700; max-width: calc(100% - 24px); }
#banner.gold b { color: #ffd24d; }
#banner.on { animation: banner 2.4s; }
@keyframes banner { 0% { opacity: 0; transform: scale(.6); } 12% { opacity: 1; transform: scale(1.08); } 20% { transform: scale(1); } 80% { opacity: 1; } 100% { opacity: 0; transform: translateY(-12px); } }

/* ---- panel ---- */
#panel { grid-area: panel; min-height: 0; display: flex; flex-direction: column; background: var(--bg); }
#expand { flex: none; padding: 10px 10px 4px; }
.exp { display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; align-items: center; padding: 10px; border-radius: 16px; background: linear-gradient(135deg, #fff7da, #ffe9a6); border: 2px solid #ffd24d; position: relative; }
.exp.ready { animation: ready 1.2s ease-in-out infinite; }
@keyframes ready { 50% { box-shadow: 0 0 0 4px rgba(255,180,0,.35); } }
.exp .ic { width: 44px; height: 44px; border-radius: 12px; background: #fff; display: grid; place-items: center; color: #8a5a00; }
.exp .t { font-weight: 900; }
.exp .d { font-size: 12.5px; color: #6b5200; line-height: 1.25; }
.exp .bar { background: rgba(138,90,0,.18); }
.exp .bar i { background: #e29a00; }
#tabs { flex: none; display: flex; gap: 6px; padding: 6px 10px; }
#tabs button { flex: 1; min-height: 40px; border: 2px solid var(--line); background: #fff; border-radius: 12px; font-weight: 800; color: var(--muted); }
#tabs button[aria-selected="true"] { background: var(--ink); border-color: var(--ink); color: #fff; }
#list { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; touch-action: pan-y; -webkit-overflow-scrolling: touch; padding: 6px 0 max(14px, env(safe-area-inset-bottom)); }

.card { display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; align-items: center; padding: 10px; margin: 6px 10px 8px; background: var(--card); border: 2px solid var(--line); border-radius: 14px; position: relative; }
.card.afford { border-color: #8fdcbd; }
.card.rec { border-color: var(--gold); }
.card.locked { opacity: .68; }
.card.owned { background: #f3fbf7; border-color: #bfe8d6; }
.card.bought { animation: bought .5s; }
@keyframes bought { 0% { transform: scale(.97); background: #d9f7e9; } 40% { transform: scale(1.02); } 100% { transform: scale(1); } }
.ic { width: 44px; height: 44px; border-radius: 12px; background: #eef5fc; display: grid; place-items: center; color: #2b3560; }
.ic svg { width: 28px; height: 28px; }
.uic { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.body { min-width: 0; }
.t { font-weight: 800; display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.lv { font-size: 12px; font-weight: 700; color: var(--muted); }
.d { font-size: 12.5px; color: var(--muted); line-height: 1.25; margin-top: 1px; }
.bar { height: 5px; border-radius: 3px; background: #e4edf7; margin-top: 6px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 3px; transition: width .3s; }
.d .g { color: var(--green); font-weight: 800; }
.d .g.none { color: #64718c; font-weight: 700; }
.tag { position: absolute; top: -9px; right: 12px; font-size: 10px; font-weight: 900; letter-spacing: .04em; background: var(--gold); color: #3a2800; padding: 2px 8px; border-radius: 999px; display: none; }
.card.rec .tag { display: block; }

.buy { min-width: 88px; min-height: 48px; padding: 4px 10px; border: 0; border-radius: 12px; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.1; font-weight: 900; touch-action: manipulation; }
.buy .s { font-size: 10px; letter-spacing: .08em; opacity: .85; }
.buy .s:empty { display: none; }
.buy .p { font-size: 15px; font-variant-numeric: tabular-nums; }
.buy.ok { background: var(--green); color: #fff; box-shadow: 0 3px 0 #06694d; }
.buy.ok:active { transform: translateY(2px); box-shadow: 0 1px 0 #06694d; }
.buy.poor { background: #e3e9f3; color: #4a5878; box-shadow: 0 3px 0 #c7d1e2; }
.buy.off { background: transparent; color: var(--muted); border: 2px dashed var(--line); }
.buy.off .p { font-size: 12px; }
.buy.gold.ok { background: linear-gradient(#ffd24d, #ffb400); color: #3a2800; box-shadow: 0 3px 0 #b27b00; }
.buy.gold.poor { background: #f3e6c0; color: #6b5200; box-shadow: 0 3px 0 #dcc98c; }
.buy.nope { animation: nope .3s; }
@keyframes nope { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }

/* ---- dialogs / dev ---- */
#modal { position: absolute; inset: 0; z-index: 20; background: rgba(20,30,60,.55); display: grid; place-items: center; padding: 20px; }
#modal[hidden] { display: none; }
.sheet { width: 100%; max-width: 380px; background: #fff; border-radius: 22px; padding: 22px 20px 18px; box-shadow: 0 20px 60px rgba(0,0,0,.35); animation: sheet .25s; }
@keyframes sheet { from { opacity: 0; transform: translateY(14px) scale(.96); } }
.sheet h2 { margin: 0 0 8px; font-size: 24px; }
.sheet p { margin: 8px 0; line-height: 1.45; }
.sheet ol { margin: 8px 0; padding-left: 20px; line-height: 1.5; }
.sheet li { margin: 5px 0; }
.sheet .big { font-size: 40px; font-weight: 900; color: var(--green); text-align: center; margin: 10px 0 2px; }
.sheet .small { font-size: 13px; color: var(--muted); }
#mbtns { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.mb { min-height: 48px; border: 0; border-radius: 14px; font-weight: 900; font-size: 16px; background: #e3e9f3; color: var(--ink); }
.mb.gold { background: linear-gradient(#ffd24d, #ffb400); color: #3a2800; box-shadow: 0 3px 0 #b27b00; }
.mb.danger { background: #fde4e8; color: #a3202f; }
.devbadge { position: absolute; left: 50%; bottom: 6px; transform: translateX(-50%); z-index: 15; pointer-events: none; font: 700 11px system-ui, sans-serif; padding: 4px 10px; border-radius: 999px; background: #ffc93c; color: #1c2541; white-space: nowrap; max-width: calc(100% - 12px); overflow: hidden; text-overflow: ellipsis; }
#fx { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 10; }

/* ---- phones: the panel is a drawer over the full-size shop, opened by the HUD icons ---- */
#hud .btns button.tg[aria-pressed="true"] { background: var(--ink); color: #fff; border-color: var(--ink); opacity: 1; }
#hud .tg svg { width: 22px; height: 22px; display: block; margin: auto; }
@media (max-width: 819px), (max-height: 560px) {
  #stage { grid-template-rows: auto minmax(0, 1fr); grid-template-areas: "hud" "shop"; }
  #tabs { display: none; }
  #panel { position: absolute; z-index: 8; left: 0; right: 0; bottom: 0; height: min(58%, 460px); border-radius: 18px 18px 0 0; box-shadow: 0 -8px 28px rgba(28,37,65,.25); transform: translateY(105%); transition: transform .25s; visibility: hidden; }
  .panel-open #panel { transform: none; visibility: visible; }
}
/* landscape phones: the drawer slides in from the right so the shop stays visible */
@media (max-height: 560px) and (orientation: landscape) {
  #stage { max-width: none; }
  #panel { left: auto; top: 0; bottom: 0; width: min(400px, 52vw); height: auto; border-radius: 0; border-left: 2px solid var(--line); box-shadow: -8px 0 28px rgba(28,37,65,.25); transform: translateX(105%); }
  .panel-open #panel { transform: none; }
  #list { padding-right: env(safe-area-inset-right); }
}
/* ---- wide, tall screens: shop on the left, upgrades always visible on the right ---- */
@media (min-width: 820px) and (min-height: 561px) {
  #stage { max-width: 1100px; grid-template-columns: minmax(0, 1fr) min(400px, 42vw); grid-template-rows: auto minmax(0, 1fr); grid-template-areas: "hud hud" "shop panel"; --shop-h: auto; }
  #panel { border-left: 2px solid var(--line); }
  #hud .tg { display: none; }
}
/* ---- narrow phones ---- */
@media (max-width: 370px) {
  #stagename { display: none; }
  #money { font-size: 29px; }
  .card { grid-template-columns: 38px 1fr auto; gap: 8px; margin-left: 8px; margin-right: 8px; }
  .ic { width: 38px; height: 38px; } .ic svg { width: 24px; height: 24px; }
  .buy { min-width: 76px; }
}
/* ---- short screens (landscape phones): keep the upgrade list usable ---- */
@media (max-height: 560px) {
  #hud { padding: 6px 12px 4px; }
  #money { font-size: 26px; } #rate { font-size: 12px; }
  #hud .btns button { width: 36px; height: 36px; }
  #expand { padding: 6px 8px 2px; }
  .exp { padding: 6px 8px; gap: 8px; } .exp .d, .exp .bar { display: none; } .exp .ic { width: 34px; height: 34px; }
  #tabs { padding: 4px 8px; } #tabs button { min-height: 34px; }
  .card { padding: 6px 8px; margin: 4px 8px 6px; } .card .bar { display: none; } .card .d { font-size: 11.5px; }
  .buy { min-height: 40px; min-width: 72px; }
  .card { grid-template-columns: 34px 1fr auto; gap: 8px; } .ic { width: 34px; height: 34px; } .ic svg { width: 22px; height: 22px; }
  .t { font-size: 14px; gap: 6px; } .exp .t { font-size: 14px; }
}
@media (prefers-reduced-motion: reduce) {
  .exp.ready, .card.bought, #toast.on, #banner.on, .sheet, #money.bump { animation: none; }
  #toast.on, #banner.on { opacity: 1; }
}
