/* Sparkle Chess — Dreamy Storybook Pastel UI (100% Kid-Safe, Bulletproof Viewport Lock) */

:root {
  --bg-top: #EBC9FF;
  --bg-mid: #FFE3F4;
  --bg-bot: #DFF4FF;
  --cream: #FFFDF9;
  --ink: #4A2B52;
  --ink-soft: #7B5987;
  --pink-bg: #FFE3F0;
  --pink-border: #F57BB5;
  --pink-shadow: #D84E8F;
  --sky-bg: #E5E7FF;
  --sky-border: #8689F5;
  --sky-shadow: #5C5ED6;
  --gold: #FFE054;
  --gold-shadow: #DBA112;
}

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

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  font-family: 'Fredoka', 'Nunito', 'Rounded Mplus 1c', ui-rounded, 'Hiragino Maru Gothic ProN', system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: linear-gradient(175deg, var(--bg-top) 0%, var(--bg-mid) 52%, var(--bg-bot) 100%);
  user-select: none;
  -webkit-user-select: none;
}

body {
  position: fixed;
  inset: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

/* Dreamy Pastel Clouds & Twinkles */
.sky-backdrop {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 0;
}

.cloud {
  position: absolute;
  background: rgba(255, 255, 255, 0.65);
  border-radius: 999px;
  filter: blur(0.5px);
}

.cloud::before,
.cloud::after {
  content: '';
  position: absolute;
  background: inherit;
  border-radius: 50%;
}

.cloud-1 { width: 136px; height: 42px; top: 6%; left: 5%; opacity: 0.72; }
.cloud-1::before { width: 60px; height: 60px; top: -24px; left: 20px; }
.cloud-1::after { width: 46px; height: 46px; top: -16px; left: 66px; }

.cloud-2 { width: 162px; height: 48px; top: 12%; right: 6%; opacity: 0.65; }
.cloud-2::before { width: 68px; height: 68px; top: -28px; left: 26px; }
.cloud-2::after { width: 52px; height: 52px; top: -18px; left: 82px; }

.cloud-3 { width: 112px; height: 34px; bottom: 16%; left: 9%; opacity: 0.5; }
.cloud-3::before { width: 48px; height: 48px; top: -20px; left: 16px; }
.cloud-3::after { width: 38px; height: 38px; top: -14px; left: 50px; }

.twinkle {
  position: absolute;
  color: rgba(255, 255, 255, 0.85);
  text-shadow: 0 0 10px rgba(255, 224, 110, 0.8);
  animation: gentleTwinkle 3.2s ease-in-out infinite;
}

.twinkle-1 { top: 9%; left: 26%; font-size: 18px; animation-delay: 0s; }
.twinkle-2 { top: 18%; right: 22%; font-size: 15px; animation-delay: 1.1s; }
.twinkle-3 { bottom: 24%; right: 10%; font-size: 20px; animation-delay: 0.6s; }
.twinkle-4 { bottom: 32%; left: 4%; font-size: 16px; animation-delay: 1.9s; }

@keyframes gentleTwinkle {
  0%, 100% { opacity: 0.45; transform: scale(0.88); }
  50% { opacity: 1; transform: scale(1.18); }
}

/* App Shell — Locked strictly to the exact visible PWA viewport */
.app-shell {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 1060px;
  height: var(--app-height, 100%);
  max-height: 100%;
  margin: 0 auto;
  overflow: hidden;
  padding: calc(8px + env(safe-area-inset-top, 0px)) calc(12px + env(safe-area-inset-right, 0px)) calc(12px + env(safe-area-inset-bottom, 0px)) calc(12px + env(safe-area-inset-left, 0px));
  gap: 7px;
}

/* Header */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex: 0 0 auto;
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.app-title {
  margin: 0;
  font-size: clamp(1.12rem, 3vw, 1.62rem);
  font-weight: 700;
  color: var(--ink);
  letter-spacing: -0.01em;
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
  text-shadow: 0 2px 0 rgba(255, 255, 255, 0.9);
}

.header-pills {
  display: flex;
  align-items: center;
  gap: 7px;
  flex-shrink: 0;
}

.turn-pill,
.icon-pill-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  height: 38px;
  padding: 0 13px;
  border-radius: 999px;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--ink);
  background: var(--cream);
  border: 2.5px solid #E5C2F2;
  box-shadow: 0 3px 0 #C899DC;
  white-space: nowrap;
}

