:root {
  --bg: #0a0d12;
  --panel: #121821;
  --line: #233041;
  --text: #e6edf3;
  --muted: #8b98a8;
  --malus: #ff5f6d;
  --bonus: #4ade80;
  --blocked: #6ec8ff;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  background: var(--bg);
  color: var(--text);
}

#stage {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 10px;
  height: 100vh;
}

#hud { display: flex; flex-direction: column; gap: 6px; min-height: 52px; }
.hud-stats { font-weight: 600; font-variant-numeric: tabular-nums; }
.hud-chips { display: flex; flex-wrap: wrap; gap: 6px; }

.chip {
  position: relative;
  overflow: hidden;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 13px;
  border: 1px solid currentColor;
}
.chip.malus { color: var(--malus); }
.chip.bonus { color: var(--bonus); }
.chip-label, .chip-icon { position: relative; z-index: 1; }
.chip-icon { margin-right: 4px; }
.chip-bar {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 3px;
  background: currentColor;
  transition: width 0.2s linear;
}

#board-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#board { display: block; border-radius: 8px; }
#board.tilt {
  border-radius: 0;
  transform: perspective(1600px) rotateX(20deg);
  transform-origin: 50% 65%;
}
#board-wrap { perspective-origin: 50% 0; }

#announcements {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  width: min(94%, 540px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.announce {
  --accent: var(--malus);
  --glow: rgba(255, 95, 109, 0.45);
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px 8px 8px;
  border-radius: 18px;
  background: linear-gradient(135deg, rgba(28, 16, 22, 0.94), rgba(12, 14, 20, 0.94));
  border: 2px solid var(--accent);
  box-shadow: 0 0 22px var(--glow), 0 8px 24px rgba(0, 0, 0, 0.5);
  font-size: clamp(15px, 2.3vw, 21px);
  animation: announce-in 0.5s cubic-bezier(0.2, 1.5, 0.4, 1), announce-out 0.4s ease-in 2.8s forwards;
}
.announce.bonus {
  --accent: var(--bonus);
  --glow: rgba(74, 222, 128, 0.4);
  background: linear-gradient(135deg, rgba(14, 28, 20, 0.94), rgba(12, 14, 20, 0.94));
}
.announce.blocked {
  --accent: var(--blocked);
  --glow: rgba(110, 200, 255, 0.45);
  background: linear-gradient(135deg, rgba(12, 24, 34, 0.94), rgba(12, 14, 20, 0.94));
}
.announce.malus:not(.blocked) {
  animation: announce-in 0.5s cubic-bezier(0.2, 1.5, 0.4, 1), announce-shake 0.35s 0.5s, announce-out 0.4s ease-in 2.8s forwards;
}
/* Light sweep across the card. */
.announce::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(100deg, transparent 30%, rgba(255, 255, 255, 0.18) 50%, transparent 70%);
  transform: translateX(-120%);
  animation: announce-shine 0.9s 0.25s ease-out;
}

