:root {
  --bg: #faf8f5;
  --bg-elevated: #ffffff;
  --bg-elevated-2: #f5f3f0;
  --border: #e0ddd8;
  --text: #1a1a1a;
  --text-dim: #555;
  --text-faint: #777;
  --accent: #a8823f;
  --accent-text: #faf8f5;
  --danger: #b3261e;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --font-display: "Playfair Display", Georgia, serif;
  --font-body: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

.is-hidden {
  display: none !important;
}

html, body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

button {
  font: inherit;
  color: inherit;
}

input[type="search"] {
  font: inherit;
}

.app {
  display: flex;
  flex-direction: column;
  /* Fallback first: some TV/older browsers (e.g. Samsung Smart TV's Tizen
     browser) don't support the newer dvh unit and would otherwise just drop
     the whole declaration, leaving .app with no explicit height at all —
     which breaks every overflow:auto region nested inside it (nothing has a
     bounded height to scroll within, so the whole page stretches instead).
     A browser that doesn't understand 100dvh ignores that single line and
     keeps 100vh; one that does support it uses the more accurate value. */
  height: 100vh;
  height: 100dvh;
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 20;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  flex-shrink: 0;
}

.brand-mark img {
  display: block;
  width: 22px;
  height: 22px;
}

.app-title {
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 700;
  margin: 0;
  white-space: nowrap;
}

.search-wrap {
  position: relative;
  flex: 1;
  min-width: 0;
  margin-left: auto;
  max-width: 360px;
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  color: var(--text-faint);
  pointer-events: none;
}

.search-wrap input {
  width: 100%;
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 9px 12px 9px 32px;
  color: var(--text);
  outline: none;
}

.search-wrap input::placeholder {
  color: var(--text-faint);
}

.search-wrap input:focus {
  border-color: var(--accent);
}

.search-wrap input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.go-home-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 9px 16px;
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  text-decoration: none;
  white-space: nowrap;
}

.go-home-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.go-home-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: none;
  background: transparent;
  border-radius: 10px;
  cursor: pointer;
  flex-shrink: 0;
}

.icon-btn svg {
  width: 22px;
  height: 22px;
}

.icon-btn:hover {
  background: var(--bg-elevated-2);
}

.icon-btn:active {
  transform: scale(0.94);
}

/* Buttons have no visible :hover on TV remotes / keyboard nav (no pointer) —
   without this, moving focus between playlists/tracks/controls with a D-pad
   or Tab key gives no visible indication of where you are. */
.icon-btn:focus-visible,
.playlist-item:focus-visible,
.track-row:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.icon-btn.small {
  width: 34px;
  height: 34px;
}

.icon-btn.small svg {
  width: 18px;
  height: 18px;
}

.icon-btn.toggle.active {
  color: var(--accent);
}

.icon-btn[disabled] {
  opacity: 0.35;
  cursor: default;
}

.icon-btn[disabled]:hover {
  background: transparent;
}

/* Visual-only dimming (unlike [disabled], the button stays clickable) — used
   for the share button before any track is chosen, so a click still always
   does *something* (a "pick a song first" nudge) instead of doing nothing
   and looking broken. */
.icon-btn.inactive {
  opacity: 0.35;
}

/* ---------- Body layout ---------- */

.body {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 25;
}

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(78vw, 300px);
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  z-index: 30;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.22s ease;
  padding-top: var(--safe-top);
}

.sidebar.open {
  transform: translateX(0);
}

.sidebar.open ~ .sidebar-overlay,
.sidebar-overlay.visible {
  display: block;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 8px 10px 16px;
  font-weight: 700;
  font-size: 0.95rem;
  border-bottom: 1px solid var(--border);
}

.playlist-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
  flex: 1;
}

.playlist-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  padding: 11px 10px;
  border: none;
  background: transparent;
  color: var(--text);
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.94rem;
}

.playlist-item:hover {
  background: var(--bg-elevated-2);
}

.playlist-item.selected {
  background: var(--bg-elevated-2);
  color: var(--accent);
}

.playlist-item .pl-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  opacity: 0.8;
}

.playlist-item .pl-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.playlist-item .pl-count {
  font-size: 0.78rem;
  color: var(--text-faint);
  flex-shrink: 0;
}

.playlist-item.selected .pl-count {
  color: var(--accent);
  opacity: 0.8;
}

/* ---------- Main content ---------- */

.content {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 16px 16px 0;
}

.content-header {
  flex-shrink: 0;
  margin-bottom: 8px;
}

.content-title {
  font-family: var(--font-display);
  font-weight: 700;
  margin: 4px 0 2px;
  font-size: 1.4rem;
  overflow-wrap: anywhere;
}

.track-count {
  margin: 0 0 10px;
  color: var(--text-faint);
  font-size: 0.85rem;
}