.turn-pill.team-pink {
  background: var(--pink-bg);
  border-color: var(--pink-border);
  box-shadow: 0 3px 0 var(--pink-shadow);
}

.turn-pill.team-sky {
  background: var(--sky-bg);
  border-color: var(--sky-border);
  box-shadow: 0 3px 0 var(--sky-shadow);
}

.turn-pill.team-free {
  background: linear-gradient(90deg, #FFE4F2 0%, #FFF5C8 50%, #DCF6FF 100%);
  border-color: #F2A2D8;
  box-shadow: 0 3px 0 #CE7CB5;
}

.icon-pill-btn {
  cursor: pointer;
  transition: transform 0.12s ease, background 0.15s ease;
}

.icon-pill-btn:hover {
  transform: translateY(-1px);
}

.icon-pill-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #C899DC;
}

.install-btn {
  width: 40px;
  padding: 0;
  font-size: 1.08rem;
  background: #E8FCEB;
  border-color: #7ED99A;
  box-shadow: 0 3px 0 #55BA76;
}

.install-btn[hidden] {
  display: none;
}

.mute-btn {
  width: 40px;
  padding: 0;
  font-size: 1.12rem;
}

/* Mode & Board Options Bar */
.mode-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 7px;
  flex: 0 0 auto;
}

.mode-group {
  display: flex;
  align-items: center;
  gap: 5px;
  background: rgba(255, 253, 249, 0.72);
  padding: 3px;
  border-radius: 999px;
  border: 2px solid rgba(255, 255, 255, 0.9);
}

.mode-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 36px;
  padding: 0 13px;
  border: none;
  border-radius: 999px;
  background: transparent;
  font-family: inherit;
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--ink-soft);
  cursor: pointer;
  white-space: nowrap;
  transition: transform 0.12s ease, background 0.15s ease, color 0.15s ease;
}

.mode-pill:hover {
  background: rgba(255, 255, 255, 0.72);
  color: var(--ink);
}

.mode-pill.active {
  background: linear-gradient(180deg, #FFE666 0%, #FFD23F 100%);
  color: var(--ink);
  box-shadow: 0 3px 0 #DF9E14;
}

.toggle-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 38px;
  padding: 0 13px;
  border-radius: 999px;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
  transition: transform 0.12s ease;
}

.chip-opponent {
  background: #EFEAFF;
  border: 2.5px solid #B6A4FA;
  box-shadow: 0 3px 0 #8E78E8;
}

.chip-size {
  background: #E7FAFF;
  border: 2.5px solid #8ADAF5;
  box-shadow: 0 3px 0 #5BB8DC;
}

.toggle-chip:hover {
  transform: translateY(-1px);
  background: #FFFFFF;
}

.toggle-chip:active {
  transform: translateY(2px);
}

/* Main Playfield Stage — Flexible middle container that NEVER overflows */
.stage-wrap {
  position: relative;
  flex: 1 1 0;
  min-height: 0;
  width: 100%;
  background: rgba(255, 253, 250, 0.45);
  border: 3px solid rgba(255, 255, 255, 0.85);
  border-radius: 26px;
  box-shadow: inset 0 4px 18px rgba(173, 108, 214, 0.12), 0 8px 24px rgba(74, 43, 82, 0.08);
  overflow: hidden;
}

#board-canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: manipulation;
  cursor: pointer;
}

/* Friendly Piece-Move Guide Pill at Top of Board */
.piece-guide-pill {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%) translateY(-8px);
  padding: 5px 15px;
  border-radius: 999px;
  background: rgba(255, 253, 249, 0.95);
  border: 2.5px solid #F7B5DC;
  box-shadow: 0 4px 12px rgba(74, 43, 82, 0.14);
  font-size: clamp(0.8rem, 2.2vw, 0.96rem);
  font-weight: 700;
  color: var(--ink);
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.22s cubic-bezier(0.34, 1.56, 0.64, 1);
  z-index: 8;
}

.piece-guide-pill.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Floating Magic Banner (Fairy Upgrade, Free Play, etc.) */
.magic-banner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.8);
  padding: 13px 24px;
  border-radius: 999px;
  background: linear-gradient(135deg, #FFE45C 0%, #FF9BD2 100%);
  border: 4px solid #FFFFFF;
  box-shadow: 0 12px 30px rgba(74, 43, 82, 0.25);
  font-size: clamp(1.1rem, 3.4vw, 1.55rem);
  font-weight: 700;
  color: var(--ink);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.24s ease, transform 0.26s cubic-bezier(0.34, 1.56, 0.64, 1);
  z-index: 12;
  white-space: nowrap;
}