.announce-icon {
  flex-shrink: 0;
  display: grid;
  place-items: center;
  width: 2.3em;
  height: 2.3em;
  border-radius: 50%;
  font-size: 1.25em;
  background: radial-gradient(circle at 35% 30%, rgba(255, 255, 255, 0.35), transparent 60%), var(--accent);
  box-shadow: inset 0 -3px 6px rgba(0, 0, 0, 0.3), 0 0 14px var(--glow);
  animation: icon-pop 0.6s cubic-bezier(0.2, 1.8, 0.4, 1) 0.1s both;
}
.announce-body { min-width: 0; text-align: left; }
.announce-tag {
  font-size: 0.55em;
  font-weight: 800;
  letter-spacing: 0.14em;
  color: var(--accent);
}
.announce-line { line-height: 1.2; }
.announce-user { color: #ffd166; overflow-wrap: anywhere; }
.announce-effect { color: var(--accent); letter-spacing: 0.02em; }
.announce-sub { font-size: 0.7em; color: var(--blocked); margin-top: 2px; }
.announce-combo {
  margin-left: auto;
  padding: 2px 10px;
  border-radius: 999px;
  font-weight: 900;
  color: #1a0b0e;
  background: linear-gradient(180deg, #ffd166, #ff8a3d);
  animation: icon-pop 0.5s cubic-bezier(0.2, 1.8, 0.4, 1) 0.2s both;
}

.announce.banner {
  display: block;
  padding: 10px 22px;
  text-align: center;
  font-weight: 900;
  letter-spacing: 0.05em;
}
.announce.banner.complete { --accent: #ffd166; --glow: rgba(255, 209, 102, 0.5); color: #ffd166; }
.announce.banner.combo {
  --accent: #ff8a3d;
  --glow: rgba(255, 138, 61, 0.6);
  font-size: clamp(22px, 4vw, 34px);
  color: #ffd166;
  text-shadow: 0 2px 0 #b3401a, 0 0 18px rgba(255, 138, 61, 0.8);
  background: none;
  border: 0;
  box-shadow: none;
  animation: combo-in 0.45s cubic-bezier(0.2, 1.8, 0.4, 1), announce-out 0.35s ease-in 1.4s forwards;
}
.announce.banner.combo::after { display: none; }

@keyframes announce-in {
  from { opacity: 0; transform: translateY(-18px) scale(0.8); }
  to { opacity: 1; transform: none; }
}
@keyframes announce-shake {
  25% { transform: translateX(-6px) rotate(-1.2deg); }
  75% { transform: translateX(6px) rotate(1.2deg); }
}
@keyframes announce-out {
  to { opacity: 0; transform: translateY(-8px) scale(0.95); }
}
@keyframes announce-shine {
  to { transform: translateX(120%); }
}
@keyframes icon-pop {
  from { transform: scale(0) rotate(-40deg); }
  to { transform: none; }
}
@keyframes combo-in {
  from { opacity: 0; transform: scale(2.2) rotate(-6deg); }
  to { opacity: 1; transform: rotate(-3deg); }
}

#panel {
  width: 340px;
  flex-shrink: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 16px;
  background: var(--panel);
  border-left: 1px solid var(--line);
}
#panel h2 { margin: 0 0 12px; font-size: 18px; }
#panel h3 { margin: 16px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }

.sim-row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.sim-row input:not([type="checkbox"]), .sim-row select { flex: 1; min-width: 0; }
.sim-check { font-size: 13px; color: var(--muted); white-space: nowrap; }

input, select, button {
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: #0d131b;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 10px;
}
button { cursor: pointer; background: #1a2330; }
button:hover { background: #223043; }
button:active { transform: scale(0.97); }

.sim-effects { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.sim-effect { display: flex; flex-direction: column; align-items: flex-start; text-align: left; border-left: 3px solid var(--malus); }
.sim-effect.bonus { border-left-color: var(--bonus); }
.sim-effect small { color: var(--muted); font-size: 11px; }

.sim-queue, .sim-log { margin: 0; padding-left: 20px; font-size: 13px; color: var(--muted); }
.sim-log { list-style: none; padding: 0; }
.sim-log li { padding: 3px 0; border-bottom: 1px solid var(--line); overflow-wrap: anywhere; }
.sim-log .malus { color: var(--malus); }
.sim-log .bonus { color: var(--bonus); }
.sim-log .ok { color: var(--text); }
.sim-log .rejected { color: #f0a05a; }

@media (max-width: 860px) {
  body { flex-direction: column; }
  #stage { height: 80vh; }
  #panel { width: 100%; height: auto; border-left: 0; border-top: 1px solid var(--line); }
}

@media (prefers-reduced-motion: reduce) {
  .announce, .announce.malus:not(.blocked), .announce.banner.combo { animation: announce-out 0.4s ease-in 2.6s forwards; }
  .announce::after, .announce-icon, .announce-combo { animation: none; }
}

/* Start menu */
.start-menu {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  overflow-y: auto;
  background: radial-gradient(circle at 50% 20%, #17263a, var(--bg) 70%);
  animation: menu-in 0.35s ease-out;
}
.start-menu.leaving { animation: menu-out 0.25s ease-in forwards; }
.start-card {
  width: min(100%, 560px);
  padding: 28px 24px;
  border-radius: 22px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  text-align: center;
}
.start-logo { font-size: 44px; animation: icon-pop 0.6s cubic-bezier(0.2, 1.8, 0.4, 1) both; }
.start-card h1 { margin: 4px 0 2px; font-size: 32px; letter-spacing: 0.02em; }
.start-sub { margin: 0 0 20px; color: var(--muted); }
.start-presets { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.start-preset {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 12px 6px;
  border-radius: 14px;
  border: 2px solid var(--line);
  transition: transform 0.15s, border-color 0.15s, background 0.15s;
}
.start-preset:hover { transform: translateY(-2px); }
.start-preset.selected { border-color: var(--bonus); background: #173024; }
.start-preset small { color: var(--muted); font-size: 11px; }
.start-mini {
  width: 48px;
  height: 48px;
  margin-bottom: 6px;
  border-radius: 6px;
  background-color: #16202b;
  background-image:
    linear-gradient(90deg, rgba(124, 240, 176, 0.35) 1px, transparent 1px),
    linear-gradient(rgba(124, 240, 176, 0.35) 1px, transparent 1px);
  background-size: calc(48px / var(--n)) calc(48px / var(--n));
  border: 1px solid #2c3b4f;
}
.start-custom {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 18px;
  color: var(--muted);
}
.start-custom input { width: 72px; text-align: center; }
.start-cells { margin: 0; font-size: 13px; }
.start-error { min-height: 1.3em; margin: 10px 0 4px; color: #f0a05a; font-size: 14px; }
.start-go {
  width: 100%;
  margin-top: 6px;
  padding: 12px;
  font-size: 18px;
  font-weight: 800;
  border: 0;
  border-radius: 14px;
  color: #08140d;
  background: linear-gradient(180deg, #7cf0b0, #3fbf7f);
}
.start-go:hover { background: linear-gradient(180deg, #8ff5bd, #47cc89); }
.start-go:disabled { opacity: 0.4; cursor: not-allowed; }
@keyframes menu-in { from { opacity: 0; } }
@keyframes menu-out { to { opacity: 0; transform: scale(1.03); } }
@media (max-width: 520px) {
  .start-presets { grid-template-columns: repeat(2, 1fr); }
}

/* Like goal bar */
.like-goal {
  position: relative;
  overflow: hidden;
  max-width: 420px;
  height: 24px;
  border-radius: 999px;
  background: #1a1220;
  border: 1px solid #5a2a44;
}
.like-goal[hidden] { display: none; }
.like-fill {
  position: absolute;
  inset: 0 auto 0 0;
  background: linear-gradient(90deg, #ff3d7f, #ff8fb5);
  transition: width 0.3s ease-out;
}
.like-text {
  position: relative;
  display: block;
  padding: 0 12px;
  line-height: 22px;
  font-size: 13px;
  font-weight: 700;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
}

/* Live scene (?live=1): fixed 1080x1920, scaled by main.js.
   Top ~170px and bottom ~300px stay clear for TikTok's own UI and comments. */
body.live {
  overflow: hidden;
  background: radial-gradient(circle at 50% 0%, #13253a, #06090f 60%);
}
body.live #stage {
  position: absolute;
  top: 0;
  left: 0;
  flex: none;
  width: 1080px;
  height: 1920px;
  padding: 170px 24px 250px;
  gap: 14px;
  transform-origin: 0 0;
}
body.live #hud { min-height: 0; gap: 10px; }
/* The tiles and like bar say it all: drop the stats line on stream. */
body.live .hud-stats { display: none; }
body.live .like-goal { max-width: none; height: 44px; border-width: 2px; }
body.live .like-text { line-height: 40px; font-size: 24px; padding: 0 18px; }
body.live .chip { font-size: 20px; padding: 4px 14px; }
body.live .announce { font-size: 30px; }
body.live .announce.banner.combo { font-size: 52px; }
body.live #announcements { width: 860px; }

#live-legend {
  flex-shrink: 0;
  padding: 14px 14px 16px;
  border-radius: 28px;
  background: linear-gradient(180deg, rgba(30, 32, 48, 0.92), rgba(18, 20, 31, 0.92));
  border: 1px solid rgba(255, 255, 255, 0.09);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.06);
}
.live-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0 4px 12px;
}
.live-title { font-size: 25px; font-weight: 800; }
.live-keys { display: flex; gap: 8px; }
.key {
  padding: 3px 12px;
  border-radius: 999px;
  font-size: 15px;
  font-weight: 900;
  letter-spacing: 0.08em;
}
.key.malus { color: #12060a; background: var(--malus); }
.key.bonus { color: #04140a; background: var(--bonus); }
.live-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}
.gift-tile {
  --accent: var(--malus);
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
  padding: 8px 6px 8px;
  border-radius: 18px;
  text-align: center;
  background:
    radial-gradient(circle at 50% 0%, color-mix(in srgb, var(--accent) 22%, transparent), transparent 70%),
    rgba(255, 255, 255, 0.045);
  border: 2px solid color-mix(in srgb, var(--accent) 35%, transparent);
}
.gift-tile.bonus { --accent: var(--bonus); }
.gift-tile.info.combo { --accent: var(--gold, #ffd166); }
.gift-tile.info.like { --accent: #ff4f8b; }
.tile-top { display: flex; align-items: center; justify-content: center; gap: 6px; height: 60px; }
.gift-art { width: 60px; height: 60px; flex-shrink: 0; filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.45)); }
.gift-art svg, .gift-art img { display: block; width: 100%; height: 100%; object-fit: contain; }
.tile-arrow { font-size: 18px; color: rgba(255, 255, 255, 0.45); }
.effect-badge {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  font-size: 21px;
  background: radial-gradient(circle at 35% 30%, rgba(255, 255, 255, 0.35), transparent 60%), var(--accent);
  box-shadow: inset 0 -3px 6px rgba(0, 0, 0, 0.3);
}
.info-icon { display: grid; place-items: center; height: 60px; font-size: 46px; }
.gift-effect {
  margin-top: 2px;
  max-width: 100%;
  font-size: 20px;
  font-weight: 800;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.gift-short { font-size: 15px; color: var(--muted); line-height: 1.2; }
.tile-foot {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  max-width: 100%;
  margin-top: 5px;
  padding: 2px 10px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.3);
}
.gift-name {
  min-width: 0;
  font-size: 15px;
  color: rgba(255, 255, 255, 0.75);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.gift-price { display: flex; align-items: center; gap: 4px; font-size: 17px; font-weight: 900; color: #ffe08a; }
.coin {
  width: 17px;
  height: 17px;
  flex-shrink: 0;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff3c4, #f5b301 55%, #b37800);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.35);
}
.gift-tile.hit {
  border-color: var(--accent);
  box-shadow: 0 0 34px color-mix(in srgb, var(--accent) 80%, transparent);
  animation: tile-hit 0.9s cubic-bezier(0.2, 1.6, 0.4, 1);
}
.gift-tile.hit .gift-art { animation: art-wiggle 0.7s ease-in-out; }
@keyframes tile-hit {
  30% { transform: scale(1.12) rotate(-2deg); }
  60% { transform: scale(0.97); }
}
@keyframes art-wiggle {
  25% { transform: rotate(-14deg) scale(1.15); }
  75% { transform: rotate(10deg) scale(1.1); }
}
@media (prefers-reduced-motion: reduce) {
  .gift-tile.hit, .gift-tile.hit .gift-art { animation: none; }
}
.start-live {
  display: block;
  margin-top: 10px;
  padding: 11px;
  border-radius: 14px;
  font-weight: 800;
  text-decoration: none;
  color: var(--text);
  background: #1a2330;
  border: 2px solid #ff4f8b;
  transition: background 0.15s;
}
.start-live:hover { background: #2a1a26; }
.start-live.disabled { opacity: 0.4; pointer-events: none; }
.start-hint { margin: 6px 0 0; font-size: 12px; color: var(--muted); }
#panel .sim-row { flex-wrap: wrap; }

/* TikFinity connection pill (shown while disconnected). */
#bridge-status {
  position: absolute;
  top: 110px;
  right: 24px;
  z-index: 5;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 15px;
  font-weight: 800;
  background: rgba(40, 14, 20, 0.9);
  border: 2px solid var(--malus);
}
#bridge-status[data-status="open"] { background: rgba(10, 36, 22, 0.9); border-color: var(--bonus); }
#bridge-status[hidden] { display: none; }
body.live #bridge-status { font-size: 22px; padding: 10px 20px; }
#stage { position: relative; }