/* The list is its own scroll region, independent of the header above it —
   the title/count stay put and only the tracks scroll underneath them. */
.track-list {
  list-style: none;
  margin: 0;
  padding: 0 0 calc(24px + var(--safe-bottom));
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.track-list > li {
  display: flex;
  align-items: center;
  gap: 2px;
}

.track-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
  text-align: left;
  padding: 11px 10px;
  border: none;
  background: transparent;
  color: var(--text);
  border-radius: 10px;
  cursor: pointer;
  min-height: 48px;
}

.track-row:hover {
  background: var(--bg-elevated);
}

.track-row.playing {
  background: var(--bg-elevated);
  color: var(--accent);
}

.track-index {
  width: 26px;
  flex-shrink: 0;
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-faint);
}

.track-row.playing .track-index {
  color: var(--accent);
}

.track-index .eq {
  display: inline-flex;
  gap: 2px;
  align-items: flex-end;
  height: 14px;
}

.track-index .eq span {
  width: 3px;
  background: var(--accent);
  animation: eq 0.9s ease-in-out infinite;
}

.track-index .eq span:nth-child(1) { height: 40%; animation-delay: -0.6s; }
.track-index .eq span:nth-child(2) { height: 100%; animation-delay: -0.3s; }
.track-index .eq span:nth-child(3) { height: 65%; animation-delay: 0s; }

@keyframes eq {
  0%, 100% { transform: scaleY(0.4); }
  50% { transform: scaleY(1); }
}

.track-titles {
  flex: 1;
  min-width: 0;
}

.track-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.95rem;
}

.track-sub {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.78rem;
  color: var(--text-faint);
  margin-top: 2px;
}

.empty-state {
  flex-shrink: 0;
  color: var(--text-dim);
  line-height: 1.5;
  max-width: 440px;
  margin-top: 24px;
}

.empty-state code {
  background: var(--bg-elevated-2);
  padding: 2px 6px;
  border-radius: 6px;
  font-size: 0.9em;
}

/* ---------- Player bar ---------- */

.player-bar {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  z-index: 20;
}

.player-meta {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.cover-fallback {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), #8a6733);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  color: var(--accent-text);
}

.meta-text {
  min-width: 0;
  margin-right: auto;
}

.now-title {
  font-size: 0.92rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.now-sub {
  font-size: 0.78rem;
  color: var(--text-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.share-status {
  position: absolute;
  right: 0;
  bottom: calc(100% + 8px);
  background: var(--text);
  color: var(--bg);
  font-size: 0.78rem;
  white-space: nowrap;
  padding: 6px 12px;
  border-radius: 999px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
}

.player-controls {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.transport {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.icon-btn.play {
  width: 46px;
  height: 46px;
  background: var(--accent);
  color: var(--accent-text);
}

.icon-btn.play:hover {
  background: var(--accent);
  opacity: 0.9;
}

.icon-btn.play svg {
  width: 22px;
  height: 22px;
}

.repeat-one-dot {
  position: absolute;
  transform: translate(9px, -9px);
  font-size: 0.55rem;
  font-weight: 700;
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 50%;
  width: 13px;
  height: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#repeatBtn {
  position: relative;
}

.seek-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.time {
  font-size: 0.72rem;
  color: var(--text-faint);
  width: 34px;
  flex-shrink: 0;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  height: 20px;
  background: transparent;
  flex: 1;
  min-width: 0;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 999px;
  background: var(--border);
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  margin-top: -5px;
  border-radius: 50%;
  background: var(--text);
  border: none;
}

input[type="range"]::-moz-range-track {
  height: 4px;
  border-radius: 999px;
  background: var(--border);
}

input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--text);
  border: none;
}

input[type="range"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
}

.volume-wrap {
  display: none;
  align-items: center;
  gap: 8px;
}

.volume-wrap .volume-bar {
  width: 90px;
  flex: none;
}

/* ---------- Desktop / tablet ---------- */

@media (min-width: 860px) {
  .topbar {
    padding: 14px 20px;
  }

  #menuBtn,
  #closeSidebar,
  .sidebar-overlay {
    display: none !important;
  }

  .sidebar {
    position: static;
    transform: none;
    width: 260px;
    flex-shrink: 0;
    z-index: 1;
  }

  .content {
    padding: 20px 28px 0;
  }

  .track-list {
    padding-bottom: calc(28px + var(--safe-bottom));
  }

  .player-bar {
    grid-template-columns: 260px 1fr 200px;
    align-items: center;
    gap: 20px;
    padding: 12px 20px calc(12px + var(--safe-bottom));
  }

  .player-controls {
    max-width: 520px;
    margin: 0 auto;
    width: 100%;
  }

  .volume-wrap {
    display: flex;
    justify-self: end;
  }
}

@media (max-width: 859.98px) {
  .sidebar-header span {
    font-size: 1rem;
  }
}
