/* Kupas Bawang! — chunky, sunny, kid-sized, and happy on a phone. */

:root {
  --sun: #FFD93D;
  --coral: #FF6B6B;
  --leaf: #6BCB77;
  --sky: #4D96FF;
  --onion: #A33D63;
  --cream: #FFF7E8;
  --cream-2: #FFEBC9;
  --ink: #43281C;
  --ink-soft: #7A5946;
  --grey: #B8AFA6;

  --font: "Baloo 2", "Comic Sans MS", "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  --radius: 18px;
  --pad: 8px;
  --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; }

/* Class-level `display` beats the user-agent [hidden] rule, so state it here. */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
}

body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 8%, rgba(255, 217, 61, 0.35), transparent 42%),
    radial-gradient(circle at 88% 18%, rgba(107, 203, 119, 0.30), transparent 45%),
    linear-gradient(170deg, #A6E4FB 0%, #CFF0FF 40%, #FFE9C2 100%);
  overflow: hidden;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-text-size-adjust: 100%;
}

.icon-sheet { position: absolute; width: 0; height: 0; overflow: hidden; }

.icon {
  width: 1.3em;
  height: 1.3em;
  flex: none;
  display: block;
  overflow: visible;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  padding: calc(var(--pad) + var(--safe-t)) calc(var(--pad) + var(--safe-r))
           calc(var(--pad) + var(--safe-b)) calc(var(--pad) + var(--safe-l));
  gap: var(--pad);
}

/* ---------- HUD ---------- */

#hud {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 6px 12px;
  padding: 8px 12px;
  background: var(--cream);
  border: 4px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: 0 5px 0 rgba(67, 40, 28, 0.32);
}

.hud-level {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.badge {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  font-size: 20px;
  background: var(--sun);
  border: 4px solid var(--ink);
  border-radius: 50%;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.12);
  animation: bob 2.6s ease-in-out infinite;
}

.level-text {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}

.level-text strong {
  font-size: 15px;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.level-text small { font-size: 11px; color: var(--ink-soft); }

.bar {
  width: min(200px, 40vw);
  height: 11px;
  background: #E8DCC8;
  border: 3px solid var(--ink);
  border-radius: 999px;
  overflow: hidden;
}

#levelFill {
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--leaf), var(--sun));
  transition: width 0.35s ease;
}

.hud-metrics { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }

.metric {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 12px;
  font-size: 15px;
  background: var(--cream-2);
  border: 3px solid var(--ink);
  border-radius: 999px;
}

.metric.money { background: #D8F5DC; }
.metric.streak { background: #FFD9D9; }
.metric-body { display: flex; flex-direction: column; line-height: 1.05; }
.metric-body small { font-size: 10px; color: var(--ink-soft); }
.metric-body strong { font-size: 17px; font-variant-numeric: tabular-nums; white-space: nowrap; }

.hud-buttons { display: flex; gap: 6px; }

/* ---------- buttons ---------- */

button {
  font-family: var(--font);
  font-weight: 700;
  cursor: pointer;
  color: var(--ink);
  border: 4px solid var(--ink);
  transition: transform 0.08s ease, box-shadow 0.08s ease, filter 0.15s ease;
}

button:active:not(:disabled) {
  transform: translateY(4px);
  box-shadow: 0 0 0 rgba(67, 40, 28, 0.4) !important;
}

.chip {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 44px;
  padding: 8px 14px;
  font-size: 15px;
  border-radius: 999px;
  box-shadow: 0 4px 0 rgba(67, 40, 28, 0.4);
}

.chip-green { background: var(--leaf); }
.chip-blue { background: #8FC1FF; }
.chip-round { padding: 8px; width: 44px; justify-content: center; }
.chip.muted { background: var(--grey); filter: grayscale(0.4); }

.dot {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 15px;
  height: 15px;
  background: var(--coral);
  border: 3px solid var(--ink);
  border-radius: 50%;
  animation: ping 1.1s ease-in-out infinite;
}

.big {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  min-height: 50px;
  padding: 12px 22px;
  font-size: 18px;
  border-radius: 16px;
  box-shadow: 0 5px 0 rgba(67, 40, 28, 0.45);
}

.big.wide { width: calc(100% - 40px); margin: 6px 20px 20px; }
.yellow { background: var(--sun); }
.green { background: var(--leaf); }
.red { background: var(--coral); color: #fff; }
.grey { background: var(--grey); }

button:disabled { cursor: not-allowed; opacity: 0.65; }

/* ---------- stage ---------- */

#stage {
  position: relative;
  flex: 1 1 auto;
  display: grid;
  place-items: center;
  min-height: 0;
}

#game {
  display: block;
  border: 4px solid var(--ink);
  border-radius: 20px;
  box-shadow: 0 8px 0 rgba(67, 40, 28, 0.32), 0 16px 36px rgba(30, 60, 90, 0.22);
  touch-action: none;
  background: #9FDDF7;
}

/* Anchored bottom-left, where the table is always empty — centring it would
   put it on top of the timbangan on shorter windows. The bob animation drives
   `transform`, so the hidden state may only touch opacity. */
#hint {
  position: absolute;
  left: 18px;
  bottom: 14px;
  margin: 0;
  max-width: min(52%, 400px);
  padding: 9px 18px;
  text-align: center;
  font-size: 14px;
  font-weight: 700;
  background: var(--cream);
  border: 4px solid var(--ink);
  border-radius: 999px;
  box-shadow: 0 5px 0 rgba(67, 40, 28, 0.4);
  animation: bob 2s ease-in-out infinite;
  pointer-events: none;
  transition: opacity 0.45s ease;
}

#hint.hidden { opacity: 0; }

/* ---------- toast ---------- */

#toast {
  position: absolute;
  top: 0;
  left: 50%;
  z-index: 30;
  padding: 10px 22px;
  font-size: 16px;
  font-weight: 800;
  color: var(--ink);
  background: var(--sun);
  border: 4px solid var(--ink);
  border-radius: 999px;
  box-shadow: 0 5px 0 rgba(67, 40, 28, 0.45);
  white-space: nowrap;
  max-width: 92%;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -130%);
  transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.25s ease;
}

