:root {
  --page-bg: #f4dfbc;
  --panel: rgba(255, 252, 242, 0.9);
  --ink: #3a2a1e;
  --accent: #dc6d34;
  --accent-dark: #9f4827;
  --shadow: rgba(89, 55, 31, 0.18);
}

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

html,
body {
  width: 100%;
  height: 100%;
  min-height: 100dvh;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  background:
    radial-gradient(circle at 20% 10%, rgba(255, 245, 202, 0.95), transparent 26rem),
    radial-gradient(circle at 80% 92%, rgba(224, 123, 73, 0.3), transparent 22rem),
    linear-gradient(145deg, #f8e9c9 0%, var(--page-bg) 52%, #eec28e 100%);
  color: var(--ink);
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  touch-action: none;
}

button {
  appearance: none;
  -webkit-appearance: none;
  border: none;
  outline: none;
  margin: 0;
  background: transparent;
  font: inherit;
}

.game-shell {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.game-window {
  position: relative;
  width: min(540px, 100vw, calc(100vh * 0.5625));
  width: min(540px, 100dvw, calc(100dvh * 0.5625));
  aspect-ratio: 9 / 16;
  border-radius: 28px;
  box-shadow: 0 28px 70px var(--shadow), inset 0 0 0 2px rgba(255, 255, 255, 0.55);
  overflow: hidden;
}

.loading-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background: #fff4d6;
  transition: opacity 220ms ease, visibility 220ms ease;
}

.loading-bar {
  width: 48%;
  max-width: 260px;
  height: 14px;
  overflow: hidden;
  border: 2px solid rgba(75, 48, 35, 0.16);
  border-radius: 999px;
  background: rgba(75, 48, 35, 0.1);
}

.loading-bar-fill {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #2fbd73 0%, #67d996 100%);
  transform: scaleX(var(--loading-progress, 0.08));
  transform-origin: left center;
  transition: transform 260ms ease;
  overflow: hidden;
}

.loading-bar-fill::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent 0%, rgba(255, 255, 255, 0.55) 50%, transparent 100%);
  transform: translateX(-100%);
  animation: loading-shine 900ms ease-in-out infinite;
}

.game-window.game-ready .loading-overlay {
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
}

@keyframes loading-shine {
  0% {
    transform: translateX(-100%);
  }

  100% {
    transform: translateX(100%);
  }
}

#gameCanvas {
  width: 100%;
  height: 100%;
  display: block;
  background: #fff7df;
  touch-action: none;
}

.hud {
  position: absolute;
  top: 16px;
  left: 16px;
  right: 16px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  pointer-events: none;
}

.game-window:not(.game-ready) .hud,
.game-window:not(.game-ready) .completion-actions,
.game-window:not(.game-ready) .message {
  opacity: 0;
  pointer-events: none;
}

.level-counter,
.message {
  border: 2px solid rgba(83, 48, 25, 0.12);
  background: var(--panel);
  box-shadow: 0 12px 28px rgba(83, 48, 25, 0.12);
  backdrop-filter: blur(8px);
}

.level-counter {
  flex: 0 0 auto;
  padding: 9px 13px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 0.02em;
}

.hud-actions {
  display: flex;
  justify-content: flex-end;
  flex-wrap: nowrap;
  gap: 8px;
  pointer-events: auto;
}

.hud-button {
  min-height: 38px;
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--panel);
  box-shadow: 0 12px 24px rgba(83, 48, 25, 0.14);
  backdrop-filter: blur(8px);
  color: var(--ink);
  font-size: 14px;
  font-weight: 800;
  white-space: nowrap;
  cursor: pointer;
  transition: transform 120ms ease, opacity 120ms ease;
}

.hud-button.primary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.ad-icon {
  width: 18px;
  height: 18px;
  display: block;
  flex: 0 0 auto;
  object-fit: contain;
}

.primary-button {
  color: #fff9ee;
  background: linear-gradient(180deg, var(--accent) 0%, var(--accent-dark) 100%);
  box-shadow: 0 12px 24px rgba(83, 48, 25, 0.14);
}

.hud-button:active,
.round-action:active {
  transform: translateY(1px) scale(0.97);
}

.hud-button:disabled {
  cursor: default;
  opacity: 0.52;
}

.completion-actions {
  position: absolute;
  left: 50%;
  bottom: 170px;
  display: flex;
  gap: 18px;
  transform: translateX(-50%);
  pointer-events: auto;
}

.round-action {
  width: 58px;
  height: 58px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--panel);
  box-shadow: 0 14px 26px rgba(83, 48, 25, 0.18);
  backdrop-filter: blur(8px);
  color: var(--ink);
  font-size: 30px;
  font-weight: 900;
  line-height: 1;
  cursor: pointer;
  transition: transform 120ms ease, opacity 120ms ease;
}

.round-action.primary-round {
  color: #fff9ee;
  background: linear-gradient(180deg, var(--accent) 0%, var(--accent-dark) 100%);
  box-shadow: 0 12px 24px rgba(83, 48, 25, 0.14);
}

