:root {
  --cream: #fff8ee;
  --ink: #38294b;
  --purple: #49345f;
  --purple-dark: #2f213f;
  --purple-light: #6c4f87;
  --pink: #f37d9b;
  --pink-dark: #d85678;
  --yellow: #ffd35c;
  --orange: #ff9d5c;
  --mint: #65d6bc;
  --panel: rgba(255, 250, 244, 0.94);
  --shadow: 0 8px 20px rgba(53, 31, 79, 0.22);
  --safe-top: max(8px, env(safe-area-inset-top));
  --safe-bottom: max(8px, env(safe-area-inset-bottom));
}

* { box-sizing: border-box; }

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  overscroll-behavior: none;
  background: #3d2c58;
}

body {
  overflow: hidden;
  color: var(--ink);
  font-family: "Nunito", "Arial Rounded MT Bold", "Segoe UI", system-ui, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  background:
    radial-gradient(circle at 10% 15%, rgba(255, 213, 112, 0.24) 0 4%, transparent 4.4%),
    radial-gradient(circle at 86% 19%, rgba(244, 133, 171, 0.20) 0 5%, transparent 5.4%),
    radial-gradient(circle at 72% 77%, rgba(100, 218, 190, 0.18) 0 7%, transparent 7.4%),
    linear-gradient(160deg, #5d4278 0%, #392950 55%, #2c203f 100%);
}

button {
  font: inherit;
  -webkit-tap-highlight-color: transparent;
}

#game-wrapper {
  width: 100%;
  height: 100vh;
  height: 100dvh;
  padding: var(--safe-top) max(8px, env(safe-area-inset-right)) var(--safe-bottom) max(8px, env(safe-area-inset-left));
  display: flex;
  justify-content: center;
  overflow: hidden;
}

#game-ui {
  width: min(100%, 980px);
  height: 100%;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  justify-items: center;
  gap: clamp(5px, 1vh, 10px);
}

#top-bar {
  width: 100%;
  min-height: 67px;
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: clamp(4px, 1vw, 10px);
  overflow: visible;
}

.brand,
.stat-box,
.duck-wrapper {
  background: var(--panel);
  border: 2px solid rgba(255, 255, 255, 0.7);
  box-shadow: 0 4px 10px rgba(33, 19, 53, 0.17);
}

