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

/* Global UI scale — recomputed from the viewport in JS (see Game.updateUiScale).
   Top/bottom bars express their pixel sizes as calc(Npx * var(--ui-scale)) so
   they grow proportionally with the gameplay area, including in fullscreen. */
:root {
  --ui-scale: 1;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  touch-action: manipulation;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: none;
  background: #0a0a14;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  color: #e0e0e0;
}

#game-canvas {
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  cursor: crosshair;
  touch-action: none;
  outline: none;
}

#game-canvas:focus,
#game-canvas:focus-visible {
  outline: none;
}

#start-hint {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 16px 28px;
  border-radius: 14px;
  background: rgba(10, 10, 20, 0.55);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: #f0f0f0;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 0.3px;
  text-align: center;
  pointer-events: none;
  z-index: 5;
  animation: start-hint-pulse 1.8s ease-in-out infinite;
  transition: opacity 0.3s ease, transform 0.3s ease;
  white-space: nowrap;
}

#start-hint.fade-out {
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.9);
}

@keyframes start-hint-pulse {
  0%, 100% { opacity: 0.85; }
  50% { opacity: 1; }
}

@media (max-width: 480px) {
  #start-hint { font-size: 18px; padding: 12px 22px; }
}

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

#ui-root > * {
  pointer-events: auto;
}

/* Top bar */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(6px * var(--ui-scale)) calc(12px * var(--ui-scale));
  background: linear-gradient(180deg, #1a1a2e 0%, #16213e 100%);
  border-bottom: 1px solid rgba(255,255,255,0.06);
  height: calc(44px * var(--ui-scale));
}

.level-info {
  display: flex;
  align-items: center;
  gap: calc(8px * var(--ui-scale));
  flex-shrink: 1;
  min-width: 0;
}

.level-badge {
  font-size: calc(13px * var(--ui-scale));
  font-weight: 700;
  color: #e94560;
  white-space: nowrap;
  flex-shrink: 0;
}

.level-bar {
  width: calc(80px * var(--ui-scale));
  height: calc(6px * var(--ui-scale));
  background: rgba(255,255,255,0.08);
  border-radius: calc(3px * var(--ui-scale));
  overflow: hidden;
  flex-shrink: 1;
  min-width: calc(40px * var(--ui-scale));
}

.level-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #e94560, #ff6b6b);
  border-radius: calc(3px * var(--ui-scale));
  transition: width 0.3s ease;
}

.top-controls {
  display: flex;
  gap: calc(4px * var(--ui-scale));
  align-items: center;
  flex-shrink: 0;
}