#toast.show { opacity: 1; transform: translate(-50%, 12px); }
#toast.rare { background: #C77DFF; color: #fff; }

/* ---------- overlays ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: none;
  /* start + auto block margins: centred when it fits, top-aligned (and so
     scrollable to the header) when the panel is taller than the screen. */
  place-items: start center;
  padding: calc(12px + var(--safe-t)) 12px calc(12px + var(--safe-b));
  background: rgba(40, 22, 14, 0.55);
  backdrop-filter: blur(3px);
  overflow-y: auto;
  overscroll-behavior: contain;
}

.overlay.top { z-index: 70; }
.overlay.open { display: grid; }

.panel {
  width: min(760px, 100%);
  background: var(--cream);
  border: 5px solid var(--ink);
  border-radius: 24px;
  box-shadow: 0 9px 0 rgba(67, 40, 28, 0.45);
  margin-block: auto;
  overflow: hidden;
  animation: pop 0.26s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.panel.narrow { width: min(520px, 100%); }
.panel.tiny { width: min(420px, 100%); }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 5px solid var(--ink);
}

.panel-head h2 {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0;
  font-size: 21px;
}

.panel-head.green { background: var(--leaf); }
.panel-head.blue { background: #8FC1FF; }
.panel-head.red { background: var(--coral); color: #fff; }
.panel-head.red .icon path { stroke: #fff; fill: #fff; }

.close {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  background: var(--cream);
  border-radius: 50%;
  box-shadow: 0 4px 0 rgba(67, 40, 28, 0.4);
}

.panel-note {
  margin: 0;
  padding: 12px 18px 0;
  font-size: 14px;
  color: var(--ink-soft);
}

.panel-note strong { color: var(--ink); font-size: 17px; }

.section-head {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 4px 0 0;
  padding: 14px 18px 0;
  font-size: 16px;
}

.row {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
  padding: 16px 18px 20px;
}

/* ---------- shop ---------- */

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(272px, 1fr));
  gap: 12px;
  padding: 14px 18px 20px;
}

.card {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  padding: 12px;
  background: #fff;
  border: 4px solid var(--ink);
  border-radius: 18px;
  box-shadow: 0 5px 0 rgba(67, 40, 28, 0.3);
}

.card-emoji {
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  font-size: 30px;
  background: var(--cream-2);
  border: 3px solid var(--ink);
  border-radius: 16px;
}

.card-body h3 { margin: 0 0 3px; font-size: 17px; }
.card-body p { margin: 0 0 5px; font-size: 13px; line-height: 1.35; color: var(--ink-soft); }
.card-price { font-size: 16px; font-weight: 800; color: var(--onion); }

.card .buy {
  grid-column: 1 / -1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  margin-top: 8px;
  padding: 9px;
  font-size: 15px;
  background: var(--sun);
  border-radius: 13px;
  box-shadow: 0 4px 0 rgba(67, 40, 28, 0.4);
}

.card.owned { background: #E6F8E9; }
.card.owned .buy { background: var(--leaf); }
.card.locked { filter: saturate(0.55); }
.card.locked .buy { background: var(--grey); }

/* ---------- stats ---------- */

.stats {
  list-style: none;
  margin: 0;
  padding: 12px 18px 0;
}

.stats li {
  display: grid;
  grid-template-columns: 26px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 9px 4px;
  border-bottom: 3px dotted rgba(67, 40, 28, 0.22);
}

.stats li:last-child { border-bottom: none; }
.stats .icon { width: 22px; height: 22px; }
.stat-label { font-size: 14px; color: var(--ink-soft); }
.stat-value { font-size: 16px; font-weight: 800; font-variant-numeric: tabular-nums; }

.collection {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(112px, 1fr));
  gap: 8px;
  list-style: none;
  margin: 0;
  padding: 12px 18px 4px;
}

.collection li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 13px;
}