.brand {
  min-width: 82px;
  padding: 7px 9px;
  border-radius: 15px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.brand__title {
  color: var(--pink-dark);
  font-size: clamp(0.78rem, 2.8vw, 1.1rem);
  font-weight: 900;
  line-height: 1;
}

.brand__hint {
  margin-top: 3px;
  color: var(--purple-light);
  font-size: 0.61rem;
  font-weight: 800;
}

.stat-box {
  min-width: 55px;
  padding: 6px 8px;
  border-radius: 15px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.label {
  color: #816b8c;
  font-size: clamp(0.53rem, 1.8vw, 0.68rem);
  font-weight: 900;
  letter-spacing: 0.045em;
  line-height: 1.1;
  text-transform: uppercase;
}

.value {
  color: var(--purple);
  font-size: clamp(1rem, 3.8vw, 1.45rem);
  font-variant-numeric: tabular-nums;
  line-height: 1.05;
}

.next-piece-box {
  min-width: 76px;
  align-items: center;
  padding: 4px 6px;
}

#next {
  width: 76px;
  height: 47px;
  display: block;
}

.duck-wrapper {
  position: relative;
  width: 62px;
  min-width: 62px;
  border-radius: 16px;
  overflow: visible;
  display: flex;
  align-items: end;
  justify-content: center;
}

.duck {
  position: relative;
  width: 56px;
  height: 61px;
  transform-origin: center bottom;
  transition: transform 0.2s ease;
}

.duck.laugh { animation: duckLaugh 0.48s ease-in-out infinite; }
.duck.sad { transform: translateY(3px) rotate(-4deg); }

.hat {
  position: absolute;
  z-index: 5;
  top: 3px;
  left: 8px;
  width: 40px;
  height: 10px;
  border-radius: 3px;
  background: #3a3c59;
  box-shadow: 0 2px 0 #27283f;
}

.hat::before {
  content: "";
  position: absolute;
  width: 21px;
  height: 17px;
  left: 10px;
  bottom: 7px;
  border-radius: 3px 3px 0 0;
  background: #3a3c59;
}

.hat::after {
  content: "";
  position: absolute;
  width: 27px;
  height: 4px;
  left: 6px;
  top: -2px;
  border-radius: 5px;
  background: #6a6683;
}

.head {
  position: absolute;
  z-index: 3;
  top: 13px;
  left: 4px;
  width: 48px;
  height: 40px;
  border-radius: 48% 48% 43% 43%;
  background: linear-gradient(145deg, #ffe480, #f9b93c);
  box-shadow: inset -4px -3px rgba(182, 104, 17, 0.18), 0 3px 5px rgba(54, 31, 64, 0.15);
}

.eyes {
  position: absolute;
  top: 13px;
  width: 100%;
  display: flex;
  justify-content: center;
  gap: 11px;
}

.eye {
  width: 5px;
  height: 8px;
  border-radius: 50%;
  background: #37233d;
}

.eye::after {
  content: "";
  display: block;
  width: 2px;
  height: 2px;
  margin: 1px 0 0 1px;
  border-radius: 50%;
  background: white;
}

.beak {
  position: absolute;
  z-index: 5;
  top: 23px;
  left: 17px;
  width: 16px;
  height: 10px;
}

.beak-top {
  height: 7px;
  border-radius: 50% 50% 4px 4px;
  background: #ff864e;
}

.beak-bottom {
  width: 12px;
  height: 4px;
  margin: -1px auto 0;
  border-radius: 0 0 8px 8px;
  background: #e86236;
}

.cheeks {
  position: absolute;
  top: 25px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding: 0 4px;
}

.cheek {
  width: 7px;
  height: 4px;
  border-radius: 50%;
  background: rgba(237, 105, 126, 0.34);
}

.mouth {
  position: absolute;
  left: 18px;
  bottom: 4px;
  width: 13px;
  height: 7px;
}

.smile {
  width: 100%;
  height: 100%;
  border-bottom: 2px solid #8a4b28;
  border-radius: 0 0 12px 12px;
}

.duck.laugh .eye {
  height: 3px;
  margin-top: 3px;
  border-radius: 0 0 8px 8px;
}

.duck.laugh .smile {
  height: 9px;
  background: #87452c;
  border: 0;
  border-radius: 0 0 8px 8px;
}

.duck.sad .smile {
  margin-top: 4px;
  border: 0;
  border-top: 2px solid #8a4b28;
  border-radius: 10px 10px 0 0;
}

.tear {
  position: absolute;
  top: 24px;
  right: 6px;
  width: 5px;
  height: 8px;
  opacity: 0;
  border-radius: 50% 50% 55% 55%;
  background: #65c8ee;
}

.duck.sad .tear {
  opacity: 1;
  animation: tearDrop 1.1s infinite;
}

.body {
  position: absolute;
  z-index: 2;
  bottom: 0;
  left: 10px;
  width: 37px;
  height: 19px;
  border-radius: 50% 50% 35% 35%;
  background: linear-gradient(#ffc944, #ee9d24);
}

.bubble {
  position: absolute;
  z-index: 10;
  top: -26px;
  right: -30px;
  max-width: 105px;
  padding: 5px 7px;
  opacity: 0;
  color: var(--purple);
  border-radius: 10px;
  background: white;
  box-shadow: 0 3px 8px rgba(40, 24, 59, 0.2);
  font-size: 0.65rem;
  font-weight: 900;
  pointer-events: none;
  transform: scale(0.75);
  transition: opacity 0.2s, transform 0.2s;
  white-space: nowrap;
}

.bubble.show {
  opacity: 1;
  transform: scale(1);
}

#board-column {
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board-container {
  position: relative;
  width: 180px;
  height: 300px;
  flex: none;
  overflow: hidden;
  border: 5px solid #fff6e7;
  border-radius: clamp(12px, 3vw, 20px);
  background: #302743;
  box-shadow:
    0 0 0 3px rgba(87, 59, 115, 0.65),
    0 10px 20px rgba(25, 14, 40, 0.35);
  line-height: 0;
}

#board,
#effects {
  display: block;
  width: 100%;
  height: 100%;
}

#effects {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

#mobile-controls {
  width: min(100%, 520px);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: clamp(5px, 1.6vw, 10px);
}

.m-btn {
  min-height: clamp(48px, 8dvh, 64px);
  border: 0;
  border-bottom: 4px solid #bd607c;
  border-radius: 16px;
  color: white;
  background: linear-gradient(180deg, #ff9eb6, #ec7394);
  box-shadow: 0 4px 8px rgba(45, 22, 55, 0.22);
  font-size: clamp(1.35rem, 6vw, 1.8rem);
  font-weight: 900;
  cursor: pointer;
  touch-action: manipulation;
}

.m-btn.rotate {
  background: linear-gradient(180deg, #a98ae5, #8061c3);
  border-bottom-color: #60489e;
}

.m-btn.drop {
  background: linear-gradient(180deg, #71d9c0, #45b99f);
  border-bottom-color: #32917c;
}

.m-btn:active {
  transform: translateY(3px);
  border-bottom-width: 1px;
  box-shadow: 0 1px 3px rgba(45, 22, 55, 0.2);
}

#bottom-bar {
  display: flex;
  justify-content: center;
  gap: 7px;
  width: 100%;
}

.btn {
  border: 0;
  border-bottom: 5px solid #c65d76;
  border-radius: 14px;
  padding: 13px 28px;
  color: white;
  background: linear-gradient(180deg, #ff9eb6, #ee7899);
  box-shadow: 0 5px 10px rgba(44, 23, 62, 0.24);
  cursor: pointer;
  font-size: 1.05rem;
  font-weight: 900;
}

.btn:active {
  transform: translateY(4px);
  border-bottom-width: 1px;
}

.btn.small {
  padding: 7px 11px;
  border-bottom-width: 3px;
  border-radius: 10px;
  font-size: clamp(0.68rem, 2.4vw, 0.82rem);
}

.screen {
  position: fixed;
  z-index: 30;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: max(18px, env(safe-area-inset-top)) 18px max(18px, env(safe-area-inset-bottom));
  background: rgba(44, 30, 63, 0.76);
  backdrop-filter: blur(7px);
}

.screen.active { display: flex; }

.panel {
  width: min(92vw, 420px);
  padding: clamp(25px, 6vw, 42px);
  border: 3px solid rgba(255, 255, 255, 0.75);
  border-radius: 28px;
  color: var(--ink);
  background: linear-gradient(145deg, #fffdf8, #ffece1);
  box-shadow: 0 16px 35px rgba(23, 12, 38, 0.36);
  text-align: center;
  animation: popIn 0.28s ease-out;
}

.panel-duck { font-size: 3rem; }

.panel h1 {
  margin: 4px 0 7px;
  color: var(--pink-dark);
  font-size: clamp(2rem, 9vw, 3.1rem);
  line-height: 1;
}

.subtitle {
  margin: 0 0 24px;
  color: #705775;
  font-weight: 700;
}

.final-score {
  display: flex;
  justify-content: center;
  gap: 18px;
  margin: 0 0 24px;
  color: #705775;
  font-weight: 800;
}

.final-score strong { color: var(--pink-dark); }

.keyboard-help {
  margin: 17px 0 0;
  color: #987d94;
  font-size: 0.72rem;
  font-weight: 700;
}

@keyframes popIn {
  from { opacity: 0; transform: scale(0.86); }
  to { opacity: 1; transform: scale(1); }
}

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

@keyframes tearDrop {
  0% { transform: translateY(0); opacity: 1; }
  100% { transform: translateY(10px); opacity: 0; }
}

@media (min-width: 760px) and (min-height: 620px) {
  #game-ui {
    grid-template-columns: 185px minmax(280px, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
    column-gap: 16px;
    max-width: 740px;
  }

  #top-bar {
    grid-column: 1;
    grid-row: 1;
    flex-direction: column;
    align-items: stretch;
    justify-content: center;
  }

  .brand { min-height: 62px; }
  .stat-box { min-height: 53px; }
  .next-piece-box { min-height: 82px; }
  .duck-wrapper {
    width: 100%;
    height: 84px;
    align-self: center;
  }

  #board-column {
    grid-column: 2;
    grid-row: 1 / span 2;
  }

  #mobile-controls {
    grid-column: 1;
    grid-row: 2;
    width: 185px;
    grid-template-columns: repeat(3, 1fr);
  }

  #m-drop {
    grid-column: span 2;
  }

  .m-btn { min-height: 45px; }

  #bottom-bar {
    position: fixed;
    bottom: max(10px, env(safe-area-inset-bottom));
    right: max(10px, env(safe-area-inset-right));
    width: auto;
  }
}

@media (max-width: 390px) {
  #top-bar { gap: 3px; }
  .brand { min-width: 65px; padding: 5px; }
  .brand__hint { display: none; }
  .stat-box { min-width: 47px; padding: 5px; }
  .next-piece-box { min-width: 65px; }
  #next { width: 62px; }
  .duck-wrapper { width: 54px; min-width: 54px; }
  .duck { transform: scale(0.88); transform-origin: center bottom; }
}