.btn-icon {
  width: calc(36px * var(--ui-scale));
  height: calc(36px * var(--ui-scale));
  border: none;
  border-radius: calc(8px * var(--ui-scale));
  background: rgba(255,255,255,0.08);
  color: #ccc;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* Icons scale with their buttons so glyphs stay proportional in fullscreen. */
.top-bar .btn-icon svg,
.bottom-panel .eraser-btn svg {
  transform: scale(var(--ui-scale));
  transform-origin: center;
}

@media (hover: hover) {
  .btn-icon:hover {
    background: rgba(255,255,255,0.15);
    color: #fff;
  }
}

.btn-icon:active {
  background: rgba(233,69,96,0.3);
}

/* Bottom panel */
.bottom-panel {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background: linear-gradient(0deg, #1a1a2e 0%, #16213e 100%);
  border-top: 1px solid rgba(255,255,255,0.06);
  padding: calc(6px * var(--ui-scale)) calc(8px * var(--ui-scale));
  padding-bottom: max(calc(6px * var(--ui-scale)), env(safe-area-inset-bottom));
}

/* Scrollable containers — swipe-to-refresh safe */
.scrollable-container {
  overscroll-behavior: contain;
}

.scrollable-container.scroll-x {
  touch-action: pan-x;
}

/* Categories */
.categories {
  display: flex;
  gap: calc(4px * var(--ui-scale));
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  padding-bottom: calc(6px * var(--ui-scale));
  mask-image: linear-gradient(to right, transparent 0, black 4px, black calc(100% - 20px), transparent 100%);
  -webkit-mask-image: linear-gradient(to right, transparent 0, black 4px, black calc(100% - 20px), transparent 100%);
}

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

.cat-btn {
  flex-shrink: 0;
  padding: calc(5px * var(--ui-scale)) calc(12px * var(--ui-scale));
  border: none;
  border-radius: calc(16px * var(--ui-scale));
  background: rgba(255,255,255,0.06);
  color: #aaa;
  font-size: calc(12px * var(--ui-scale));
  font-weight: 500;
  cursor: pointer;
  /* Only animate visual state, never geometry — otherwise the padding/
     font-size would animate on a --ui-scale change and the resize handler
     would measure the bar mid-animation (stale height). */
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
}

@media (hover: hover) {
  .cat-btn:hover {
    background: rgba(255,255,255,0.12);
    color: #ddd;
  }
}

.cat-btn.active {
  background: linear-gradient(135deg, #e94560, #c23152);
  color: #fff;
}

/* Materials */
.materials {
  display: flex;
  gap: calc(4px * var(--ui-scale));
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  padding-bottom: calc(6px * var(--ui-scale));
}

.materials::-webkit-scrollbar { display: none; }

.mat-btn {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: calc(5px * var(--ui-scale));
  padding: calc(5px * var(--ui-scale)) calc(10px * var(--ui-scale));
  border: 2px solid transparent;
  border-radius: calc(10px * var(--ui-scale));
  background: rgba(255,255,255,0.06);
  color: #ccc;
  font-size: calc(12px * var(--ui-scale));
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  white-space: nowrap;
}

@media (hover: hover) {
  .mat-btn:hover {
    background: rgba(255,255,255,0.12);
    border-color: rgba(255,255,255,0.1);
  }
}

.mat-btn.active {
  background: rgba(255,255,255,0.15);
  border-color: #e94560;
  color: #fff;
}

.mat-swatch {
  width: calc(16px * var(--ui-scale));
  height: calc(16px * var(--ui-scale));
  border-radius: calc(4px * var(--ui-scale));
  flex-shrink: 0;
}

.mat-name {
  font-weight: 500;
}

/* Brush controls */
.brush-controls {
  display: flex;
  align-items: center;
  gap: calc(6px * var(--ui-scale));
}

.brush-label {
  font-size: calc(11px * var(--ui-scale));
  color: #888;
  margin-right: calc(2px * var(--ui-scale));
}

.brush-sizes {
  display: flex;
  gap: calc(3px * var(--ui-scale));
}

.brush-btn {
  width: calc(32px * var(--ui-scale));
  height: calc(28px * var(--ui-scale));
  border: none;
  border-radius: calc(6px * var(--ui-scale));
  background: rgba(255,255,255,0.06);
  color: #aaa;
  font-size: calc(11px * var(--ui-scale));
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

@media (hover: hover) {
  .brush-btn:hover {
    background: rgba(255,255,255,0.12);
    color: #ddd;
  }
}

.brush-btn.active {
  background: rgba(233,69,96,0.3);
  color: #e94560;
}

.eraser-btn {
  width: calc(36px * var(--ui-scale));
  height: calc(28px * var(--ui-scale));
  border: none;
  border-radius: calc(6px * var(--ui-scale));
  background: rgba(255,255,255,0.06);
  color: #aaa;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
  margin-left: calc(4px * var(--ui-scale));
}

@media (hover: hover) {
  .eraser-btn:hover {
    background: rgba(255,255,255,0.12);
    color: #ddd;
  }
}

.eraser-btn.active {
  background: rgba(233,69,96,0.3);
  color: #e94560;
}

/* Small buttons */
.btn-sm {
  width: calc(30px * var(--ui-scale));
  height: calc(30px * var(--ui-scale));
  border-radius: calc(6px * var(--ui-scale));
}

.control-divider {
  width: 1px;
  height: calc(24px * var(--ui-scale));
  background: rgba(255,255,255,0.1);
  margin: 0 calc(2px * var(--ui-scale));
  flex-shrink: 0;
}

/* Weather active state */
.btn-icon.active {
  background: rgba(100,180,255,0.25);
  color: #7cc4ff;
}

/* Sound muted */
.btn-icon.muted {
  color: #666;
}

.btn-icon.muted .sound-waves {
  display: none;
}

/* Save panel */
.save-card {
  max-width: 360px;
  width: 90%;
}

.save-hint {
  font-size: 12px;
  color: #888;
  text-align: center;
  margin-top: -4px;
  margin-bottom: 4px;
  line-height: 1.4;
}

.save-slots {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 55vh;
  max-height: 55dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.save-slots::-webkit-scrollbar { display: none; }

.save-slot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: rgba(255,255,255,0.05);
  border-radius: 8px;
  border: 1px solid transparent;
}

.save-slot.save-slot-active {
  background: rgba(76, 255, 124, 0.08);
  border-color: rgba(76, 255, 124, 0.25);
}

.save-slot-active-badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 600;
  color: #4cff7c;
  background: rgba(76, 255, 124, 0.15);
  padding: 1px 6px;
  border-radius: 4px;
  margin-left: 4px;
  vertical-align: 1px;
}

.save-slot-active-tag {
  font-size: 11px;
  color: #4cff7c;
  font-weight: 600;
  padding: 5px 10px;
}

.save-slot-btn.use-action {
  background: rgba(240, 192, 64, 0.18);
  color: #f0c040;
}

@media (hover: hover) {
  .save-slot-btn.use-action:hover {
    background: rgba(240, 192, 64, 0.32);
  }
}

.save-slot-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.save-slot-num {
  font-size: 13px;
  font-weight: 600;
  color: #ccc;
}

.save-slot-date {
  font-size: 11px;
  color: #777;
}

.save-slot-actions {
  display: flex;
  gap: 6px;
}

.save-slot-btn {
  padding: 5px 12px;
  border: none;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}

.save-slot-btn.save-action {
  background: rgba(100,180,255,0.2);
  color: #7cc4ff;
}

@media (hover: hover) {
  .save-slot-btn.save-action:hover {
    background: rgba(100,180,255,0.35);
  }
}

.save-slot-btn.load-action {
  background: rgba(100,255,150,0.15);
  color: #7cffaa;
}

@media (hover: hover) {
  .save-slot-btn.load-action:hover {
    background: rgba(100,255,150,0.3);
  }
}

.save-slot-btn:disabled {
  opacity: 0.3;
  cursor: default;
}

/* Locked materials */
.mat-btn.locked {
  opacity: 0.7;
  position: relative;
}

.mat-btn.locked .mat-swatch {
  filter: grayscale(0.6) brightness(0.8);
}

.mat-ad-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f0c040, #e09020);
  border-radius: 50%;
  color: #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.4);
  pointer-events: none;
}

.mat-ad-badge svg {
  margin-left: 1px;
}

/* Locked save slot */
.save-slot-locked {
  opacity: 0.6;
}

.save-slot-locked .save-slot-date {
  color: #f0c040;
}

.slot-unlock-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  background: linear-gradient(135deg, rgba(240,192,64,0.25), rgba(224,144,32,0.25));
  color: #f0c040;
}

@media (hover: hover) {
  .slot-unlock-btn:hover {
    background: linear-gradient(135deg, rgba(240,192,64,0.4), rgba(224,144,32,0.4));
  }
}

/* Unlock popup */
.unlock-popup {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  opacity: 0;
  transition: opacity 0.2s;
  padding: 12px;
}

.unlock-popup.visible {
  opacity: 1;
}

.unlock-popup-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.unlock-popup-card {
  position: relative;
  background: linear-gradient(160deg, #1e2140 0%, #161830 100%);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 16px;
  padding: 28px 32px;
  max-width: 320px;
  width: 85%;
  max-height: calc(100vh - 24px);
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}

.unlock-popup-swatch {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}

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

.unlock-popup-desc {
  font-size: 13px;
  color: #999;
  text-align: center;
  line-height: 1.4;
}

.unlock-popup-buttons {
  display: flex;
  gap: 10px;
  margin-top: 8px;
  width: 100%;
}

.unlock-popup-btn {
  flex: 1;
  padding: 10px 16px;
  border: none;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.15s;
}

.unlock-popup-watch {
  background: linear-gradient(135deg, #e94560, #c23152);
  color: #fff;
}

@media (hover: hover) {
  .unlock-popup-watch:hover {
    background: linear-gradient(135deg, #ff5a75, #d93d62);
  }
}

.unlock-popup-watch:disabled {
  opacity: 0.6;
  cursor: default;
}

.unlock-popup-cancel {
  background: rgba(255,255,255,0.08);
  color: #aaa;
}

@media (hover: hover) {
  .unlock-popup-cancel:hover {
    background: rgba(255,255,255,0.14);
    color: #ddd;
  }
}

/* Mobile adjustments — bar sizing is handled by --ui-scale (JS), so only
   popup tweaks remain here. */
@media (max-width: 480px) {
  .unlock-popup-card {
    padding: 20px 20px;
  }

  .unlock-popup-swatch {
    width: 40px;
    height: 40px;
  }

  .unlock-popup-title {
    font-size: 17px;
  }

  .unlock-popup-desc {
    font-size: 12px;
  }

  .unlock-popup-btn {
    padding: 9px 12px;
    font-size: 13px;
  }
}

/* Quest panel */
.quest-card {
  max-width: 380px;
  width: 90%;
}

.quest-list {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 55vh;
  max-height: 55dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.quest-list::-webkit-scrollbar { display: none; }

.quest-item {
  display: flex;
  gap: 10px;
  padding: 10px 12px;
  background: rgba(255,255,255,0.05);
  border-radius: 10px;
  align-items: flex-start;
}

.quest-swatch {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  flex-shrink: 0;
  margin-top: 2px;
}

.quest-info {
  flex: 1;
  min-width: 0;
}

.quest-name {
  font-size: 13px;
  font-weight: 600;
  color: #e0e0e0;
  display: flex;
  align-items: center;
  gap: 6px;
}

.quest-xp {
  font-size: 11px;
  font-weight: 700;
  color: #e94560;
  white-space: nowrap;
}

.quest-desc {
  font-size: 11px;
  color: #888;
  margin-top: 2px;
  line-height: 1.3;
}

.quest-bar {
  width: 100%;
  height: 5px;
  background: rgba(255,255,255,0.08);
  border-radius: 3px;
  overflow: hidden;
  margin-top: 6px;
}

.quest-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, #e94560, #ff6b6b);
  border-radius: 3px;
  transition: width 0.3s ease;
}

.quest-count {
  font-size: 10px;
  color: #666;
  margin-top: 2px;
  text-align: right;
}

.quest-empty {
  text-align: center;
  padding: 20px;
  color: #888;
  font-size: 14px;
}

/* Quest completion toast */
.quest-toast {
  position: fixed;
  top: 52px;
  left: 50%;
  transform: translateX(-50%) translateY(-20px);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 18px;
  background: linear-gradient(135deg, #1e2a40 0%, #1a2035 100%);
  border: 1px solid rgba(76, 255, 124, 0.3);
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.4);
  z-index: 200;
  opacity: 0;
  transition: opacity 0.3s ease, transform 0.3s ease;
  pointer-events: none;
}

.quest-toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.quest-toast-icon {
  flex-shrink: 0;
}

.quest-toast-title {
  font-size: 13px;
  font-weight: 600;
  color: #e0e0e0;
}

.quest-toast-xp {
  font-size: 12px;
  font-weight: 700;
  color: #4cff7c;
}

/* Very narrow screens — bar element sizes come from --ui-scale; here we only
   drop the dividers and tighten control gaps so all top buttons still fit. */
@media (max-width: 360px) {
  .level-bar {
    min-width: 0;
    width: calc(48px * var(--ui-scale));
  }

  .top-controls {
    gap: calc(2px * var(--ui-scale));
  }

  .control-divider {
    display: none;
  }
}
