/*
  IMPROVEMENTS MADE:
  1.  Best Practices: Added a universal `box-sizing: border-box` rule to prevent common layout issues with padding and borders.
  2.  DRY (Don't Repeat Yourself):
      - Combined common styles for `.nav-link` and `.theme-toggle-btn` to reduce redundancy.
      - Removed a duplicated rule for `.dark-mode .nav-link.active`.
  3.  Enhanced Variables:
      - Introduced new CSS variables for hover states (`--card-bg-light-hover`, `--card-bg-dark-hover`, etc.) to improve theme consistency.
      - Created a variable for the form input's focus shadow (`--accent-red-focus-shadow`) to link it to the theme's accent color.
  4.  Accessibility: Added a `:focus-visible` style to buttons and links to provide a clear focus indicator for keyboard users without affecting mouse clicks.
  5.  Robustness: The `stat-content` wrapper around the stat text helps prevent potential flexbox overflow issues. A placeholder class `.body-no-scroll` is added for locking body scroll when the modal is open (requires JS).
  6.  Consistency: Ensured hover effects are consistent across different components like nav items, table rows, and cards.
*/

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

:root {
    --bg-color-light: #f3f4f6;
    --card-bg-light: #ffffff;
    --card-bg-light-hover: #f9fafb;
    --text-primary-light: #111827;
    --text-secondary-light: #4b5563;
    --text-tertiary-light: #6b7280;
    --border-light: #e5e7eb;
    --border-light-hover: #d1d5db;

    --bg-color-dark: #121212;
    --card-bg-dark: #1e1e1e;
    --card-bg-dark-hover: #2a2a2a;
    --text-primary-dark: #e5e5e5;
    --text-secondary-dark: #a3a3a3;
    --text-tertiary-dark: #737373;
    --border-dark: #2e2e2e;
    --border-dark-hover: #444444;

    --accent-red: #ef4444;
    --accent-red-hover: #dc2626;
    --accent-red-focus-shadow: rgba(239, 68, 68, 0.2);
    --accent-green: #22c55e;
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

body {
    font-family: var(--font-sans);
    background-color: var(--bg-color-light);
    color: var(--text-primary-light);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    transition: background-color 0.3s, color 0.3s;
}

.dark-mode body {
    background-color: var(--bg-color-dark);
    color: var(--text-primary-dark);
}

/* Added for modal scroll lock */
.body-no-scroll {
    overflow: hidden;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 1.5rem;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
    flex-wrap: wrap;
    gap: 1rem;
}

.header-title {
    font-size: 1.875rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.header-title i {
    color: var(--accent-red);
}

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

/* Combined .nav-link and .theme-toggle-btn for DRY principle */
.nav-link, .theme-toggle-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.625rem 1rem;
    background-color: var(--card-bg-light);
    border: 1px solid var(--border-light);
    color: var(--text-secondary-light);
    text-decoration: none;
    border-radius: 0.5rem;
    font-weight: 500;
    font-size: 0.875rem;
    transition: all 0.2s ease;
}
.dark-mode .nav-link, .dark-mode .theme-toggle-btn {
    background-color: var(--card-bg-dark);
    border-color: var(--border-dark);
    color: var(--text-secondary-dark);
}

.nav-link:hover, .theme-toggle-btn:hover {
    background-color: var(--card-bg-light-hover);
    border-color: var(--border-light-hover);
    color: var(--text-primary-light);
}
.dark-mode .nav-link:hover, .dark-mode .theme-toggle-btn:hover {
    background-color: var(--card-bg-dark-hover);
    border-color: var(--border-dark-hover);
    color: var(--text-primary-dark);
}

/* Added focus-visible for better accessibility */
.nav-link:focus-visible, .theme-toggle-btn:focus-visible {
    outline: 2px solid var(--accent-red);
    outline-offset: 2px;
}

/* .active state is the same for light and dark, so no need to repeat */
.nav-link.active {
    background-color: var(--accent-red);
    color: white;
    border-color: var(--accent-red);
}

.theme-toggle-btn {
    width: 42px;
    height: 42px;
    justify-content: center;
    padding: 0;
    cursor: pointer;
}

/* Stats Cards */
.stats-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 1rem;
    margin-bottom: 2rem;
}

.stat-card {
    background-color: var(--card-bg-light);
    border: 1px solid var(--border-light);
    border-radius: 0.75rem;
    padding: 1.5rem;
    display: flex;
    align-items: center;
    gap: 1rem;
}
.dark-mode .stat-card {
    background-color: var(--card-bg-dark);
    border-color: var(--border-dark);
}

