:root {
  --night: #0b1a33;
  --deep: #12284a;
  --pine: #0f5a3c;
  --green: #1f9d5c;
  --green-dark: #0b5d34;
  --red: #e32b3a;
  --red-dark: #9c1220;
  --gold: #ffcf4a;
  --gold-dark: #b8860b;
  --snow: #f4f8ff;
  --ice: #b9d4ff;
  --good: #4fe08f;
  --bad: #ff7b8a;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body {
  width: 100%; height: 100%; overflow: hidden;
  font-family: "Kanit", system-ui, sans-serif;
  color: var(--snow);
  touch-action: none;
  user-select: none; -webkit-user-select: none;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%; text-size-adjust: 100%;
}
body {
  background:
    radial-gradient(circle at 80% 12%, rgba(255, 240, 200, .18), transparent 22%),
    radial-gradient(ellipse at 50% 120%, #1d4c7a 0%, transparent 60%),
    linear-gradient(180deg, #050d1e 0%, #0b1a33 55%, #143258 100%);
}
button, a, label { font-family: inherit; touch-action: manipulation; }
.hidden { display: none !important; }

/* Phone-shaped stage, centered on desktop */
#app {
  position: fixed; top: 0; bottom: 0; left: 50%; transform: translateX(-50%);
  width: 100%; max-width: 480px; overflow: hidden; background: var(--night);
}
@media (min-width: 560px) {
  #app {
    top: 12px; bottom: 12px; border-radius: 28px;
    box-shadow: 0 0 0 2px rgba(255, 207, 74, .3), 0 30px 80px rgba(0, 0, 0, .6), 0 0 60px rgba(227, 43, 58, .25);
  }
}
#game { display: block; width: 100%; height: 100%; }

