:root {
  --ink: #1d1b2c;
  --paper: #fff7e8;
  --pink: #ff4f8b;
  --pink-dark: #d8346c;
  --yellow: #ffd23f;
  --blue: #3fb6ff;
  --green: #48d597;
  --muted: #6d6880;
  --font: "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", "Heiti TC", sans-serif;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  background: #2a2340 radial-gradient(circle at 50% 0, #4a3466, #1a1528 70%) fixed;
  color: var(--ink);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

[hidden] {
  display: none !important;
}

#app {
  position: relative;
  max-width: 480px;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  background: var(--paper);
  overflow: hidden;
  box-shadow: 0 0 40px rgba(0, 0, 0, .5);
}

.screen {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}

.screen.center {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  text-align: center;
  background:
    repeating-conic-gradient(from 0deg at 50% 30%, #ffe1ec 0 10deg, #fff3d6 10deg 20deg);
}

/* ---- 共用元件 ---- */

.card {
  width: 100%;
  max-width: 380px;
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 18px;
  box-shadow: 5px 5px 0 var(--ink);
  padding: 20px;
  text-align: left;
}

.card h2 {
  margin-bottom: 10px;
  text-align: center;
}

.card p {
  margin-bottom: 10px;
  line-height: 1.6;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 380px;
}

.btn {
  font: inherit;
  font-weight: 900;
  font-size: 18px;
  min-height: 52px;
  padding: 10px 16px;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: #fff;
  color: var(--ink);
  box-shadow: 4px 4px 0 var(--ink);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform .06s, box-shadow .06s;
}

.btn:active {
  transform: translate(3px, 3px);
  box-shadow: 1px 1px 0 var(--ink);
}

.btn.primary {
  background: var(--pink);
  color: #fff;
}

.btn.yellow {
  background: var(--yellow);
}

.btn.ghost {
  background: transparent;
  box-shadow: none;
  border-color: transparent;
  text-decoration: underline;
}

.btn.small {
  min-height: 40px;
  font-size: 15px;
  padding: 6px 12px;
  box-shadow: 3px 3px 0 var(--ink);
}

.btn.ghost.small {
  box-shadow: none;
}

.btn.icon {
  width: 40px;
  padding: 0;
}

.muted {
  color: var(--muted);
}

.error {
  min-height: 1.4em;
  color: #e0244f;
  font-size: 14px;
  margin: 4px 0;
}

.footnote {
  font-size: 12px;
  color: var(--muted);
}

label {
  display: block;
  font-weight: 700;
  margin-bottom: 6px;
}

input {
  width: 100%;
  font: inherit;
  font-size: 18px;
  padding: 12px 14px;
  border: 3px solid var(--ink);
  border-radius: 12px;
  outline: none;
}

input:focus {
  border-color: var(--pink);
}

.badge {
  width: 64px;
  height: 64px;
  margin: 0 auto 8px;
  display: grid;
  place-items: center;
  border: 3px solid var(--ink);
  border-radius: 50%;
  background: #e0244f;
  color: #fff;
  font-weight: 900;
  font-size: 22px;
}

/* ---- 主畫面 ---- */

.logo h1 {
  font-size: clamp(56px, 18vw, 84px);
  font-weight: 900;
  line-height: 1;
  color: var(--pink);
  letter-spacing: 2px;
  -webkit-text-stroke: 3px var(--ink);
  paint-order: stroke fill;
  text-shadow: 5px 5px 0 var(--ink);
  transform: rotate(-4deg);
}

.logo-sub {
  display: inline-block;
  margin-bottom: 8px;
  padding: 4px 14px;
  background: var(--yellow);
  border: 3px solid var(--ink);
  border-radius: 999px;
  font-weight: 900;
  transform: rotate(-4deg);
}

/* ---- 選關、排行榜 ---- */

.bar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.stage-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 4px;
  text-align: left;
  font: inherit;
  padding: 16px;
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 16px;
  box-shadow: 4px 4px 0 var(--ink);
  cursor: pointer;
}

.stage-card b {
  font-size: 19px;
}

.stage-card span {
  color: var(--muted);
}

.stage-card em {
  position: absolute;
  top: 12px;
  right: 12px;
  font-style: normal;
  font-weight: 900;
  font-size: 13px;
  padding: 2px 10px;
  background: var(--green);
  border: 2px solid var(--ink);
  border-radius: 999px;
  transform: rotate(6deg);
}

.stage-card:disabled {
  opacity: .5;
  cursor: not-allowed;
}

.pad {
  margin-bottom: 12px;
}

