/* ══════════════════════════════════════════════════════════════════
   Theme system — light / dark with system preference support
   ══════════════════════════════════════════════════════════════════ */

/* Fallback before inline script runs (rare) */
:root:not([data-theme]) {
  color-scheme: light;
  --bg-body: #ffffff;
  --bg-surface: #ffffff;
  --bg-surface-alt: #e2e7ed;
  --bg-surface-muted: #f3f4f6;
  --bg-card: #ffffff;
  --bg-card-node: #ffffff;
  --bg-modal: #ffffff;
  --bg-scroll-btn: #ffffff;
  --text-primary: #1f2328;
  --text-secondary: #2a3139;
  --text-muted: #3d454f;
  --text-heading: #1f2328;
  --text-timeline-sub: #212529;
  --text-modal: #212529;
  --border-card: rgba(61, 155, 98, 0.28);
  --bg-about: #d4ebdc;
  --bg-timeline: #e8f4ec;
  --bg-portfolio: var(--bg-about);
  --timeline-fade: var(--bg-timeline);
  --portfolio-fade: var(--bg-about);
  --bg-surface-alt: var(--bg-timeline);
  --bg-surface-muted: var(--bg-about);
  --scrollbar-track: #d4e8da;
  --link: var(--gold-dark);
  --link-hover: var(--gold);
  --link-underline: rgba(30, 92, 58, 0.35);
  --ink: #1f2328;
  --muted: #3d454f;
  --shadow-card: 0 4px 20px rgba(0, 0, 0, 0.06), 0 1px 4px rgba(0, 0, 0, 0.04);
  --shadow-card-hover: 0 8px 30px rgba(61, 155, 98, 0.22), 0 4px 12px rgba(0, 0, 0, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    --bg-body: #0a120e;
    --bg-surface: #0e1612;
    --bg-surface-alt: #0f1a14;
    --bg-surface-muted: #121a14;
    --bg-card: #1a2420;
    --bg-card-node: #212b26;
    --bg-modal: #1a2420;
    --bg-scroll-btn: #212b26;
    --text-primary: #e8eaed;
    --text-secondary: #dde3ea;
    --text-muted: #c2cad4;
    --text-heading: #f3f4f6;
    --text-timeline-sub: #f5f7fa;
    --text-modal: #e8eaed;
    --border-card: rgba(142, 212, 168, 0.26);
    --bg-about: #121a14;
    --bg-timeline: #0f1a14;
    --bg-portfolio: var(--bg-about);
    --timeline-fade: var(--bg-timeline);
    --portfolio-fade: var(--bg-about);
    --scrollbar-track: #1a2e22;
    --gold: #4aad72;
    --gold-dark: #2a7a50;
    --gold-bright: #9ee0b4;
    --link: #a8e8be;
    --link-hover: #d0f5de;
    --link-underline: rgba(168, 232, 190, 0.5);
    --ink: #e8eaed;
    --muted: #c2cad4;
    --shadow-card: 0 4px 24px rgba(0, 0, 0, 0.35), 0 1px 4px rgba(0, 0, 0, 0.2);
    --shadow-card-hover: 0 8px 32px rgba(61, 143, 90, 0.22), 0 4px 16px rgba(0, 0, 0, 0.3);
  }
}

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

  --bg-body: #ffffff;
  --bg-surface: #ffffff;
  --bg-surface-alt: #e2e7ed;
  --bg-surface-muted: #f3f4f6;
  --bg-card: #ffffff;
  --bg-card-node: #ffffff;
  --bg-modal: #ffffff;
  --bg-scroll-btn: #ffffff;

  --text-primary: #1f2328;
  --text-secondary: #2a3139;
  --text-muted: #3d454f;
  --text-heading: #1f2328;
  --text-timeline-sub: #212529;
  --text-modal: #212529;

  --border-card: rgba(61, 155, 98, 0.28);
  --border-subtle: rgba(0, 0, 0, 0.08);

  --shadow-card: 0 4px 20px rgba(0, 0, 0, 0.06), 0 1px 4px rgba(0, 0, 0, 0.04);
  --shadow-card-hover: 0 8px 30px rgba(61, 155, 98, 0.22), 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-image: 0 12px 36px rgba(0, 0, 0, 0.15);

  --bg-about: #d4ebdc;
  --bg-timeline: #e8f4ec;
  --bg-portfolio: var(--bg-about);
  --timeline-fade: var(--bg-timeline);
  --portfolio-fade: var(--bg-about);
  --bg-surface-alt: var(--bg-timeline);
  --bg-surface-muted: var(--bg-about);
  --scrollbar-track: #d4e8da;
  --link: var(--gold-dark);
  --link-hover: var(--gold);
  --link-underline: rgba(30, 92, 58, 0.35);
  --ink: #1f2328;
  --muted: #3d454f;
}

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

  --bg-body: #0a120e;
  --bg-surface: #0e1612;
  --bg-surface-alt: #0f1a14;
  --bg-surface-muted: #121a14;
  --bg-card: #1a2420;
  --bg-card-node: #212b26;
  --bg-modal: #1a2420;
  --bg-scroll-btn: #212b26;

  --text-primary: #e8eaed;
  --text-secondary: #dde3ea;
  --text-muted: #c2cad4;
  --text-heading: #f3f4f6;
  --text-timeline-sub: #f5f7fa;
  --text-modal: #e8eaed;

  --border-card: rgba(142, 212, 168, 0.26);
  --border-subtle: rgba(255, 255, 255, 0.08);

  --shadow-card: 0 4px 24px rgba(0, 0, 0, 0.35), 0 1px 4px rgba(0, 0, 0, 0.2);
  --shadow-card-hover: 0 8px 32px rgba(74, 173, 114, 0.24), 0 4px 16px rgba(0, 0, 0, 0.3);
  --shadow-image: 0 12px 40px rgba(0, 0, 0, 0.45);

  --bg-about: #121a14;
  --bg-timeline: #0f1a14;
  --bg-portfolio: var(--bg-about);
  --timeline-fade: var(--bg-timeline);
  --portfolio-fade: var(--bg-about);
  --scrollbar-track: #1a2e22;
  --gold: #4aad72;
  --gold-dark: #2a7a50;
  --gold-bright: #9ee0b4;
  --link: #a8e8be;
  --link-hover: #d0f5de;
  --link-underline: rgba(168, 232, 190, 0.5);
  --ink: #e8eaed;
  --muted: #c2cad4;
}