/* ---------- HUD ---------- */
#hud {
  position: absolute; top: max(8px, env(safe-area-inset-top)); left: 8px; right: 58px; z-index: 3;
  display: flex; gap: 6px; align-items: stretch; pointer-events: none;
}
.hud-box, .icon-btn {
  background: rgba(11, 26, 51, .82); border: 2.5px solid var(--gold); border-radius: 14px;
  box-shadow: 0 3px 0 var(--gold-dark), inset 0 0 12px rgba(31, 157, 92, .45);
}
.hud-box {
  padding: 2px 8px; flex: 1; min-width: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.label { font-size: 12px; color: var(--ice); line-height: 1.2; }
.hud-box span:last-child { font-size: 26px; font-weight: 800; line-height: 1.05; color: var(--gold); }
.hud-logo {
  flex: none; width: 44px; height: 44px; align-self: center; object-fit: contain;
  background: rgba(255, 255, 255, .92); border-radius: 12px; padding: 4px; box-shadow: 0 3px 0 rgba(0, 0, 0, .3);
}

.icon-btn {
  position: absolute; top: max(10px, env(safe-area-inset-top)); right: 8px; z-index: 5;
  width: 44px; height: 44px; border-radius: 50%; font-size: 20px; cursor: pointer; color: #fff;
}

.tap-hint {
  position: absolute; left: 0; right: 0; bottom: 22%; z-index: 1; text-align: center; pointer-events: none;
  font-size: 20px; font-weight: 600; text-shadow: 0 2px 6px rgba(0, 0, 0, .8);
  animation: hintPulse 1.1s ease-in-out infinite;
}
@keyframes hintPulse { 50% { opacity: .45; transform: scale(.96); } }

/* ---------- Screens ---------- */
.screen {
  position: absolute; inset: 0; z-index: 2;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  background: rgba(5, 13, 30, .55); backdrop-filter: blur(2px);
  overflow-y: auto; touch-action: pan-y;
  animation: fadeIn .3s ease;
}
@keyframes fadeIn { from { opacity: 0; } }
.screen.enter { animation: fadeIn .28s ease; }
.screen.enter .panel, .screen.enter .photo-wrap { animation: panelIn .42s cubic-bezier(.2, 1.35, .4, 1); }
.screen-start.enter .btn-stack > * { animation: rise .45s cubic-bezier(.2, 1.3, .4, 1) both; }
.screen-start.enter .btn-stack > :nth-child(2) { animation-delay: .06s; }
@keyframes panelIn { from { opacity: 0; transform: translateY(24px) scale(.94); } }
@keyframes rise { from { opacity: 0; transform: translateY(18px); } }
@media (prefers-reduced-motion: reduce) {
  .screen.enter, .screen.enter .panel, .screen.enter .photo-wrap, .screen-start.enter .btn-stack > *, .tap-hint { animation: none; }
}

/* combo + last-seconds countdown */
.combo-chip {
  position: absolute; top: calc(100% + 6px); left: 50%; transform: translateX(-50%);
  padding: 2px 12px; border-radius: 999px; font-size: 14px; font-weight: 800; white-space: nowrap;
  background: rgba(11, 26, 51, .85); border: 2px solid var(--ice); color: #fff;
}
.combo-chip.hot {
  background: linear-gradient(90deg, #e32b3a, #ffcf4a); color: #4a1000; border-color: #fff3c4;
  box-shadow: 0 0 16px rgba(255, 170, 40, .8); animation: comboPulse .6s ease-in-out infinite;
}
@keyframes comboPulse { 50% { transform: translateX(-50%) scale(1.08); } }
.big-count {
  position: absolute; left: 0; right: 0; top: 30%; z-index: 1; text-align: center; pointer-events: none;
  font-family: "Mitr", sans-serif; font-size: 140px; line-height: 1; color: var(--red);
  -webkit-text-stroke: 8px #fff; paint-order: stroke fill; opacity: 0;
}
.big-count.pop { animation: bigCount 1s ease-out forwards; }
@keyframes bigCount { 0% { opacity: .9; transform: scale(1.6); } 60% { opacity: .55; transform: scale(1); } 100% { opacity: 0; transform: scale(.9); } }

.toast {
  position: absolute; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); z-index: 40; transform: translateX(-50%);
  max-width: calc(100% - 32px); padding: 10px 18px; border-radius: 14px; font-size: 15px; text-align: center;
  background: rgba(5, 13, 30, .94); border: 2px solid var(--gold); box-shadow: 0 8px 24px rgba(0, 0, 0, .5);
  animation: rise .3s ease;
}

.screen:not(.screen-start) { padding-top: max(62px, env(safe-area-inset-top)); }
.screen-start { background: linear-gradient(180deg, rgba(5, 13, 30, .35), rgba(5, 13, 30, .05) 40%, rgba(5, 13, 30, .6)); justify-content: space-between; gap: 6px; }

/* Brand logo spots (filled from config.js; hidden when a spot is off or the image is missing) */
.title-logo {
  height: 56px; width: auto; max-width: 60%; object-fit: contain; margin-top: 4px;
  filter: drop-shadow(0 4px 10px rgba(0, 0, 0, .5));
}
.presented { display: flex; align-items: center; justify-content: center; gap: 8px; margin-top: 14px; }
.presented small { font-size: 12px; color: var(--ice); letter-spacing: .5px; }
.presented img { height: 34px; width: auto; max-width: 140px; object-fit: contain; background: rgba(255, 255, 255, .92); border-radius: 8px; padding: 3px 6px; }
.presented.top { margin: -4px 0 8px; }

/* Title — sticker-style lettering */
.title { display: flex; flex-direction: column; align-items: center; font-family: "Mitr", "Kanit", sans-serif; line-height: 1; transform: rotate(-3deg); }
.title > span:not(.title-sub) {
  position: relative; display: block; font-weight: 700; color: #fff;
  -webkit-text-stroke: 12px #fff; paint-order: stroke fill;
  text-shadow: 0 6px 0 #7a0d18, 0 8px 18px rgba(0, 0, 0, .5);
}
.title > span:not(.title-sub)::after {
  content: attr(data-text); position: absolute; inset: 0;
  -webkit-text-stroke: 0; text-shadow: none; color: transparent;
  -webkit-background-clip: text; background-clip: text;
}
.title-top { font-size: clamp(44px, 13vw, 60px); }
.title-top::after { background-image: linear-gradient(180deg, #ff6b6b 0%, #e32b3a 55%, #a3121f 100%); }
.title-mid { margin-top: 4px; font-size: clamp(40px, 12vw, 56px); transform: rotate(2deg); }
.title-mid::after { background-image: linear-gradient(180deg, #ffe48a 0%, #ffcf4a 50%, #e09a10 100%); }
.title-sub {
  margin-top: 10px; padding: 3px 14px; border-radius: 999px; transform: rotate(2deg);
  font-family: "Kanit", sans-serif; font-size: clamp(13px, 3.8vw, 16px); font-weight: 600; color: #fff;
  background: var(--green); border: 2px solid #c9f5d9; box-shadow: 0 3px 0 var(--green-dark);
}
#hero { width: min(78vw, 300px); height: min(70vw, 270px); flex-shrink: 1; min-height: 150px; }

.btn-stack { display: grid; gap: 12px; width: 100%; max-width: 320px; }
.btn {
  position: relative; display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  font-size: 24px; font-weight: 800; color: #fff; border: none; border-radius: 999px;
  padding: 12px 28px; cursor: pointer; width: 100%; white-space: nowrap; text-decoration: none;
  text-shadow: 0 2px 0 rgba(0, 0, 0, .25);
  transition: transform .08s, filter .15s;
}
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(4px) scale(.98); }
.btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn-row .btn { padding-left: 10px; padding-right: 10px; }
.btn .arrow { font-size: .8em; }
.btn-red {
  background: linear-gradient(180deg, #ff6b6b 0%, #e32b3a 55%, #c41a2a 100%);
  border: 3px solid #ffd9d9;
  box-shadow: 0 6px 0 var(--red-dark), 0 10px 24px rgba(227, 43, 58, .45);
}
.btn-red:active { box-shadow: 0 2px 0 var(--red-dark); }
.btn-green {
  font-size: 20px; background: linear-gradient(180deg, #3fcf86, #1f9d5c);
  border: 3px solid #c9f5d9; box-shadow: 0 6px 0 var(--green-dark), 0 10px 24px rgba(31, 157, 92, .4);
}
.btn-green:active { box-shadow: 0 2px 0 var(--green-dark); }
.btn-sm { font-size: 18px; padding: 10px 18px; }
.btn-line {
  background: #06c755; border: 3px solid #c9f5d9;
  box-shadow: 0 6px 0 #04893b, 0 10px 24px rgba(6, 199, 85, .4);
}
.btn-line:active { box-shadow: 0 2px 0 #04893b; }
.link-btn { background: none; border: none; color: var(--ice); font-size: 15px; margin-top: 12px; cursor: pointer; text-decoration: underline; text-underline-offset: 3px; }
.best { font-size: 13px; color: var(--ice); }

/* Panel card */
.panel {
  width: 100%; max-width: 400px; margin: auto; text-align: center;
  background: linear-gradient(180deg, rgba(18, 52, 92, .96), rgba(8, 24, 48, .96));
  border: 3px solid var(--gold); border-radius: 26px; padding: 20px 18px;
  box-shadow: 0 0 0 4px rgba(227, 43, 58, .75), 0 16px 40px rgba(0, 0, 0, .6), inset 0 0 30px rgba(185, 212, 255, .15);
}
.panel > .btn { margin-top: 10px; }
.panel-title { font-family: "Mitr", sans-serif; font-size: 28px; color: var(--gold); text-shadow: 0 3px 0 #7a0d18; margin-bottom: 6px; }
.lead { font-size: 15px; margin-bottom: 10px; }
.hl { color: var(--gold); }
.muted { color: var(--ice); font-size: 14px; }

/* Rules */
.rule-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.rule-grid figure {
  background: rgba(0, 0, 0, .25); border: 2px solid rgba(185, 212, 255, .25); border-radius: 14px;
  padding: 6px 2px 4px; display: flex; flex-direction: column; align-items: center;
}
.rule-grid figure.rare {
  position: relative; overflow: hidden;
  border-color: var(--gold);
  background: radial-gradient(circle at 50% 38%, rgba(255, 207, 74, .45), rgba(120, 70, 10, .35) 70%);
  animation: rareGlow 1.6s ease-in-out infinite;
}
.rule-grid figure.rare::after {
  content: ''; position: absolute; top: -20%; bottom: -20%; left: -60%; width: 40%;
  background: linear-gradient(100deg, transparent, rgba(255, 255, 255, .55), transparent);
  transform: skewX(-18deg); animation: rareShine 2.4s ease-in-out infinite; pointer-events: none;
}
.rule-grid figure.rare canvas { animation: float 2.2s ease-in-out infinite; }
.rule-grid figure.rare figcaption { color: #fff3c4; font-weight: 600; }
.rare-tag {
  position: absolute; top: 3px; right: -18px; z-index: 1; transform: rotate(35deg);
  width: 64px; text-align: center; font-size: 9px; font-weight: 800; line-height: 14px;
  background: linear-gradient(90deg, #ff4f5e, #c41a2a); color: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .4);
}
@keyframes rareGlow {
  0%, 100% { box-shadow: 0 0 6px rgba(255, 207, 74, .5), inset 0 0 10px rgba(255, 207, 74, .3); }
  50% { box-shadow: 0 0 18px rgba(255, 207, 74, .95), inset 0 0 18px rgba(255, 207, 74, .55); }
}
@keyframes rareShine { 0%, 40% { left: -60%; } 100% { left: 130%; } }
@keyframes float { 50% { transform: translateY(-6px) rotate(2deg); } }
@media (prefers-reduced-motion: reduce) {
  .rule-grid figure.rare, .rule-grid figure.rare::after, .rule-grid figure.rare canvas { animation: none; }
}
.rule-grid canvas { width: 58px; height: 58px; }
.rule-grid figcaption { font-size: 12px; line-height: 1.25; display: flex; flex-direction: column; }
.pt { font-size: 15px; font-weight: 800; }
.pt.good { color: var(--good); } .pt.gold { color: var(--gold); }
.steps { list-style: none; text-align: left; display: grid; gap: 6px; margin: 14px 0 6px; font-size: 14px; }
.steps li { display: flex; gap: 8px; align-items: flex-start; }
.steps span {
  flex: none; width: 22px; height: 22px; border-radius: 50%; background: var(--red); color: #fff;
  font-weight: 800; font-size: 13px; display: grid; place-items: center; margin-top: 1px;
}

/* End */
.end-emoji { font-size: 56px; line-height: 1; margin-bottom: 4px; animation: bob 1.6s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-6px) rotate(-4deg); } }
#endMsg { font-size: 17px; margin: 4px 0 10px; }
.final { display: flex; flex-direction: column; align-items: center; margin-bottom: 4px; }
.final small { font-size: 14px; color: var(--ice); }
#finalScore { font-size: 56px; font-weight: 800; color: var(--gold); line-height: 1; text-shadow: 0 4px 0 #7a0d18; }
.panel-end .best { margin-bottom: 4px; }
.new-best {
  display: inline-block; margin: 2px 0 6px; padding: 3px 14px; border-radius: 999px; font-weight: 800;
  background: linear-gradient(90deg, #e32b3a, #ffcf4a); color: #4a1000; box-shadow: 0 0 20px rgba(255, 207, 74, .7);
  animation: popIn .5s cubic-bezier(.2, 1.6, .4, 1);
}
@keyframes popIn { from { opacity: 0; transform: scale(.3); } }
.end-rank { font-size: 16px; font-weight: 600; color: var(--ice); margin-bottom: 4px; animation: fadeIn .4s ease; }
.end-rank.up { color: var(--good); }
.lead-cta { width: 100%; margin-top: 16px; padding-top: 14px; border-top: 1px dashed rgba(185, 212, 255, .35); }
.lead-cta p { font-size: 15px; font-weight: 600; color: var(--ice); margin-bottom: 10px; }

/* ---------- Photo booth ---------- */
.screen-photo { justify-content: flex-start; background: rgba(5, 13, 30, .9); }
.photo-wrap { width: 100%; max-width: 400px; margin: auto 0; display: flex; flex-direction: column; align-items: center; gap: 12px; flex-shrink: 0; }
.photo-stage {
  position: relative; width: min(100%, 52vh); aspect-ratio: 4 / 5; border-radius: 18px; overflow: hidden;
  box-shadow: 0 0 0 3px var(--gold), 0 14px 36px rgba(0, 0, 0, .6);
}
#photoCanvas { width: 100%; height: 100%; display: block; }
#photoVideo { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.photo-count {
  position: absolute; inset: 0; display: grid; place-items: center;
  font-family: "Mitr", sans-serif; font-size: 120px; color: #fff; text-shadow: 0 6px 0 #7a0d18, 0 0 30px rgba(255, 207, 74, .8);
  animation: popIn .5s cubic-bezier(.2, 1.6, .4, 1);
}
.photo-hint {
  position: absolute; left: 10px; right: 10px; top: 42%; padding: 10px; border-radius: 12px;
  background: rgba(0, 0, 0, .65); font-size: 14px; text-align: center;
}
.photo-actions { width: 100%; display: grid; gap: 10px; justify-items: center; }
.pair-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; width: 100%; }
.pair-actions .btn { padding-left: 10px; padding-right: 10px; }
.file-btn { margin-top: 0; }
.profile-form {
  width: 100%; display: grid; gap: 8px; text-align: left;
  background: rgba(18, 52, 92, .75); border: 2px solid rgba(255, 207, 74, .45); border-radius: 18px; padding: 12px 14px;
}
.form-title { font-weight: 600; color: var(--gold); text-align: center; }
.nick-input {
  font: inherit; font-size: 18px; width: 100%; padding: 10px 14px; border-radius: 12px;
  border: 2px solid var(--ice); background: #061226; color: var(--snow); user-select: text; -webkit-user-select: text;
}
.consent { font-size: 13px; color: var(--ice); display: flex; gap: 8px; align-items: center; line-height: 1.4; white-space: nowrap; overflow: hidden; }
.consent input { width: 18px; height: 18px; flex: none; margin: 0; accent-color: var(--red); }
.form-msg { font-size: 13px; color: var(--good); min-height: 1em; text-align: center; }
.form-msg.bad { color: var(--bad); }
.profile-form .btn:disabled { filter: grayscale(.6) brightness(.8); }

/* ---------- Leaderboard ---------- */
.avatar {
  flex: none; width: 40px; height: 40px; border-radius: 50%; overflow: hidden; display: grid; place-items: center;
  background: #12284a; border: 2px solid var(--ice); font-size: 22px;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.viewable { cursor: zoom-in; transition: transform .15s; }
.avatar.viewable:active { transform: scale(.92); }
.photo-viewer {
  position: absolute; inset: 0; z-index: 30; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  padding: 24px 16px; background: rgba(3, 8, 20, .9); backdrop-filter: blur(4px); text-align: center; cursor: zoom-out;
  animation: viewerIn .18s ease-out;
}
.photo-viewer img {
  width: min(84vw, 360px, 60vh); aspect-ratio: 1; object-fit: cover; border-radius: 24px;
  border: 4px solid var(--gold); box-shadow: 0 0 30px rgba(255, 207, 74, .5); background: var(--deep);
}
.photo-viewer h3 { margin-top: 6px; font-size: 22px; color: var(--snow); max-width: 100%; overflow-wrap: anywhere; }
.photo-viewer p { color: var(--gold); font-weight: 700; }
@keyframes viewerIn { from { opacity: 0; transform: scale(.96); } }
.avatar.lg { width: 68px; height: 68px; font-size: 36px; border-width: 3px; }
.podium { display: grid; grid-template-columns: repeat(3, 1fr); align-items: end; gap: 6px; margin: 4px 0 10px; }
.podium:empty { display: none; }
.podium-spot { position: relative; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 4px; border-radius: 16px; background: rgba(0, 0, 0, .28); min-width: 0; }
.podium-spot.p1 { grid-area: 1 / 2; padding-top: 4px; background: linear-gradient(180deg, rgba(255, 207, 74, .35), rgba(0, 0, 0, .28)); }
.podium-spot.p1 .avatar { border-color: var(--gold); box-shadow: 0 0 16px rgba(255, 207, 74, .8); }
.podium-spot.p2 { grid-area: 1 / 1; }
.podium-spot.p3 { grid-area: 1 / 3; }
.podium-spot .medal { font-size: 22px; line-height: 1; margin-top: -12px; }
.podium-spot .name { font-size: 13px; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.podium-spot b { color: var(--gold); font-size: 18px; }
.podium-spot.me, .board-row.me { outline: 2px solid var(--red); }
.board-list { list-style: none; display: grid; gap: 6px; max-height: 34vh; overflow-y: auto; margin-bottom: 8px; text-align: left; }
.board-row { display: flex; align-items: center; gap: 10px; padding: 6px 10px; border-radius: 14px; background: rgba(0, 0, 0, .25); }
.board-row .rank { width: 26px; text-align: center; font-weight: 800; color: var(--ice); }
.board-row .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board-row .pts { color: var(--gold); font-size: 18px; }
#boardMe { margin-bottom: 4px; background: rgba(227, 43, 58, .2); }
.board-rank { display: flex; align-items: baseline; justify-content: center; flex-wrap: wrap; gap: 4px 8px; padding: 8px 14px; margin: 4px 0 12px; border-radius: 999px; background: rgba(227, 43, 58, .18); border: 1px solid rgba(227, 43, 58, .6); color: var(--ice); font-weight: 600; }
.board-rank b { color: var(--gold); font-size: 24px; line-height: 1; }
.board-rank small { color: var(--gold); font-size: 13px; }
.report-btn {
  flex: none; width: 28px; height: 28px; border-radius: 50%; border: none; cursor: pointer;
  background: rgba(255, 255, 255, .08); color: var(--ice); font-size: 13px;
}
.report-btn:disabled { color: var(--good); cursor: default; }
.podium-spot .report-btn { position: absolute; top: 4px; right: 4px; width: 22px; height: 22px; font-size: 11px; }

/* ---------- Profile ---------- */
.profile-body { margin: 6px 0 14px; }
.profile-head { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.profile-head h3 { font-size: 22px; }
.avatar.xl { width: 104px; height: 104px; font-size: 56px; border-width: 4px; border-color: var(--gold); box-shadow: 0 0 20px rgba(255, 207, 74, .6); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 14px; }
.stats div { background: rgba(0, 0, 0, .28); border-radius: 14px; padding: 8px 4px; display: flex; flex-direction: column; }
.stats b { font-size: 22px; color: var(--gold); }
.stats small { font-size: 12px; color: var(--ice); }
.link-btn.danger { color: var(--bad); }
.panel-profile .link-btn { display: block; margin: 12px auto 0; }
.legal { margin-top: 10px; font-size: 12px; }
.legal a { color: var(--ice); }

@media (max-height: 640px) {
  #hero { height: 140px; }
  .title-top { font-size: 40px; }
  .title-mid { font-size: 36px; }
  .title-logo { height: 40px; }
  .btn { font-size: 20px; padding: 9px 22px; }
}
@media (max-width: 380px) {
  .hud-box { padding: 2px 4px; }
  .hud-box span:last-child { font-size: 22px; }
  .hud-logo { width: 38px; height: 38px; }
}
