*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --surface-hover: #252525;
  --border: #333;
  --border-dim: #1f1f1f;
  --text: #e8e8e8;
  --text-dim: #888;
  --text-faint: #666;
  --accent: #ff0000;
  --placeholder: #555;
  --link: #3ea6ff;
  --card-hover: rgba(255, 255, 255, 0.04);
  --card-shadow: rgba(0, 0, 0, 0.4);
  --badge-bg: rgba(255, 255, 255, 0.08);
  --shimmer: rgba(255, 255, 255, 0.04);
  --overlay: rgba(255, 255, 255, 0.1);
  --overlay-subtle: rgba(255, 255, 255, 0.05);
  --icon-filter: brightness(0) invert(0.6);
  --icon-hover-filter: brightness(0) invert(1);
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --font: "JetBrains Mono", "SF Mono", "Fira Code", "Cascadia Code", monospace;
}

[data-theme="light"] {
  --bg: #ffffff;
  --surface: #f8f8f8;
  --surface-hover: #ebebeb;
  --border: #e0e0e0;
  --border-dim: #eee;
  --text: #0f0f0f;
  --text-dim: #555;
  --text-faint: #777;
  --placeholder: #999;
  --link: #065fd4;
  --card-hover: rgba(0, 0, 0, 0.03);
  --card-shadow: rgba(0, 0, 0, 0.08);
  --badge-bg: rgba(0, 0, 0, 0.06);
  --shimmer: rgba(0, 0, 0, 0.04);
  --overlay: rgba(0, 0, 0, 0.06);
  --overlay-subtle: rgba(0, 0, 0, 0.03);
  --icon-filter: brightness(0) invert(0.4);
  --icon-hover-filter: brightness(0);
}

html {
  scroll-behavior: smooth;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.6;
  min-height: 100vh;
  width: 100%;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  transition:
    background 0.3s var(--ease),
    color 0.3s var(--ease);
}

/* ---- Right Controls (Auth & Theme) ---- */

.right-controls {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
}

[data-view="results"] .right-controls,
[data-view="player"] .right-controls {
  position: static;
  justify-self: end;
}

.auth-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 6px;
  font-family: var(--font);
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s var(--ease);
  white-space: nowrap;
}

.auth-btn:hover {
  background: var(--surface-hover);
}

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

.theme-toggle {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-dim);
  transition:
    background 0.2s var(--ease),
    border-color 0.2s var(--ease),
    color 0.2s var(--ease);
}

.icon-sun,
.icon-moon {
  display: none;
}

[data-theme="dark"] .icon-sun {
  display: block;
}
[data-theme="dark"] .icon-moon {
  display: none;
}

[data-theme="light"] .icon-sun {
  display: none;
}
[data-theme="light"] .icon-moon {
  display: block;
}

/* ---- Search Wrapper (animates center -> top) ---- */

.search-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 30vh 20px 0;
  width: 100%;
  position: relative;
  transition:
    padding 0.5s var(--ease),
    border-color 0.5s var(--ease),
    background 0.3s var(--ease);
  border-bottom: 1px solid transparent;
}

[data-view="results"] .search-wrapper,
[data-view="player"] .search-wrapper {
  display: grid;
  grid-template-columns: 160px 1fr 160px;
  align-items: center;
  padding: 14px 20px;
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg);
  border-bottom-color: var(--border);
}

/* ---- Brand ---- */

.brand {
  text-align: center;
  max-height: 120px;
  margin-bottom: 32px;
  opacity: 1;
  overflow: hidden;
  transition:
    max-height 0.5s var(--ease),
    opacity 0.3s var(--ease),
    margin 0.5s var(--ease);
}

[data-view="results"] .brand,
[data-view="player"] .brand {
  position: absolute;
  max-height: 0;
  opacity: 0;
  margin: 0;
}

.logo {
  font-size: 2.5rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -1px;
}

.tagline {
  color: var(--text-dim);
  font-size: 0.85rem;
  margin-top: 8px;
  font-weight: 400;
}

/* ---- Home Button ---- */

.home-btn {
  display: none;
  background: none;
  border: none;
  color: var(--accent);
  font-family: var(--font);
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  letter-spacing: -0.5px;
  padding: 4px 0;
  transition: opacity 0.2s var(--ease);
}

.home-btn:hover {
  opacity: 0.7;
}

[data-view="results"] .home-btn,
[data-view="player"] .home-btn {
  display: block;
  justify-self: start;
}

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

/* ---- Search Bar ---- */

.search-bar {
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 600px;
  justify-self: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  position: relative;
  transition:
    border-color 0.25s var(--ease),
    box-shadow 0.25s var(--ease),
    background 0.3s var(--ease);
}

.search-bar:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(255, 0, 0, 0.1);
}

#searchInput {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.9rem;
  padding: 12px 36px 12px 16px;
  outline: none;
  min-width: 0;
}

#searchInput::placeholder {
  color: var(--placeholder);
}

.clear-btn {
  position: absolute;
  right: 52px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  border-radius: 50%;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition:
    opacity 0.2s var(--ease),
    background 0.2s var(--ease);
}

