:root {
  --cream: #F6EADB;
  --cream-deep: #EFDFC8;
  --card: #FFFDF8;
  --ink: #5B4636;
  --ink-soft: #8A7461;
  --accent: #E8674F;
  --accent-deep: #C94F39;
  --sage: #6FA85A;
  --sage-deep: #4E9E4E;
  --gold: #F6C453;
  --line: #E4D2B8;
  --shadow: 0 4px 14px rgba(120, 90, 60, 0.14);
  --radius: 20px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%;
  overscroll-behavior: none;
  background: var(--cream);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  user-select: none;
}
button { font-family: inherit; }

#app {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  background: linear-gradient(180deg, #FBF3E7 0%, var(--cream) 40%);
}

/* ---------------- HEADER ---------------- */
#header {
  display: flex; gap: 8px; padding: 10px 10px 6px;
  align-items: center;
}
.pill {
  background: var(--card);
  border-radius: 999px;
  display: flex; align-items: center; gap: 6px;
  padding: 6px 12px 6px 6px;
  box-shadow: var(--shadow);
  border: none;
  color: var(--ink);
  font-weight: 700;
  font-size: 14px;
  flex: none;
}
.pillIcon { width: 26px; height: 26px; display: block; flex: none; }
.pillIcon svg { width: 100%; height: 100%; display: block; }
#coinsPill { font-size: 15px; }
.energyPill { flex: 1; min-width: 0; padding-right: 10px; }
.energyBarWrap { flex: 1; min-width: 0; }
.energyBar { height: 8px; background: var(--cream-deep); border-radius: 999px; overflow: hidden; }
.energyBar__fill { height: 100%; background: linear-gradient(90deg, var(--sage-deep), var(--sage)); border-radius: 999px; transition: width .4s ease; }
.energyLabels { display: flex; justify-content: space-between; font-size: 10px; font-weight: 600; color: var(--ink-soft); margin-top: 2px; }
.streakPill { cursor: pointer; }

/* ---------------- EVENT BANNER ---------------- */
#eventBanner {
  margin: 4px 10px 0; padding: 8px 14px;
  border-radius: 16px;
  background: linear-gradient(90deg, #8C6ea0, #B190C4);
  color: #fff; font-weight: 700; font-size: 12px;
  cursor: pointer;
}
#eventBanner.active { background: linear-gradient(90deg, var(--accent-deep), var(--accent)); }
.eventTitleRow { display: flex; justify-content: space-between; align-items: center; }
.eventTrack { display: flex; gap: 4px; margin-top: 5px; }
.eventDot { width: 8px; height: 8px; border-radius: 50%; background: rgba(255,255,255,0.35); }
.eventDot.reached { background: rgba(255,255,255,0.75); }
.eventDot.got { background: var(--gold); }

