* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

html {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  overscroll-behavior-y: none;
}

body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: fixed;
  touch-action: manipulation;
  overscroll-behavior: none;
  overscroll-behavior-y: none;
  -webkit-overflow-scrolling: none;
  background: #1a1a2e;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

#game-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  touch-action: none;
}

#ui-root {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  pointer-events: none;
}

.screen {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.3s ease, visibility 0.3s;
}

.screen.active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.gameplay-screen {
  pointer-events: none !important;
}

.gameplay-screen > * {
  pointer-events: auto;
}

/* Header Sound Button */
.header-sound-btn {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  background: rgba(255,255,255,0.2);
  border: none;
  color: #fff;
  font-size: 22px;
  cursor: pointer;
  flex-shrink: 0;
  margin-left: auto;
}

/* Tutorial Overlay */
.tutorial-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

.tutorial-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.tutorial-hand {
  font-size: 64px;
  animation: tutorialBounce 1s ease-in-out infinite;
}

.tutorial-text {
  color: #fff;
  font-size: clamp(16px, 4vw, 22px);
  font-weight: 700;
  text-align: center;
  max-width: 320px;
  padding: 16px 24px;
  margin-top: 16px;
  background: rgba(255,255,255,0.15);
  border-radius: 16px;
  line-height: 1.4;
}

@keyframes tutorialBounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-12px); }
}

/* Categories Screen */
.categories-screen {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  flex-direction: column;
}

.screen-header {
  display: flex;
  align-items: center;
  padding: calc(12px + env(safe-area-inset-top, 0px)) 16px 12px;
  gap: 12px;
}

.back-btn {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  background: rgba(255,255,255,0.2);
  border: none;
  color: #fff;
  font-size: 22px;
  cursor: pointer;
  flex-shrink: 0;
}

.screen-title {
  font-size: clamp(18px, 4vw, 28px);
  font-weight: 800;
  color: #fff;
}

.categories-grid {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom, 0px));
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
  align-content: start;
  scrollbar-width: none;
  -ms-overflow-style: none;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  overscroll-behavior: contain;
}

.categories-grid::-webkit-scrollbar { display: none; }

