/* ==========================================================================
   Rewards (js/rewards.js): star counter, flying stars, stickers, medals, cups,
   the ceremony after a lesson and the treasure screen. Gold and silver gradients
   are #rw-gold, #rw-gold-face and #rw-silver in index.html's .svg-defs.
   ========================================================================== */

/* ---------- Star counter = the treasure button (top right) ---------- */
.treasure-btn {
  position: absolute;
  top: calc(10px + env(safe-area-inset-top, 0px));
  right: calc(10px + env(safe-area-inset-right, 0px));
  height: 76px;
  min-width: 124px;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 8px 6px 18px;
  border: 4px solid #fff;
  border-radius: 40px;
  background: radial-gradient(circle at 35% 30%, #fff6cf, #ffd66b 75%);
  box-shadow: 0 6px 0 #e1b33f, 0 10px 16px rgba(74, 63, 122, .2);
  color: #7a4b00;
  font: 800 32px/1 system-ui, sans-serif;
  z-index: 2;
  transition: translate .08s;
}
.treasure-btn:disabled { opacity: 1; color: #7a4b00; }
.treasure-btn svg { flex: none; width: 56px; height: 56px; overflow: visible; filter: drop-shadow(0 2px 0 rgba(150, 95, 0, .35)); }
.treasure-btn:not(:disabled) svg { animation: rw-twinkle 4s ease-in-out infinite; }
.treasure-btn.is-bump svg { animation: rw-bump .5s cubic-bezier(.3, 1.6, .5, 1); }
.star-count { min-width: 1ch; text-align: center; font-variant-numeric: tabular-nums; }
@keyframes rw-twinkle { 0%, 80%, 100% { rotate: 0deg; scale: 1; } 88% { rotate: -14deg; scale: 1.12; } 94% { rotate: 8deg; } }
@keyframes rw-bump { 40% { scale: 1.45; rotate: 18deg; } }

.rw-star { fill: url(#rw-gold); stroke: #c98a00; stroke-width: 1.2; stroke-linejoin: round; }
.rw-star-hi { fill: none; stroke: #fff; stroke-width: 1.5; stroke-linecap: round; opacity: .85; }

/* A star on its way from the tapped picture to the counter */
.fly-star {
  position: fixed;
  width: 64px;
  height: 64px;
  transform: translate(-50%, -50%);
  z-index: 30;
  pointer-events: none;
}
.fly-star svg { display: block; width: 100%; height: 100%; overflow: visible; filter: drop-shadow(0 0 10px rgba(255, 214, 60, .95)); }

/* ---------- Medal: ribbon + coin with the room's picture ---------- */
.medal { position: relative; display: block; width: var(--size, 96px); aspect-ratio: 100 / 125; flex: none; }
.medal svg { display: block; width: 100%; height: 100%; overflow: visible; }
.md-strap { fill: #d9dce4; stroke: rgba(0, 0, 0, .12); stroke-width: 1.5; stroke-linejoin: round; }
.medal.is-gold .md-strap--l { fill: var(--tier, #6ec6ff); }
.medal.is-gold .md-strap--r { fill: #ff7aa8; }
.md-rim { fill: url(#rw-silver); stroke: #aab2c2; stroke-width: 2; }
.md-edge { fill: none; stroke: #fff; stroke-width: 2.2; stroke-dasharray: 3 4; opacity: .75; }
.md-face { fill: #eef0f5; stroke: #b9c0ce; stroke-width: 2; }
.md-shine { fill: none; stroke: #fff; stroke-width: 4; stroke-linecap: round; opacity: .65; }
.medal.is-gold .md-rim { fill: url(#rw-gold); stroke: #c98a00; }
.medal.is-gold .md-face { fill: url(#rw-gold-face); stroke: #d99a00; }
.md-icon {
  position: absolute;
  left: 25%;
  top: 44%;
  width: 50%;
  height: 40%;
  object-fit: contain;
  filter: grayscale(1);
  opacity: .35;
  pointer-events: none;
}
.medal.is-gold .md-icon { filter: none; opacity: 1; }

/* ---------- Cup: one per level ---------- */
.cup { position: relative; display: block; width: var(--size, 64px); aspect-ratio: 100 / 110; flex: none; }
.cup svg { display: block; width: 100%; height: 100%; overflow: visible; }
.cup-handle { fill: none; stroke: #c9cfdb; stroke-width: 7; stroke-linecap: round; }
.cup-body, .cup-stem, .cup-base { fill: url(#rw-silver); stroke: #aab2c2; stroke-width: 2.5; stroke-linejoin: round; }
.cup-band { stroke: var(--tier, #ffd84d); stroke-width: 6; }
.cup-shine { fill: none; stroke: #fff; stroke-width: 4; stroke-linecap: round; opacity: .7; }
.cup.is-gold .cup-handle { stroke: #f0ac10; }
.cup.is-gold .cup-body, .cup.is-gold .cup-stem, .cup.is-gold .cup-base { fill: url(#rw-gold); stroke: #c98a00; }
.cup:not(.is-gold) .cup-num { color: #8d96aa; text-shadow: 0 1px 0 #fff; }
.cup-num {
  position: absolute;
  left: 0;
  right: 0;
  top: 25%;
  text-align: center;
  color: #fff;
  font: 800 calc(var(--size, 64px) * .28)/1 system-ui, sans-serif;
  text-shadow: 0 2px 0 rgba(0, 0, 0, .28);
  pointer-events: none;
}

/* ---------- Sticker: one per lesson, on its room's card ---------- */
.sticker {
  position: relative;
  display: block;
  flex: none;
  width: var(--s, 30px);
  aspect-ratio: 1;
  padding: calc(var(--s, 30px) * .13);   /* % would resolve against the window */
  border: 2px dashed rgba(74, 63, 122, .35);
  border-radius: 50%;
  background: rgba(255, 255, 255, .55);
}
.sticker img { display: block; width: 100%; height: 100%; object-fit: contain; filter: grayscale(1); opacity: .22; pointer-events: none; }
.sticker.is-on { border: 3px solid #ffd84d; background: #fff; box-shadow: 0 3px 0 rgba(74, 63, 122, .2); }
.sticker.is-on img { filter: none; opacity: 1; }
.sticker.is-new { animation: rw-stick .7s cubic-bezier(.3, 1.7, .5, 1); }
@keyframes rw-stick { 0% { scale: 1.6; } 40% { scale: .85; } 100% { scale: 1; } }

.rw-card { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.rw-slots { display: flex; gap: 4px; justify-content: center; }

/* ---------- Ceremony after a lesson: the sticker flies onto the room card ---------- */
.reward-show { background: rgba(40, 34, 70, .55); touch-action: none; }
.rw-stage { position: relative; display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; }
.rw-card.is-big {
  --size: clamp(140px, 44vmin, 250px);
  --s: clamp(44px, 12.5vmin, 76px);
  gap: 4vmin;
  padding: 5vmin 5vmin 6vmin;
  border-radius: 32px;
  background: rgba(255, 255, 255, .94);
  box-shadow: 0 8px 0 rgba(40, 34, 70, .25);
}
.rw-card.is-big .rw-slots { gap: 2vmin; }
.rw-new {
  --n: clamp(110px, 32vmin, 180px);
  position: absolute;
  top: max(8px, 5%);
  left: calc(50% - var(--n) / 2);
  width: var(--n);
  padding: calc(var(--n) * .14);
  border: 6px solid #ffd84d;
  box-shadow: 0 0 0 8px rgba(255, 255, 255, .6), 0 0 40px 10px rgba(255, 220, 90, .8);
  z-index: 2;
  animation: rw-pop .55s cubic-bezier(.3, 1.6, .5, 1);
}
@keyframes rw-pop { from { scale: 0; rotate: -30deg; } }

/* Gold at last: the medal spins with rays of light behind it */
.medal::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 64%;
  width: 170%;
  aspect-ratio: 1;
  translate: -50% -50%;
  border-radius: 50%;
  background: repeating-conic-gradient(rgba(255, 230, 120, .9) 0 9deg, transparent 9deg 24deg);
  -webkit-mask: radial-gradient(closest-side, #000 30%, transparent 72%);
  mask: radial-gradient(closest-side, #000 30%, transparent 72%);
  opacity: 0;
  z-index: -1;
  pointer-events: none;
}
.rw-card.is-big.is-won .medal::before { opacity: 1; animation: rw-rays 6s linear infinite, rw-fade .6s ease-out; }
.rw-card.is-big.is-won .medal { animation: rw-spin 1.3s cubic-bezier(.3, 1.2, .5, 1); }
.tr-room.is-tap .medal.is-gold { animation: rw-spin .9s ease-out; }
@keyframes rw-spin {
  0% { transform: perspective(500px) rotateY(0) scale(1); }
  50% { transform: perspective(500px) rotateY(540deg) scale(1.25); }
  100% { transform: perspective(500px) rotateY(720deg) scale(1); }
}
@keyframes rw-rays { to { rotate: 1turn; } }
@keyframes rw-fade { from { opacity: 0; scale: .4; } }

.cup.is-big { --size: clamp(160px, 50vmin, 280px); }
.cup.is-big::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 40%;
  width: 180%;
  aspect-ratio: 1;
  translate: -50% -50%;
  border-radius: 50%;
  background: repeating-conic-gradient(rgba(255, 230, 120, .9) 0 9deg, transparent 9deg 24deg);
  -webkit-mask: radial-gradient(closest-side, #000 30%, transparent 72%);
  mask: radial-gradient(closest-side, #000 30%, transparent 72%);
  z-index: -1;
  animation: rw-rays 6s linear infinite;
}
.cup.is-won { animation: rw-cup 1.4s cubic-bezier(.3, 1.4, .5, 1); }
@keyframes rw-cup { 0% { scale: 0; rotate: -20deg; } 60% { scale: 1.15; rotate: 6deg; } 100% { scale: 1; rotate: 0deg; } }

/* A finished story: its cover gets a gold star */
.rw-book {
  --b: clamp(150px, 44vmin, 240px);
  position: relative;
  width: var(--b);
  aspect-ratio: 1;
  padding: calc(var(--b) * .12);
  border: 8px solid #ffd84d;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 0 10px rgba(255, 255, 255, .5), 0 8px 0 rgba(40, 34, 70, .25);
  animation: rw-pop .55s cubic-bezier(.3, 1.6, .5, 1);
}
.rw-book img { display: block; width: 100%; height: 100%; object-fit: contain; }
.rw-book::after {
  content: "";
  position: absolute;
  top: -8%;
  right: -8%;
  width: 44%;
  aspect-ratio: 1;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 1.8l3.1 6.4 7 .9-5.1 4.9 1.3 7L12 17.6 5.7 21l1.3-7L1.9 9.1l7-.9Z' fill='%23ffc21a' stroke='%23c98a00' stroke-width='1.2' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
  scale: 0;
}
.rw-book.is-won::after { animation: rw-stamp .6s cubic-bezier(.3, 1.7, .5, 1) forwards; }
@keyframes rw-stamp { 0% { scale: 2.4; rotate: -40deg; opacity: 0; } 100% { scale: 1; rotate: 0deg; opacity: 1; } }

/* ---------- Treasure: stars on top, one cup tab per level, the level's rooms below ---------- */
.treasure {
  flex-direction: column;
  gap: 3vmin;
  padding: calc(12px + env(safe-area-inset-top, 0px)) 12px calc(12px + env(safe-area-inset-bottom, 0px));
  background:
    radial-gradient(circle at 50% 0%, rgba(255, 236, 160, .35), transparent 55%),
    linear-gradient(#6a58c9, #3f3384);
  touch-action: none;
}
.tr-stars {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px 6px 24px;
  border: 4px solid #fff;
  border-radius: 44px;
  background: radial-gradient(circle at 35% 30%, #fff6cf, #ffd66b 75%);
  box-shadow: 0 6px 0 #e1b33f;
  color: #7a4b00;
  font: 800 36px/1 system-ui, sans-serif;
}
.tr-stars svg { width: 64px; height: 64px; overflow: visible; }
.tr-tabs { display: flex; flex-wrap: wrap; justify-content: center; gap: 2vmin; }
.tr-tab {
  width: clamp(60px, 16vmin, 92px);
  height: clamp(60px, 16vmin, 92px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px;
  border: 4px solid transparent;
  border-radius: 24px;
  background: rgba(255, 255, 255, .22);
  transition: scale .1s;
}
.tr-tab.is-on { background: #fff; border-color: #ffd84d; box-shadow: 0 0 0 5px rgba(255, 216, 77, .45); }
.tr-tab .cup { --size: clamp(46px, 12vmin, 66px); }
.tr-tab--books svg { width: 80%; height: auto; overflow: visible; }
.tr-page { display: flex; flex-wrap: wrap; justify-content: center; align-content: center; gap: 3vmin; max-width: 640px; }
.tr-room {
  --size: clamp(70px, 21vmin, 120px);
  --s: clamp(24px, 6.6vmin, 40px);
  padding: 10px 10px 12px;
  border: 0;
  border-radius: 24px;
  background: rgba(255, 255, 255, .9);
  box-shadow: 0 5px 0 rgba(74, 63, 122, .25);
  transition: scale .1s;
}
.tr-room .rw-card.is-won { --glow: 1; }
.tr-room:has(.is-won) { background: #fffbe8; box-shadow: 0 0 0 4px #ffd84d, 0 5px 0 rgba(150, 95, 0, .3); }
.tr-room.is-tap:not(:has(.is-won)) { animation: opt-wiggle .45s ease-in-out; }
.tr-page--books { gap: 4vmin; }
.tr-book {
  position: relative;
  width: clamp(76px, 22vmin, 120px);
  aspect-ratio: 4 / 5;
  display: flex;
  align-items: center;
  padding: 0 14px 0 8px;
  border: 0;
  border-radius: 16px 6px 6px 16px;   /* Arabic books bind on the right */
  background: #ff9fc0;
  box-shadow: inset -10px 0 0 rgba(0, 0, 0, .12), 0 5px 0 rgba(74, 63, 122, .3);
  transition: scale .1s;
}
.tr-book:nth-child(3n+2) { background: #6ec6ff; }
.tr-book:nth-child(3n) { background: #7fd36b; }
.tr-book img { display: block; width: 100%; aspect-ratio: 1; padding: 10%; border-radius: 50%; background: #fff; object-fit: contain; pointer-events: none; }
.tr-book:not(.is-won) { filter: grayscale(.85); opacity: .6; }
.tr-book.is-won::after {
  content: "";
  position: absolute;
  top: -14px;
  right: -14px;
  width: 44px;
  height: 44px;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 1.8l3.1 6.4 7 .9-5.1 4.9 1.3 7L12 17.6 5.7 21l1.3-7L1.9 9.1l7-.9Z' fill='%23ffc21a' stroke='%23c98a00' stroke-width='1.2' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
}
.tr-book.is-tap { animation: opt-pop .45s ease-out; }

@media (orientation: landscape) {
  .treasure-btn { height: 64px; min-width: 112px; top: calc(8px + env(safe-area-inset-top, 0px)); font-size: 28px; }
  .treasure-btn svg { width: 48px; height: 48px; }
  .treasure {
    display: grid;
    grid-template-columns: auto auto;
    grid-template-rows: auto 1fr;
    justify-content: center;
    align-content: center;
    column-gap: 5vmin;
    row-gap: 3vmin;
  }
  .tr-head { grid-column: 1; grid-row: 1; justify-self: center; }
  .tr-tabs { grid-column: 1; grid-row: 2; display: grid; grid-template-columns: repeat(2, auto); align-content: start; justify-content: center; }
  .tr-page { grid-column: 2; grid-row: 1 / span 2; max-width: 62vw; }
  .tr-room { --size: clamp(60px, 18vmin, 100px); --s: clamp(22px, 6vmin, 34px); }
  .rw-card.is-big { --size: clamp(120px, 38vmin, 220px); --s: clamp(40px, 11vmin, 64px); }
  .rw-new { top: calc(50% - var(--n) / 2); left: 7vw; }
}

@media (prefers-reduced-motion: reduce) {
  .treasure-btn svg, .medal, .medal::before, .cup, .cup::before, .rw-new, .sticker, .rw-book { animation: none !important; }
}
