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

/* Disable iOS auto-detection links (addresses, dates, etc) */
a[x-apple-data-detectors] {
  color: inherit !important;
  text-decoration: none !important;
  font-size: inherit !important;
  font-family: inherit !important;
  font-weight: inherit !important;
  line-height: inherit !important;
  pointer-events: none !important;
}

html, body {
  font-family: 'Inter', sans-serif;
  background: #1a1a2e;
  overflow: hidden;
  width: 100%;
  height: 100%;
  height: 100dvh;
  touch-action: none;
  overscroll-behavior: none;
  position: fixed;
  top: 0;
  left: 0;
}

/* PWA standalone: use window height directly, no dvh */
@media all and (display-mode: standalone) {
  html, body {
    height: 100vh;
    height: -webkit-fill-available;
  }
}
html.pwa-standalone, html.pwa-standalone body {
  height: 100vh;
  height: -webkit-fill-available;
}

.hidden {
  display: none !important;
}

/* Safe area padding */
: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);
}

/* In PWA standalone mode, no browser nav bar — remove bottom safe area */
@media all and (display-mode: standalone) {
  :root {
    --sab: 0px;
  }
  #splash-overlay {
    padding-bottom: 20px;
  }
  #gameover-popup {
    padding-bottom: 0;
  }
  #leaderboard-screen {
    padding-bottom: 0;
  }
}

/* Fallback for iOS standalone (navigator.standalone) */
html.pwa-standalone {
  --sab: 0px;
}
html.pwa-standalone #splash-overlay {
  padding-bottom: 20px;
}
html.pwa-standalone #gameover-popup {
  padding-bottom: 0;
}
html.pwa-standalone #leaderboard-screen {
  padding-bottom: 0;
}

/* SPLASH SCREEN */
#splash-screen {
  position: relative;
  width: 100%;
  height: 100%;
}

#splash-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 1;
}

#splash-overlay {
  position: relative;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  background: rgba(15, 15, 30, 0.55);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  padding: 20px;
  padding-top: var(--sat);
  padding-bottom: var(--sab);
  padding-bottom: calc(20px + var(--sab));
}

#game-title {
  font-family: 'Russo One', sans-serif;
  font-size: clamp(40px, 12vw, 80px);
  color: #e74c3c;
  text-shadow: 0 0 30px rgba(231, 76, 60, 0.5), 0 4px 8px rgba(0,0,0,0.5);
  letter-spacing: 6px;
  margin-bottom: 4px;
}

#game-subtitle {
  font-size: clamp(12px, 3.5vw, 20px);
  color: #888;
  letter-spacing: 4px;
  text-transform: uppercase;
  margin-bottom: clamp(8px, 2vh, 16px);
}

#btn-legende {
  display: inline-block;
  background: rgba(255,255,255,0.1);
  color: #ddd;
  font-family: 'Inter', sans-serif;
  font-size: clamp(11px, 3vw, 14px);
  font-weight: 700;
  text-decoration: none;
  letter-spacing: 1px;
  padding: 6px 16px;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 20px;
  margin-bottom: clamp(12px, 3vh, 24px);
  transition: all 0.2s;
  cursor: pointer;
}

#btn-legende:active {
  background: rgba(255,255,255,0.15);
  color: #fff;
}

#top3-container {
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 12px;
  padding: 14px 20px;
  margin-bottom: clamp(16px, 4vh, 32px);
  width: 100%;
  max-width: 320px;
}

#top3-container h3 {
  color: #ccc;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 2px;
  text-align: center;
  margin-bottom: 10px;
}

.top3-entry {
  display: flex;
  align-items: center;
  padding: 5px 0;
  color: #ddd;
  font-size: 15px;
}

