* {
  box-sizing: border-box;
}

:root {
  --bg-page: #111827;
  --text-primary: #e5e7eb;
  --text-muted: #94a3b8;
  --topbar-bg: #020617;
  --topbar-text: #e2e8f0;
  --surface: #1f2937;
  --surface-elevated: #0f172a;
  --surface-subtle: #1e293b;
  --border: #334155;
  --border-soft: #475569;
  --divider: #334155;
  --button-primary-bg: #2563eb;
  --button-primary-border: #2563eb;
  --button-primary-text: #ffffff;
  --button-secondary-bg: #334155;
  --button-secondary-border: #475569;
  --button-secondary-text: #e2e8f0;
  --button-secondary-hover-bg: #3b4a61;
  --badge-bg: #0b3a6e;
  --badge-border: #1d4f85;
  --badge-text: #dbeafe;
  --list-selected-bg: #1e3a8a;
  --season-header-bg: #1e293b;
  --season-header-border: #334155;
  --danger-text: #f87171;
  --overlay-bg: rgba(2, 6, 23, 0.7);
  --shadow-lg: 0 8px 22px rgba(2, 6, 23, 0.45);
  --series-image-bg: #0b1220;
}

[data-theme="light"] {
  --bg-page: #f2f4f8;
  --text-primary: #1a1f2b;
  --text-muted: #64748b;
  --topbar-bg: #0f172a;
  --topbar-text: #e2e8f0;
  --surface: #ffffff;
  --surface-elevated: #ffffff;
  --surface-subtle: #f8fafc;
  --border: #d7dee9;
  --border-soft: #c3cedf;
  --divider: #e4eaf2;
  --button-primary-bg: #0f172a;
  --button-primary-border: #0f172a;
  --button-primary-text: #ffffff;
  --button-secondary-bg: #e2e8f0;
  --button-secondary-border: #c3cedf;
  --button-secondary-text: #1a1f2b;
  --button-secondary-hover-bg: #eef2f7;
  --badge-bg: #dbeafe;
  --badge-border: #bfdbfe;
  --badge-text: #0f172a;
  --list-selected-bg: #e0f2fe;
  --season-header-bg: #eef2ff;
  --season-header-border: #dbe4fb;
  --danger-text: #b91c1c;
  --overlay-bg: rgba(2, 6, 23, 0.55);
  --shadow-lg: 0 8px 22px rgba(15, 23, 42, 0.12);
  --series-image-bg: #f8fafc;
}

body {
  margin: 0;
  padding: 0;
  font-family: "Segoe UI", Tahoma, sans-serif;
  background: var(--bg-page);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: var(--topbar-bg);
  color: var(--topbar-text);
}

.topbar h1 {
  margin: 0;
  font-size: 20px;
}

#sessionMeta {
  font-size: 12px;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.theme-toggle-btn {
  border: 1px solid var(--button-secondary-border);
  background: var(--button-secondary-bg);
  color: var(--button-secondary-text);
  border-radius: 999px;
  width: 34px;
  height: 34px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(280px, 1fr));
  gap: 12px;
  padding: 12px;
  align-items: start;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  min-height: 420px;
  display: flex;
  flex-direction: column;
}

#panelSearch,
#panelEpisodes,
#panelSelectedEpisodes,
#panelSelectedFiles {
  height: 100%;
  min-height: 0;
  max-height: 100%;
  align-self: stretch;
  overflow: hidden;
}

#panelSearch .panel-body {
  flex: 1;
  min-height: 0;
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 16px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
  min-height: 38px;
}

.panel-head h2 {
  margin: 0;
}

.panel-head-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.panel-head-actions-empty {
  min-width: 172px;
}

.panel h3 {
  margin: 10px 0 6px;
  font-size: 14px;
}

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

.search-combobox-row input[type="text"] {
  flex: 1;
  min-width: 0;
}

#searchDropdownBtn {
  min-width: 34px;
  padding-left: 8px;
  padding-right: 8px;
}

.search-combobox {
  position: relative;
}

.combobox-dropdown {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% - 6px);
  z-index: 20;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: var(--shadow-lg);
  padding: 6px;
}