/* ---------------- GOAL STRIP ---------------- */
.goalStrip {
  margin: 8px 10px 0; padding: 10px 12px;
  background: var(--card); border-radius: 16px; box-shadow: var(--shadow);
}
.goalHead { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.goalTitle { font-weight: 800; font-size: 14px; }
.goalChapterNo { font-size: 11px; color: var(--ink-soft); font-weight: 600; }
.goalList { display: flex; flex-direction: column; gap: 4px; }
.goalItem { display: flex; align-items: center; gap: 8px; font-size: 12px; font-weight: 600; color: var(--ink-soft); }
.goalItem.done { color: var(--sage-deep); text-decoration: line-through; opacity: 0.7; }
.goalIcon { width: 22px; height: 22px; flex: none; }
.goalIcon svg { width: 100%; height: 100%; }
.goalLabel { flex: 1; }
.goalCount { font-variant-numeric: tabular-nums; }

/* ---------------- ORDERS ---------------- */
.ordersStrip {
  display: flex; gap: 8px; overflow-x: auto;
  margin: 8px 0 0; padding: 2px 10px 4px;
  scrollbar-width: none;
}
.ordersStrip::-webkit-scrollbar { display: none; }
.orderCard {
  flex: none; width: 108px;
  background: var(--card); border-radius: 16px; box-shadow: var(--shadow);
  padding: 8px; text-align: center; position: relative;
  border: 2px solid transparent;
}
.orderCard.festival { border-color: var(--gold); background: linear-gradient(180deg, #FFF8E6, var(--card)); }
.orderIcon { width: 52px; height: 52px; margin: 0 auto; }
.orderIcon svg { width: 100%; height: 100%; }
.orderName { font-size: 11px; font-weight: 700; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.orderNeed { font-size: 11px; color: var(--ink-soft); font-weight: 700; }
.orderReward { display: flex; align-items: center; justify-content: center; gap: 3px; margin-top: 3px; font-size: 11px; font-weight: 700; }
.orderReward svg { width: 15px; height: 15px; vertical-align: middle; }
.ptsTag { color: #8C6ea0; }

/* ---------------- BOARD ---------------- */
#boardWrap { flex: 1; overflow-y: auto; padding: 8px 10px 4px; -webkit-overflow-scrolling: touch; }
#board {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  max-width: 480px;
  margin: 0 auto;
}
.cell {
  aspect-ratio: 1 / 1;
  background: rgba(255,255,255,0.5);
  border: 2px dashed var(--line);
  border-radius: 16px;
  position: relative;
}
.cell.dropTarget { background: rgba(111, 168, 90, 0.22); border-color: var(--sage); }
.tile {
  position: absolute; inset: 3px;
  touch-action: none;
  cursor: grab;
  border-radius: 16px;
}
.tile svg { width: 100%; height: 100%; display: block; pointer-events: none; }
.tile.gen { cursor: pointer; }
.tile.gen.locked svg { opacity: 0.55; filter: grayscale(0.6); }
.tile.gen.cooling::after, .tile.gen.noenergy::after { content: ''; }
.genCooldown {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 800; color: #fff;
  background: radial-gradient(circle, rgba(0,0,0,0.0) 40%, rgba(0,0,0,0.0) 40%);
  pointer-events: none;
  opacity: 0;
  border-radius: 16px;
}
.tile.gen.cooling .genCooldown, .tile.gen.noenergy .genCooldown {
  opacity: 1;
  background: rgba(60, 40, 20, 0.45);
}
.tierChip {
  position: absolute; bottom: 2px; right: 4px;
  background: rgba(60,40,20,0.55); color: #fff;
  font-size: 9px; font-weight: 800; padding: 1px 5px; border-radius: 999px;
  pointer-events: none;
}
.hintBubble {
  position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%);
  margin-bottom: 8px; background: var(--ink); color: #fff;
  font-size: 10px; font-weight: 700; padding: 6px 8px; border-radius: 10px;
  width: 110px; text-align: center; white-space: normal;
  animation: floatHint 1.6s ease-in-out infinite;
  z-index: 5;
}
.tile.hintPulse { animation: hintGlow 1.4s ease-in-out infinite; }
@keyframes hintGlow { 0%,100% { filter: drop-shadow(0 0 0 rgba(232,103,79,0)); } 50% { filter: drop-shadow(0 0 10px rgba(232,103,79,0.6)); } }
@keyframes floatHint { 0%,100% { transform: translateX(-50%) translateY(0); } 50% { transform: translateX(-50%) translateY(-4px); } }

.dragClone {
  position: fixed; z-index: 500; pointer-events: none;
  transform: scale(1.14) rotate(-2deg);
  filter: drop-shadow(0 10px 18px rgba(0,0,0,0.28));
}
.dragging-source { opacity: 0.25; }

@keyframes mergePop { 0% { transform: scale(0.6); } 55% { transform: scale(1.22); } 100% { transform: scale(1); } }
.mergePop { animation: mergePop 0.42s cubic-bezier(.34,1.56,.64,1); }
@keyframes spawnPop { 0% { transform: scale(0); opacity: 0; } 70% { transform: scale(1.15); opacity: 1; } 100% { transform: scale(1); } }
.spawnPop { animation: spawnPop 0.4s cubic-bezier(.34,1.56,.64,1); }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
.shake { animation: shake 0.35s ease; }

/* ---------------- FX LAYER ---------------- */
#dragLayer, #toastLayer { position: fixed; inset: 0; pointer-events: none; z-index: 400; }
.particle {
  position: fixed; width: 8px; height: 8px; border-radius: 50%;
  background: var(--gold);
  animation: particleFly 0.6s ease-out forwards;
}
@keyframes particleFly { to { transform: translate(var(--dx), var(--dy)) scale(0); opacity: 0; } }
.floatText {
  position: fixed; transform: translate(-50%, -10px);
  font-weight: 800; font-size: 15px; color: var(--sage-deep);
  animation: floatUp 0.9s ease-out forwards;
}
.floatText.coin { color: var(--gold); text-shadow: 0 1px 2px rgba(0,0,0,0.25); }
@keyframes floatUp { 0% { opacity: 0; transform: translate(-50%, 0); } 20% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%, -46px); } }
.toast {
  position: fixed; left: 50%; bottom: 130px; transform: translate(-50%, 10px);
  background: var(--ink); color: #fff; font-size: 13px; font-weight: 700;
  padding: 9px 16px; border-radius: 999px; opacity: 0; transition: all .25s ease;
  pointer-events: none; white-space: nowrap; max-width: 80vw; overflow: hidden; text-overflow: ellipsis;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------------- BOTTOM NAV ---------------- */
#bottomNav {
  display: flex; justify-content: space-around;
  padding: 6px 10px calc(6px + var(--safe-bottom));
  background: var(--card);
  box-shadow: 0 -4px 14px rgba(120,90,60,0.1);
}
.navBtn {
  background: none; border: none; display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--ink-soft); font-size: 10px; font-weight: 700; padding: 4px 14px;
}
.navBtn span { width: 26px; height: 26px; }
.navBtn svg { width: 100%; height: 100%; }