.top3-entry:first-child .rank { color: #f1c40f; }
.top3-entry:nth-child(2) .rank { color: #bdc3c7; }
.top3-entry:nth-child(3) .rank { color: #cd7f32; }

.rank {
  font-weight: 700;
  width: 28px;
  font-size: 17px;
}

.top3-name {
  flex: 1;
  font-weight: 600;
}

.top3-score {
  font-family: 'Russo One', sans-serif;
  color: #e74c3c;
  font-size: 17px;
}

/* BUTTONS */
#splash-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 280px;
}

.btn {
  border: none;
  border-radius: 10px;
  padding: 16px 32px;
  font-family: 'Inter', sans-serif;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  text-transform: uppercase;
  letter-spacing: 2px;
  /* Mobile touch targets - min 48px height */
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn:active {
  transform: scale(0.96);
}

.btn-primary {
  background: linear-gradient(135deg, rgba(231, 76, 60, 0.7), rgba(192, 57, 43, 0.7));
  color: white;
  box-shadow: 0 4px 15px rgba(231, 76, 60, 0.3);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.15);
}

.btn-primary:active {
  box-shadow: 0 2px 8px rgba(231, 76, 60, 0.2);
}

.btn-secondary {
  background: rgba(255,255,255,0.1);
  color: #ddd;
  border: 1px solid rgba(255,255,255,0.2);
}

.btn-link {
  background: none;
  color: #888;
  font-size: 14px;
  padding: 8px;
  min-height: 40px;
  text-decoration: underline;
  letter-spacing: 1px;
}

#copyright-text {
  color: rgba(255,255,255,0.35);
  font-size: 11px;
  text-align: center;
  letter-spacing: 1px;
  margin-top: 4px;
}

/* GAME SCREEN */
#game-screen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  /* Overshoot bottom to cover home indicator area */
  height: calc(100% + env(safe-area-inset-bottom, 0px));
  background: #5a4a30;
}

#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

#btn-pause {
  position: absolute;
  top: calc(12px + var(--sat));
  left: calc(16px + var(--sal));
  z-index: 20;
  background: rgba(0,0,0,0.25);
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  font-size: 18px;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  touch-action: manipulation;
}

#btn-pause:active {
  background: rgba(0,0,0,0.4);
}

.pause-icon {
  display: flex;
  gap: 3px;
}

.pause-icon::before,
.pause-icon::after {
  content: '';
  display: block;
  width: 4px;
  height: 14px;
  background: #fff;
  border-radius: 1px;
}

.play-icon {
  display: block;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 7px 0 7px 12px;
  border-color: transparent transparent transparent #fff;
  margin-left: 2px;
}

#btn-music {
  position: absolute;
  top: calc(12px + var(--sat));
  left: calc(64px + var(--sal));
  z-index: 20;
  background: rgba(0,0,0,0.25);
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  font-size: 18px;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  touch-action: manipulation;
}

#btn-music:active {
  background: rgba(0,0,0,0.4);
}

#score-display {
  position: absolute;
  top: calc(12px + var(--sat));
  right: calc(16px + var(--sar));
  font-family: 'Russo One', sans-serif;
  font-size: clamp(20px, 5vw, 32px);
  color: #fff;
  text-shadow: 0 1px 4px rgba(0,0,0,0.5);
  pointer-events: none;
}

/* GAMEOVER POPUP */
#gameover-popup {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(15, 15, 30, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  padding: var(--sat) var(--sar) var(--sab) var(--sal);
}

#gameover-content {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 16px;
  padding: 28px 24px;
  text-align: center;
  width: 90%;
  max-width: 360px;
  animation: popIn 0.3s ease-out;
}

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

#gameover-content h2 {
  font-family: 'Russo One', sans-serif;
  font-size: 28px;
  color: #e74c3c;
  margin-bottom: 6px;
}

#final-score-text {
  font-size: 16px;
  color: #ccc;
  margin-bottom: 4px;
}

#final-rank-text {
  font-size: 13px;
  color: #888;
  margin-bottom: 16px;
}

#final-score {
  font-family: 'Russo One', sans-serif;
  font-size: 26px;
  color: #f1c40f;
}

#username-input {
  width: 100%;
  padding: 14px 16px;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 8px;
  background: rgba(255,255,255,0.08);
  color: white;
  font-size: 16px;
  font-family: 'Inter', sans-serif;
  margin-bottom: 14px;
  outline: none;
  transition: border-color 0.2s;
  /* Prevent iOS zoom on focus */
  font-size: 16px;
  -webkit-user-select: text;
  user-select: text;
}

#username-input:focus {
  border-color: #e74c3c;
}

#username-input::placeholder {
  color: #999;
}

#gameover-buttons {
  display: flex;
  gap: 10px;
}

#gameover-buttons .btn {
  flex: 1;
  padding: 14px 12px;
  font-size: 13px;
}

#btn-back-home {
  margin-top: 10px;
  margin-left: auto;
  margin-right: auto;
  width: auto;
  display: inline-flex;
}

/* LEADERBOARD */
#leaderboard-screen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(15, 15, 30, 0.55);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: var(--sat) var(--sar) var(--sab) var(--sal);
}