.stat-icon {
    font-size: 1.5rem;
    color: var(--text-tertiary-light);
    flex-shrink: 0;
}
.dark-mode .stat-icon { color: var(--text-tertiary-dark); }

/* Added wrapper for robust flexbox layout */
.stat-content {
    min-width: 0;
}

.stat-label {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-secondary-light);
}
.dark-mode .stat-label { color: var(--text-secondary-dark); }

.stat-value {
    font-size: 1.5rem;
    font-weight: 700;
    line-height: 1.2;
}
.stat-value.stale {
    color: var(--accent-red);
    cursor: help;
}

/* Main Content Section */
.content-section {
    background-color: var(--card-bg-light);
    border: 1px solid var(--border-light);
    border-radius: 0.75rem;
    padding: 1.5rem;
    margin-bottom: 2rem;
}
.dark-mode .content-section {
    background-color: var(--card-bg-dark);
    border-color: var(--border-dark);
}

.section-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 1.5rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

/* Table Styles */
.table-container { overflow-x: auto; }
.video-table {
    width: 100%;
    border-collapse: collapse;
    text-align: left;
}
.video-table th, .video-table td {
    padding: 1rem;
    vertical-align: middle;
    border-bottom: 1px solid var(--border-light);
}
.dark-mode .video-table th, .dark-mode .video-table td {
    border-bottom-color: var(--border-dark);
}
.video-table th {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-tertiary-light);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    user-select: none;
}
.dark-mode .video-table th { color: var(--text-tertiary-dark); }