/* ---------------- MODALS ---------------- */
#modalScrim {
  position: fixed; inset: 0; background: rgba(60,40,20,0.4);
  opacity: 0; pointer-events: none; transition: opacity .25s ease; z-index: 600;
}
#modalScrim.open { opacity: 1; pointer-events: auto; }
.modal {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 601;
  display: flex; justify-content: center;
  transform: translateY(100%); transition: transform .3s cubic-bezier(.32,.72,0,1);
  padding-bottom: var(--safe-bottom);
  pointer-events: none;
}
.modal.open { transform: translateY(0); pointer-events: auto; }
.modalCard {
  background: var(--card); width: 100%; max-width: 480px;
  border-radius: 24px 24px 0 0; padding: 22px 20px 28px;
  max-height: 82vh; overflow-y: auto;
  box-shadow: 0 -10px 30px rgba(0,0,0,0.2);
}
.modalClose {
  position: fixed; z-index: 602;
  right: calc(50vw - 220px); top: calc(env(safe-area-inset-top, 0px) + 12px);
  width: 34px; height: 34px; border-radius: 50%; border: none;
  background: rgba(255,255,255,0.9); color: var(--ink); font-weight: 700;
  transform: translateY(-200%); transition: transform .3s ease;
  box-shadow: var(--shadow);
}
.modal.open .modalClose { transform: translateY(0); position: sticky; float: right; top: -4px; }
.modalCard h2 { margin: 4px 0 2px; font-size: 20px; }
.modalSub { color: var(--ink-soft); font-size: 13px; margin: 0 0 10px; }
.modalBadge { width: 64px; height: 64px; margin: 0 auto 6px; }
.modalBadge svg { width: 100%; height: 100%; }
.rewardRow { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.rewardChip {
  display: flex; align-items: center; gap: 5px; background: var(--cream);
  border-radius: 999px; padding: 6px 12px; font-weight: 700; font-size: 13px;
}
.rewardChip svg { width: 20px; height: 20px; }
.bigBtn {
  width: 100%; padding: 14px; border: none; border-radius: 16px;
  background: var(--accent); color: #fff; font-weight: 800; font-size: 16px;
  margin-top: 8px;
}
.bigBtn:active { transform: scale(0.98); }
.dangerBtn { background: #B0483A; margin-top: 20px; }

.milestoneList { display: flex; flex-direction: column; gap: 8px; margin: 12px 0; }
.milestoneRow { display: flex; align-items: center; gap: 8px; font-size: 12px; font-weight: 700; }
.milestoneRow.done { color: var(--sage-deep); }
.msPoints { width: 52px; flex: none; }
.msBar { flex: 1; height: 8px; background: var(--cream-deep); border-radius: 999px; overflow: hidden; }
.msFill { display: block; height: 100%; background: linear-gradient(90deg, var(--gold), var(--accent)); }
.msState { width: 22px; text-align: center; }

.dailyRow { display: flex; gap: 6px; margin: 10px 0; flex-wrap: wrap; }
.dailyDay {
  flex: 1; min-width: 38px; background: var(--cream); border-radius: 12px; padding: 6px 4px;
  text-align: center; font-size: 10px; font-weight: 800; color: var(--ink-soft);
}
.dailyDay span { display: block; margin-bottom: 3px; }
.dailyDay svg { width: 22px; height: 22px; }
.dailyDay.today { background: var(--gold); color: var(--ink); box-shadow: 0 0 0 2px var(--accent) inset; }
.dailyDay.past { opacity: 0.5; }

.shopItem { display: flex; align-items: center; gap: 10px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.shopIcon { width: 40px; height: 40px; flex: none; }
.shopInfo { flex: 1; }
.shopName { font-weight: 800; font-size: 14px; }
.shopDesc { font-size: 11px; color: var(--ink-soft); }
.shopBuyBtn {
  border: none; background: var(--sage); color: #fff; font-weight: 800; font-size: 13px;
  padding: 8px 12px; border-radius: 12px;
}
.shopBuyBtn:disabled { background: var(--line); color: var(--ink-soft); }

.mapRow { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.mapRow.locked { opacity: 0.55; }
.mapIcon { width: 28px; height: 28px; flex: none; }
.mapTitle { font-weight: 800; font-size: 13px; }
.mapGoals { font-size: 11px; color: var(--ink-soft); }

.settingsRow { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); font-weight: 700; font-size: 14px; }
.toggleBtn { border: none; border-radius: 999px; padding: 6px 14px; font-weight: 800; background: var(--line); color: var(--ink-soft); }
.toggleBtn.on { background: var(--sage); color: #fff; }

@media (min-width: 480px) {
  #header, .goalStrip, .ordersStrip, #boardWrap { max-width: 480px; margin-left: auto; margin-right: auto; width: 100%; }
}