#leaderboard-content {
  width: 90%;
  max-width: 400px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 16px;
  padding: 24px 20px;
  margin-top: calc(24px + var(--sat));
  margin-bottom: calc(24px + var(--sab));
}

#leaderboard-content h2 {
  font-family: 'Russo One', sans-serif;
  font-size: 26px;
  color: #e74c3c;
  text-align: center;
  margin-bottom: 18px;
}

.lb-entry {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  margin-bottom: 4px;
  border-radius: 8px;
  color: #ddd;
  font-size: 15px;
}

.lb-entry:nth-child(odd) {
  background: rgba(255,255,255,0.04);
}

.lb-entry:first-child {
  background: rgba(241, 196, 15, 0.1);
  border: 1px solid rgba(241, 196, 15, 0.2);
}

.lb-rank {
  font-weight: 700;
  width: 32px;
  font-size: 16px;
  color: #888;
}

.lb-entry:nth-child(1) .lb-rank { color: #f1c40f; }
.lb-entry:nth-child(2) .lb-rank { color: #bdc3c7; }
.lb-entry:nth-child(3) .lb-rank { color: #cd7f32; }

.lb-name {
  flex: 1;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lb-score {
  font-family: 'Russo One', sans-serif;
  color: #e74c3c;
  font-size: 17px;
  margin-left: 8px;
}

#btn-back {
  width: 100%;
  margin-top: 20px;
}

/* Empty leaderboard */
.lb-empty {
  text-align: center;
  color: #666;
  padding: 40px 0;
  font-size: 15px;
}

/* GUIDE SCREEN */
#guide-screen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(15, 15, 30, 0.55);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: 50;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: var(--sat) var(--sar) var(--sab) var(--sal);
}

#guide-content {
  width: 90%;
  max-width: 420px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 16px;
  padding: 24px 20px;
  margin-top: calc(24px + var(--sat));
  margin-bottom: calc(24px + var(--sab));
}

.guide-header {
  text-align: center;
  margin-bottom: 28px;
}

.guide-header h2 {
  font-family: 'Russo One', sans-serif;
  font-size: clamp(28px, 8vw, 48px);
  color: #e74c3c;
  text-shadow: 0 0 20px rgba(231, 76, 60, 0.4), 0 3px 6px rgba(0,0,0,0.4);
  letter-spacing: 4px;
  margin-bottom: 4px;
}

.guide-subtitle {
  font-size: clamp(11px, 3vw, 15px);
  color: #aaa;
  letter-spacing: 3px;
  text-transform: uppercase;
}

.guide-section {
  margin-bottom: 24px;
}

.guide-section-title {
  font-family: 'Russo One', sans-serif;
  font-size: 16px;
  color: #e74c3c;
  text-transform: uppercase;
  letter-spacing: 3px;
  margin-bottom: 14px;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(231, 76, 60, 0.3);
}

.guide-card {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 14px;
}

.guide-bonus-card {
  border-color: rgba(255, 200, 50, 0.2);
  background: rgba(255, 200, 50, 0.05);
}

.guide-card-visual {
  flex-shrink: 0;
  width: 80px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.guide-card-visual canvas {
  display: block;
}

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

.guide-card-name {
  font-family: 'Russo One', sans-serif;
  font-size: 15px;
  color: #fff;
  margin-bottom: 4px;
  letter-spacing: 1px;
}

.guide-tag {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 2px 8px;
  border-radius: 4px;
  margin-bottom: 6px;
}

.tag-danger {
  background: rgba(231, 76, 60, 0.2);
  color: #e74c3c;
}

.tag-obstacle {
  background: rgba(241, 196, 15, 0.15);
  color: #f1c40f;
}

.tag-bonus {
  background: rgba(46, 204, 113, 0.15);
  color: #2ecc71;
}

.guide-card-desc {
  font-size: 13px;
  color: #bbb;
  line-height: 1.5;
}

.guide-card-tip {
  font-size: 12px;
  color: #e74c3c;
  margin-top: 4px;
  font-weight: 600;
}

#btn-guide-back {
  width: 100%;
  max-width: 280px;
  margin: 24px auto 0;
  display: flex;
}

/* Small phones (SE, etc) */
@media screen and (max-height: 600px) {
  #game-title {
    font-size: clamp(32px, 10vw, 52px);
  }
  #game-subtitle {
    margin-bottom: 10px;
    font-size: 11px;
  }
  #top3-container {
    padding: 10px 16px;
    margin-bottom: 12px;
  }
  .top3-entry {
    padding: 3px 0;
    font-size: 14px;
  }
  #splash-buttons {
    gap: 8px;
  }
  .btn {
    padding: 12px 24px;
    font-size: 14px;
  }
  #gameover-content {
    padding: 20px 18px;
  }
  #gameover-content h2 {
    font-size: 24px;
  }
}