.clear-btn.visible {
  opacity: 1;
  pointer-events: auto;
}

.clear-btn:hover {
  background: var(--overlay);
}

.search-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-left: 1px solid var(--border);
  width: 48px;
  min-height: 44px;
  cursor: pointer;
  transition: background 0.2s var(--ease);
  flex-shrink: 0;
}

.search-btn:hover {
  background: var(--overlay-subtle);
}

.search-bar img {
  width: 18px;
  height: 18px;
  filter: var(--icon-filter);
  transition: filter 0.2s var(--ease);
}

.search-btn:hover img,
.clear-btn:hover img {
  filter: var(--icon-hover-filter);
}

/* ---- Main Sections ---- */

main {
  display: none;
}

[data-view="results"] main,
[data-view="player"] main {
  display: block;
}

.results-section {
  max-width: 900px;
  margin: 0 auto;
  padding: 0 20px;
}

.player-section {
  display: none;
}

[data-view="player"] .results-section {
  display: none;
}
[data-view="player"] .player-section {
  display: block;
  animation: fadeIn 0.35s var(--ease);
}

/* ---- Filters Bar ---- */

.filters-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0;
  gap: 12px;
  border-bottom: 1px solid var(--border-dim);
}

.filter-chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.filter-chips::-webkit-scrollbar {
  display: none;
}

.filter-chip {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text-dim);
  font-family: var(--font);
  font-size: 0.78rem;
  padding: 6px 16px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s var(--ease);
}

.filter-chip:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.filter-chip.active {
  background: var(--text);
  color: var(--bg);
  border-color: var(--text);
}

#sortSelect {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-dim);
  font-family: var(--font);
  font-size: 0.78rem;
  padding: 6px 28px 6px 12px;
  cursor: pointer;
  outline: none;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23888' d='M2 4l4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  flex-shrink: 0;
  transition: border-color 0.2s var(--ease);
}

#sortSelect:focus {
  border-color: var(--accent);
}

#sortSelect option {
  background: var(--surface);
  color: var(--text);
}

/* ---- Results ---- */

#resultsContainer {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 0;
  min-height: 200px;
}

.result-card {
  display: flex;
  gap: 16px;
  padding: 12px;
  border-radius: 8px;
  cursor: pointer;
  opacity: 0;
  animation: fadeIn 0.4s var(--ease) forwards;
  animation-delay: var(--delay, 0s);
  transition:
    background 0.2s var(--ease),
    transform 0.2s var(--ease),
    box-shadow 0.2s var(--ease);
}

.result-card:hover {
  background: var(--card-hover);
  transform: translateY(-2px);
  box-shadow: 0 4px 16px var(--card-shadow);
}

.result-card:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.result-card .thumb {
  flex-shrink: 0;
  width: 320px;
  aspect-ratio: 16/9;
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}

.result-card .thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.result-card .info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
  padding: 4px 0;
}

.result-card .title {
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.4;
}

.result-card .channel {
  color: var(--text-dim);
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.result-card .meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  color: var(--text-dim);
  font-size: 0.78rem;
}

.result-card .meta span + span::before {
  content: "\00b7";
  margin-right: 6px;
}

.result-card .desc {
  color: var(--text-faint);
  font-size: 0.78rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.5;
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.68rem;
  font-weight: 500;
  background: var(--badge-bg);
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* ---- Skeleton Loading ---- */

.skeleton-card {
  display: flex;
  gap: 16px;
  padding: 12px;
}

.skeleton {
  background: var(--surface);
  border-radius: 8px;
  position: relative;
  overflow: hidden;
}

.skeleton::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, var(--shimmer), transparent);
  animation: shimmer 1.5s infinite;
}

.skeleton-thumb {
  width: 320px;
  aspect-ratio: 16/9;
  flex-shrink: 0;
  border-radius: 8px;
}

.skeleton-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 4px 0;
}

.skeleton-title {
  height: 18px;
  width: 85%;
  border-radius: 4px;
}

.skeleton-meta {
  height: 14px;
  width: 45%;
  border-radius: 4px;
}

.skeleton-desc {
  height: 14px;
  width: 60%;
  border-radius: 4px;
}

/* ---- Load More ---- */

.load-more-wrap {
  display: flex;
  justify-content: center;
  padding: 24px 0 48px;
}

#loadMoreBtn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.85rem;
  padding: 10px 32px;
  cursor: pointer;
  transition:
    background 0.2s var(--ease),
    border-color 0.2s var(--ease),
    opacity 0.2s var(--ease);
}

#loadMoreBtn:hover {
  background: var(--surface-hover);
  border-color: var(--border);
}

#loadMoreBtn:disabled {
  opacity: 0.5;
  cursor: default;
}

#loadMoreBtn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

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

.player-section {
  max-width: 900px;
  margin: 0 auto;
  padding: 20px;
}

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-dim);
  font-family: var(--font);
  font-size: 0.82rem;
  padding: 8px 16px;
  cursor: pointer;
  margin-bottom: 20px;
  transition:
    color 0.2s var(--ease),
    border-color 0.2s var(--ease);
}