.level-menu {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(58, 42, 30, 0.26);
  backdrop-filter: blur(5px);
  pointer-events: auto;
}

.level-menu-panel {
  width: min(100%, 430px);
  max-height: 92%;
  padding: 20px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border: 2px solid rgba(83, 48, 25, 0.12);
  border-radius: 28px;
  background: rgba(255, 252, 242, 0.96);
  box-shadow: 0 24px 54px rgba(83, 48, 25, 0.22);
}

.level-menu-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.level-menu h2,
.level-menu p {
  margin: 0;
}

.level-menu h2 {
  color: var(--ink);
  font-size: 26px;
  line-height: 1.1;
}

.level-menu p {
  margin-top: 6px;
  color: rgba(58, 42, 30, 0.68);
  font-size: 15px;
  font-weight: 800;
}

.level-menu-close {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--panel);
  box-shadow: 0 10px 22px rgba(83, 48, 25, 0.14);
  color: var(--ink);
  font-size: 28px;
  font-weight: 900;
  line-height: 1;
  cursor: pointer;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  min-height: 0;
  overflow: visible;
}

.level-grid-button {
  min-height: 58px;
  border-radius: 18px;
  background: #f8dfad;
  box-shadow: inset 0 -3px 0 rgba(83, 48, 25, 0.1), 0 10px 18px rgba(83, 48, 25, 0.1);
  color: var(--ink);
  font-size: 20px;
  font-weight: 900;
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease;
}

.level-pager {
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 10px;
}

.level-page-button {
  width: 46px;
  min-height: 46px;
  border-radius: 50%;
  background: #f8dfad;
  box-shadow: inset 0 -3px 0 rgba(83, 48, 25, 0.1), 0 10px 18px rgba(83, 48, 25, 0.1);
  color: var(--ink);
  font-size: 24px;
  font-weight: 900;
  cursor: pointer;
}

.level-page-button:disabled {
  opacity: 0.42;
  cursor: default;
}

