/* ==========================================================================
   神圣奶蛙合成器 · HTML 版
   结构交给 HTML，样式交给 CSS，JS 只负责重力、合成和数值
   ========================================================================== */

:root {
  --ink:        #4A2C14;
  --ink-soft:   #8A6A46;
  --cream:      #FFF6E2;
  --cream-2:    #FDEBD0;
  --paper:      #FFFDF7;
  --orange:     #F5A623;
  --orange-d:   #D9820B;
  --green:      #74A85E;
  --green-d:    #4E7A3E;
  --pink:       #F58BA0;
  --line:       #E8D9BF;
  --shadow:     0 6px 18px rgba(122, 86, 40, .14);
  --shadow-sm:  0 2px 8px rgba(122, 86, 40, .12);
  --radius:     18px;
  --hud-h:      62px;
  --tabs-h:     64px;
  --line-top:   74px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: #F7EFE0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB",
               "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  -webkit-user-select: none;
  user-select: none;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
}

button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }

/* --------------------------------------------------------------- 舞台骨架 */

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(120% 60% at 50% 0%, #EAF2FA 0%, #F4F1E4 42%, #FFF6E2 100%);
  max-width: 520px;
  margin: 0 auto;
}

/* 顶部：金币 + 标题 */
.hud {
  height: var(--hud-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px 0;
  position: relative;
  z-index: 30;
}

.coin-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 38px;
  padding: 0 14px 0 8px;
  border-radius: 19px;
  background: var(--paper);
  border: 2px solid var(--line);
  box-shadow: var(--shadow-sm);
  font-size: 17px;
  font-weight: 800;
  letter-spacing: .3px;
  font-variant-numeric: tabular-nums;
}

.coin-icon {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: radial-gradient(circle at 34% 30%, #FFD98A, var(--orange) 62%, #C9760A 100%);
  border: 2px solid #B8690A;
  position: relative;
  flex: 0 0 auto;
}
.coin-icon::after {
  content: "";
  position: absolute;
  inset: 5px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, .55);
}

.hud-spacer { flex: 1; }

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  background: var(--paper);
  border: 2px solid var(--line);
  box-shadow: var(--shadow-sm);
  display: grid;
  place-items: center;
  color: var(--ink-soft);
  transition: transform .12s;
}
.icon-btn:active { transform: scale(.94); }
.icon-btn svg { width: 20px; height: 20px; }

/* 中间：游戏区 —— 只有一个画布，画面由 JS 绘制 */
.stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

#cv {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* 落地提示 */
.hint {
  position: absolute;
  left: 50%;
  top: 52%;
  transform: translate(-50%, -50%);
  font-size: 14px;
  color: var(--ink-soft);
  background: rgba(255, 253, 247, .86);
  border: 1.5px dashed var(--line);
  border-radius: 14px;
  padding: 9px 16px;
  pointer-events: none;
  transition: opacity .3s;
  z-index: 6;
}
.hint.hide { opacity: 0; }

/* 底部导航 */
.tabs {
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  align-items: center;
  background: var(--paper);
  border-top: 1.5px solid var(--line);
  box-shadow: 0 -6px 18px rgba(122, 86, 40, .07);
  z-index: 30;
}

.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 6px 0;
  color: var(--ink-soft);
  font-size: 11px;
  font-weight: 600;
  position: relative;
  transition: color .15s, transform .12s;
}
.tab:active { transform: scale(.93); }
.tab svg { width: 23px; height: 23px; }
.tab.on { color: var(--green-d); }

.tab .dot {
  position: absolute;
  top: 2px;
  right: 50%;
  margin-right: -22px;
  min-width: 17px;
  height: 17px;
  padding: 0 4px;
  border-radius: 9px;
  background: #E8452F;
  color: #fff;
  font-size: 10px;
  font-weight: 800;
  line-height: 17px;
  text-align: center;
  box-shadow: 0 2px 5px rgba(232, 69, 47, .35);
}
.tab .dot:empty { display: none; }

/* --------------------------------------------------------------- 弹层 */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(52, 34, 16, .38);
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s;
}
.sheet.open { opacity: 1; pointer-events: auto; }