#rememberedDropdownList {
  max-height: 220px;
}

#rememberedDropdownList li {
  align-items: center;
}

#rememberedDropdownList .remembered-item-title {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

#rememberedDropdownList .remembered-remove-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  width: 22px;
  height: 22px;
  line-height: 1;
  border-radius: 4px;
  padding: 0;
  font-size: 15px;
  cursor: pointer;
}

#rememberedDropdownList .remembered-remove-btn:hover {
  background: var(--button-secondary-hover-bg);
  color: var(--text-primary);
}

.stack {
  display: flex;
  flex-direction: column;
}

.panel-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

input[type="text"],
select {
  border: 1px solid var(--border-soft);
  border-radius: 6px;
  padding: 6px 8px;
  min-width: 160px;
  background: var(--surface-subtle);
  color: var(--text-primary);
}

button {
  border: 1px solid var(--button-primary-border);
  background: var(--button-primary-bg);
  color: var(--button-primary-text);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
}

button.secondary {
  background: var(--button-secondary-bg);
  color: var(--button-secondary-text);
  border-color: var(--button-secondary-border);
}

.list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 260px;
  overflow: auto;
  border: 1px solid var(--divider);
  border-radius: 6px;
  background: var(--surface-elevated);
}

.linked-list-wrap {
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.linked-list {
  flex: 1;
  height: 100%;
  max-height: none;
  overflow-y: auto;
}

#panelEpisodes .panel-body,
#panelSelectedEpisodes .panel-body,
#panelSelectedFiles .panel-body {
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

#panelEpisodes .linked-list-wrap,
#panelSelectedEpisodes .linked-list-wrap,
#panelSelectedFiles .linked-list-wrap {
  flex: 1;
  min-height: 0;
}

#panelEpisodes .linked-list-wrap .list,
#panelSelectedEpisodes .linked-list-wrap .list,
#panelSelectedFiles .linked-list-wrap .list {
  max-height: none;
  height: 100%;
}

#episodesList .episode-main {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#episodesList li.episode-row {
  cursor: pointer;
}

#episodesList .episode-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#episodesList .episode-date {
  margin-top: 2px;
  font-size: 12px;
  color: var(--text-muted);
}

#episodesList .episode-date-future {
  color: var(--button-primary-bg);
}

#episodesList li.episode-row.episode-anchor {
  box-shadow: inset 0 0 0 1px var(--button-primary-border);
}

.badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  color: var(--badge-text);
  background: var(--badge-bg);
  border: 1px solid var(--badge-border);
  border-radius: 999px;
  padding: 1px 6px;
  margin-right: 6px;
}

.list li.selected {
  background: var(--list-selected-bg);
}

.list li.season-header {
  background: var(--season-header-bg);
  border-bottom: 1px solid var(--season-header-border);
  color: var(--text-primary);
  font-weight: 700;
  justify-content: flex-start;
  padding: 8px;
}

.panel-footer {
  position: sticky;
  bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--divider);
  padding-top: 8px;
  margin-top: 10px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  min-height: 40px;
}

.panel-search-footer {
  justify-content: flex-end;
}

.mismatch {
  color: var(--danger-text);
  font-weight: 700;
}

.modal {
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal.hidden {
  display: none;
}

.modal-card {
  width: min(1400px, 90vw);
  height: 80vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.tvdb-modal-card {
  width: min(1500px, 92vw);
  height: 86vh;
}

.tvdb-modal-head {
  margin-bottom: 6px;
}

.tvdb-fallback {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  margin-bottom: 8px;
  background: var(--surface-subtle);
}

.tvdb-fallback h4 {
  margin: 0 0 6px;
}

.tvdb-fallback p {
  margin: 0;
  color: var(--text-muted);
}

.tvdb-modal-frame {
  width: 100%;
  flex: 1;
  min-height: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.modal-root-row {
  margin-bottom: 8px;
}

.modal-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 4px;
  flex: 1;
  min-height: 0;
}

.modal-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.modal-pane .list {
  flex: 1;
  min-height: 0;
  max-height: none;
}

#modalFoldersList li,
#modalFilesList li {
  cursor: pointer;
}

#modalFilesList li.modal-anchor {
  box-shadow: inset 0 0 0 1px var(--button-primary-border);
}

