:root {
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --tg-theme-bg-color: #050b14;
  --tg-theme-text-color: #ffffff;
  --tg-theme-hint-color: #7bdff2;
  --tg-theme-link-color: #00b4d8;
  --tg-theme-button-color: #0077b6;
  --tg-theme-button-text-color: #ffffff;
  --tg-theme-secondary-bg-color: #0d1e33;
}

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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background-color: var(--tg-theme-bg-color);
  font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  color: var(--tg-theme-text-color);
}

#game-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background: radial-gradient(ellipse at bottom, #112a45 0%, #060e1a 100%);
}

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

/* UI Layers */
.ui-layer {
  position: absolute;
  z-index: 10;
  pointer-events: none;
}

.ui-layer button, .modal-backdrop {
  pointer-events: auto;
}

/* Top HUD */
#hud {
  top: max(10px, var(--sat));
  left: max(10px, var(--sal));
  right: max(10px, var(--sar));
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.tg-user-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(13, 27, 42, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(138, 201, 255, 0.3);
  border-radius: 12px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 700;
  color: #caf0f8;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
}

.tg-user-badge .user-avatar {
  font-size: 16px;
}

.hud-stat {
  background: rgba(13, 27, 42, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(138, 201, 255, 0.25);
  border-radius: 12px;
  padding: 6px 14px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
}

.hud-label {
  font-size: 10px;
  letter-spacing: 1px;
  color: #7bdff2;
  text-transform: uppercase;
  font-weight: 700;
}

.hud-value {
  font-size: 18px;
  font-weight: 800;
  color: #ffffff;
  text-shadow: 0 0 10px rgba(0, 200, 255, 0.5);
}

.hud-value small {
  font-size: 12px;
  color: #a0c4e2;
  font-weight: normal;
}

.coins-box {
  flex-direction: row;
  align-items: center;
  gap: 6px;
  background: rgba(30, 25, 10, 0.8);
  border-color: rgba(255, 215, 0, 0.4);
}

.coins-box .hud-value {
  color: #ffd166;
  text-shadow: 0 0 10px rgba(255, 209, 102, 0.6);
}

.hud-icon {
  font-size: 18px;
}

.hud-controls {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-btn {
  background: rgba(13, 27, 42, 0.85);
  border: 1px solid rgba(138, 201, 255, 0.3);
  color: #fff;
  font-size: 16px;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.icon-btn:hover {
  background: rgba(30, 60, 95, 0.9);
  border-color: #7bdff2;
  transform: scale(1.05);
}

.hud-btn-shop {
  background: linear-gradient(135deg, #0077b6, #0096c7);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  padding: 10px 16px;
  border-radius: 12px;
  cursor: pointer;
  box-shadow: 0 4px 15px rgba(0, 119, 182, 0.5);
  transition: all 0.2s;
  letter-spacing: 0.5px;
}

.hud-btn-shop:hover {
  background: linear-gradient(135deg, #0096c7, #48cae4);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(72, 202, 228, 0.6);
}

/* Booster HUD */
#boosterHud {
  bottom: max(16px, calc(var(--sab) + 12px));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 20;
}

.mobile-boost-btn {
  background: linear-gradient(135deg, #ff4800, #ff0054);
  border: 2px solid rgba(255, 255, 255, 0.4);
  color: #fff;
  font-size: 15px;
  font-weight: 800;
  padding: 12px 28px;
  border-radius: 25px;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(255, 72, 0, 0.6);
  letter-spacing: 0.5px;
  pointer-events: auto;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  transition: transform 0.1s, box-shadow 0.1s;
}

.mobile-boost-btn:active {
  transform: scale(0.93);
  background: linear-gradient(135deg, #ff6a00, #ff1a6a);
  box-shadow: 0 2px 10px rgba(255, 72, 0, 0.8);
}

.boost-meter-container {
  background: rgba(10, 20, 35, 0.85);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 100, 50, 0.4);
  border-radius: 16px;
  padding: 8px 18px;
  box-shadow: 0 6px 25px rgba(255, 80, 0, 0.25);
  width: 280px;
}

.boost-label {
  font-size: 12px;
  font-weight: 700;
  color: #ff9e00;
  margin-bottom: 5px;
  display: flex;
  justify-content: space-between;
}

.boost-bar-track {
  width: 100%;
  height: 12px;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.boost-bar-fill {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, #ff4800, #ff9e00, #ffea00);
  border-radius: 6px;
  transition: width 0.1s ease;
  box-shadow: 0 0 10px rgba(255, 120, 0, 0.7);
}

.boost-tip {
  font-size: 12px;
  color: #fff;
  background: rgba(0, 0, 0, 0.6);
  padding: 4px 12px;
  border-radius: 20px;
  letter-spacing: 0.5px;
}

.pulse {
  animation: pulse-anim 1.5s infinite;
}

@keyframes pulse-anim {
  0%, 100% { opacity: 0.8; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.05); }
}

/* Aim Tip */
.aim-hint {
  position: absolute;
  top: 40%;
  left: 20%;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
  animation: float-aim 2s infinite ease-in-out;
}

.hint-arrow {
  font-size: 38px;
  color: #48cae4;
  text-shadow: 0 0 12px #00b4d8;
}

.hint-text {
  font-size: 14px;
  font-weight: 700;
  background: rgba(5, 25, 45, 0.85);
  padding: 6px 14px;
  border-radius: 20px;
  border: 1px solid rgba(72, 202, 228, 0.5);
  color: #caf0f8;
  box-shadow: 0 4px 15px rgba(0, 180, 216, 0.3);
}

@keyframes float-aim {
  0%, 100% { transform: translate(0, 0); }
  50% { transform: translate(-10px, -6px); }
}

/* Modals */
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(3, 8, 16, 0.88);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
  padding-top: max(16px, var(--sat));
  padding-bottom: max(16px, var(--sab));
  padding-left: max(12px, var(--sal));
  padding-right: max(12px, var(--sar));
  opacity: 1;
  transition: opacity 0.25s ease;
}

.modal-backdrop.hidden, .hidden {
  display: none !important;
}

.modal-window {
  background: linear-gradient(145deg, #0d1e33, #081220);
  border: 1px solid rgba(138, 201, 255, 0.3);
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.7), 0 0 30px rgba(0, 150, 255, 0.15);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  max-height: calc(100vh - max(32px, calc(var(--sat) + var(--sab) + 16px)));
}

/* Shop Modal */
.shop-window {
  width: 900px;
  max-width: 96vw;
}

.modal-header {
  padding: 16px 22px;
  background: rgba(15, 30, 50, 0.6);
  border-bottom: 1px solid rgba(138, 201, 255, 0.15);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.modal-title {
  display: flex;
  align-items: center;
  gap: 10px;
}

.modal-title h2 {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: 0.5px;
  color: #caf0f8;
}

.title-icon {
  font-size: 24px;
}

.shop-balance {
  margin-left: auto;
  background: rgba(255, 215, 0, 0.15);
  border: 1px solid rgba(255, 215, 0, 0.4);
  padding: 6px 14px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
}

.shop-balance strong {
  font-size: 18px;
  color: #ffd166;
}

.close-btn {
  background: transparent;
  border: none;
  color: #a0c4e2;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
  transition: color 0.2s;
}

.close-btn:hover {
  color: #ff6b6b;
}

.shop-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 14px;
  padding: 20px;
  overflow-y: auto;
  max-height: 60vh;
}

.shop-card {
  background: rgba(14, 28, 48, 0.7);
  border: 1px solid rgba(72, 202, 228, 0.2);
  border-radius: 14px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  transition: transform 0.2s, border-color 0.2s, background 0.2s;
}

.shop-card:hover {
  background: rgba(20, 40, 68, 0.85);
  border-color: rgba(72, 202, 228, 0.6);
  transform: translateY(-2px);
}

.card-top {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}

.card-icon {
  font-size: 28px;
  background: rgba(0, 119, 182, 0.25);
  width: 44px;
  height: 44px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid rgba(72, 202, 228, 0.3);
}

.card-title-group h3 {
  font-size: 15px;
  font-weight: 700;
  color: #ffffff;
}

.card-level {
  font-size: 11px;
  color: #48cae4;
  font-weight: 600;
}

.card-desc {
  font-size: 12px;
  color: #90e0ef;
  opacity: 0.85;
  line-height: 1.4;
  margin-bottom: 12px;
  min-height: 34px;
}

.card-progress-bar {
  height: 6px;
  background: rgba(0, 0, 0, 0.4);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 12px;
}

.card-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #0077b6, #00f5d4);
  border-radius: 3px;
}

.btn-buy-upgrade {
  margin-top: auto;
  background: linear-gradient(135deg, #06d6a0, #05a87d);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #032b21;
  font-weight: 800;
  font-size: 13px;
  padding: 10px;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.2s;
}

.btn-buy-upgrade:hover:not(:disabled) {
  background: linear-gradient(135deg, #10f0b4, #06d6a0);
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(6, 214, 160, 0.4);
}

.btn-buy-upgrade:disabled {
  background: rgba(60, 80, 100, 0.4);
  color: #7b92a6;
  border-color: rgba(255, 255, 255, 0.05);
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-buy-upgrade.maxed {
  background: rgba(255, 215, 0, 0.2);
  color: #ffd166;
  border-color: rgba(255, 215, 0, 0.4);
}

.modal-footer {
  padding: 16px 22px;
  background: rgba(15, 30, 50, 0.6);
  border-top: 1px solid rgba(138, 201, 255, 0.15);
  display: flex;
  justify-content: center;
}

/* Primary Glow Button */
.primary-btn {
  background: linear-gradient(135deg, #ff5400, #ff0054);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #fff;
  font-size: 16px;
  font-weight: 800;
  padding: 12px 32px;
  border-radius: 14px;
  cursor: pointer;
  letter-spacing: 0.5px;
  box-shadow: 0 6px 25px rgba(255, 84, 0, 0.4);
  transition: all 0.2s;
}

.primary-btn:hover {
  background: linear-gradient(135deg, #ff6b1a, #ff1a6b);
  transform: translateY(-2px);
  box-shadow: 0 8px 30px rgba(255, 84, 0, 0.6);
}

.secondary-btn {
  background: linear-gradient(135deg, #0077b6, #0096c7);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  padding: 12px 24px;
  border-radius: 14px;
  cursor: pointer;
  transition: all 0.2s;
}

.secondary-btn:hover {
  background: linear-gradient(135deg, #0096c7, #48cae4);
  transform: translateY(-2px);
}

.share-btn {
  background: linear-gradient(135deg, #229ED9, #0088cc);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  padding: 12px 20px;
  border-radius: 14px;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 4px 15px rgba(34, 158, 217, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.share-btn:hover {
  background: linear-gradient(135deg, #3bb3eb, #0099e6);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(34, 158, 217, 0.6);
}

.toast-msg {
  position: fixed;
  bottom: calc(var(--sab) + 40px);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(13, 27, 42, 0.95);
  border: 1px solid #7bdff2;
  color: #caf0f8;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 700;
  z-index: 1000;
  pointer-events: none;
  animation: fadeInOut 2.8s forwards;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.6);
}

@keyframes fadeInOut {
  0% { opacity: 0; transform: translate(-50%, 15px); }
  15% { opacity: 1; transform: translate(-50%, 0); }
  85% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, -15px); }
}

/* Summary Window */
.summary-window {
  width: 440px;
  max-width: 92vw;
  padding: 26px;
  text-align: center;
  position: relative;
}

.summary-badge {
  display: inline-block;
  align-self: center;
  background: linear-gradient(135deg, #ffd166, #ffb703);
  color: #3d2800;
  font-weight: 800;
  font-size: 13px;
  padding: 4px 14px;
  border-radius: 20px;
  margin-bottom: 12px;
  box-shadow: 0 4px 15px rgba(255, 209, 102, 0.5);
}

.summary-window h2 {
  font-size: 24px;
  font-weight: 800;
  color: #caf0f8;
  margin-bottom: 18px;
}

.summary-stats {
  background: rgba(10, 22, 38, 0.7);
  border: 1px solid rgba(72, 202, 228, 0.2);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 22px;
  text-align: left;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  font-size: 14px;
}

.stat-row.highlight {
  font-size: 18px;
  font-weight: 800;
  color: #ffffff;
}

.stat-row.highlight .stat-val {
  color: #48cae4;
  font-size: 20px;
}

.stat-name {
  color: #90e0ef;
}

.stat-val {
  font-weight: 700;
  color: #ffffff;
}

.stat-divider {
  height: 1px;
  background: rgba(72, 202, 228, 0.2);
  margin: 10px 0;
}

.earned-row {
  font-size: 16px;
  font-weight: 800;
}

.earned-val {
  color: #ffd166;
  font-size: 18px;
  text-shadow: 0 0 10px rgba(255, 209, 102, 0.5);
}

.summary-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

/* Mobile Responsive & Telegram WebApp Adjustments */
@media (max-width: 600px) {
  #hud {
    gap: 6px;
  }
  .hud-stat {
    padding: 4px 8px;
    border-radius: 10px;
  }
  .hud-value {
    font-size: 13px;
  }
  .hud-label {
    font-size: 8px;
  }
  .hud-controls {
    gap: 6px;
  }
  .hud-btn-shop {
    padding: 8px 12px;
    font-size: 11px;
    border-radius: 10px;
    min-height: 38px;
  }
  .icon-btn {
    width: 38px;
    height: 38px;
  }
  .tg-user-badge {
    padding: 4px 8px;
    font-size: 11px;
    border-radius: 10px;
  }
  .boost-meter-container {
    width: 240px;
    padding: 6px 12px;
  }
  .mobile-boost-btn {
    width: 240px;
    padding: 12px 18px;
    font-size: 14px;
  }
  .modal-window {
    width: 96vw;
    border-radius: 16px;
  }
  .modal-header {
    padding: 12px 14px;
    gap: 8px;
  }
  .modal-title h2 {
    font-size: 16px;
  }
  .shop-balance {
    padding: 4px 10px;
    font-size: 12px;
  }
  .shop-balance strong {
    font-size: 15px;
  }
  .shop-grid {
    grid-template-columns: 1fr;
    padding: 10px;
    gap: 10px;
    max-height: 52vh;
  }
  .btn-buy-upgrade {
    min-height: 42px;
    font-size: 13px;
  }
  .summary-window {
    padding: 18px 14px;
  }
  .summary-window h2 {
    font-size: 20px;
    margin-bottom: 12px;
  }
  .stat-row {
    font-size: 13px;
    padding: 4px 0;
  }
  .stat-row.highlight .stat-val {
    font-size: 17px;
  }
  .summary-actions {
    flex-direction: column-reverse;
    gap: 8px;
  }
  .summary-actions button {
    width: 100%;
    min-height: 44px;
  }
}
