/* base + layout */
* { 
  margin: 0; 
  padding: 0; 
  box-sizing: border-box;
  /* CRITICAL: Prevent zoom on double-tap */
  touch-action: manipulation;
}

html, body { 
  height: 100%; 
  width: 100%;
  font-family: 'Inter', sans-serif; 
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  /* CRITICAL: Prevent text selection that causes zoom */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  /* Prevent overscroll bounce */
  overscroll-behavior: none;
  position: fixed;
}

/* Allow selection only in inputs */
input, textarea {
  -webkit-user-select: text;
  user-select: text;
  touch-action: manipulation;
}

.light-theme { background: #f9f9fb; color: #222; }
.dark-theme { background: #121212; color: #eee; }

/* APP LAYOUT - FIXED for Android scroll */
.app {
  display: grid;
  height: 100vh;
  height: 100dvh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  grid-template-rows: auto auto auto 1fr;
  grid-template-areas:
    "top"
    "search"
    "trigger"
    "main";
  /* Player is now fixed, not in grid */
}

/* TOP BAR */
.top-bar {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.2rem 1.5rem;
  background: #4a90e2;
  color: #fff;
  gap: 1rem;
  height: 80px;
  position: sticky;
  top: 0;
  z-index: 1000;
  /* Prevent zoom on tap */
  touch-action: manipulation;
}

/* logo area */
.logo-area {
  display: flex;
  align-items: center;
  gap: 12px;
  touch-action: manipulation;
}
.app-logo {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  object-fit: cover;
  display: block;
  flex-shrink: 0;
  box-shadow: 0 2px 6px rgba(0,0,0,0.12);
  background: rgba(255,255,255,0.06);
  pointer-events: none;
}
.app-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: #ffffff;
  letter-spacing: 0.2px;
  pointer-events: none;
}

/* profile button */
#profile-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  overflow: hidden;
  border: 3px solid rgba(255,255,255,0.18);
  padding: 0;
  transition: transform .18s;
  background: none;
  cursor: pointer;
  display: grid;
  place-items: center;
  touch-action: manipulation;
}
#profile-btn:active { transform: scale(0.95); }
#nav-avatar { 
  width: 100%; 
  height: 100%; 
  object-fit: cover; 
  display: block;
  pointer-events: none;
}

/* SEARCH */
.search-section {
  grid-area: search;
  padding: 1rem 1.5rem;
  background: #f8f9fa;
  border-bottom: 1px solid #eee;
  z-index: 999;
  touch-action: manipulation;
}
#search {
  width: 100%;
  padding: 1rem 1.3rem;
  font-size: 1.1rem;
  border: 2px solid #ddd;
  border-radius: 14px;
  outline: none;
  transition: all .25s;
  background: #fff;
  touch-action: manipulation;
  -webkit-user-select: text;
  user-select: text;
}
#search:focus { 
  border-color: #4a90e2; 
  box-shadow: 0 0 0 6px rgba(74,144,226,0.08); 
}

/* GENRE DROPDOWN */
.genre-wrapper {
  grid-area: trigger;
  padding: 0.6rem 1.5rem;
  background: #f8f9fa;
  border-bottom: 1px solid #eee;
  z-index: 998;
  display: flex;
  align-items: center;
  touch-action: manipulation;
}
.genre-btn {
  width: 180px;
  padding: 0.8rem 1rem;
  font-size: 1.02rem;
  font-weight: 500;
  background: #4a90e2;
  color: #fff;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .4rem;
  transition: 0.18s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.genre-btn:active { 
  background: #357abd;
  transform: scale(0.98);
}
.genre-btn .arrow { 
  transition: transform .28s;
  pointer-events: none;
}
.genre-btn.open .arrow { transform: rotate(180deg); }

/* dropdown panel */
.genre-menu {
  margin-left: 12px;
  width: 220px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 8px 28px rgba(0,0,0,0.12);
  overflow: hidden;
  max-height: 0;
  opacity: 0;
  transform: translateY(-8px);
  transition: max-height .32s cubic-bezier(.2,.9,.2,1), opacity .22s, transform .22s;
  pointer-events: none;
  touch-action: manipulation;
}
.genre-menu.open {
  max-height: 360px;
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}
.genre-item {
  padding: 0.9rem 1rem;
  cursor: pointer;
  font-size: 1rem;
  color: #333;
  border-bottom: 1px solid rgba(0,0,0,0.04);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.genre-item:last-child { border-bottom: none; }
.genre-item:active { 
  background: #4a90e2; 
  color: #fff; 
}
.genre-item.active { 
  color: #4a90e2; 
  font-weight: 600; 
}

/* CONTENT - FIXED SCROLL for Android */
.content {
  grid-area: main;
  padding: 1rem 1.5rem;
  padding-bottom: 120px; /* Space for fixed player */
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  height: 100%;
  width: 100%;
  position: relative;
  /* CRITICAL: Ensure scroll works on Android */
  overscroll-behavior-y: contain;
  touch-action: pan-y;
  /* Force hardware acceleration */
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
  will-change: scroll-position;
}

/* Smooth scrollbar styling */
.content::-webkit-scrollbar {
  width: 6px;
}
.content::-webkit-scrollbar-track {
  background: transparent;
}
.content::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.2);
  border-radius: 3px;
}
.dark-theme .content::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.2);
}

/* Song grids */
.song-grid {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fill, minmax(160px,1fr));
  width: 100%;
  padding-bottom: 2rem;
  /* Ensure grid doesn't interfere with scroll */
  touch-action: pan-y;
}

/* Hide grids properly */
#grid[hidden], #search-grid[hidden] {
  display: none !important;
}

