:root {
  --primary-bg-color: #141414;
  --secondary-bg-color: #1e1e1e;
  --card-bg-color: #2a2a2a;
  --text-color: #f5f5f5;
  --text-secondary-color: #bbb;
  --box-border-color: #333;
  --accent-color: #e50914;
  --accent-hover-color: #f40612;
  --modal-overlay: rgba(0, 0, 0, 0.9);
  --modal-bg: #18181c;
  --radius-lg: 12px;
  --radius-md: 8px;
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.3);
  --transition-fast: 0.2s ease;
}

body {
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu,
    Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
  margin: 0;
  padding: 20px;
  background-color: var(--primary-bg-color);
  color: var(--text-color);
  line-height: 1.6;
}

header {
  text-align: center;
  margin-bottom: 2rem;
}

h1 {
  font-size: 2.5em;
  margin-bottom: 1rem;
  color: var(--accent-color);
}

.controls {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

#search-bar {
  padding: 0.5rem 1rem;
  border-radius: 5px;
  border: 1px solid var(--box-border-color);
  background-color: var(--secondary-bg-color);
  color: var(--text-color);
  font-size: 1em;
  width: 300px;
  max-width: 100%;
}

#search-bar:focus,
#sort-by:focus {
  outline: none;
  border: 1px solid var(--accent-color); /* matches your theme */
  box-shadow: 0 0 5px var(--accent-color); /* optional glow */
}

.sort-container {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

#sort-by {
  padding: 0.5rem;
  border-radius: 5px;
  border: 1px solid var(--box-border-color);
  background-color: var(--secondary-bg-color);
  color: var(--text-color);
  font-size: 1em;
}

.sort-order {
  display: inline-flex;
  margin-left: 0.5rem;
}

.sort-button {
  border: none;
  background: transparent;
  color: inherit;
  padding: 0.5rem 0.75rem;
  cursor: pointer;
  border-radius: 4px;
  transition: background 0.2s;
}

.sort-button:hover {
  background: rgba(255, 255, 255, 0.1);
}

.sort-button.active {
  background: var(--accent-color);
  color: #fff;
}

.genre-container {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

#genre-filter {
  padding: 0.5rem;
  border-radius: 5px;
  border: 1px solid var(--box-border-color);
  background-color: var(--secondary-bg-color);
  color: var(--text-color);
  font-size: 1em;
}

.tab-container {
  display: flex;
  justify-content: center;
  margin-bottom: 15px;
}

.tab-button {
  background: #333;
  color: white;
  border: none;
  padding: 10px 20px;
  margin: 0 5px;
  cursor: pointer;
  border-radius: 5px;
  font-size: 1em;
  transition: background 0.2s ease-in-out;
}

.tab-button.active {
  background: var(--accent-color);
}

.media-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 15px;
  padding: 10px;
}

.media-card {
  background: var(--card-bg-color);
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
  transition:
    transform 0.2s ease-in-out,
    box-shadow 0.2s ease-in-out;
  text-align: center;
  cursor: pointer;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.media-card:hover {
  transform: translateY(-5px);
  box-shadow: var(--shadow-lg);
}

.media-card .media-image {
  flex: 0 0 auto;
}

.media-image {
  position: relative;
}

.media-image img {
  width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  display: block;
  height: auto;
}

.media-rating-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  background-color: rgba(0, 0, 0, 0.6);
  color: var(--text-color);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.9em;
  font-weight: bold;
}

.media-info {
  padding: 0.5rem;
}

.media-title {
  font-size: 1em;
  font-weight: bold;
  margin-bottom: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.media-rating {
  font-size: 0.9em;
  color: var(--text-secondary-color);
}

/* Modal */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--modal-overlay);
  justify-content: center;
  align-items: center;
  z-index: 1000;
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
}
.modal.show {
  display: flex;
  opacity: 1;
}