.sheet-card {
  width: 100%;
  max-width: 520px;
  max-height: 88vh;
  background: var(--cream);
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -12px 40px rgba(80, 50, 20, .25);
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform .28s cubic-bezier(.22, .9, .3, 1);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.sheet.open .sheet-card { transform: translateY(0); }

.sheet-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 18px 10px;
  border-bottom: 1.5px solid rgba(232, 217, 191, .8);
}
.sheet-head h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: .5px;
}
.sheet-head .sub {
  font-size: 12px;
  color: var(--ink-soft);
  font-weight: 500;
}
.sheet-close {
  margin-left: auto;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(232, 217, 191, .55);
  display: grid;
  place-items: center;
  font-size: 17px;
  color: var(--ink-soft);
  font-weight: 700;
}
.sheet-body {
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 16px 18px calc(20px + env(safe-area-inset-bottom, 0px));
}

/* 通用卡片 / 按钮 */
.card {
  background: var(--paper);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow-sm);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 42px;
  padding: 0 18px;
  border-radius: 21px;
  background: var(--orange);
  color: #fff;
  font-weight: 800;
  font-size: 15px;
  box-shadow: 0 4px 0 var(--orange-d);
  transition: transform .1s, box-shadow .1s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--orange-d); }
.btn.ghost {
  background: var(--paper);
  color: var(--ink);
  border: 2px solid var(--line);
  box-shadow: 0 3px 0 rgba(232, 217, 191, .9);
}
.btn.ghost:active { box-shadow: 0 1px 0 rgba(232, 217, 191, .9); }
.btn.green { background: var(--green); box-shadow: 0 4px 0 var(--green-d); }
.btn.green:active { box-shadow: 0 1px 0 var(--green-d); }
.btn:disabled { filter: grayscale(.5); opacity: .55; pointer-events: none; }