.back-btn:hover {
  color: var(--text);
  border-color: var(--text-dim);
}

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

.player-wrap {
  position: relative;
  width: 100%;
  padding-bottom: 56.25%;
  border-radius: 12px;
  overflow: hidden;
  background: #000;
}

.player-wrap iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: none;
}

.video-info {
  padding: 20px 0;
}

.video-info h2 {
  font-size: 1.15rem;
  font-weight: 700;
  line-height: 1.4;
  margin-bottom: 12px;
}

.video-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  color: var(--text-dim);
  font-size: 0.82rem;
  margin-bottom: 20px;
}

.video-meta span + span::before {
  content: "\00b7";
  margin-right: 6px;
}

.video-description {
  border-top: 1px solid var(--border-dim);
  padding-top: 16px;
}

.desc-text {
  color: var(--text-dim);
  font-size: 0.82rem;
  line-height: 1.8;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 80px;
  overflow: hidden;
  transition: max-height 0.4s var(--ease);
}

.desc-text a {
  color: var(--link);
  text-decoration: none;
  transition: opacity 0.2s var(--ease);
}

.desc-text a:hover {
  opacity: 0.8;
}

.desc-text a.timestamp {
  color: var(--link);
  font-weight: 500;
}

.desc-text.expanded {
  max-height: 5000px;
}

.show-more-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-family: var(--font);
  font-size: 0.78rem;
  padding: 8px 0;
  cursor: pointer;
  transition: color 0.2s var(--ease);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.show-more-btn:hover {
  color: var(--text);
}

.player-loading {
  padding: 20px 0;
}

/* ---- Error States ---- */

.error-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-dim);
}

.error-state p {
  font-size: 0.9rem;
  margin-bottom: 16px;
  line-height: 1.6;
}

.error-state button {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.82rem;
  padding: 8px 24px;
  cursor: pointer;
  transition: background 0.2s var(--ease);
}

.error-state button:hover {
  background: var(--surface-hover);
}

/* ---- Animations ---- */

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes shimmer {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(100%);
  }
}

/* ---- Focus States ---- */

.filter-chip:focus-visible,
.search-btn:focus-visible,
.clear-btn:focus-visible,
.show-more-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---- Responsive ---- */

@media (max-width: 768px) {
  .search-wrapper {
    padding-top: 25vh;
  }

  [data-view="results"] .search-wrapper,
  [data-view="player"] .search-wrapper {
    grid-template-columns: auto 1fr auto;
    padding: 12px 16px;
    gap: 12px;
  }

  .logo {
    font-size: 2rem;
  }
  .search-bar {
    max-width: 100%;
  }

  .home-btn {
    font-size: 0.95rem;
  }
  .theme-toggle {
    width: 36px;
    height: 36px;
  }

  .result-card {
    flex-direction: column;
  }
  .result-card .thumb {
    width: 100%;
  }

  .skeleton-card {
    flex-direction: column;
  }
  .skeleton-thumb {
    width: 100%;
  }

  .filters-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  .filter-chips {
    width: 100%;
    overflow-x: auto;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none; /* Firefox */
  }
  .filter-chips::-webkit-scrollbar {
    display: none; /* Chrome/Safari */
  }
  #sortSelect {
    margin-left: 0;
  }

  .player-section {
    padding: 16px;
  }
  .player-wrap {
    border-radius: 8px;
  }
}

@media (max-width: 480px) {
  .search-wrapper {
    padding-top: 20vh;
  }

  [data-view="results"] .search-wrapper,
  [data-view="player"] .search-wrapper {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: 
      "home controls"
      "search search";
    gap: 12px;
  }

  [data-view="results"] .home-btn,
  [data-view="player"] .home-btn {
    grid-area: home;
    justify-self: start;
    display: block; /* Restore visibility */
  }

  [data-view="results"] .search-bar,
  [data-view="player"] .search-bar {
    grid-area: search;
  }

  [data-view="results"] .right-controls,
  [data-view="player"] .right-controls {
    grid-area: controls;
  }

  .brand {
    margin-bottom: 24px;
  }
  .logo {
    font-size: 1.7rem;
  }
  .tagline {
    font-size: 0.78rem;
  }

  #searchInput {
    font-size: 0.82rem;
    padding: 10px 32px 10px 12px;
  }
  .clear-btn {
    right: 44px;
  }
  .search-btn {
    width: 40px;
  }
  .search-bar img {
    width: 16px;
    height: 16px;
  }

  .result-card {
    padding: 8px;
    gap: 12px;
  }
  .result-card .title {
    font-size: 0.88rem;
  }

  .filters-bar {
    padding: 12px 0;
  }
  .filter-chip {
    font-size: 0.72rem;
    padding: 5px 12px;
  }
  #sortSelect {
    font-size: 0.72rem;
  }

  .theme-toggle {
    width: 32px;
    height: 32px;
  }
  .theme-toggle svg {
    width: 15px;
    height: 15px;
  }
}