.level-grid-button.completed {
  color: #fff9ee;
  background: linear-gradient(180deg, #56b978 0%, #2f8d55 100%);
}

.level-grid-button.current {
  outline: 3px solid rgba(220, 109, 52, 0.55);
  box-shadow: inset 0 -3px 0 rgba(83, 48, 25, 0.1), 0 0 0 5px rgba(220, 109, 52, 0.14);
}

.level-grid-button.locked,
.level-grid-button:disabled {
  cursor: default;
  color: rgba(75, 48, 35, 0.45);
  background: #e8dcc5;
  box-shadow: inset 0 -3px 0 rgba(83, 48, 25, 0.06);
}

.level-grid-button:active,
.level-menu-close:active,
.level-page-button:active {
  transform: translateY(1px) scale(0.97);
}

.hidden {
  display: none;
}

.message {
  position: absolute;
  left: 22px;
  right: 22px;
  bottom: 24px;
  padding: 13px 16px;
  border-radius: 20px;
  text-align: center;
  font-weight: 800;
  transition: opacity 180ms ease, transform 180ms ease;
}

.message-hidden {
  opacity: 0;
  transform: translateY(12px);
  pointer-events: none;
}

@media (max-height: 800px) {
  .hud {
    top: 12px;
    left: 12px;
    right: 12px;
    gap: 8px;
  }

  .level-counter {
    padding: 7px 10px;
    font-size: 12px;
  }

  .hud-actions {
    gap: 6px;
  }

  .hud-button {
    min-height: 34px;
    padding: 7px 9px;
    font-size: 12px;
  }
}

@media (max-height: 760px) {
  .hud {
    top: 12px;
    left: 12px;
    right: 12px;
  }

  .hud-button,
  .level-counter {
    min-height: 34px;
    padding: 7px 10px;
    font-size: 12px;
  }

  .level-menu {
    padding: 16px;
  }

  .level-menu-panel {
    padding: 16px;
    border-radius: 24px;
  }

  .level-menu h2 {
    font-size: 23px;
  }

  .level-grid {
    gap: 8px;
  }

  .level-grid-button {
    min-height: 50px;
    border-radius: 16px;
    font-size: 18px;
  }

  .level-pager {
    margin-top: 12px;
  }

  .level-page-button {
    width: 40px;
    min-height: 40px;
    font-size: 21px;
  }

  .completion-actions {
    bottom: 136px;
  }

  .round-action {
    width: 52px;
    height: 52px;
    font-size: 27px;
  }
}

@media (max-height: 700px), (max-width: 390px) {
  .game-window {
    border-radius: 20px;
  }

  .hud {
    top: 9px;
    left: 9px;
    right: 9px;
    gap: 6px;
  }

  .level-counter {
    min-height: 30px;
    padding: 6px 9px;
    font-size: 11px;
    line-height: 1;
  }

  .hud-actions {
    gap: 5px;
  }

  .hud-button {
    min-height: 30px;
    padding: 6px 8px;
    font-size: 11px;
    box-shadow: 0 8px 16px rgba(83, 48, 25, 0.12);
  }

  .hud-button.primary-button {
    gap: 4px;
  }

  .ad-icon {
    width: 15px;
    height: 15px;
  }

  .message {
    left: 14px;
    right: 14px;
    bottom: 14px;
    padding: 10px 12px;
    border-radius: 16px;
    font-size: 13px;
  }

  .completion-actions {
    bottom: 112px;
    gap: 14px;
  }

  .round-action {
    width: 48px;
    height: 48px;
    font-size: 25px;
  }

  .level-menu {
    padding: 10px;
  }

  .level-menu-panel {
    width: 100%;
    max-height: calc(100% - 8px);
    padding: 12px;
    border-radius: 20px;
  }

  .level-menu-header {
    gap: 10px;
    margin-bottom: 10px;
  }

  .level-menu h2 {
    font-size: 20px;
  }

  .level-menu p {
    margin-top: 3px;
    font-size: 12px;
  }

  .level-menu-close {
    width: 34px;
    height: 34px;
    font-size: 22px;
  }

  .level-grid {
    gap: 6px;
  }

  .level-grid-button {
    min-height: 38px;
    border-radius: 13px;
    font-size: 15px;
    box-shadow: inset 0 -2px 0 rgba(83, 48, 25, 0.1), 0 6px 12px rgba(83, 48, 25, 0.08);
  }

  .level-grid-button.current {
    outline-width: 2px;
    box-shadow: inset 0 -2px 0 rgba(83, 48, 25, 0.1), 0 0 0 3px rgba(220, 109, 52, 0.13);
  }

  .level-pager {
    gap: 8px;
    margin-top: 8px;
  }

  .level-page-button {
    width: 34px;
    min-height: 34px;
    font-size: 18px;
    box-shadow: inset 0 -2px 0 rgba(83, 48, 25, 0.1), 0 6px 12px rgba(83, 48, 25, 0.08);
  }
}

@media (max-height: 660px), (max-width: 360px) {
  .hud {
    top: 7px;
    left: 7px;
    right: 7px;
  }

  .level-counter {
    min-height: 28px;
    padding: 5px 7px;
    font-size: 10px;
    line-height: 1;
  }

  .hud-button {
    min-height: 28px;
    padding: 5px 7px;
    font-size: 10px;
  }

  .ad-icon {
    width: 14px;
    height: 14px;
  }

  .completion-actions {
    bottom: 96px;
  }

  .round-action {
    width: 44px;
    height: 44px;
    font-size: 23px;
  }

  .level-menu-panel {
    padding: 10px;
  }

  .level-menu-header {
    margin-bottom: 8px;
  }

  .level-menu h2 {
    font-size: 18px;
  }

  .level-menu p {
    font-size: 11px;
  }

  .level-grid {
    gap: 5px;
  }

  .level-grid-button {
    min-height: 34px;
    border-radius: 11px;
    font-size: 14px;
  }

  .level-page-button {
    width: 32px;
    min-height: 32px;
    font-size: 17px;
  }
}

@media (max-height: 590px), (max-width: 330px) {
  .game-window {
    border-radius: 14px;
  }

  .hud {
    top: 5px;
    left: 5px;
    right: 5px;
    gap: 4px;
  }

  .level-counter {
    min-height: 22px;
    padding: 3px 6px;
    font-size: 9px;
    border-width: 1px;
    line-height: 1;
    box-shadow: none;
    backdrop-filter: none;
  }

  .hud-actions {
    gap: 4px;
  }

  .hud-button {
    min-height: 22px;
    padding: 3px 6px;
    font-size: 9px;
    box-shadow: none;
    backdrop-filter: none;
  }

  .ad-icon {
    width: 12px;
    height: 12px;
  }

  .message {
    left: 10px;
    right: 10px;
    bottom: 10px;
    padding: 8px 10px;
    border-radius: 13px;
    font-size: 11px;
  }

  .completion-actions {
    bottom: 78px;
    gap: 10px;
  }

  .round-action {
    width: 39px;
    height: 39px;
    font-size: 20px;
  }

  .level-menu {
    padding: 6px;
  }

  .level-menu-panel {
    max-height: calc(100% - 4px);
    padding: 8px;
    border-radius: 16px;
  }

  .level-menu-header {
    gap: 8px;
    margin-bottom: 6px;
  }

  .level-menu h2 {
    font-size: 16px;
  }

  .level-menu p {
    font-size: 10px;
  }

  .level-menu-close {
    width: 30px;
    height: 30px;
    font-size: 19px;
  }

  .level-grid {
    gap: 4px;
    padding-right: 2px;
  }

  .level-grid-button {
    min-height: 30px;
    border-radius: 10px;
    font-size: 12px;
  }

  .level-pager {
    margin-top: 6px;
  }

  .level-page-button {
    width: 29px;
    min-height: 29px;
    font-size: 15px;
  }
}