.modal-files-tools {
  margin-bottom: 8px;
}

.modal-files-tools input[type="text"] {
  flex: 1;
  min-width: 180px;
}

.modal-actions {
  margin-top: 10px;
  margin-bottom: 0;
  justify-content: flex-end;
  border-top: 1px solid var(--divider);
  padding-top: 10px;
}

.settings-card {
  width: min(520px, 94vw);
}

.settings-section h4 {
  margin: 0 0 8px;
  font-size: 14px;
  color: var(--text-primary);
}

.settings-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 4px;
}

.settings-field label {
  font-size: 12px;
  color: var(--text-muted);
}

.settings-field select {
  min-width: 0;
  width: 100%;
}

.settings-field input[type="number"] {
  min-width: 0;
  width: 100%;
}

.settings-danger-row {
  margin-top: 6px;
}

.settings-check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-primary);
}

.settings-actions {
  justify-content: flex-end;
  margin-top: 12px;
}

#panelSelectedEpisodes .panel-footer button:first-child,
#panelSelectedEpisodes .panel-footer button:last-child,
#panelSelectedFiles .panel-footer button:first-child,
#panelSelectedFiles .panel-footer button:last-child {
  border-color: var(--button-primary-border);
  background: var(--button-primary-bg);
  color: var(--button-primary-text);
}

.list li {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  border-bottom: 1px solid var(--divider);
  padding: 6px 8px;
  font-size: 13px;
}

.list li:last-child {
  border-bottom: none;
}

#selectedEpisodesList li,
#selectedFilesList li {
  height: 38px;
  min-height: 38px;
  align-items: center;
}

#selectedEpisodesList li > span,
#selectedFilesList li > span {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#selectedEpisodesList li > div,
#selectedFilesList li > div {
  flex-shrink: 0;
}

.muted {
  color: var(--text-muted);
  font-size: 12px;
  margin-bottom: 8px;
}

.hidden {
  display: none !important;
}

#panelSearch #searchResults {
  margin-bottom: 10px;
  height: 220px;
  max-height: 220px;
  overflow-y: auto;
}

#panelSearch #searchResults li {
  min-height: 38px;
  height: 38px;
  align-items: center;
}

#panelSearch #searchResults li > span {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.series-details {
  border-top: 1px solid var(--divider);
  padding-top: 10px;
}

.series-media {
  margin-bottom: 8px;
  width: 100%;
  padding: 0 4px;
}

.series-media img {
  width: 100%;
  max-width: none;
  height: 92px;
  object-fit: contain;
  object-position: center center;
  display: block;
  margin: 0;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--series-image-bg);
}

.series-meta {
  font-size: 12px;
  color: var(--text-primary);
  display: grid;
  gap: 4px;
  margin-bottom: 8px;
}

.series-meta span:first-child {
  color: var(--text-muted);
}

.series-overview {
  margin: 0 0 8px;
  font-size: 12px;
  line-height: 1.35;
  color: var(--text-primary);
}

.series-link {
  font-size: 12px;
  color: var(--text-muted);
  text-decoration: none;
}

.series-link:hover {
  text-decoration: underline;
}

#outputBox {
  margin: 0;
  background: var(--surface-subtle);
  color: var(--text-primary);
  border-radius: 6px;
  padding: 10px;
  max-height: 320px;
  overflow: auto;
  font-size: 12px;
  border: 1px solid var(--border);
}

.debug-page {
  margin: 12px;
}

@media (max-width: 1600px) {
  .grid {
    grid-template-columns: repeat(2, minmax(280px, 1fr));
  }
}

@media (max-width: 900px) {
  .grid {
    grid-template-columns: 1fr;
  }

  #panelEpisodes,
  #panelSelectedEpisodes,
  #panelSelectedFiles {
    min-height: 420px;
  }

  .modal-grid {
    grid-template-columns: 1fr;
  }
}
