:root {
  --bg: #ffe9c9;
  --bg-2: #ffd9a8;
  --ink: #4a2a1a;
  --ink-soft: #8a5a3c;
  --card: #fff8ec;
  --rim: #e2873a;
  --rim-dark: #b8601f;
  --accent: #ff7a45;
  --green: #2fa65a;
  --shadow: 0 6px 0 rgba(140, 70, 20, 0.18), 0 14px 30px rgba(140, 70, 20, 0.14);
  --radius: 22px;
  font-family: 'Fredoka', ui-rounded, 'SF Pro Rounded', system-ui, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }

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

body {
  background:
    radial-gradient(circle at 20% 10%, rgba(255, 255, 255, 0.55), transparent 40%),
    radial-gradient(rgba(226, 135, 58, 0.16) 2px, transparent 2.5px) 0 0 / 26px 26px,
    linear-gradient(160deg, var(--bg), var(--bg-2));
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

button { font: inherit; color: inherit; }

.app {
  display: grid;
  grid-template-columns: 220px auto 260px;
  grid-template-rows: auto 1fr;
  grid-template-areas:
    'top top top'
    'hud stage side';
  gap: 18px 28px;
  justify-content: center;
  align-items: start;
  padding: 18px 24px 24px;
  min-height: 100dvh;
}

.top {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 1040px;
  justify-self: center;
}

.logo {
  margin: 0;
  font-size: 52px;
  font-weight: 700;
  letter-spacing: 1px;
  line-height: 1;
  display: flex;
}

.logo span {
  display: inline-block;
  color: #fff;
  -webkit-text-stroke: 7px var(--ink);
  paint-order: stroke fill;
  text-shadow: 0 5px 0 var(--ink);
  animation: bob 3.2s ease-in-out infinite;
}
.logo span:nth-child(1) { color: #ff5a6e; animation-delay: 0s; }
.logo span:nth-child(2) { color: #ffd23f; animation-delay: 0.12s; }
.logo span:nth-child(3) { color: #8fd16a; animation-delay: 0.24s; }
.logo span:nth-child(4) { color: #ff9f1c; animation-delay: 0.36s; }
.logo span:nth-child(5) { color: #9a5cf0; animation-delay: 0.48s; }

@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(-2deg); }
  50% { transform: translateY(-5px) rotate(2deg); }
}

.actions { display: flex; gap: 10px; }

.icon-btn {
  width: 48px;
  height: 48px;
  border-radius: 16px;
  border: 3px solid var(--ink);
  background: var(--card);
  box-shadow: 0 4px 0 var(--ink);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform 0.12s, box-shadow 0.12s;
}
.icon-btn svg { width: 24px; height: 24px; fill: currentColor; }
.icon-btn:hover { transform: translateY(-2px); box-shadow: 0 6px 0 var(--ink); }
.icon-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.icon-btn:focus-visible, .btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
#btn-sound .off { display: none; }
#btn-sound[aria-pressed='true'] .on { display: none; }
#btn-sound[aria-pressed='true'] .off { display: block; }

.card {
  background: var(--card);
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.hud {
  grid-area: hud;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.hud .card { padding: 14px 18px; text-align: center; }
.label {
  font-size: 15px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 2px;
  color: var(--ink-soft);
}
.value {
  font-size: 44px;
  font-weight: 700;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.score-card { background: linear-gradient(180deg, #fffaf1, #ffeacc); }
.score-card .value.bump { animation: bump 0.32s cubic-bezier(.3, 1.6, .5, 1); }
@keyframes bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.22); color: var(--accent); }
  100% { transform: scale(1); }
}
.next-card canvas {
  width: 96px;
  height: 96px;
  display: block;
  margin: 6px auto 0;
}
.next-card canvas.pop { animation: pop-in 0.35s cubic-bezier(.3, 1.7, .5, 1); }
@keyframes pop-in {
  0% { transform: scale(0.4) rotate(-30deg); opacity: 0; }
  100% { transform: scale(1) rotate(0); opacity: 1; }
}

.stage {
  grid-area: stage;
  position: relative;
  height: calc(100dvh - 130px);
  max-height: 820px;
  min-height: 420px;
  aspect-ratio: 420 / 620;
}

.jar {
  position: absolute;
  inset: 0;
  padding: 0 10px 10px;
  border-radius: 10px 10px 34px 34px;
  background: linear-gradient(90deg, var(--rim-dark), var(--rim) 12%, #f3a457 50%, var(--rim) 88%, var(--rim-dark));
  border: 3px solid var(--ink);
  border-top: none;
  box-shadow: var(--shadow);
}
.jar::before, .jar::after {
  content: '';
  position: absolute;
  top: -8px;
  width: 26px;
  height: 16px;
  background: var(--rim);
  border: 3px solid var(--ink);
  border-radius: 8px;
}
.jar::before { left: -10px; }
.jar::after { right: -10px; }

#board {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 0 0 26px 26px;
  touch-action: none;
  cursor: none;
}

.stage.shake { animation: shake 0.42s cubic-bezier(.36, .07, .19, .97); }
@keyframes shake {
  10%, 90% { transform: translate(-1px, 1px); }
  20%, 80% { transform: translate(3px, -2px); }
  30%, 50%, 70% { transform: translate(-5px, 2px); }
  40%, 60% { transform: translate(5px, -1px); }
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(74, 42, 26, 0.35);
  backdrop-filter: blur(3px);
  border-radius: 10px 10px 34px 34px;
  animation: fade 0.25s ease-out;
  z-index: 3;
}
.overlay[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } }

.panel {
  position: relative;
  background: var(--card);
  border: 3px solid var(--ink);
  border-radius: 26px;
  box-shadow: var(--shadow);
  padding: 26px 30px 24px;
  text-align: center;
  display: grid;
  gap: 12px;
  min-width: 230px;
  animation: rise 0.45s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes rise { from { transform: translateY(40px) scale(0.85); opacity: 0; } }
.panel h2 { margin: 0; font-size: 32px; }
.final { font-size: 64px; font-weight: 700; line-height: 1; color: var(--accent); -webkit-text-stroke: 2px var(--ink); paint-order: stroke fill; }
.final-meta { color: var(--ink-soft); font-weight: 500; }
.final-fruit { display: flex; align-items: center; justify-content: center; gap: 10px; font-weight: 500; }
.final-fruit img { width: 40px; height: 40px; border-radius: 50%; border: 3px solid; }
.ribbon {
  position: absolute;
  top: -18px;
  left: 50%;
  transform: translateX(-50%) rotate(-4deg);
  background: #ffd23f;
  border: 3px solid var(--ink);
  border-radius: 12px;
  padding: 4px 16px;
  font-weight: 700;
  white-space: nowrap;
  animation: wiggle 1s ease-in-out infinite;
}
.ribbon[hidden] { display: none; }
@keyframes wiggle {
  0%, 100% { transform: translateX(-50%) rotate(-4deg) scale(1); }
  50% { transform: translateX(-50%) rotate(4deg) scale(1.08); }
}

.btn {
  border: 3px solid var(--ink);
  border-radius: 16px;
  background: var(--card);
  box-shadow: 0 4px 0 var(--ink);
  padding: 10px 22px;
  font-size: 20px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s, box-shadow 0.12s;
}
.btn.primary { background: var(--green); color: #fff; text-shadow: 0 2px 0 rgba(0, 0, 0, 0.2); }
.btn:hover { transform: translateY(-2px); box-shadow: 0 6px 0 var(--ink); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }

.side {
  grid-area: side;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.side h2, .sheet h2 { margin: 0 0 10px; font-size: 20px; text-align: center; }
.evo { padding: 14px; }
.evo-ring {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
}
.evo-item {
  position: absolute;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  border: 3px solid;
  background-size: cover;
  background-position: center;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
  transition: transform 0.3s cubic-bezier(.3, 1.6, .5, 1), filter 0.3s;
}
.evo-item.locked { filter: grayscale(0.85) opacity(0.55); }
.evo-item.flash { animation: evo-flash 0.6s cubic-bezier(.3, 1.6, .5, 1); }
@keyframes evo-flash {
  0% { transform: translate(-50%, -50%) scale(1); }
  40% { transform: translate(-50%, -50%) scale(1.45); }
  100% { transform: translate(-50%, -50%) scale(1); }
}
.evo-center {
  position: absolute;
  inset: 32%;
  display: grid;
  place-items: center;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
  line-height: 1.2;
}

.scores { padding: 14px 16px; }
.score-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 4px;
  counter-reset: rank;
}
.score-list li {
  counter-increment: rank;
  display: grid;
  grid-template-columns: 26px 28px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  border-radius: 12px;
  font-variant-numeric: tabular-nums;
}
.score-list li::before { content: counter(rank); font-weight: 700; color: var(--ink-soft); text-align: center; }
.score-list li:nth-child(1) { background: #ffe58a; }
.score-list li:nth-child(2) { background: #f0e6da; }
.score-list li:nth-child(3) { background: #ffd9b8; }
.score-list li.fresh { animation: fresh 1.4s ease-out; outline: 3px solid var(--accent); }
@keyframes fresh { 0% { transform: scale(1.08); } 100% { transform: scale(1); } }
.score-list img { width: 26px; height: 26px; border-radius: 50%; border: 2px solid; }
.score-list .pts { font-weight: 700; font-size: 18px; }
.score-list .when { font-size: 12px; color: var(--ink-soft); }
.score-list .empty { display: block; text-align: center; color: var(--ink-soft); }
.score-list .empty::before { content: none; }
.global-list li { grid-template-columns: 26px 28px minmax(0, 1fr) auto; }
.global-list .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.global-list .dot { width: 26px; height: 26px; border-radius: 50%; border: 2px dashed var(--ink-soft); opacity: 0.5; }
.board-note { display: block; text-align: center; color: var(--ink-soft); font-size: 14px; }
.board-note::before { content: none; }
.sheet h3 { margin: 12px 0 6px; font-size: 15px; letter-spacing: 1px; text-transform: uppercase; color: var(--ink-soft); }
.sheet h3:first-of-type { margin-top: 0; }
.submit-form { display: grid; gap: 6px; text-align: left; width: min(300px, 100%); justify-self: center; }
.submit-form[hidden] { display: none; }
.submit-form label { font-size: 14px; font-weight: 600; color: var(--ink-soft); }
.submit-row { display: flex; gap: 8px; }
.submit-row input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-weight: 600;
  font-size: 17px;
  color: var(--ink);
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 14px;
  padding: 6px 12px;
}
.submit-row input:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.btn.small { padding: 6px 14px; font-size: 17px; }
.btn:disabled { opacity: 0.6; cursor: default; transform: none; }
.submit-status { min-height: 1.3em; font-size: 14px; font-weight: 500; }
.submit-status.error { color: #c0392b; }
.submit-status.ok { color: var(--green); font-weight: 700; }
.stats { margin-top: 10px; font-size: 13px; color: var(--ink-soft); text-align: center; }

.sheet {
  border: 3px solid var(--ink);
  border-radius: 24px;
  background: var(--card);
  color: var(--ink);
  width: min(92vw, 380px);
  padding: 16px 18px 20px;
  box-shadow: var(--shadow);
}
.sheet[open] { animation: rise 0.35s cubic-bezier(.2, 1.4, .4, 1); }
.sheet::backdrop { background: rgba(74, 42, 26, 0.4); backdrop-filter: blur(2px); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet-head h2 { margin: 0; }
.sheet .icon-btn { width: 40px; height: 40px; }

.only-narrow { display: none; }

@media (max-width: 1000px) {
  .app { grid-template-columns: 190px auto 220px; gap: 14px 18px; }
  .value { font-size: 36px; }
}

@media (max-width: 760px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
    grid-template-areas: 'top' 'hud' 'stage' 'side';
    gap: 10px;
    padding: max(10px, env(safe-area-inset-top)) 12px max(14px, env(safe-area-inset-bottom));
    align-content: start;
  }
  .logo { font-size: 36px; }
  .logo span { -webkit-text-stroke: 5px var(--ink); text-shadow: 0 3px 0 var(--ink); }
  .icon-btn { width: 42px; height: 42px; border-radius: 14px; }
  .only-narrow { display: grid; }
  .only-wide { display: none; }
  .hud { flex-direction: row; gap: 8px; }
  .hud .card { flex: 1; padding: 6px 8px; display: flex; flex-direction: column; justify-content: center; }
  .label { font-size: 11px; letter-spacing: 1.5px; }
  .value { font-size: 26px; }
  .next-card { flex: 0 0 76px !important; }
  .next-card canvas { width: 46px; height: 46px; margin-top: 2px; }
  .stage {
    height: auto;
    width: min(100%, calc((100dvh - 240px) * 420 / 620));
    min-height: 0;
    justify-self: center;
  }
  .evo { padding: 8px 10px; }
  .evo h2 { display: none; }
  .evo-ring { aspect-ratio: auto; display: flex; justify-content: space-between; align-items: center; gap: 2px; }
  .evo-item { position: static; transform: none; width: auto !important; height: auto !important; flex: 1 1 0; aspect-ratio: 1; max-width: 30px; border-width: 2px; }
  .evo-item.flash { animation: none; }
  .evo-center { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; }
}