/* 图鉴 */
.codex-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.codex-cell {
  background: var(--paper);
  border: 1.5px solid var(--line);
  border-radius: 16px;
  padding: 10px 8px 8px;
  text-align: center;
  position: relative;
  overflow: hidden;
}
.codex-cell .pic {
  width: 64px;
  height: 64px;
  margin: 0 auto 4px;
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}
.codex-cell .nm { font-size: 13px; font-weight: 800; }
.codex-cell .ds {
  font-size: 10.5px;
  color: var(--ink-soft);
  line-height: 1.35;
  margin-top: 3px;
  min-height: 28px;
}
.codex-cell .tag {
  display: inline-block;
  margin-top: 5px;
  font-size: 10px;
  font-weight: 800;
  color: #fff;
  padding: 1px 8px;
  border-radius: 8px;
}
.codex-cell.locked .pic { filter: grayscale(1) brightness(.55) opacity(.14); }
.codex-cell.locked .nm { color: #B9A98F; }
.codex-cell.locked .ds { color: #C6B9A4; }
.codex-cell.locked::after {
  content: "?";
  position: absolute;
  inset: 0 0 26px;
  display: grid;
  place-items: center;
  font-size: 30px;
  font-weight: 800;
  color: rgba(180, 160, 130, .5);
}

/* 工坊 */
.workshop-top {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}
.workshop-top .card { flex: 1; }
.stat-k { font-size: 11px; color: var(--ink-soft); font-weight: 600; }
.stat-v { font-size: 17px; font-weight: 800; font-variant-numeric: tabular-nums; }

.slots {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}
.slot {
  aspect-ratio: 1 / 1;
  border-radius: 16px;
  border: 1.5px dashed var(--line);
  background: rgba(255, 253, 247, .7);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  position: relative;
  overflow: hidden;
}
.slot.filled {
  border-style: solid;
  background: var(--paper);
  box-shadow: var(--shadow-sm);
}
.slot .pic {
  width: 52px;
  height: 52px;
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}
.slot .nm { font-size: 11px; font-weight: 800; }
.slot .cd {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 4px;
  background: var(--green);
  border-radius: 0 3px 0 0;
  transition: width .2s linear;
}
.slot.empty .nm { font-size: 22px; color: #D8C9B0; font-weight: 700; }

.tray {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  padding: 12px;
  background: rgba(232, 217, 191, .28);
  border-radius: 16px;
  border: 1.5px solid rgba(232, 217, 191, .7);
}
.tray-cell {
  aspect-ratio: 1 / 1;
  border-radius: 12px;
  background: rgba(255, 253, 247, .5);
  display: grid;
  place-items: center;
  transition: background .12s, box-shadow .12s;
}
.tray-cell.hot {
  background: rgba(116, 168, 94, .18);
  box-shadow: inset 0 0 0 2px var(--green);
}
.coin {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 800;
  color: #7A4A06;
  background: radial-gradient(circle at 34% 30%, #FFE0A0, var(--orange) 60%, #C9760A 100%);
  border: 2px solid #B8690A;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .35);
  animation: coinIn .3s ease-out;
  /* 拖拽必须独占手势，否则手机上会变成滚动面板，pointercancel 直接把拖拽掐断 */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
  cursor: grab;
}
.coin:active { cursor: grabbing; }
.coin:active { transform: scale(.9); }
@keyframes coinIn {
  0%   { transform: scale(.2); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

/* 博物馆 / 榜单 / 设置 */
.row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid rgba(232, 217, 191, .7);
}
.row:last-child { border-bottom: 0; }
.row .grow { flex: 1; min-width: 0; }
.row .ttl { font-size: 14px; font-weight: 700; }
.row .ds { font-size: 11.5px; color: var(--ink-soft); margin-top: 2px; }

.bar {
  height: 9px;
  border-radius: 5px;
  background: rgba(232, 217, 191, .8);
  overflow: hidden;
  margin-top: 8px;
}
.bar > i {
  display: block;
  height: 100%;
  border-radius: 5px;
  background: linear-gradient(90deg, #8CC072, var(--green));
  transition: width .3s;
}

.rank-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid rgba(232, 217, 191, .6);
}
.rank-row .no {
  width: 24px;
  text-align: center;
  font-weight: 800;
  color: var(--ink-soft);
  font-size: 13px;
}
.rank-row .no.top { color: var(--orange-d); }
.rank-row .who { flex: 1; font-size: 13.5px; font-weight: 700; }
.rank-row .val { font-size: 13px; font-weight: 800; font-variant-numeric: tabular-nums; }
.rank-row.me { background: rgba(116, 168, 94, .12); border-radius: 10px; padding-left: 6px; }

.seg {
  display: flex;
  gap: 6px;
  background: rgba(232, 217, 191, .5);
  padding: 4px;
  border-radius: 14px;
}
.seg button {
  flex: 1;
  height: 32px;
  border-radius: 11px;
  font-size: 13px;
  font-weight: 700;
  color: var(--ink-soft);
}
.seg button.on { background: var(--paper); color: var(--ink); box-shadow: var(--shadow-sm); }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: rgba(232, 217, 191, .9);
  outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--orange);
  border: 3px solid #fff;
  box-shadow: 0 2px 6px rgba(120, 80, 30, .3);
}

/* 拆包 */
.parcel-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 6px 0 4px;
}
.parcel-box {
  width: 168px;
  height: 168px;
  border-radius: 26px;
  background: linear-gradient(160deg, #FFDFAE, #F7CB80);
  border: 3px solid #C58A34;
  box-shadow: 0 10px 24px rgba(197, 138, 52, .3), inset 0 -6px 0 rgba(197, 138, 52, .18);
  display: grid;
  place-items: center;
  position: relative;
  transition: transform .12s;
}
.parcel-box:active { transform: scale(.96); }
.parcel-box::before,
.parcel-box::after {
  content: "";
  position: absolute;
  background: var(--green);
}
.parcel-box::before { left: 50%; top: 0; bottom: 0; width: 26px; margin-left: -13px; }
.parcel-box::after { top: 42px; left: 0; right: 0; height: 24px; }
.parcel-box .q {
  position: relative;
  z-index: 2;
  font-size: 46px;
  font-weight: 900;
  color: #FFF6E2;
  text-shadow: 0 2px 0 rgba(197, 138, 52, .5);
}
.parcel-box.ready { animation: bob 1.6s ease-in-out infinite; }
@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-7px); }
}

/* 结算 */
.over {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(52, 34, 16, .45);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}
.over.open { opacity: 1; pointer-events: auto; }
.over-card {
  width: 100%;
  max-width: 320px;
  background: var(--cream);
  border-radius: 24px;
  padding: 22px 20px 18px;
  text-align: center;
  box-shadow: 0 18px 50px rgba(60, 36, 12, .35);
  transform: scale(.92);
  transition: transform .24s cubic-bezier(.22, .9, .3, 1);
}
.over.open .over-card { transform: scale(1); }
.over-card h3 { margin: 0 0 4px; font-size: 20px; font-weight: 800; }
.over-card .big {
  font-size: 34px;
  font-weight: 900;
  color: var(--orange-d);
  line-height: 1.2;
  margin: 6px 0 2px;
  font-variant-numeric: tabular-nums;
}
.over-card .cap { font-size: 12px; color: var(--ink-soft); }
.over-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 18px; }

/* 小屏微调 */
@media (max-height: 620px) {
  :root { --line-top: 62px; --hud-h: 56px; --tabs-h: 58px; }
  .codex-cell .pic { width: 54px; height: 54px; }
}
