/* =================================================================
   DARK THEME FOR THE WHOLE APP
   Loaded on every page after that page's own stylesheet.

   Two jobs:
   1. [data-theme="dark"] gives the admin and tool pages a dark surface set.
      styles.css was written against a light palette with many literal
      colours, so the overrides here are component by component rather than
      variable only.
   2. [data-theme="light"] gives the public pages a light variant of the
      deep-ocean theme. public-theme.css is dark by design, so without this
      a visitor on a light machine would see the toggle do nothing.

   The controller lives in static/theme.js and writes the attribute.
   ================================================================= */

/* -----------------------------------------------------------------
   1. DARK: shared tokens
   ----------------------------------------------------------------- */

[data-theme="dark"] {
  color-scheme: dark;

  /* The core tokens styles.css was written against. Redefining them here
     fixes every rule that reads them, including the native audio control
     panel, which is painted with --bg-light. */
  --bg-light: #0f1a2b;
  --bg-white: #16233a;
  --bg-dark: #060e1a;
  --text-dark: #e2e8f0;
  --text-light: #94a3b8;
  --text-white: #ffffff;
  --border: rgba(148, 163, 184, 0.2);
  --border-color: rgba(148, 163, 184, 0.2);
  --shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 10px 24px rgba(0, 0, 0, 0.45);

  --surface: #101a2b;
  --surface-raised: #16233a;
  --surface-sunken: #0a1120;
  --surface-hover: #1b2a45;

  --text-strong: #f1f5f9;
  --text-body: #cbd5e1;
  --text-dim: #94a3b8;

  --border-soft: rgba(148, 163, 184, 0.22);
  --border-strong: rgba(148, 163, 184, 0.4);

  --accent: #2dd4bf;
  --accent-dark: #14b8a6;
  --accent-soft: rgba(45, 212, 191, 0.14);
}

/* Page background, whatever the page class is called */
[data-theme="dark"] body {
  background-color: var(--surface-sunken);
  background-image:
    radial-gradient(ellipse at 15% 0%, rgba(45, 212, 191, 0.08) 0%, transparent 45%),
    radial-gradient(ellipse at 85% 10%, rgba(30, 58, 95, 0.35) 0%, transparent 50%);
  background-attachment: fixed;
  color: var(--text-body);
}

[data-theme="dark"] body.admin-page,
[data-theme="dark"] body.public-page {
  background-color: var(--surface-sunken);
}

/* -----------------------------------------------------------------
   2. DARK: admin chrome
   ----------------------------------------------------------------- */

[data-theme="dark"] .admin-container {
  color: var(--text-body);
}

[data-theme="dark"] .admin-header {
  background: linear-gradient(135deg, rgba(45, 212, 191, 0.16) 0%, rgba(30, 58, 95, 0.55) 100%);
  border: 1px solid var(--border-soft);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  color: var(--text-strong);
}

[data-theme="dark"] .admin-header h1,
[data-theme="dark"] .admin-header h2,
[data-theme="dark"] .admin-header p,
[data-theme="dark"] .admin-header .text-muted {
  color: var(--text-strong);
}

[data-theme="dark"] .admin-header .text-muted,
[data-theme="dark"] .text-muted,
[data-theme="dark"] .small-text,
[data-theme="dark"] small {
  color: var(--text-dim);
}

/* Tabs */
[data-theme="dark"] .tab-container {
  background: var(--surface-raised);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 0.35rem;
}

[data-theme="dark"] .tab {
  background: transparent;
  color: var(--text-body);
  border: 1px solid transparent;
}

[data-theme="dark"] .tab:hover {
  background: var(--surface-hover);
  color: var(--text-strong);
}

[data-theme="dark"] .tab.active,
[data-theme="dark"] .tab[aria-selected="true"] {
  background: var(--accent-soft);
  border-color: var(--border-strong);
  color: var(--text-strong);
}