.magic-banner.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

/* Bottom Tactile Kid Action Bar */
.action-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  flex: 0 0 auto;
  padding-bottom: 4px;
}

.kid-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 48px;
  padding: 0 17px;
  border-radius: 20px;
  border: 3px solid #FFFFFF;
  font-family: inherit;
  font-size: 0.98rem;
  font-weight: 700;
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
  transition: transform 0.12s ease, box-shadow 0.12s ease, opacity 0.15s ease;
}

.kid-btn:hover:not(:disabled) {
  transform: translateY(-2px);
}

.kid-btn:active:not(:disabled) {
  transform: translateY(3px);
}

.kid-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-hint {
  background: #FFE054;
  box-shadow: 0 5px 0 #DBA112;
}
.btn-hint:active:not(:disabled) { box-shadow: 0 2px 0 #DBA112; }

.btn-undo {
  background: #BAE8FF;
  box-shadow: 0 5px 0 #5BAEE0;
}
.btn-undo:active:not(:disabled) { box-shadow: 0 2px 0 #5BAEE0; }

.btn-new {
  background: #C6F7C0;
  box-shadow: 0 5px 0 #62C265;
}
.btn-new:active:not(:disabled) { box-shadow: 0 2px 0 #62C265; }

.btn-stickers {
  background: #FFD1EC;
  box-shadow: 0 5px 0 #E078B6;
}
.btn-stickers:active:not(:disabled) { box-shadow: 0 2px 0 #E078B6; }

.mini-count {
  display: inline-flex;
  align-items: center;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.78);
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--ink);
}

/* Modals (Victory Celebration & Sticker Book) */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(65, 36, 94, 0.48);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 40;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.25s ease, visibility 0.25s ease;
}

.modal-backdrop.open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

#celebration-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 41;
  pointer-events: none;
}

.modal-backdrop.open #celebration-canvas {
  pointer-events: auto;
}

.modal-card {
  position: relative;
  z-index: 42;
  width: 100%;
  max-width: 440px;
  max-height: 92%;
  overflow-y: auto;
  background: var(--cream);
  border: 5px solid #FFE066;
  border-radius: 32px;
  padding: 22px 20px 20px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(65, 36, 94, 0.3);
  transform: scale(0.88);
  transition: transform 0.28s cubic-bezier(0.34, 1.56, 0.64, 1);
  pointer-events: none;
}

.modal-backdrop.open .modal-card {
  transform: scale(1);
  pointer-events: auto;
}

.win-badge {
  display: inline-block;
  padding: 5px 14px;
  border-radius: 999px;
  background: #FFE8F6;
  color: #B43884;
  font-size: 0.9rem;
  font-weight: 700;
  margin-bottom: 6px;
}

.win-title {
  margin: 4px 0 8px;
  font-size: clamp(1.55rem, 5vw, 2.05rem);
  color: var(--ink);
}

.win-sub {
  margin: 0 0 12px;
  font-size: 0.98rem;
  color: var(--ink-soft);
}

.sticker-showcase {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(145deg, #FFFDF9 0%, #FFEAF7 100%);
  border: 4px solid #F7C744;
  border-radius: 26px;
  padding: 10px 22px;
  margin: 4px auto 14px;
  box-shadow: 0 8px 20px rgba(240, 165, 0, 0.2);
  animation: stickerBob 2.2s ease-in-out infinite;
}

@keyframes stickerBob {
  0%, 100% { transform: translateY(0) rotate(-1.5deg); }
  50% { transform: translateY(-6px) rotate(1.5deg); }
}

.sticker-showcase canvas {
  width: 108px;
  height: 108px;
  display: block;
}

.sticker-earned-name {
  margin-top: 6px;
  font-size: 1.02rem;
  font-weight: 700;
  color: var(--ink);
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.win-tip {
  margin-top: 12px;
  font-size: 0.86rem;
  color: var(--ink-soft);
  opacity: 0.9;
}

/* Sticker Book Modal */
.stickerbook-card {
  max-width: 560px;
  max-height: 88%;
  display: flex;
  flex-direction: column;
  border-color: #E2A8FF;
}

.sb-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.sb-title-wrap {
  text-align: left;
}

.sb-title {
  margin: 0;
  font-size: 1.4rem;
}

.sb-stats {
  font-size: 0.9rem;
  color: var(--ink-soft);
  font-weight: 600;
}

.close-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2.5px solid #E5C2F2;
  background: #FFF2FC;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--ink);
  cursor: pointer;
  flex-shrink: 0;
  box-shadow: 0 3px 0 #C899DC;
}

.close-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #C899DC;
}