/* Smooth theme transitions */
body,
.page-section,
#about,
#timeline,
#portfolio,
#portfolio.bg-light,
.timeline-horizontal .timeline-panel,
.timeline-horizontal .timeline-image,
#portfolio .portfolio-grid .portfolio-item,
#portfolio .portfolio-grid .portfolio-caption,
.portfolio-modal .modal-content,
.timeline-modal .modal-content,
.modal-content,
.timeline-scroll-btn,
.portfolio-scroll-btn {
  transition:
    background-color 0.3s ease,
    color 0.3s ease,
    border-color 0.3s ease,
    box-shadow 0.3s ease;
}

@media (prefers-reduced-motion: reduce) {
  body,
  .page-section,
  #about,
  #timeline,
  #portfolio,
  .timeline-horizontal .timeline-panel,
  #portfolio .portfolio-grid .portfolio-item,
  .modal-content {
    transition: none;
  }
}

/* ── Base ───────────────────────────────────────────────────────── */
body {
  background-color: var(--bg-body);
  color: var(--text-primary);
  font-family: var(--font-sans, "Montserrat", -apple-system, BlinkMacSystemFont,
    "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif);
}

.page-section#about {
  background-color: var(--bg-about);
}

.about-image img {
  box-shadow: var(--shadow-image);
}

.text-muted {
  color: var(--text-muted) !important;
}

.page-section h2.section-heading,
.page-section-short h2.section-heading {
  color: var(--text-heading);
}

.page-section h3.section-subheading {
  color: var(--text-muted);
}

#about .section-heading {
  color: var(--text-heading);
}

.about-text p {
  color: var(--text-secondary);
}

[data-theme="dark"] .about-text p {
  color: var(--text-primary);
}

#about a,
.about-text a,
.timeline-modal .modal-content a,
.portfolio-modal .modal-content a {
  color: var(--link);
  border-bottom-color: var(--link-underline);
}

#about a:hover,
.about-text a:hover,
.timeline-modal .modal-content a:hover,
.portfolio-modal .modal-content a:hover {
  color: var(--link-hover);
  border-bottom-color: var(--link-hover);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) .about-text p {
    color: var(--text-primary);
  }

  :root:not([data-theme]) #about a,
  :root:not([data-theme]) .about-text a,
  :root:not([data-theme]) .timeline-modal .modal-content a,
  :root:not([data-theme]) .portfolio-modal .modal-content a {
    color: var(--link);
    border-bottom-color: var(--link-underline);
  }

  :root:not([data-theme]) #about a:hover,
  :root:not([data-theme]) .about-text a:hover,
  :root:not([data-theme]) .timeline-modal .modal-content a:hover,
  :root:not([data-theme]) .portfolio-modal .modal-content a:hover {
    color: var(--link-hover);
    border-bottom-color: var(--link-hover);
  }
}

/* ── Timeline ───────────────────────────────────────────────────── */
#timeline {
  background-color: var(--bg-timeline) !important;
}

.timeline-scroll-outer::before {
  background: linear-gradient(to right, var(--timeline-fade) 30%, transparent);
}

