:root {
  --panel: rgba(38, 50, 120, 0.88);
  --panel-edge: #8ea0ff;
  --panel-dark: rgba(20, 26, 70, 0.92);
  --slot: rgba(63, 82, 170, 0.75);
  --slot-hover: rgba(88, 110, 210, 0.9);
  --text: #f4f1ff;
  --muted: #b9c0ee;
  --gold: #ffe066;
  --pink: #ffb8d9;
  --bad: #ff8a8a;
  --shop-w: 360px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: #4f8fe0;
  color: var(--text);
  font-family: "Pixelify Sans", ui-monospace, monospace;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
@supports (height: 100dvh) {
  html, body { height: 100dvh; }
}

button { font: inherit; color: inherit; cursor: pointer; }
img { image-rendering: pixelated; }

.panel {
  background: var(--panel);
  border: 2px solid var(--panel-edge);
  border-radius: 6px;
  box-shadow: inset 0 0 0 2px rgba(0, 0, 0, 0.35), 0 4px 0 rgba(0, 0, 0, 0.25);
}

#app {
  display: grid;
  grid-template-columns: 1fr var(--shop-w);
  grid-template-rows: auto 1fr;
  grid-template-areas: "hud hud" "play shop";
  height: 100%;
  gap: 8px;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
}

/* HUD */
#hud {
  grid-area: hud;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 8px 14px;
  text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.5);
}
.stat { display: flex; flex-direction: column; line-height: 1.1; }
.stat .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.stat b { font-size: 20px; color: var(--pink); }
.stat.main { flex-direction: row; align-items: center; gap: 10px; font-size: 18px; margin-right: auto; }
.stat.main img { width: 40px; height: 34px; object-fit: contain; }
.stat.main b { font-size: 30px; color: var(--gold); }
.buttons { display: flex; gap: 6px; }
.tool {
  width: 40px; height: 40px;
  background: var(--slot);
  border: 2px solid var(--panel-edge);
  border-radius: 6px;
  font-size: 18px;
}
.tool:hover { background: var(--slot-hover); }
.tool[aria-pressed="false"] { opacity: 0.45; text-decoration: line-through; }

/* Play area */
#play {
  grid-area: play;
  position: relative;
  border-radius: 6px;
  overflow: hidden;
  border: 2px solid var(--panel-edge);
  min-height: 0;
}
#cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#spin {
  position: absolute;
  border-radius: 50%;
  background: transparent;
  border: 0;
  touch-action: manipulation;
  user-select: none;
}
#spin:focus-visible { outline: 3px dashed var(--gold); outline-offset: 4px; }
#hint {
  position: absolute;
  left: 50%;
  top: 52px;
  transform: translateX(-50%);
  padding: 4px 12px;
  background: var(--panel-dark);
  border: 2px solid var(--panel-edge);
  border-radius: 6px;
  pointer-events: none;
  animation: bob 1.2s ease-in-out infinite;
  transition: opacity 0.4s;
}
#hint.gone { opacity: 0; }
@keyframes bob { 50% { transform: translate(-50%, 4px); } }

#legend {
  position: absolute;
  left: 50%;
  bottom: 6px;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  padding: 2px 10px;
  font-size: 13px;
  white-space: nowrap;
  pointer-events: none;
}
#legend span { display: flex; align-items: center; gap: 4px; }
#legend img { height: 18px; }
#legend .x { color: var(--gold); }

/* Clicker / Pen switch */
#modeTabs { position: absolute; left: 8px; top: 8px; display: flex; gap: 6px; z-index: 2; }
#modeTabs button {
  padding: 5px 12px;
  background: var(--panel-dark);
  border: 2px solid rgba(142, 160, 255, 0.6);
  border-radius: 6px;
  font-weight: 700;
  position: relative;
}
#modeTabs button[aria-pressed="true"] { border-color: var(--gold); color: var(--gold); }
#penDot:not(:empty) { position: absolute; right: -5px; top: -5px; width: 12px; height: 12px; border-radius: 50%; background: #ff4f8b; border: 2px solid #fff; font-size: 0; }
body.pen #spin, body.pen #legend, body.pen #hint { display: none; }
body.pen #cv { cursor: pointer; }