/* Panels, cards and the tables inside them */
[data-theme="dark"] .content-panel,
[data-theme="dark"] .admin-podcast-card,
[data-theme="dark"] .admin-card,
[data-theme="dark"] .topic-list-panel,
[data-theme="dark"] .topic-detail-panel,
[data-theme="dark"] .topic-schedule-panel,
[data-theme="dark"] .topic-podcast-bar,
[data-theme="dark"] .login-box,
[data-theme="dark"] .login-container,
[data-theme="dark"] .modal-content,
[data-theme="dark"] .episode-table,
[data-theme="dark"] .audio-library-card {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  color: var(--text-body);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

[data-theme="dark"] .content-panel h2,
[data-theme="dark"] .content-panel h3,
[data-theme="dark"] .admin-podcast-card h3,
[data-theme="dark"] .topic-detail-panel h2,
[data-theme="dark"] .topic-detail-panel h3,
[data-theme="dark"] .topic-schedule-panel h2,
[data-theme="dark"] .login-box h1,
[data-theme="dark"] .login-box h2 {
  color: var(--text-strong);
}

/* Form controls */
[data-theme="dark"] .admin-input,
[data-theme="dark"] .admin-textarea,
[data-theme="dark"] .admin-select,
[data-theme="dark"] input[type="text"],
[data-theme="dark"] input[type="search"],
[data-theme="dark"] input[type="email"],
[data-theme="dark"] input[type="password"],
[data-theme="dark"] input[type="date"],
[data-theme="dark"] input[type="number"],
[data-theme="dark"] textarea,
[data-theme="dark"] select {
  background-color: var(--surface-raised);
  border-color: var(--border-soft);
  color: var(--text-strong);
}

[data-theme="dark"] .admin-input::placeholder,
[data-theme="dark"] .admin-textarea::placeholder,
[data-theme="dark"] input::placeholder,
[data-theme="dark"] textarea::placeholder {
  color: #64748b;
}

[data-theme="dark"] .admin-input:focus,
[data-theme="dark"] .admin-textarea:focus,
[data-theme="dark"] .admin-select:focus,
[data-theme="dark"] input:focus,
[data-theme="dark"] textarea:focus,
[data-theme="dark"] select:focus {
  border-color: var(--accent);
  outline: 2px solid rgba(45, 212, 191, 0.25);
  outline-offset: 1px;
}

[data-theme="dark"] .admin-label,
[data-theme="dark"] label {
  color: var(--text-body);
}

/* Tables */
[data-theme="dark"] table,
[data-theme="dark"] .admin-table {
  background: var(--surface);
  color: var(--text-body);
}

[data-theme="dark"] thead th,
[data-theme="dark"] .admin-table th {
  background: var(--surface-raised);
  color: var(--text-strong);
  border-bottom: 1px solid var(--border-soft);
}

[data-theme="dark"] tbody tr,
[data-theme="dark"] tbody td {
  border-color: var(--border-soft);
  color: var(--text-body);
}

[data-theme="dark"] tbody tr:hover {
  background: var(--surface-hover);
}

/* Buttons keep their brand colours but need readable text */
[data-theme="dark"] .admin-button {
  border: 1px solid var(--border-soft);
  color: var(--text-strong);
  background: var(--surface-raised);
}

[data-theme="dark"] .admin-button:hover {
  background: var(--surface-hover);
}

[data-theme="dark"] .admin-button.primary,
[data-theme="dark"] .topic-primary-button,
[data-theme="dark"] .btn-primary {
  background: var(--accent-dark);
  border-color: var(--accent-dark);
  color: #04211d;
  font-weight: 600;
}

[data-theme="dark"] .admin-button.primary:hover,
[data-theme="dark"] .topic-primary-button:hover {
  background: var(--accent);
  border-color: var(--accent);
}

/* Messages */
[data-theme="dark"] .success-message {
  background: rgba(52, 211, 153, 0.14);
  border-color: rgba(52, 211, 153, 0.4);
  color: #6ee7b7;
}

[data-theme="dark"] .error-message,
[data-theme="dark"] .topic-error,
[data-theme="dark"] .alert-error {
  background: rgba(248, 113, 113, 0.14);
  border-color: rgba(248, 113, 113, 0.4);
  color: #fca5a5;
}

/* -----------------------------------------------------------------
   3. DARK: topic bank specifics
   ----------------------------------------------------------------- */

[data-theme="dark"] .topic-card {
  background: var(--surface-raised);
  border-color: var(--border-soft);
  color: var(--text-body);
}

[data-theme="dark"] .topic-card:hover {
  background: var(--surface-hover);
  border-color: var(--accent);
}

[data-theme="dark"] .topic-card.is-selected {
  background: rgba(45, 212, 191, 0.12);
  border-color: var(--accent);
}

[data-theme="dark"] .topic-card-title,
[data-theme="dark"] .topic-schedule-title {
  color: var(--accent);
}

[data-theme="dark"] .topic-status-chip {
  background: var(--surface-raised);
  border-color: var(--border-soft);
  color: var(--text-body);
}

[data-theme="dark"] .topic-status-chip.is-active {
  background: var(--accent-dark);
  border-color: var(--accent-dark);
  color: #04211d;
}

[data-theme="dark"] .topic-view-switch {
  background: var(--surface-raised);
  border-color: var(--border-soft);
}

[data-theme="dark"] .topic-view-tab {
  color: var(--text-dim);
}

[data-theme="dark"] .topic-view-tab.is-active {
  background: var(--accent-dark);
  color: #04211d;
}

[data-theme="dark"] .topic-note-item,
[data-theme="dark"] .topic-schedule-item {
  background: var(--surface-raised);
  border-color: var(--border-soft);
}

[data-theme="dark"] .topic-note-body,
[data-theme="dark"] .topic-schedule-date {
  color: var(--text-strong);
}

[data-theme="dark"] .topic-note-meta,
[data-theme="dark"] .topic-schedule-guest,
[data-theme="dark"] .topic-schedule-hint,
[data-theme="dark"] .topic-empty,
[data-theme="dark"] .topic-detail-placeholder {
  color: var(--text-dim);
}

[data-theme="dark"] .topic-badge {
  background: var(--surface-hover);
  border-color: var(--border-soft);
  color: var(--text-body);
}

/* -----------------------------------------------------------------
   4. DARK: audio library
   ----------------------------------------------------------------- */

[data-theme="dark"] .audio-library-header,
[data-theme="dark"] .upload-zone,
[data-theme="dark"] .audio-item,
[data-theme="dark"] .audio-card {
  background: var(--surface-raised);
  border-color: var(--border-soft);
  color: var(--text-body);
}

[data-theme="dark"] .upload-zone {
  border-color: var(--border-strong);
}

[data-theme="dark"] .upload-zone.dragover {
  background: rgba(45, 212, 191, 0.12);
  border-color: var(--accent);
}

/* -----------------------------------------------------------------
   5. LIGHT: public pages
   public-theme.css designs the public pages dark. This block gives the
   same pages a light variant so the toggle is honest everywhere.
   ----------------------------------------------------------------- */

[data-theme="light"] body.public-page {
  color-scheme: light;

  --primary: #0f766e;
  --primary-dark: #115e59;
  --primary-light: rgba(15, 118, 110, 0.1);
  --secondary: #1e3a5f;

  --text-dark: #1e293b;
  --text-light: #475569;
  --text-white: #ffffff;
  --text-muted: #64748b;
  --text-color: #1e293b;
  --header-text: #0f172a;

  --bg-light: #f8fafc;
  --bg-white: #ffffff;
  --bg-dark: #e2e8f0;
  --bg-color: #ffffff;
  --bg-gradient: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);

  --card-bg: #ffffff;
  --header-bg: #ffffff;
  --surface: #ffffff;

  --border: rgba(15, 23, 42, 0.12);
  --border-light: rgba(15, 118, 110, 0.25);
  --border-color: rgba(15, 23, 42, 0.12);

  --shadow: 0 1px 3px rgba(15, 23, 42, 0.12);
  --shadow-light: 0 1px 2px rgba(15, 23, 42, 0.08);
  --shadow-medium: 0 4px 12px rgba(15, 23, 42, 0.1);
  --shadow-heavy: 0 12px 24px rgba(15, 23, 42, 0.12);
  --shadow-lg: 0 10px 15px rgba(15, 23, 42, 0.1);
  --shadow-modal: 0 20px 60px rgba(15, 23, 42, 0.18);

  --glass-bg: #ffffff;
  --glass-bg-hover: #f8fafc;
  --glass-border: 1px solid rgba(15, 23, 42, 0.1);
  --glass-shadow: 0 4px 20px rgba(15, 23, 42, 0.08);

  --ocean-gradient:
    radial-gradient(ellipse at 20% 0%, rgba(15, 118, 110, 0.08) 0%, transparent 45%),
    radial-gradient(ellipse at 80% 10%, rgba(30, 58, 95, 0.06) 0%, transparent 45%);
}

