:root {
  --ink: #1c2430;
  --muted: #5b6573;
  --paper: #ffffff;
  --paper-soft: #f7f8fb;
  --sun: #ffd84d;
  --mint: #2ee6a6;
  --sky: #4db7ff;
  --orange: #ff8f3d;
  --lime: #b6f23a;
  --violet: #7b6cff;
  --panel: #ffffff;
  --stroke: #e6eaf0;
  --shadow: 0 12px 32px rgba(28, 36, 48, 0.08);
  --radius: 22px;
  --font-display: "ZCOOL KuaiLe", "Fredoka", system-ui, sans-serif;
  --font-ui: "Fredoka", "ZCOOL KuaiLe", system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
  color: var(--ink);
  font-family: var(--font-ui);
  overflow-x: hidden;
  touch-action: manipulation;
  background: var(--paper);
}

body {
  background-color: #ffffff;
  background-image:
    radial-gradient(circle at 12% 18%, rgba(77, 183, 255, 0.12), transparent 22%),
    radial-gradient(circle at 88% 12%, rgba(255, 216, 77, 0.16), transparent 20%),
    radial-gradient(circle at 80% 78%, rgba(46, 230, 166, 0.12), transparent 24%),
    radial-gradient(circle at 18% 86%, rgba(255, 143, 61, 0.1), transparent 22%);
}

/* doodle layer */
.doodles {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}

.doodle {
  position: absolute;
  opacity: 0.9;
  animation: doodleFloat 9s ease-in-out infinite;
}

.doodle svg {
  display: block;
  overflow: visible;
}