/* Landscape mode adjustments */
@media screen and (orientation: landscape) and (max-height: 500px) {

  /* ---- SPLASH ---- */
  #splash-overlay {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 8px 24px;
    padding: 10px 24px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #game-title {
    font-size: clamp(28px, 8vh, 40px);
    width: 100%;
    text-align: center;
    margin-bottom: 0;
    letter-spacing: 4px;
  }

  #game-subtitle {
    width: 100%;
    text-align: center;
    font-size: 10px;
    margin-bottom: 4px;
  }

  #btn-legende {
    width: 100%;
    text-align: center;
    margin-bottom: 8px;
    font-size: 11px;
    padding: 4px 12px;
  }

  #top3-container {
    margin-bottom: 0;
    max-width: 220px;
    padding: 10px 14px;
  }

  #top3-container h3 {
    font-size: 10px;
    margin-bottom: 6px;
  }

  .top3-entry {
    padding: 2px 0;
    font-size: 13px;
  }

  #splash-buttons {
    max-width: 180px;
    gap: 8px;
  }

  #splash-buttons .btn {
    padding: 10px 16px;
    font-size: 13px;
    min-height: 40px;
  }

  #copyright-text {
    font-size: 9px;
    margin-top: 2px;
  }

  /* ---- GAMEOVER POPUP ---- */
  #gameover-popup {
    padding: 8px;
  }

  #gameover-content {
    padding: 16px 20px;
    max-width: 90%;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 4px 20px;
  }

  #gameover-content h2 {
    width: 100%;
    font-size: 22px;
    margin-bottom: 2px;
  }

  #final-score-text {
    font-size: 14px;
    margin-bottom: 2px;
    width: 100%;
  }

  #final-rank-text {
    font-size: 11px;
    margin-bottom: 8px;
    width: 100%;
  }

  #final-score {
    font-size: 20px;
  }

  #gameover-form {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
  }

  #username-input {
    flex: 1;
    min-width: 140px;
    padding: 10px 12px;
    margin-bottom: 0;
  }

  #gameover-buttons {
    flex: 1;
    min-width: 200px;
  }

  #gameover-buttons .btn {
    padding: 10px 8px;
    font-size: 12px;
    min-height: 38px;
  }

  #btn-back-home {
    width: 100%;
    margin-top: 6px;
    font-size: 12px;
    padding: 4px;
    min-height: 30px;
  }

  /* ---- LEADERBOARD ---- */
  #leaderboard-screen {
    align-items: flex-start;
    padding: 8px;
  }

  #leaderboard-content {
    padding: 16px;
    margin-top: 8px;
    margin-bottom: 8px;
    max-height: calc(100vh - 16px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #leaderboard-content h2 {
    font-size: 20px;
    margin-bottom: 10px;
  }

  .lb-entry {
    padding: 6px 10px;
    font-size: 13px;
    margin-bottom: 2px;
  }

  #btn-back {
    margin-top: 12px;
    padding: 10px 16px;
    font-size: 13px;
    min-height: 38px;
  }

  /* ---- GUIDE ---- */
  #guide-screen {
    align-items: flex-start;
    padding: 8px;
  }

  #guide-content {
    padding: 16px;
    margin-top: 8px;
    margin-bottom: 8px;
    max-height: calc(100vh - 16px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .guide-header {
    margin-bottom: 16px;
  }

  .guide-header h2 {
    font-size: clamp(24px, 6vh, 36px);
  }

  .guide-section {
    margin-bottom: 16px;
  }

  .guide-section-title {
    font-size: 13px;
    margin-bottom: 10px;
  }

  .guide-card {
    padding: 10px;
    margin-bottom: 8px;
    gap: 12px;
  }

  .guide-card-visual {
    width: 60px;
    height: 60px;
  }

  .guide-card-name {
    font-size: 13px;
  }

  .guide-card-desc {
    font-size: 12px;
  }

  .guide-card-tip {
    font-size: 11px;
  }

  #btn-guide-back {
    margin-top: 16px;
    padding: 10px 16px;
    font-size: 13px;
    min-height: 38px;
  }
}