#penBar {
  position: absolute;
  left: 50%;
  bottom: 8px;
  transform: translateX(-50%);
  width: min(560px, calc(100% - 16px));
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  flex-wrap: wrap;
}
#penBar[hidden] { display: none; }
.happy { display: flex; align-items: center; gap: 8px; flex: 1 1 220px; min-width: 0; }
.happy .h { color: #ff4f8b; font-size: 22px; text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.4); }
.meter { flex: 1; height: 14px; background: rgba(0, 0, 0, 0.4); border: 2px solid rgba(255, 255, 255, 0.4); border-radius: 4px; overflow: hidden; }
.meter i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #ff4f8b, #ffb8d9); transition: width 0.3s; }
#happyText { font-size: 13px; white-space: nowrap; color: var(--pink); }
.pen-buttons { display: flex; gap: 6px; }
.pen-buttons button { padding: 6px 12px; background: var(--slot); border: 2px solid var(--gold); border-radius: 6px; font-weight: 700; white-space: nowrap; }
.pen-buttons button:hover { background: var(--slot-hover); }
.pen-buttons button:disabled { border-color: rgba(142, 160, 255, 0.5); opacity: 0.6; cursor: default; }

/* Shop */
#shop {
  grid-area: shop;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 10px;
}
#shop h2 { margin: 0 0 8px; font-size: 22px; color: var(--gold); text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.5); }
#shopToggle { display: none; }
.list { overflow-y: auto; display: flex; flex-direction: column; gap: 6px; padding-right: 2px; min-height: 0; }
.list[hidden], #buyAmount[hidden], .item[hidden] { display: none; }
.list .empty { color: var(--muted); font-size: 14px; text-align: center; margin: 20px 8px; }
.tabs { display: flex; gap: 6px; margin-bottom: 6px; }
.tab { flex: 1; padding: 6px; background: var(--slot); border: 2px solid rgba(142,160,255,0.5); border-radius: 6px; font-weight: 700; }
.tab[aria-selected="true"] { border-color: var(--gold); color: var(--gold); background: var(--slot-hover); }
.badge:not(:empty) { font-size: 12px; color: #241b00; background: var(--gold); border-radius: 10px; padding: 0 7px; margin-left: 4px; }
#buyAmount { display: flex; align-items: center; gap: 6px; margin-bottom: 6px; font-size: 14px; color: var(--muted); }
#buyAmount button { padding: 2px 10px; background: var(--slot); border: 2px solid rgba(142,160,255,0.5); border-radius: 6px; }
#buyAmount button[aria-pressed="true"] { border-color: var(--gold); color: var(--gold); }

.item {
  display: grid;
  grid-template-columns: 48px 1fr auto;
  align-items: center;
  gap: 10px;
  text-align: left;
  padding: 6px 8px;
  background: var(--slot);
  border: 2px solid rgba(142, 160, 255, 0.5);
  border-radius: 6px;
  opacity: 0.6;
  transition: background 0.1s, opacity 0.15s, transform 0.05s;
}
.item.afford { opacity: 1; border-color: var(--gold); }
.item.afford:hover { background: var(--slot-hover); }
.item:active { transform: translateY(1px); }
.item.done { opacity: 0.4; cursor: default; }
.item.hidden .icon, .item.hidden .icon.moon { filter: brightness(0) opacity(0.55); }
.item .icon { width: 48px; height: 44px; object-fit: contain; border: 2px solid transparent; border-radius: 6px; }
.item:not(.hidden) .icon.moon { filter: grayscale(1) sepia(1) hue-rotate(180deg) saturate(2.2) brightness(1.1); }
.item .info { display: flex; flex-direction: column; min-width: 0; }
.item .name { font-weight: 700; font-size: 16px; }
.item .desc { font-size: 12px; color: var(--muted); }
.item .meta { display: flex; flex-direction: column; align-items: flex-end; }
.item .cost { color: var(--gold); font-weight: 700; }
.item:not(.afford) .cost { color: var(--bad); }
.item.done .cost { color: var(--muted); }
.item .owned { font-size: 22px; font-weight: 700; color: rgba(255, 255, 255, 0.55); }
.item.flash { animation: flash 0.3s; }
@keyframes flash { 0% { background: #fff6b0; } }

/* Toast + menu */
#toast {
  position: fixed;
  left: 50%;
  top: 70px;
  transform: translate(-50%, -20px);
  max-width: min(90vw, 460px);
  padding: 10px 16px;
  background: var(--panel-dark);
  border: 2px solid var(--gold);
  border-radius: 6px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  z-index: 10;
  text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

dialog#menu { color: var(--text); max-width: min(92vw, 380px); padding: 16px 18px; }
dialog#menu::backdrop { background: rgba(10, 12, 40, 0.6); }
dialog#menu h2 { margin: 0 0 6px; color: var(--gold); }
.menu-buttons { display: flex; flex-direction: column; gap: 8px; margin: 12px 0; }
.menu-buttons button { padding: 8px; background: var(--slot); border: 2px solid var(--panel-edge); border-radius: 6px; }
.menu-buttons button:hover { background: var(--slot-hover); }
.menu-buttons .danger { border-color: var(--bad); }
.credits { font-size: 12px; color: var(--muted); }

/* Mobile: shop becomes a bottom tray */
@media (max-width: 899px) {
  #app { grid-template-columns: 1fr; grid-template-areas: "hud" "play"; gap: 6px; padding: max(6px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right)) calc(62px + env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left)); }
  #hud { gap: 10px; padding: 6px 10px; flex-wrap: wrap; }
  .stat.main b { font-size: 24px; }
  .stat b { font-size: 16px; }
  .stat.main img { width: 30px; height: 26px; }
  .tool { width: 34px; height: 34px; font-size: 15px; }
  #shop {
    position: fixed;
    left: max(6px, env(safe-area-inset-left)); right: max(6px, env(safe-area-inset-right)); bottom: max(6px, env(safe-area-inset-bottom));
    height: 50px;
    padding: 0 10px;
    transition: height 0.25s;
    z-index: 5;
    background: #222c6c;
  }
  body.shop-open #shop { height: 62vh; }
  #shop h2 { display: none; }
  #shopToggle {
    display: flex; align-items: center; gap: 8px;
    height: 46px; flex: none;
    background: none; border: 0;
    font-size: 20px; font-weight: 700; color: var(--gold);
  }
  #shopToggle .chev { margin-left: auto; transition: transform 0.25s; }
  body.shop-open #shopToggle .chev { transform: rotate(180deg); }
  #shopBadge:not(:empty) { font-size: 13px; color: #241b00; background: var(--gold); border-radius: 10px; padding: 1px 8px; }
  #legend { font-size: 11px; gap: 6px; }
  #modeTabs button { padding: 4px 9px; font-size: 14px; }
  .pen-buttons button { padding: 5px 9px; font-size: 14px; }
  #legend img { height: 14px; }
}