[data-theme="light"] body.public-page .header {
  background: rgba(255, 255, 255, 0.9);
  border-bottom: 1px solid rgba(15, 23, 42, 0.1);
  box-shadow: 0 2px 12px rgba(15, 23, 42, 0.06);
}

[data-theme="light"] body.public-page .header h1,
[data-theme="light"] body.public-page .podcast-title,
[data-theme="light"] body.public-page .podcast-hero-title,
[data-theme="light"] body.public-page .episode-title {
  color: #0f172a;
}

[data-theme="light"] body.public-page .header p,
[data-theme="light"] body.public-page .podcast-description,
[data-theme="light"] body.public-page .episode-description {
  color: #475569;
}

[data-theme="light"] body.public-page .podcast-card,
[data-theme="light"] body.public-page .episode-item,
[data-theme="light"] body.public-page .podcast-hero {
  background: #ffffff;
  border: 1px solid rgba(15, 23, 42, 0.1);
  box-shadow: 0 4px 18px rgba(15, 23, 42, 0.08);
}

[data-theme="light"] body.public-page .episode-count {
  background: rgba(15, 118, 110, 0.1);
  color: #0f766e;
  border-color: rgba(15, 118, 110, 0.25);
}

[data-theme="light"] body.public-page .btn-secondary,
[data-theme="light"] body.public-page .rss-btn {
  background: #ffffff;
  color: #1e293b;
  border: 1px solid rgba(15, 23, 42, 0.15);
}