.loader {
  border: 4px solid var(--secondary-bg-color);
  border-top: 4px solid var(--accent-color);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  animation: spin 1s linear infinite;
  margin: 50px auto;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes slideIn {
  from {
    transform: translateY(-50px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@media (max-width: 480px) {
  .media-grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 10px;
  }
}

#analysis-container {
  padding: 20px;
  max-width: 900px;
  margin: auto;
}

.chart-container {
  margin-bottom: 40px;
}

.chart-title {
  font-size: 1.8em;
  margin-bottom: 20px;
  text-align: center;
  color: var(--accent-color);
}

.chart {
  display: grid;
  gap: 10px;
}

.chart-bar {
  display: flex;
  align-items: center;
  background-color: var(--card-bg-color);
  border-radius: 5px;
  overflow: hidden;
  color: var(--text-color);
}

.chart-label {
  padding: 10px;
  font-weight: bold;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 0;
  width: 200px; /* Adjust as needed */
}

.chart-value {
  height: 100%;
  background-color: var(--accent-color);
  color: white;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding-left: 10px;
  font-size: 0.9em;
  white-space: nowrap;
  transition: width 0.5s ease-in-out;
}

.header-controls {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: nowrap;
}

/* Tabs aligned left, search expands, sort sticks right */
.header-controls .tab-container {
  margin: 0;
  flex-shrink: 0;
}

.header-controls #search-bar {
  flex: 1;
  min-width: 150px;
}

.header-controls .sort-container {
  flex-shrink: 0;
}

body.analysis-mode #search-bar,
body.analysis-mode .sort-container {
  display: none;
}
body.analysis-mode .genre-container {
  display: none;
}

.totals-container {
  text-align: center;
  margin-bottom: 1rem;
}

.totals-line {
  font-size: 1.4em;
  margin: 0.25rem 0;
}

.totals-label {
  color: var(--accent-color); /* red */
  font-weight: bold;
  margin-right: 0.25rem;
}

.totals-value {
  color: var(--text-color); /* regular white */
}

.results-count {
  text-align: center;
  font-size: 0.9em;
  color: var(--text-secondary-color);
  margin: 1rem 0 1rem 0;
}

.explanation pre {
  white-space: pre-wrap; /* wrap long lines */
  word-break: break-word; /* break long tokens if needed */
  max-width: 100%; /* don’t exceed viewport */
  overflow-x: auto; /* allow scroll only if absolutely necessary */
}


/* ---------- Modal: premium two-column metadata layout ---------- */
.modal-content {
  display: flex;
  gap: 20px;
  background: var(--modal-bg);
  width: 90%;
  max-width: 920px;
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  padding-left: 0px;
  padding-right: 5px;
  padding-top: 0px;
  padding-bottom: 0px;
  position: relative;
  overflow-y: auto;
  max-height: 90vh;
  animation: slideIn 0.3s ease-out;
}

/* cover image */
.modal-image {
  flex: 0 0 300px;
  width: 300px;
  border-radius: 0px;
  object-fit: cover;
  aspect-ratio: 2/3;
}

/* right-side container */
.modal-details {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0; /* allow children to shrink properly */
  padding: 24px;
}

/* title centered in the available modal-details space */
.modal-title {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text-color);
  margin: 0;
  text-align: center;           /* centered horizontally */
  width: 100%;
  margin-bottom: 0.6rem;
}

/* metadata grid (two columns on desktop) */
.modal-meta {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px 16px;
  align-items: start;
}

.modal-info {
  margin: 0;
  display: grid;              /* keeps label + value on same line */
  grid-template-columns: auto 1fr; /* label = auto width, value takes the rest */
  gap: 0.25rem;               /* small spacing between label and value */
  margin-bottom: 0.5rem;   /* slightly larger than 0.1rem for consistency */
  font-size: 0.9rem;        /* slightly smaller text for dense layout */
  line-height: 1.5;         /* tighter vertical rhythm */
  align-items: start;
  color: var(--text-secondary-color);
}