.sticker-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 10px;
  overflow-y: auto;
  padding: 4px 2px 8px;
}

.sticker-slot {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 6px 7px;
  border-radius: 18px;
  background: #FFFDFB;
  border: 2.5px solid #F0D2F8;
  transition: transform 0.15s ease;
}

.sticker-slot.unlocked {
  cursor: pointer;
  border-color: #F7C744;
  box-shadow: 0 4px 0 #E5B12A;
}

.sticker-slot.unlocked:hover {
  transform: translateY(-2px) scale(1.03);
}

.sticker-slot.unlocked:active {
  transform: scale(0.95);
}

.sticker-slot.locked {
  background: #F5EEF8;
  border-style: dashed;
  border-color: #D7C2E3;
}

.sticker-slot canvas {
  width: 68px;
  height: 68px;
  display: block;
}

.sticker-slot-name {
  margin-top: 4px;
  font-size: 0.74rem;
  font-weight: 700;
  color: var(--ink);
  text-align: center;
  line-height: 1.15;
}

.sticker-slot.locked .sticker-slot-name {
  color: #9C85AA;
}

/* Narrow Mobile Portrait (390×844) */
@media (max-width: 560px) {
  .app-shell {
    padding: calc(7px + env(safe-area-inset-top, 0px)) calc(8px + env(safe-area-inset-right, 0px)) calc(9px + env(safe-area-inset-bottom, 0px)) calc(8px + env(safe-area-inset-left, 0px));
    gap: 5px;
  }

  .app-title {
    font-size: 1.06rem;
  }

  .turn-pill,
  .icon-pill-btn {
    height: 34px;
    padding: 0 9px;
    font-size: 0.82rem;
  }

  .install-btn,
  .mute-btn {
    width: 34px;
    padding: 0;
  }

  .mode-bar {
    gap: 5px;
  }

  .mode-pill {
    height: 32px;
    padding: 0 9px;
    font-size: 0.8rem;
  }

  .toggle-chip {
    height: 34px;
    padding: 0 10px;
    font-size: 0.8rem;
  }

  .stage-wrap {
    border-radius: 20px;
  }

  .action-bar {
    gap: 5px;
  }

  .kid-btn {
    min-height: 44px;
    padding: 0 8px;
    font-size: 0.82rem;
    gap: 4px;
    border-radius: 16px;
    flex: 1 1 0;
  }

  #sticker-btn-label {
    display: none;
  }

  .mini-count {
    padding: 2px 5px;
    font-size: 0.74rem;
  }

  .sticker-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }
}

/* Short Android Tablet Landscape (1024×600) & Split-Screen */
@media (max-height: 700px) {
  .app-shell {
    padding: calc(5px + env(safe-area-inset-top, 0px)) calc(10px + env(safe-area-inset-right, 0px)) calc(8px + env(safe-area-inset-bottom, 0px)) calc(10px + env(safe-area-inset-left, 0px));
    gap: 5px;
  }

  .app-title {
    font-size: 1.25rem;
  }

  .turn-pill,
  .icon-pill-btn {
    height: 34px;
    font-size: 0.88rem;
  }

  .install-btn,
  .mute-btn {
    width: 36px;
  }

  .mode-pill {
    height: 32px;
    padding: 0 11px;
    font-size: 0.86rem;
  }

  .toggle-chip {
    height: 34px;
    padding: 0 11px;
    font-size: 0.86rem;
  }

  .kid-btn {
    min-height: 42px;
    padding: 0 14px;
    font-size: 0.92rem;
    border-radius: 17px;
  }
}

@media (max-height: 540px) {
  .app-shell {
    padding: calc(4px + env(safe-area-inset-top, 0px)) calc(8px + env(safe-area-inset-right, 0px)) calc(6px + env(safe-area-inset-bottom, 0px)) calc(8px + env(safe-area-inset-left, 0px));
    gap: 4px;
  }

  .kid-btn {
    min-height: 38px;
    padding: 0 11px;
    font-size: 0.86rem;
  }
}