.collection .swatch {
  width: 17px;
  height: 17px;
  flex: none;
  border: 3px solid var(--ink);
  border-radius: 50%;
}

.collection .rname { font-size: 12px; line-height: 1.15; color: var(--ink-soft); }
.collection .rcount { margin-left: auto; font-size: 15px; font-weight: 800; }
.collection li.none { opacity: 0.45; }

/* ---------- title card ---------- */

.title-card {
  width: min(560px, 100%);
  padding: 22px 22px 6px;
  text-align: center;
  background: linear-gradient(170deg, #FFF7E8 0%, #FFE7C0 100%);
}

.title-onions { display: flex; justify-content: center; gap: 10px; }
.title-onions .icon { width: 46px; height: 46px; animation: bob 1.6s ease-in-out infinite; }
.title-onions .icon:nth-child(2) { animation-delay: 0.2s; }
.title-onions .icon:nth-child(3) { animation-delay: 0.4s; }

.title-card h1 {
  margin: 8px 0;
  font-size: clamp(30px, 8vw, 50px);
  line-height: 1;
  color: var(--onion);
  text-shadow: 3px 3px 0 var(--sun), 6px 6px 0 rgba(67, 40, 28, 0.25);
  letter-spacing: 1px;
}

.tagline { margin: 0 0 14px; font-size: 16px; line-height: 1.45; }
.tagline strong { color: var(--onion); }

.howto {
  list-style: none;
  margin: 0 auto 4px;
  padding: 0;
  max-width: 420px;
  text-align: left;
}

.howto li {
  display: grid;
  grid-template-columns: 30px 1fr;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  font-size: 14px;
}

.howto li span {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  font-weight: 800;
  background: var(--sky);
  color: #fff;
  border: 3px solid var(--ink);
  border-radius: 50%;
}

/* ---------- motion ---------- */

@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

@keyframes pop {
  from { transform: scale(0.86); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@keyframes ping {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.35); }
}

@media (prefers-reduced-motion: reduce) {
  .badge, #hint, .title-onions .icon, .dot, .panel { animation: none; }
}

/* ---------- phones ---------- */

@media (max-width: 760px) {
  :root { --pad: 6px; --radius: 16px; }

  /* Level and buttons share the top row, metrics get the full width below. */
  #hud { grid-template-columns: minmax(0, 1fr) auto; padding: 7px 10px; }
  .hud-level { grid-column: 1; grid-row: 1; }
  .hud-buttons { grid-column: 2; grid-row: 1; }
  .hud-metrics { grid-column: 1 / -1; grid-row: 2; justify-content: flex-start; flex-wrap: nowrap; }
  .metric { min-width: 0; }
  .metric-body strong { overflow: hidden; text-overflow: ellipsis; }

  .badge { width: 38px; height: 38px; border-width: 3px; }
  .level-text strong { font-size: 13px; }
  .level-text small { font-size: 10px; }
  .bar { width: min(160px, 46vw); height: 9px; }

  .metric { padding: 4px 10px; border-width: 3px; }
  .metric-body strong { font-size: 15px; }
  .metric-body small { font-size: 9px; }

  .chip span { display: none; }
  .chip { padding: 8px; width: 44px; justify-content: center; }

  #game { border-width: 3px; border-radius: 16px; box-shadow: 0 5px 0 rgba(67, 40, 28, 0.3); }
  #hint { left: 10px; bottom: 10px; font-size: 12px; padding: 7px 13px; max-width: 60%; border-width: 3px; }
  #toast { font-size: 14px; padding: 8px 16px; }

  .panel-head h2 { font-size: 18px; }
  .card-grid { grid-template-columns: 1fr; padding: 12px 12px 16px; }
  .stats { padding: 10px 12px 0; }
  .collection { padding: 10px 12px 4px; grid-template-columns: repeat(auto-fit, minmax(104px, 1fr)); }
  .big.wide { width: calc(100% - 24px); margin: 6px 12px 16px; }
  .title-card h1 { font-size: clamp(26px, 9vw, 40px); }
  .title-onions .icon { width: 36px; height: 36px; }
}

/* Narrow phones: one compact line of metrics, no sub-labels. */
@media (max-width: 500px) {
  .metric-body small { display: none; }
  .metric { padding: 5px 9px; gap: 5px; }
  .metric-body strong { font-size: 14px; }
  .metric .icon { width: 1.1em; height: 1.1em; }
  .hud-metrics { gap: 5px; }
}

/* Very short landscape (a phone turned sideways): give the canvas the room. */
@media (max-height: 460px) {
  .level-text small, .metric-body small { display: none; }
  .badge { width: 34px; height: 34px; }
  #hud { padding: 5px 9px; }
  .bar { height: 8px; }
}

/* Portrait stacks the bucket and scale across the bottom, so the bottom-left is
   no longer empty — move the hint up into the sky instead. Positioning must not
   use `transform`, which the bob animation owns. */
@media (max-aspect-ratio: 4/3) {
  #hint {
    top: 8px;
    bottom: auto;
    left: 5%;
    right: 5%;
    max-width: none;
  }
}