.modal-label {
  font-weight: 600;
  color: var(--text-color);
  white-space: nowrap; /* keep label on one line */
  margin-right: 6px;
}

.modal-value {
  color: var(--text-secondary-color);
  white-space: normal;   /* allow wrapping */
  word-break: break-word;
}

/* label / value inside metadata */
.modal-info .modal-label,
.modal-info .modal-value,
.modal-info strong {
  display: inline;  /* inline so they sit on same line */
  margin: 0;
}

.modal-info .modal-label,
.modal-info strong {
  color: var(--text-color);
  font-weight: 600;
  margin-right: 6px; /* space between label and value */
  font-size: 0.95rem;
}
.modal-info .modal-value {
  color: var(--text-secondary-color);
  font-weight: 500;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* description is full-width and below the grid */
.modal-description {
  margin-top: 4px;
  border-radius: 8px;
  color: var(--text-secondary-color);
  line-height: 1.45;
  max-height: 240px;
  overflow-y: auto;
}

/* close button */
.modal-close {
  position: absolute;
  top: 10px;
  right: 14px;
  font-size: 1.8rem;
  color: var(--text-color);
  background: transparent;
  border: none;
  cursor: pointer;
}
.modal-close:hover { color: var(--accent-color); }

/* ensure long label/value pairs wrap gracefully */
.modal-info .modal-value,
.modal-info .modal-label {
  word-break: break-word;
  white-space: normal;
}


/* Cast row spans both columns */
.modal-info.cast {
  grid-column: 1 / -1;  /* span across both columns */
  grid-template-columns: auto 1fr; /* keep label + value layout */
}

@media (max-width: 768px) {
  .controls {
    flex-direction: column;
    align-items: stretch; /* full width, consistent */
    gap: 0.75rem;
  }

  #search-bar,
  #genre-filter,
  .sort-container {
    width: 100%; /* all controls same width */
  }

  .sort-container {
    justify-content: space-between; /* keeps label and buttons balanced */
  }

  .tab-container {
    flex-wrap: wrap;
  }

  .header-controls {
    flex-direction: column;
    align-items: stretch;
  }

  .header-controls .tab-container {
    justify-content: center;
  }

  .header-controls #search-bar {
    width: 100%;
  }

  .header-controls .sort-container {
    width: 100%;
    justify-content: center;
  }

  .modal-content {
    flex-direction: column;
    padding: 14px;
    max-width: 96%;
    width: 95%;
    max-width: 320px;
  }

  .modal-image {
    width: 100%;
    max-width: 260px;
    margin: 0 auto 8px;
    border-radius: 8px;
  }

  .modal-title { font-size: 1.4rem; text-align: center; white-space: normal; }

  .modal-meta {
    grid-template-columns: 1fr;
    gap: 3px;
  }

  .modal-info { min-height: auto; padding: 4px 10px; margin-bottom: 0.1rem;}
  .modal-description { max-height: 200px; }
}

@media (max-width: 600px) {
  .totals-line {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 0.5rem;
  }

  .totals-label {
    margin-right: 0;
    margin-bottom: 0.25rem;
  }

  .chart {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
  }

  .chart-bar {
    display: flex;
    flex-direction: column; /* label above the bar */
    align-items: stretch;
  }

  .chart-label {
    font-size: 0.9rem;
    margin-bottom: 0.2rem;
    word-break: break-word; /* long names won’t overflow */
  }

  .chart-value {
    background: var(--accent-color);
    color: #fff;
    border-radius: 4px;
    padding: 0.2rem 0.4rem;
    white-space: nowrap; /* keeps the stats together */
    overflow: hidden;
    text-overflow: ellipsis; /* truncates if needed */
    min-width: 3rem;
  }

  .chart-value span {
    font-size: 0.75rem; /* shrink text a bit */
  }
}