.board {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.board li {
  display: grid;
  grid-template-columns: 40px 1fr auto;
  grid-template-rows: auto auto;
  align-items: center;
  column-gap: 8px;
  padding: 10px 14px;
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 14px;
}

.board li.me {
  background: #ffe9a8;
}

.board li.empty {
  display: block;
  text-align: center;
  color: var(--muted);
}

.rank-no {
  grid-row: span 2;
  font-size: 20px;
  font-weight: 900;
  text-align: center;
}

.board .name {
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.board b {
  font-size: 18px;
  text-align: right;
}

.board small {
  grid-column: 2 / 4;
  color: var(--muted);
}

/* ---- 遊戲畫面 ---- */

.screen.game {
  display: flex;
  flex-direction: column;
  padding: max(8px, env(safe-area-inset-top)) 10px max(10px, env(safe-area-inset-bottom));
  gap: 8px;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

#hud {
  display: flex;
  flex-direction: column;
  gap: 5px;
  position: relative;
}

.hud-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tag {
  padding: 3px 10px;
  background: var(--ink);
  color: #fff;
  border-radius: 999px;
  font-weight: 700;
  font-size: 14px;
}

.time {
  flex: 1;
  text-align: right;
  font-weight: 900;
  font-size: 22px;
  font-variant-numeric: tabular-nums;
}

.meter {
  display: grid;
  grid-template-columns: 52px 1fr 34px;
  align-items: center;
  gap: 6px;
  font-weight: 900;
  font-size: 14px;
}

.meter-bar {
  height: 18px;
  border: 3px solid var(--ink);
  border-radius: 999px;
  background: #fff;
  overflow: hidden;
}

.fill {
  height: 100%;
}

.fill.excite {
  background: linear-gradient(90deg, #ffb3cc, var(--pink) 60%, #e0244f);
}

.fill.stamina {
  background: linear-gradient(90deg, #9ee8c4, var(--green));
}

.meter-val {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.meter.danger .meter-bar {
  animation: danger .3s infinite alternate;
}

@keyframes danger {
  to {
    border-color: #e0244f;
    box-shadow: 0 0 10px #ff3b5c;
  }
}

#event-banner {
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  margin-top: 6px;
  z-index: 3;
  padding: 6px 10px;
  border: 3px solid var(--ink);
  border-radius: 12px;
  font-weight: 900;
  font-size: 14px;
  text-align: center;
  box-shadow: 3px 3px 0 var(--ink);
}

#event-banner.warn {
  background: var(--yellow);
  animation: shake .15s infinite;
}

#event-banner.bad {
  background: #ff3b5c;
  color: #fff;
}

#event-banner.good {
  background: var(--green);
}

@keyframes shake {
  50% {
    transform: translateX(3px);
  }
}

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

canvas {
  display: block;
  border: 3px solid var(--ink);
  border-radius: 16px;
  background: #ffc4db;
}

#toast {
  position: absolute;
  left: 50%;
  bottom: 10px;
  max-width: 92%;
  transform: translate(-50%, 20px);
  padding: 8px 14px;
  background: var(--ink);
  color: #fff;
  border-radius: 12px;
  font-weight: 700;
  font-size: 15px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}

#toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

#quiz {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 8px;
  z-index: 2;
  padding: 10px;
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 14px;
  box-shadow: 4px 4px 0 var(--ink);
}

.quiz-head {
  font-weight: 900;
  font-size: 17px;
  text-align: center;
}

#quiz-text {
  color: var(--pink-dark);
  font-size: 22px;
}

.quiz-timer {
  height: 8px;
  margin: 6px 0 8px;
  background: #eee;
  border-radius: 999px;
  overflow: hidden;
}

#quiz-timer-fill {
  height: 100%;
  background: var(--blue);
}

#quiz-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

#controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#controls .row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.ctl {
  font: inherit;
  font-weight: 900;
  font-size: 15px;
  min-height: 52px;
  padding: 6px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  background: #fff;
  color: var(--ink);
  border: 3px solid var(--ink);
  border-radius: 14px;
  box-shadow: 3px 3px 0 var(--ink);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform .05s, box-shadow .05s;
}

.ctl:active {
  transform: translate(2px, 2px);
  box-shadow: 1px 1px 0 var(--ink);
}

.ctl small {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
}

.ctl.breathe {
  min-height: 72px;
  font-size: 24px;
  background: var(--blue);
  color: #fff;
}

.ctl.breathe small {
  color: rgba(255, 255, 255, .85);
}

.ctl.item {
  min-height: 46px;
  font-size: 13px;
  background: #fff3c4;
}

.ctl.dim {
  opacity: .45;
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(29, 27, 44, .6);
}

/* ---- 結算 ---- */

.stamp {
  font-size: 72px;
  font-weight: 900;
  color: #ff3b5c;
  -webkit-text-stroke: 3px var(--ink);
  paint-order: stroke fill;
  text-shadow: 5px 5px 0 var(--ink);
  transform: rotate(-6deg);
  animation: stamp .35s cubic-bezier(.2, 1.6, .5, 1);
}

.stamp.win {
  color: var(--yellow);
}

@keyframes stamp {
  from {
    transform: rotate(-6deg) scale(2.2);
    opacity: 0;
  }
}

.result .stat {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 8px 0;
  border-bottom: 2px dashed #ddd;
}

.result .stat.big b {
  font-size: 32px;
  color: var(--pink-dark);
}

.result .stat b {
  font-size: 20px;
}

.title-tag {
  padding: 2px 12px;
  background: var(--yellow);
  border: 2px solid var(--ink);
  border-radius: 999px;
}

.rank {
  margin: 10px 0 0 !important;
  text-align: center;
  font-weight: 700;
}

/* 矮螢幕（橫向手機、小手機）壓縮操作區 */
@media (max-height: 640px) {
  .ctl.breathe {
    min-height: 56px;
    font-size: 20px;
  }
  .ctl {
    min-height: 46px;
  }
  .ctl.item {
    min-height: 40px;
  }
  .ctl small {
    display: none;
  }
}

/* ---- 全球排行榜 ---- */

.tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-bottom: 10px;
}

.tab {
  font: inherit;
  font-weight: 900;
  font-size: 15px;
  min-height: 42px;
  border: 3px solid var(--ink);
  border-radius: 12px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  touch-action: manipulation;
}

.tab.active {
  background: var(--pink);
  color: #fff;
  box-shadow: 3px 3px 0 var(--ink);
}

.board-me {
  position: sticky;
  bottom: 0;
  margin-top: 10px;
  padding: 12px 14px;
  background: var(--yellow);
  border: 3px solid var(--ink);
  border-radius: 14px;
  font-weight: 900;
  text-align: center;
}

.rank.online {
  color: var(--pink-dark);
}

#connecting {
  z-index: 20;
}

#connecting .card {
  text-align: center;
}

#ts-box {
  display: flex;
  justify-content: center;
  margin: 8px 0;
}