.video-table tbody tr { transition: background-color 0.2s ease; }
.video-table tbody tr:hover { background-color: var(--card-bg-light-hover); }
.dark-mode .video-table tbody tr:hover { background-color: #242424; } /* Retained specific color as it differs slightly from card hover */

.video-table td { font-size: 0.875rem; }

.video-table .thumbnail-cell img {
    width: 120px;
    height: 67px;
    object-fit: cover;
    border-radius: 0.5rem;
    display: block; /* Removes bottom space under image */
}
.video-table .title-cell a {
    font-weight: 500;
    color: var(--text-primary-light);
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}
.dark-mode .video-table .title-cell a { color: var(--text-primary-dark); }
.video-table .title-cell a:hover { color: var(--accent-red); }
.change-indicator { color: var(--text-tertiary-light); }
.dark-mode .change-indicator { color: var(--text-tertiary-dark); }
.video-table .title-cell a:hover .change-indicator { color: var(--accent-red); }

.channel-cell a {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
    color: var(--text-secondary-light);
}
.dark-mode .channel-cell a { color: var(--text-secondary-dark); }
.channel-cell a:hover span { color: var(--accent-red); }
.channel-avatar {
    width: 2rem;
    height: 2rem;
    border-radius: 50%;
    object-fit: cover;
}

.rank-cell, .views-cell { text-align: right; }
.rank-change { font-size: 0.75rem; font-weight: 500; margin-top: 0.25rem; }
.rank-up { color: var(--accent-green); }
.rank-down { color: var(--accent-red); }
.rank-new { color: var(--accent-red); font-weight: 600; }
.rank-same { color: var(--text-tertiary-light); }
.dark-mode .rank-same { color: var(--text-tertiary-dark); }
.view-gain-positive { color: var(--accent-green); font-weight: 600; }
.view-gain-negative { color: var(--accent-red); font-weight: 600; }
.hourly-gain { font-size: 0.8rem; color: var(--text-secondary-light); }
.dark-mode .hourly-gain { color: var(--text-secondary-dark); }

.video-table th.text-right { text-align: right; }

/* Sortable Headers */
th[data-sort] { cursor: pointer; }
th[data-sort] .sort-icon {
    color: var(--text-tertiary-light);
    margin-left: 0.375rem;
    opacity: 0.4;
    transition: opacity 0.2s, color 0.2s;
}
.dark-mode th[data-sort] .sort-icon { color: var(--text-tertiary-dark); }
th[data-sort]:hover .sort-icon { opacity: 0.7; }
th[data-sort].sort-asc .sort-icon,
th[data-sort].sort-desc .sort-icon {
    opacity: 1;
    color: var(--accent-red);
}

/* Forms & Buttons */
.form-group { display: flex; gap: 1rem; }
.form-input {
    flex-grow: 1;
    padding: 0.75rem 1rem;
    background-color: var(--bg-color-light);
    border: 1px solid var(--border-light);
    border-radius: 0.5rem;
    color: var(--text-primary-light);
    font-size: 1rem;
}
.dark-mode .form-input {
    background-color: var(--bg-color-dark);
    border-color: var(--border-dark);
    color: var(--text-primary-dark);
}
.form-input:focus {
    outline: none;
    border-color: var(--accent-red);
    box-shadow: 0 0 0 2px var(--accent-red-focus-shadow);
}
.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 0.5rem;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}
.btn:focus-visible {
    outline: 2px solid var(--accent-red);
    outline-offset: 2px;
}
.btn-primary { background-color: var(--accent-red); color: white; }
.btn-primary:hover { background-color: var(--accent-red-hover); }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; }
.help-text { font-size: 0.875rem; color: var(--text-secondary-light); margin-top: 0.75rem; }
.dark-mode .help-text { color: var(--text-secondary-dark); }
.status-message {
    margin-top: 1rem;
    padding: 1rem;
    border-radius: 0.5rem;
    font-weight: 500;
}
.status-success { background-color: #ecfdf5; color: #059669; border: 1px solid #a7f3d0; }
.dark-mode .status-success { background-color: #052e16; color: #6ee7b7; border-color: #047857; }
.status-error { background-color: #fef2f2; color: #dc2626; border: 1px solid #fecaca; }
.dark-mode .status-error { background-color: #3f1212; color: #f87171; border-color: #b91c1c; }

/* States (Loading, Empty) */
.state-container { text-align: center; padding: 4rem 1.5rem; color: var(--text-secondary-light); }
.dark-mode .state-container { color: var(--text-secondary-dark); }
.state-container h3 { font-size: 1.25rem; font-weight: 500; margin-bottom: 0.5rem; color: var(--text-primary-light); }
.dark-mode .state-container h3 { color: var(--text-primary-dark); }
.state-container i { font-size: 2.5rem; margin-bottom: 1rem; color: var(--text-tertiary-light); }
.dark-mode .state-container i { color: var(--text-tertiary-dark); }
.spinner {
    width: 2.5rem; height: 2.5rem;
    border: 4px solid var(--border-light);
    border-top-color: var(--accent-red);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 1rem;
}
.dark-mode .spinner { border-color: var(--border-dark); border-top-color: var(--accent-red); }
@keyframes spin { to { transform: rotate(360deg); } }

/* Cards for Manage/Tracked Pages */
.item-card {
    background-color: var(--card-bg-light);
    border: 1px solid var(--border-light);
    border-radius: 0.75rem;
    padding: 1rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    transition: all 0.2s ease;
    margin-bottom: 1rem;
}
.dark-mode .item-card {
    background-color: var(--card-bg-dark);
    border-color: var(--border-dark);
}
.item-card:hover {
    border-color: var(--border-light-hover);
    background-color: var(--card-bg-light-hover);
}
.dark-mode .item-card:hover {
    border-color: var(--border-dark-hover);
    background-color: var(--card-bg-dark-hover);
}

.item-number {
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--text-tertiary-light);
    min-width: 2rem;
    text-align: center;
    flex-shrink: 0;
}
.dark-mode .item-number { color: var(--text-tertiary-dark); }

.item-thumbnail {
    width: 3rem;
    height: 3rem;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}
.item-thumbnail-video {
    width: 100px;
    height: 56px;
    border-radius: 0.375rem;
}
.item-info { flex-grow: 1; min-width: 0; }
.item-name {
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.item-stats { display: flex; gap: 1rem; flex-wrap: wrap; margin-top: 0.5rem; }
.stat-item {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    font-size: 0.875rem;
    color: var(--text-secondary-light);
}
.dark-mode .stat-item { color: var(--text-secondary-dark); }
.stat-item i { font-size: 0.875rem; color: var(--text-tertiary-light); }
.dark-mode .stat-item i { color: var(--text-tertiary-dark); }

.btn-remove {
    background-color: #fee2e2;
    border: 1px solid #fecaca;
    color: #ef4444;
    width: 2.5rem; height: 2.5rem;
    border-radius: 0.5rem;
    cursor: pointer;
    transition: all 0.2s ease;
    flex-shrink: 0;
}
.dark-mode .btn-remove {
    background-color: #3f1212;
    border-color: #b91c1c;
    color: #f87171;
}
.btn-remove:hover { background-color: #fca5a5; border-color: #f87171; color: #b91c1c; }
.dark-mode .btn-remove:hover { background-color: #5b2121; border-color: #ef4444; color: #fca5a5; }

/* Modal Styles */
.modal-overlay {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(17, 24, 39, 0.7);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  display: flex; justify-content: center; align-items: center;
  z-index: 1000;
}
.dark-mode .modal-overlay {
  background: rgba(0, 0, 0, 0.7);
}
.modal-content {
  background-color: var(--card-bg-light);
  border: 1px solid var(--border-light);
  padding: 1.5rem; border-radius: 1rem;
  width: 90%; max-width: 600px; max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 25px -5px rgba(0,0,0,0.1), 0 10px 10px -5px rgba(0,0,0,0.04);
}
.dark-mode .modal-content { background-color: var(--card-bg-dark); border-color: var(--border-dark); }
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 1rem; border-bottom: 1px solid var(--border-light); padding-bottom: 1rem;
}
.dark-mode .modal-header { border-color: var(--border-dark); }
.modal-header h3 { font-size: 1.25rem; font-weight: 600; margin: 0; }
.modal-close-btn { background: none; border: none; font-size: 1.75rem; cursor: pointer; color: var(--text-secondary-light); padding: 0; line-height: 1; }
.dark-mode .modal-close-btn { color: var(--text-secondary-dark); }
.history-list { list-style: none; padding-left: 0; margin: 0; }
.history-item { padding: 0.75rem 0; border-bottom: 1px solid var(--border-light); }
.dark-mode .history-item { border-color: var(--border-dark); }
.history-item:last-child { border-bottom: none; }
.history-timestamp { font-size: 0.8rem; color: var(--text-tertiary-light); display: block; margin-bottom: 0.25rem; }
.dark-mode .history-timestamp { color: var(--text-tertiary-dark); }
.history-type { font-size: 0.9rem; font-weight: 500; }
.history-from, .history-to { font-size: 0.875rem; color: var(--text-secondary-light); margin-top: 0.25rem; word-break: break-all; }
.dark-mode .history-from, .dark-mode .history-to { color: var(--text-secondary-dark); }

@media (max-width: 768px) {
    .header-title { font-size: 1.5rem; }
    .nav-link span { display: none; }
    .nav-link { padding: 0.625rem; width: 42px; height: 42px; justify-content: center; }
    .form-group { flex-direction: column; }
    .item-card { flex-direction: column; align-items: stretch; }
}


/* Compact List for Dashboard */
.compact-list {
  list-style: none;
  padding: 0;
  margin: 0;
}
.compact-list-item {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: center;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border-light);
}
.dark-mode .compact-list li {
  border-color: var(--border-dark);
}
.compact-list li:last-child {
  border-bottom: none;
}
.compact-list a {
  color: inherit;
  text-decoration: none;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 1rem;
}
.compact-list a:hover {
  color: var(--accent-red);
}


/* Empty state for compact lists */
.list-empty-state {
    padding: 1rem 0;
    text-align: center;
    color: var(--text-secondary-light);
    font-style: italic;
    font-size: 0.875rem;
}
.dark-mode .list-empty-state {
    color: var(--text-secondary-dark);
}


/* Rich list items for dashboard */
.list-item-thumb {
  width: 80px;
  height: 45px;
  object-fit: cover;
  border-radius: 0.375rem;
  flex-shrink: 0;
}

.list-item-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.list-item-info {
  flex-grow: 1;
  min-width: 0; /* Important for flexbox ellipsis */
}

.list-item-info a {
  display: block;
}

.list-item-meta {
  font-size: 0.8rem;
  color: var(--text-secondary-light);
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin-top: 0.25rem;
}
.dark-mode .list-item-meta {
  color: var(--text-secondary-dark);
}

.list-item-gain {
  font-weight: 600;
  text-align: right;
  min-width: 60px; /* Ensure alignment */
  flex-shrink: 0;
}


.hourly-gain-badge {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-secondary-light);
    background-color: var(--card-bg-light-hover);
    padding: 0.125rem 0.375rem;
    border-radius: 0.25rem;
    border: 1px solid var(--border-light);
}
.dark-mode .hourly-gain-badge {
    color: var(--text-secondary-dark);
    background-color: var(--card-bg-dark-hover);
    border-color: var(--border-dark);
}