.category-card {
  border-radius: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.15s;
  position: relative;
  overflow: visible;
  border: none;
  color: #fff;
  font-weight: 700;
  font-size: clamp(11px, 2.5vw, 15px);
  padding: 14px 8px;
  gap: 4px;
  text-shadow: 0 1px 4px rgba(0,0,0,0.4);
  line-height: 1.2;
  min-height: 90px;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

.category-card:active { transform: scale(0.95); }

.category-card.locked-card {
  position: relative;
}

.category-card.locked-card::after {
  content: '🔒';
  position: absolute;
  top: 8px;
  right: 8px;
  font-size: 18px;
  filter: drop-shadow(0 1px 3px rgba(0,0,0,0.3));
}

.category-icon {
  font-size: clamp(26px, 7vw, 44px);
  flex-shrink: 0;
  line-height: 1;
}

.category-name {
  font-size: clamp(10px, 2.5vw, 15px);
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
  flex-shrink: 0;
  min-height: auto;
}

.category-progress {
  font-size: clamp(9px, 2vw, 11px);
  opacity: 0.85;
  flex-shrink: 0;
  line-height: 1;
}

/* Levels Screen */
.levels-screen {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  flex-direction: column;
}

.levels-grid {
  flex: 1;
  overflow-y: auto;
  padding: 8px 16px 16px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 12px;
  align-content: start;
  scrollbar-width: none;
  -ms-overflow-style: none;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  overscroll-behavior: contain;
}

.levels-grid::-webkit-scrollbar { display: none; }

.level-card {
  aspect-ratio: 1;
  border-radius: 16px;
  background: rgba(255,255,255,0.15);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: 3px solid transparent;
  transition: transform 0.15s, border-color 0.15s;
  position: relative;
  padding: 6px;
}

.level-card:active { transform: scale(0.95); }
.level-card.completed { border-color: #4ade80; }
.level-card.locked { opacity: 0.5; cursor: default; }

.level-preview {
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}

.level-check {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  background: #4ade80;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  color: #fff;
}

/* Gameplay HUD */
.gameplay-hud {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  padding: calc(10px + env(safe-area-inset-top, 0px)) calc(16px + env(safe-area-inset-right, 0px)) 10px calc(16px + env(safe-area-inset-left, 0px));
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.hud-btn {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  background: rgba(0,0,0,0.35);
  backdrop-filter: blur(8px);
  border: none;
  color: #fff;
  font-size: 20px;
  cursor: pointer;
}

.hud-level-name {
  font-size: clamp(14px, 3vw, 16px);
  font-weight: 700;
  color: #fff;
  text-shadow: 0 2px 8px rgba(0,0,0,0.4);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  flex: 1;
  text-align: center;
}

/* Color Palette */
.palette-container {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 8px calc(12px + env(safe-area-inset-right, 0px)) calc(12px + env(safe-area-inset-bottom, 0px)) calc(12px + env(safe-area-inset-left, 0px));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  background: linear-gradient(to top, rgba(0,0,0,0.5), rgba(0,0,0,0));
  padding-top: 16px;
  max-height: 130px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.palette-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
}

.palette-color {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.3);
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s;
  position: relative;
}

.palette-color.selected {
  border-color: #fff;
  transform: scale(1.2);
  box-shadow: 0 0 15px rgba(255,255,255,0.5);
}

.palette-color .count {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 18px;
  height: 18px;
  background: rgba(0,0,0,0.7);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 3px;
}

.eraser-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: rgba(255,255,255,0.15);
  border: 3px solid rgba(255,255,255,0.3);
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.eraser-btn.selected {
  border-color: #fff;
  transform: scale(1.2);
  box-shadow: 0 0 15px rgba(255,255,255,0.5);
}

/* Preview Panel (small thumbnail) */
.preview-panel {
  position: absolute;
  top: calc(60px + env(safe-area-inset-top, 0px));
  right: calc(8px + env(safe-area-inset-right, 0px));
  width: clamp(56px, 14vw, 110px);
  aspect-ratio: 1;
  border-radius: 12px;
  background: rgba(0,0,0,0.35);
  backdrop-filter: blur(8px);
  padding: 6px;
  cursor: pointer;
  transition: transform 0.15s;
  overflow: hidden;
}

.preview-panel:active {
  transform: scale(0.95);
}

.preview-canvas {
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  border-radius: 8px;
}

/* Preview Overlay (full-size) */
.preview-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 100;
  touch-action: none;
  background: rgba(0,0,0,0.75);
  backdrop-filter: blur(6px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.25s, visibility 0.25s;
  cursor: pointer;
}

.preview-overlay.active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.preview-overlay-canvas {
  width: min(85vw, 85vh, 500px);
  height: min(85vw, 85vh, 500px);
  image-rendering: pixelated;
  border-radius: 16px;
  background: #e8e8e8;
  box-shadow: 0 10px 50px rgba(0,0,0,0.5);
}

.preview-overlay-hint {
  color: rgba(255,255,255,0.6);
  font-size: 14px;
  font-weight: 600;
}

/* Unlock Dialog */
.unlock-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 100;
  touch-action: none;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.25s, visibility 0.25s;
}

.unlock-overlay.active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.unlock-dialog {
  background: linear-gradient(135deg, #2d3436, #636e72);
  border-radius: 24px;
  padding: 30px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  max-width: 320px;
  width: 85%;
  box-shadow: 0 15px 50px rgba(0,0,0,0.5);
}

.unlock-icon {
  font-size: 56px;
}

.unlock-title {
  font-size: 20px;
  font-weight: 800;
  color: #fff;
}

.unlock-desc {
  font-size: 14px;
  color: rgba(255,255,255,0.7);
  text-align: center;
  line-height: 1.4;
}

.unlock-btn-ad {
  width: 100%;
  min-height: 52px;
  padding: 14px 20px;
  font-size: 16px;
  font-weight: 700;
  border: none;
  border-radius: 14px;
  cursor: pointer;
  color: #fff;
  background: linear-gradient(135deg, #f093fb, #f5576c);
  box-shadow: 0 4px 20px rgba(245,87,108,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.unlock-btn-ad:active { transform: scale(0.97); }

.unlock-btn-close {
  width: 100%;
  min-height: 48px;
  padding: 12px 20px;
  font-size: 14px;
  font-weight: 600;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  color: rgba(255,255,255,0.7);
  background: rgba(255,255,255,0.1);
}

/* Level Complete */
.complete-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(10px);
  touch-action: none;
}

.complete-title {
  font-size: clamp(28px, 6vw, 48px);
  font-weight: 900;
  color: #fff;
  margin-bottom: 20px;
}

.complete-image {
  width: min(60vw, 300px);
  aspect-ratio: 1;
  border-radius: 20px;
  margin-bottom: 30px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.4);
}

.complete-btns {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
  padding: 0 16px;
  max-width: 100%;
}

.complete-btn {
  min-width: 140px;
  min-height: 52px;
  padding: 12px 24px;
  font-size: clamp(14px, 2.5vw, 18px);
  font-weight: 700;
  border: none;
  border-radius: 14px;
  cursor: pointer;
  color: #fff;
}

.btn-next {
  background: linear-gradient(135deg, #4facfe, #00f2fe);
  box-shadow: 0 4px 20px rgba(79,172,254,0.4);
}

.btn-categories {
  background: rgba(255,255,255,0.2);
}

/* Iron overlay */
.iron-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 50;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s, visibility 0.3s;
}

.iron-overlay.active {
  opacity: 1;
  visibility: visible;
}

/* Small screens (iPhone SE, etc) */
@media (max-height: 600px) {
  .palette-color, .eraser-btn {
    width: 36px;
    height: 36px;
  }
  .palette-color .count {
    min-width: 16px;
    height: 16px;
    font-size: 9px;
    top: -5px;
    right: -5px;
  }
  .hud-btn {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
  .preview-panel {
    top: calc(52px + env(safe-area-inset-top, 0px));
  }
}

/* Medium mobile screens (iPhone 6/7/8, most Androids) */
@media (max-width: 420px) {
  .categories-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    padding: 8px 12px calc(12px + env(safe-area-inset-bottom, 0px));
  }
  .category-card {
    min-height: 86px;
    padding: 12px 6px;
  }
  .category-icon {
    font-size: 26px;
  }
  .category-name {
    font-size: 11px;
  }
}

/* Small mobile screens (iPhone SE, Galaxy S5, 320px devices) */
@media (max-width: 360px) {
  .categories-grid {
    gap: 8px;
    padding: 8px 10px calc(12px + env(safe-area-inset-bottom, 0px));
  }
  .category-card {
    min-height: 80px;
    padding: 10px 6px;
    border-radius: 16px;
  }
  .category-icon {
    font-size: 22px;
  }
  .category-name {
    font-size: 10px;
  }
  .category-progress {
    font-size: 9px;
  }
  .levels-grid {
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  }
}

/* Short screens (landscape-like or very small phones) */
@media (max-height: 700px) and (max-width: 420px) {
  .category-card {
    min-height: 72px;
    padding: 8px 6px;
    gap: 2px;
  }
  .category-icon {
    font-size: 20px;
  }
  .category-name {
    font-size: 10px;
  }
}

/* Landscape orientation stub for mobile */
.landscape-stub {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9999;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  color: #fff;
}

.landscape-stub-icon {
  font-size: 64px;
  animation: rotatePhone 2s ease-in-out infinite;
}

@keyframes rotatePhone {
  0%, 100% { transform: rotate(90deg); }
  50% { transform: rotate(0deg); }
}

.landscape-stub-text {
  font-size: 18px;
  font-weight: 700;
  text-align: center;
  padding: 0 24px;
}

@media (orientation: landscape) and (max-height: 500px) and (pointer: coarse) {
  .landscape-stub {
    display: flex;
  }
}