/* -----------------------------------------------------------------
   6. The toggle button itself
   ----------------------------------------------------------------- */

.theme-toggle-btn {
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0.5rem 0.65rem;
  border-radius: var(--radius, 8px);
  border: 1px solid var(--border-soft, rgba(148, 163, 184, 0.3));
  background: transparent;
  color: inherit;
  transition: background 0.2s ease, border-color 0.2s ease;
}

.theme-toggle-btn:hover {
  background: rgba(148, 163, 184, 0.15);
}

.theme-toggle-btn:focus-visible {
  outline: 2px solid var(--accent, #2dd4bf);
  outline-offset: 2px;
}

/* -----------------------------------------------------------------
   7. Toggle placement helpers
   ----------------------------------------------------------------- */

.header-theme-row,
.login-theme-row,
.unsub-theme-row {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  width: 100%;
  margin-bottom: 0.5rem;
}

.login-theme-row,
.unsub-theme-row {
  max-width: 460px;
  margin: 0 auto 0.75rem;
  padding: 0 1rem;
}

body.public-page .header .header-theme-row .theme-toggle-btn,
body.public-page .header-nav .theme-toggle-btn {
  border-color: rgba(148, 163, 184, 0.35);
  color: var(--text-dark);
}

body.public-page .header-nav {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

/* -----------------------------------------------------------------
   8. Admin podcast cards and shared list chrome
   The admin builds these with the same class names the public pages
   use, so they need explicit dark values here.
   ----------------------------------------------------------------- */

[data-theme="dark"] .admin-container .podcast-card,
[data-theme="dark"] .podcast-card {
  background: var(--surface-raised);
  border: 1px solid var(--border-soft);
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.4);
}

[data-theme="dark"] .podcast-card-body,
[data-theme="dark"] .podcast-card-footer {
  background: transparent;
  color: var(--text-body);
  border-color: var(--border-soft);
}

[data-theme="dark"] .podcast-card .podcast-title,
[data-theme="dark"] .podcast-card h3 {
  color: var(--text-strong);
}

[data-theme="dark"] .podcast-card .podcast-description,
[data-theme="dark"] .podcast-card .podcast-author,
[data-theme="dark"] .podcast-meta-text {
  color: var(--text-dim);
}

[data-theme="dark"] .podcast-group-header {
  background: var(--surface-raised);
  border-color: var(--border-soft);
  color: var(--text-strong);
}

[data-theme="dark"] .no-podcasts-message {
  color: var(--text-dim);
}

[data-theme="dark"] .podcast-card-status-row .status-published,
[data-theme="dark"] .podcast-card-status-row .status-active {
  background: rgba(52, 211, 153, 0.16);
  color: #6ee7b7;
  border-color: rgba(52, 211, 153, 0.4);
}

[data-theme="dark"] .podcast-card-status-row .status-draft {
  background: rgba(251, 191, 36, 0.16);
  color: #fcd34d;
  border-color: rgba(251, 191, 36, 0.4);
}

/* -----------------------------------------------------------------
   9. DARK: audio players and their surrounding panels
   styles.css paints .audio-player-container white and gives the native
   control panel a light background, so both need explicit dark values.
   ----------------------------------------------------------------- */

[data-theme="dark"] .audio-player-container,
[data-theme="dark"] .episode-audio-player,
[data-theme="dark"] .player-controls,
[data-theme="dark"] .playback-speed-controls {
  background: transparent;
  color: var(--text-body);
}

[data-theme="dark"] .audio-player-container {
  background: var(--surface-raised);
  border: 1px solid var(--border-soft);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

[data-theme="dark"] audio {
  color-scheme: dark;
  background: var(--surface-sunken);
  border-radius: 12px;
  /* The native transport bar follows the browser's own colour scheme. Chromium
     headless renders it light regardless, and no CSS can reach into it, so the
     player keeps its native chrome inside a dark frame. */
}

[data-theme="dark"] audio::-webkit-media-controls-panel {
  background: var(--surface-raised);
}

[data-theme="dark"] audio::-webkit-media-controls-current-time-display,
[data-theme="dark"] audio::-webkit-media-controls-time-remaining-display {
  color: var(--text-strong);
}

[data-theme="dark"] .speed-btn {
  background: var(--surface-raised);
  border-color: var(--border-soft);
  color: var(--text-body);
}

[data-theme="dark"] .speed-btn:hover {
  background: var(--surface-hover);
  border-color: var(--accent);
}

[data-theme="dark"] .speed-btn.active {
  background: var(--accent-dark);
  border-color: var(--accent-dark);
  color: #04211d;
}

[data-theme="dark"] .speed-label {
  color: var(--text-body);
}

/* Navigation pills on the public header */
[data-theme="dark"] body.public-page .nav-link {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-strong);
}

[data-theme="dark"] body.public-page .nav-link:hover {
  background: rgba(255, 255, 255, 0.16);
}

[data-theme="light"] body.public-page .nav-link {
  background: rgba(15, 23, 42, 0.06);
  color: #0f172a;
}

/* Error text needs lifting on a dark surface */
[data-theme="dark"] .error-message,
[data-theme="dark"] .error,
[data-theme="dark"] .error-text,
[data-theme="dark"] .no-podcasts-message,
[data-theme="dark"] #loading-state p,
[data-theme="dark"] .error-state {
  color: #fca5a5;
}

/* Muted badges need a little more weight in the light theme */
[data-theme="light"] .topic-badge[data-editing="not_started"],
[data-theme="light"] .topic-badge[data-marketing="not_started"] {
  background: #eef1f5;
  color: #4b5563;
  border-color: #cfd6de;
}

/* Error and empty state cards are painted white in styles.css */
[data-theme="dark"] .error-state,
[data-theme="dark"] .empty-state,
[data-theme="dark"] .loading,
[data-theme="dark"] .loading-state,
[data-theme="dark"] .no-podcasts-message,
[data-theme="dark"] .unsub-card,
[data-theme="dark"] .unsub-success {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  color: var(--text-body);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

[data-theme="dark"] .error-state h2 {
  color: #fca5a5;
}

[data-theme="dark"] .error-state p,
[data-theme="dark"] .loading p,
[data-theme="dark"] .unsub-card p,
[data-theme="dark"] .unsub-success p {
  color: var(--text-dim);
}

[data-theme="dark"] .unsub-card h1,
[data-theme="dark"] .unsub-success h1 {
  color: var(--text-strong);
}

/* Episode count and status pills inside podcast cards */
[data-theme="dark"] .episode-badge,
[data-theme="dark"] .episode-count {
  background: var(--surface-hover);
  color: var(--text-body);
  border: 1px solid var(--border-soft);
}

[data-theme="dark"] .podcast-status-badge {
  border: 1px solid var(--border-soft);
}

[data-theme="dark"] .podcast-status-badge.published,
[data-theme="dark"] .podcast-status-badge.active {
  background: rgba(52, 211, 153, 0.18);
  color: #6ee7b7;
  border-color: rgba(52, 211, 153, 0.4);
}

[data-theme="dark"] .podcast-status-badge.draft {
  background: rgba(251, 191, 36, 0.18);
  color: #fcd34d;
  border-color: rgba(251, 191, 36, 0.4);
}

[data-theme="dark"] .podcast-status-badge.inactive,
[data-theme="dark"] .podcast-status-badge.archived {
  background: rgba(148, 163, 184, 0.16);
  color: #cbd5e1;
  border-color: rgba(148, 163, 184, 0.35);
}