.timeline-scroll-outer::after {
  background: linear-gradient(to left, var(--timeline-fade) 30%, transparent);
}

.portfolio-scroll-outer::before {
  background: linear-gradient(to right, var(--portfolio-fade) 30%, transparent);
}

.portfolio-scroll-outer::after {
  background: linear-gradient(to left, var(--portfolio-fade) 30%, transparent);
}

.timeline-scroll {
  scrollbar-color: var(--gold, #3d8f5a) var(--scrollbar-track);
}

.timeline-scroll::-webkit-scrollbar-track {
  background: var(--scrollbar-track);
}

#timeline .timeline-horizontal .timeline-panel {
  background: var(--bg-card);
  border-color: var(--border-card);
  box-shadow: var(--shadow-card);
}

#timeline .timeline-horizontal > li:hover .timeline-panel {
  box-shadow: var(--shadow-card-hover);
}

#timeline .timeline-horizontal .timeline-heading h4:not(.subheading) {
  color: var(--gold-dark);
}

[data-theme="dark"] #timeline .timeline-horizontal .timeline-heading h4:not(.subheading) {
  color: var(--gold-bright, #6bc48a);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) #timeline .timeline-horizontal .timeline-heading h4:not(.subheading) {
    color: var(--gold-bright, #6bc48a);
  }
}

#timeline .timeline-horizontal .timeline-heading h4.subheading {
  color: var(--text-timeline-sub);
}

[data-theme="dark"] #timeline .timeline-horizontal .timeline-panel .timeline-heading > h4.subheading {
  color: #ffffff !important;
}

#timeline .timeline-horizontal .timeline-body p {
  color: var(--text-secondary);
}

[data-theme="dark"] #timeline .timeline-horizontal .timeline-body p {
  color: var(--text-primary);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) #timeline .timeline-horizontal .timeline-body p {
    color: var(--text-primary);
  }
}

#timeline .timeline-horizontal .timeline-image {
  background: var(--bg-card-node);
  box-shadow:
    0 0 0 8px rgba(61, 143, 90, 0.15),
    0 4px 16px rgba(0, 0, 0, 0.12);
}

.timeline-scroll-btn {
  background: var(--bg-scroll-btn);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
}

[data-theme="dark"] .timeline-scroll-btn:hover {
  color: #fff;
}

/* ── Portfolio ────────────────────────────────────────────────────── */
#portfolio,
#portfolio.bg-light {
  background-color: var(--bg-portfolio) !important;
}

#portfolio .portfolio-grid .portfolio-item {
  background: var(--bg-card);
  border-color: var(--border-card);
  box-shadow: var(--shadow-card);
}

#portfolio .portfolio-grid .portfolio-card:hover .portfolio-item {
  box-shadow: var(--shadow-card-hover);
}

#portfolio .portfolio-grid .portfolio-caption {
  background: var(--bg-card);
}

#portfolio .portfolio-grid .portfolio-caption-heading {
  color: var(--text-heading);
}

/* ── Modals ─────────────────────────────────────────────────────── */
.portfolio-modal .modal-content,
.timeline-modal .modal-content,
.modal-content {
  background-color: var(--bg-modal);
  color: var(--text-modal);
}

.portfolio-modal .modal-content p,
.timeline-modal .modal-content p,
.timeline-modal .modal-content li,
.portfolio-modal .modal-content li {
  color: var(--text-secondary);
}

.portfolio-modal .modal-content h2,
.timeline-modal .modal-content h2 {
  color: var(--text-heading);
}

.portfolio-modal .modal-content p.item-intro,
.timeline-modal .modal-content p.item-intro {
  color: var(--text-muted);
}

[data-theme="dark"] .close-modal img {
  filter: invert(1);
}

/* ── Theme toggle (navbar) ──────────────────────────────────────── */
.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 999px;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  margin-left: 0.5rem;
  color: rgba(255, 255, 255, 0.88);
  cursor: pointer;
  transition:
    background 0.2s ease,
    color 0.2s ease,
    border-color 0.2s ease,
    transform 0.2s ease;
}

.theme-toggle:hover,
.theme-toggle:focus {
  color: var(--gold-bright, #6bc48a);
  background: rgba(61, 143, 90, 0.14);
  border-color: rgba(61, 143, 90, 0.35);
  outline: none;
}

.theme-toggle:active {
  transform: scale(0.94);
}

.theme-toggle-icon {
  font-size: 0.95rem;
  line-height: 1;
}

.nav-item-theme {
  display: flex;
  align-items: center;
}

@media (min-width: 992px) {
  .nav-item-theme {
    margin-left: 0.25rem;
  }
}

@media (max-width: 991px) {
  .nav-item-theme {
    margin-top: 0.75rem;
    padding-left: 1rem;
  }

  .theme-toggle {
    margin-left: 0;
  }
}