.d1 { top: 8%; left: 6%; width: 54px; color: #4db7ff; }
.d2 { top: 14%; right: 8%; width: 48px; color: #ffd84d; animation-delay: -2s; }
.d3 { top: 42%; left: 3%; width: 42px; color: #2ee6a6; animation-delay: -4s; }
.d4 { bottom: 18%; right: 6%; width: 58px; color: #ff8f3d; animation-delay: -1.5s; }
.d5 { bottom: 10%; left: 12%; width: 36px; color: #7b6cff; animation-delay: -5s; }
.d6 { top: 58%; right: 10%; width: 44px; color: #b6f23a; animation-delay: -3s; }
.d7 { top: 28%; left: 44%; width: 28px; color: #4db7ff; animation-delay: -6s; opacity: 0.7; }
.d8 { bottom: 32%; left: 8%; width: 50px; color: #ffd84d; animation-delay: -7s; }

@keyframes doodleFloat {
  0%, 100% { transform: translateY(0) rotate(-4deg); }
  50% { transform: translateY(-10px) rotate(4deg); }
}

#app {
  position: relative;
  z-index: 1;
  width: min(560px, 100%);
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
  min-height: 100dvh;
}

.screen {
  display: none;
  animation: rise 0.35s ease both;
}

.screen.active { display: block; }

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== Auth ===== */
.auth-card {
  margin-top: 24px;
  padding: 22px 18px 18px;
  border-radius: 28px;
  background: #fff;
  border: 1.5px solid var(--stroke);
  box-shadow: var(--shadow);
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 16px;
}

.auth-tab {
  appearance: none;
  border: 1.5px solid var(--stroke);
  border-radius: 999px;
  padding: 10px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  background: var(--paper-soft);
  color: var(--muted);
}

.auth-tab.active {
  background: #fff;
  color: var(--ink);
  border-color: #1c2430;
  box-shadow: 3px 3px 0 #ffd84d;
}

.auth-form {
  display: none;
  flex-direction: column;
  gap: 10px;
}

.auth-form.active { display: flex; }

.auth-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.85rem;
  color: var(--muted);
}

.auth-form input {
  appearance: none;
  border: 1.5px solid var(--stroke);
  border-radius: 14px;
  padding: 12px 14px;
  font: inherit;
  font-size: 1rem;
  background: #fff;
  color: var(--ink);
}

.auth-form input:focus {
  outline: none;
  border-color: #4db7ff;
  box-shadow: 0 0 0 3px rgba(77, 183, 255, 0.2);
}

.auth-error {
  min-height: 1.2em;
  margin: 0;
  color: #d35400;
  font-size: 0.88rem;
}

.user-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
  padding: 10px 12px;
  border-radius: 16px;
  background: #fff;
  border: 1.5px solid var(--stroke);
  box-shadow: 3px 3px 0 rgba(77, 183, 255, 0.35);
}

.user-bar .who {
  font-weight: 700;
  font-size: 0.95rem;
}

.user-bar .meta {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  font-weight: 500;
}

.progress-strip {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 14px 0 4px;
}

.progress-pill {
  text-align: center;
  padding: 10px 4px;
  border-radius: 16px;
  background: #fff;
  border: 1.5px solid var(--stroke);
}

.progress-pill:nth-child(1) { box-shadow: 2px 2px 0 #4db7ff; }
.progress-pill:nth-child(2) { box-shadow: 2px 2px 0 #2ee6a6; }
.progress-pill:nth-child(3) { box-shadow: 2px 2px 0 #ffd84d; }
.progress-pill:nth-child(4) { box-shadow: 2px 2px 0 #ff8f3d; }

.progress-pill .n {
  display: block;
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--ink);
}

.progress-pill .k {
  display: block;
  font-size: 0.68rem;
  color: var(--muted);
  margin-top: 2px;
}

.hero {
  text-align: center;
  padding: 18px 8px 10px;
}

.brand {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(3rem, 14vw, 4.4rem);
  line-height: 1;
  letter-spacing: 0.04em;
  color: var(--ink);
  position: relative;
  display: inline-block;
  text-shadow: 3px 3px 0 #ffd84d, -2px 2px 0 rgba(77, 183, 255, 0.45);
  animation: brandBounce 3.2s ease-in-out infinite;
}

@keyframes brandBounce {
  0%, 100% { transform: rotate(-1deg); }
  50% { transform: rotate(1.5deg); }
}

.tagline {
  margin: 14px 0 8px;
  font-size: clamp(1.05rem, 4.2vw, 1.3rem);
  font-weight: 600;
  color: var(--ink);
}

.sub {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.game-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 18px;
}

.game-tile {
  appearance: none;
  border: 1.5px solid var(--stroke);
  border-radius: var(--radius);
  background: #fff;
  color: inherit;
  padding: 20px 14px 16px;
  text-align: left;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.18s ease, box-shadow 0.18s ease;
}

.game-tile:nth-child(1) { box-shadow: 4px 4px 0 #4db7ff; }
.game-tile:nth-child(2) { box-shadow: 4px 4px 0 #2ee6a6; }
.game-tile:nth-child(3) { box-shadow: 4px 4px 0 #ffd84d; }
.game-tile:nth-child(4) { box-shadow: 4px 4px 0 #ff8f3d; }

.game-tile:hover,
.game-tile:focus-visible {
  transform: translate(-2px, -2px);
  outline: none;
}

.game-tile:nth-child(1):hover { box-shadow: 6px 6px 0 #4db7ff; }
.game-tile:nth-child(2):hover { box-shadow: 6px 6px 0 #2ee6a6; }
.game-tile:nth-child(3):hover { box-shadow: 6px 6px 0 #ffd84d; }
.game-tile:nth-child(4):hover { box-shadow: 6px 6px 0 #ff8f3d; }

.game-tile:active { transform: translate(1px, 1px); }

.tile-icon {
  display: inline-grid;
  place-items: center;
  min-width: 54px;
  height: 34px;
  padding: 0 10px;
  border-radius: 12px;
  margin-bottom: 12px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  background: var(--paper-soft);
  border: 1.5px dashed #c9d2de;
  color: var(--ink);
}

.tile-name {
  display: block;
  font-family: var(--font-display);
  font-size: 1.35rem;
  margin-bottom: 4px;
}

.tile-desc {
  display: block;
  font-size: 0.82rem;
  color: var(--muted);
}

.home-foot {
  margin-top: 22px;
  text-align: center;
  font-size: 0.78rem;
  color: var(--muted);
}

.game-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}

.game-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.35rem;
}

.btn {
  appearance: none;
  border: 1.5px solid var(--stroke);
  background: #fff;
  color: var(--ink);
  border-radius: 999px;
  padding: 8px 14px;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}

.btn.ghost:hover,
.btn.ghost:focus-visible {
  border-color: var(--ink);
  outline: none;
}

.btn.primary {
  background: #1c2430;
  border: none;
  color: #fff;
  font-weight: 700;
  padding: 12px 16px;
  box-shadow: 3px 3px 0 #ffd84d;
}

.btn.primary:disabled {
  opacity: 0.6;
  cursor: wait;
}

.score-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}

.score-box {
  background: #fff;
  border: 1.5px solid var(--stroke);
  border-radius: 16px;
  padding: 10px 14px;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  box-shadow: 2px 2px 0 #e6eaf0;
}

.score-box .label {
  font-size: 0.78rem;
  color: var(--muted);
}

.score-box span:last-child {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--ink);
}

.game-stage {
  position: relative;
  width: 100%;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--paper-soft);
  border: 1.5px solid var(--stroke);
  box-shadow: var(--shadow);
  min-height: 320px;
}

.game-hint {
  margin: 12px 4px 0;
  text-align: center;
  font-size: 0.85rem;
  color: var(--muted);
  min-height: 1.2em;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 50;
  background: #1c2430;
  color: #fff;
  border: none;
  padding: 12px 18px;
  border-radius: 999px;
  font-size: 0.92rem;
  box-shadow: 3px 3px 0 #ffd84d;
  animation: rise 0.25s ease;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.82);
  backdrop-filter: blur(4px);
  padding: 20px;
  text-align: center;
  z-index: 5;
}

.overlay h3 {
  margin: 0 0 8px;
  font-family: var(--font-display);
  font-size: 1.8rem;
}

.overlay p {
  margin: 0 0 16px;
  color: var(--muted);
}

/* ===== 2048 ===== */
.g2048 { padding: 12px; }

.g2048-board {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  aspect-ratio: 1;
  background: #eef1f6;
  border-radius: 16px;
  padding: 8px;
}

.cell {
  aspect-ratio: 1;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: clamp(1rem, 6vw, 1.6rem);
  background: #dce2ea;
  color: var(--ink);
}

.cell.n2 { background: #ffffff; }
.cell.n4 { background: #e8f6ff; color: #1a5f8a; }
.cell.n8 { background: #4db7ff; color: #fff; }
.cell.n16 { background: #2ee6a6; color: #0a3d2c; }
.cell.n32 { background: #ffd84d; color: #5a4300; }
.cell.n64 { background: #ff8f3d; color: #fff; }
.cell.n128 { background: #7b6cff; color: #fff; font-size: clamp(0.9rem, 5vw, 1.35rem); }
.cell.n256 { background: #b6f23a; color: #2a3d00; font-size: clamp(0.9rem, 5vw, 1.35rem); }
.cell.n512 { background: #1c2430; color: #ffd84d; font-size: clamp(0.85rem, 4.5vw, 1.25rem); }
.cell.n1024 { background: #0f8f6a; color: #fff; font-size: clamp(0.75rem, 4vw, 1.05rem); }
.cell.n2048 { background: linear-gradient(135deg, #ffd84d, #4db7ff); color: #1c2430; font-size: clamp(0.7rem, 3.8vw, 1rem); }

/* ===== Snake ===== */
.snake-wrap { position: relative; width: 100%; }

.snake-wrap canvas {
  display: block;
  width: 100%;
  height: auto;
  background: #f0f3f8;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  max-width: 220px;
  margin: 12px auto 4px;
  padding: 0 12px 12px;
}

.dpad .spacer { visibility: hidden; }

.dpad button {
  appearance: none;
  border: 1.5px solid var(--stroke);
  background: #fff;
  color: var(--ink);
  border-radius: 14px;
  height: 48px;
  font-size: 1.2rem;
  cursor: pointer;
}

.dpad button:active {
  background: #ffd84d;
}

/* ===== Jump ===== */
.jump-wrap canvas {
  display: block;
  width: 100%;
  height: auto;
  background: linear-gradient(#dff3ff 0%, #ffffff 48%, #fff6d6 48%, #ffe9a8 100%);
  touch-action: none;
}

.power-bar {
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 14px;
  height: 10px;
  border-radius: 999px;
  background: rgba(28, 36, 48, 0.12);
  overflow: hidden;
  opacity: 0;
  transition: opacity 0.15s;
}

.power-bar.on { opacity: 1; }

.power-bar > i {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #2ee6a6, #ff8f3d);
}

/* ===== Memory ===== */
.memory-board {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  padding: 14px;
}

.mem-card {
  aspect-ratio: 3 / 4;
  border: none;
  background: transparent;
  padding: 0;
  cursor: pointer;
  border-radius: 14px;
}

.mem-face {
  width: 100%;
  height: 100%;
  border-radius: 14px;
  display: grid;
  place-items: center;
  font-size: clamp(1.25rem, 6vw, 1.8rem);
  font-weight: 700;
  border: 1.5px solid var(--stroke);
  transition: transform 0.2s ease, background 0.2s ease;
  box-shadow: 3px 3px 0 rgba(28, 36, 48, 0.08);
}

.mem-back {
  background: #1c2430;
  color: #ffd84d;
  font-family: var(--font-display);
  font-size: 1.25rem;
}

.mem-front {
  background: #fff;
  color: #1c2430;
  animation: flipIn 0.22s ease;
}

.mem-card.matched .mem-front {
  background: #eaffc8;
  border-color: #b6f23a;
  box-shadow: 3px 3px 0 #b6f23a;
}

.mem-card.open .mem-face {
  border-color: #1c2430;
}

@keyframes flipIn {
  from { transform: scaleX(0.2); opacity: 0.4; }
  to { transform: scaleX(1); opacity: 1; }
}

@media (min-width: 640px) {
  #app { padding-top: 36px; }
  .game-grid { gap: 14px; }
  .game-tile { padding: 26px 18px 20px; }
}

/* ===== Multiplayer / Social ===== */
.user-actions { display: flex; gap: 6px; }
.section-title {
  margin: 22px 4px 10px;
  font-family: var(--font-display);
  font-size: 1.15rem;
}
.game-tile.online { box-shadow: 4px 4px 0 #7b6cff; }
.game-tile.online:hover { box-shadow: 6px 6px 0 #7b6cff; }
.join-btn { width: 100%; margin-top: 14px; }
.lobby-card label { display:flex; flex-direction:column; gap:6px; margin-bottom:12px; color:var(--muted); font-size:.9rem; }
.lobby-card input {
  border: 1.5px solid var(--stroke);
  border-radius: 14px;
  padding: 12px 14px;
  font: inherit;
}
.lobby-game { font-family: var(--font-display); font-size: 1.6rem; margin: 0 0 12px; }
.lobby-wait { text-align: center; }
.room-code {
  font-family: var(--font-display);
  font-size: 2.6rem;
  letter-spacing: .2em;
  margin: 8px 0;
  color: var(--ink);
  text-shadow: 3px 3px 0 #ffd84d;
}
.lobby-link { font-size: .78rem; color: var(--muted); word-break: break-all; }
.lobby-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; margin: 12px 0; }

.mp-wrap { padding: 8px; }
.mp-status {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
  font-size: .85rem;
}
.mp-status > div:last-child { text-align: right; }
.mp-timer {
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  background: #fff;
  border: 1.5px solid var(--stroke);
}
.mp-timer.urgent { background: #ffe0e0; color: #b00020; border-color: #ff8a8a; }
.dot {
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 50%;
  margin-right: 4px;
  vertical-align: middle;
}
.dot.black { background: #111; }
.dot.white { background: #fff; border: 1px solid #999; }
.mp-tip { text-align: center; color: var(--muted); font-size: .85rem; margin: 8px 0 0; }
.mp-wrap canvas {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 14px;
  border: 1.5px solid var(--stroke);
  background: #fff;
}
.chain-pad {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  max-width: 280px;
  margin: 10px auto 0;
}
.chain-pad button {
  height: 48px;
  border-radius: 14px;
  border: 1.5px solid var(--stroke);
  background: #fff;
  font: inherit;
  font-size: 1.1rem;
  cursor: pointer;
}
.chain-pad button:active { background: #ffd84d; }

.friends-list, .feed-list { display: flex; flex-direction: column; gap: 10px; }
.friend-card, .feed-card {
  background: #fff;
  border: 1.5px solid var(--stroke);
  border-radius: 16px;
  padding: 12px 14px;
  box-shadow: 2px 2px 0 #e6eaf0;
}
.friend-card .name, .feed-card .name { font-weight: 700; }
.friend-card .scores, .feed-card .meta { color: var(--muted); font-size: .8rem; margin-top: 4px; }
.feed-card .text { margin-top: 6px; }

.modal {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(28,36,48,.35);
  display: grid; place-items: center;
  padding: 16px;
}
.modal[hidden] { display: none !important; }
.modal-card {
  width: min(400px, 100%);
  background: #fff;
  border-radius: 22px;
  padding: 20px;
  border: 1.5px solid var(--stroke);
  box-shadow: var(--shadow);
}
.modal-card h3 { margin: 0 0 12px; font-family: var(--font-display); }
.modal-card label { display:flex; flex-direction:column; gap:6px; margin-bottom:10px; font-size:.88rem; color:var(--muted); }
.modal-card input {
  border: 1.5px solid var(--stroke);
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
  color: var(--ink);
}

.badge {
  display: inline-grid;
  place-items: center;
  min-width: 18px;
  height: 18px;
  margin-left: 4px;
  padding: 0 5px;
  border-radius: 999px;
  background: #ff8f3d;
  color: #fff;
  font-size: 0.68rem;
  font-weight: 700;
  vertical-align: middle;
}
.mail-compose label,
.screen-admin label,
#screen-admin label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
  font-size: 0.88rem;
  color: var(--muted);
}
.mail-compose select,
.mail-compose textarea,
#admin-sys-text {
  border: 1.5px solid var(--stroke);
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
  color: var(--ink);
  resize: vertical;
}
.mail-list { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.mail-card {
  background: #fff;
  border: 1.5px solid var(--stroke);
  border-radius: 16px;
  padding: 12px 14px;
  box-shadow: 2px 2px 0 #e6eaf0;
}
.mail-card.unread { border-color: #7b6cff; box-shadow: 2px 2px 0 #7b6cff; }
.mail-card.system { background: #fff8e8; }
.mail-card .head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--muted);
}
.mail-card .from { font-weight: 700; color: var(--ink); }
.mail-card .body { margin-top: 8px; white-space: pre-wrap; word-break: break-word; }
.admin-summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}
.admin-summary .pill {
  background: #fff;
  border: 1.5px solid var(--stroke);
  border-radius: 14px;
  padding: 12px 8px;
  text-align: center;
  box-shadow: 2px 2px 0 #ffd84d;
}
.admin-summary .pill b { display: block; font-size: 1.3rem; }
.admin-summary .pill span { font-size: 0.72rem; color: var(--muted); }
.admin-games, .admin-players { display: flex; flex-direction: column; gap: 8px; }
.admin-row {
  background: #fff;
  border: 1.5px solid var(--stroke);
  border-radius: 14px;
  padding: 10px 12px;
  font-size: 0.88rem;
}
.admin-row .title { font-weight: 700; margin-bottom: 4px; }
.admin-row .meta { color: var(--muted); font-size: 0.78rem; line-height: 1.45; }