/* SONG CARD - FIXED: Prevent zoom on tap */
.song-card {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: transform .18s, box-shadow .18s;
  box-shadow: 0 2px 6px rgba(0,0,0,0.08);
  /* CRITICAL: Prevent zoom and improve tap response */
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(74,144,226,0.1);
  -webkit-user-select: none;
  user-select: none;
  /* Prevent layout shift */
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
  /* FIXED: Card itself must be clickable */
  pointer-events: auto;
}

.song-card:active { 
  transform: scale(0.97);
  box-shadow: 0 1px 4px rgba(0,0,0,0.1);
}

.dark-theme .song-card { background: #1e1e1e; }

.song-card img {
  width: 100%;
  height: 140px;
  object-fit: cover;
  display: block;
  /* CRITICAL: Prevent zoom on image but allow parent click */
  pointer-events: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-user-drag: none;
  /* Improve rendering */
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
}

.song-card p {
  padding: 0 .7rem .6rem;
  font-size: .87rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  /* FIXED: Text also shouldn't block clicks */
  pointer-events: none;
}

.home-btn {
  position: fixed;
  top: 20px;
  left: 20px;
  display: flex;
  align-items: center;
  gap: 6px;
  background: #4a90e2;
  color: #fff;
  padding: 0.6rem 1rem;
  border-radius: 8px;
  text-decoration: none;
  font-size: 0.95rem;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  transition: background .2s;
  touch-action: manipulation;
}
.home-btn:active {
  background: #357abd;
  transform: scale(0.97);
}

/* PLAYER - FIXED: Always visible at bottom */
.player {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  display: flex;
  align-items: center;
  padding: .8rem;
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  box-shadow: 0 -4px 12px rgba(0,0,0,0.08);
  gap: .8rem;
  transition: transform .3s ease;
  transform: translateY(100%);
  touch-action: manipulation;
  z-index: 1001;
  /* Ensure it's above everything */
  min-height: 80px;
  max-height: 100px;
}
.player.visible { 
  transform: translateY(0);
}

/* Add safe area padding for notched devices */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .player {
    padding-bottom: max(0.8rem, env(safe-area-inset-bottom));
  }
}

.thumb-placeholder {
  width: 48px;
  height: 48px;
  background: #ccc;
  border-radius: 8px;
  background-size: cover !important;
  background-position: center !important;
  flex-shrink: 0;
  pointer-events: none;
}

.info { 
  flex: 1; 
  min-width: 0;
  pointer-events: none;
}

#player-title {
  margin: 0;
  font-size: .95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}

.seek-bar {
  width: 100%;
  margin-top: 0.5rem;
  touch-action: manipulation;
  pointer-events: auto;
  -webkit-appearance: none;
  height: 4px;
  border-radius: 2px;
  background: rgba(0,0,0,0.1);
  outline: none;
}

.seek-bar::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #4a90e2;
  cursor: pointer;
  touch-action: manipulation;
}

.controls { 
  display: flex; 
  gap: .5rem; 
  flex-shrink: 0;
  touch-action: manipulation;
}

/* icon button styling */
.icon-btn {
  background: none;
  border: none;
  color: #444;
  cursor: pointer;
  padding: .5rem;
  border-radius: 50%;
  transition: .15s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  min-height: 40px;
}

.icon-btn:active { 
  background: rgba(0,0,0,0.1);
  transform: scale(0.95);
}

.icon-btn:disabled { 
  opacity: 0.5; 
  cursor: not-allowed; 
}

.icon-btn .material-icons {
  pointer-events: none;
}

.dark-theme .icon-btn { color: #ddd; }
.dark-theme .icon-btn:active { background: rgba(255,255,255,0.1); }

/* responsive */
@media (max-width:480px) {
  .genre-wrapper { padding: .6rem 1rem; }
  .genre-btn { width: 140px; font-size: .98rem; }
  .genre-menu { width: 160px; margin-left: 10px; }
  .song-card img { height: 120px; }
  .top-bar { height: 70px; padding: 1rem; }
  .content { 
    padding: 1rem;
    padding-bottom: 100px; /* More space for player on mobile */
  }
  .song-grid {
    grid-template-columns: repeat(auto-fill, minmax(140px,1fr));
    gap: 0.8rem;
  }
  .player {
    padding: 0.6rem 0.8rem;
    min-height: 70px;
    gap: 0.6rem;
  }
  .thumb-placeholder {
    width: 42px;
    height: 42px;
  }
  #player-title {
    font-size: 0.85rem;
  }
  .controls {
    gap: 0.3rem;
  }
  .icon-btn {
    padding: 0.3rem;
    min-width: 36px;
    min-height: 36px;
  }
}

/* dark adjustments */
.dark-theme .search-section { background: #1a1a1a; border-color: #333; }
.dark-theme #search { background: #2a2a2a; border-color: #444; color: #eee; }
.dark-theme .genre-wrapper { background: #1a1a1a; border-color: #333; }
.dark-theme .genre-menu { background: #1e1e1e; color: #eee; box-shadow: 0 8px 28px rgba(0,0,0,0.6); }
.dark-theme .genre-item { color: #eee; }
.dark-theme .genre-item:active { background: #4a90e2; color: #fff; }
.dark-theme .player { 
  background: rgba(30, 30, 30, 0.98);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}
.dark-theme .content { background: #121212; }

/* CRITICAL: Android scroll fix - prevent momentum scroll issues */
@supports (-webkit-overflow-scrolling: touch) {
  .content {
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
  }
}

/* Prevent iOS bounce at boundaries */
@supports (-webkit-touch-callout: none) {
  .content {
    overscroll-behavior-y: none;
  }
}