:root {
  --bg: #f3efe5;
  --bg-alt: #e3dccb;
  --surface: rgba(255, 255, 255, 0.77);
  --surface-strong: #fbfaf6;
  --text: #1f2927;
  --muted: #5c6663;
  --line: #c8cdc5;
  --accent: #0d7461;
  --accent-strong: #0a5748;
  --shadow: 0 14px 40px rgba(10, 22, 19, 0.08);
  --shadow-compact: 0 10px 28px rgba(10, 22, 19, 0.11);
  --bg-radial-1: rgba(13, 116, 97, 0.16);
  --bg-radial-2: rgba(205, 154, 62, 0.14);
  --noise-line: rgba(21, 35, 31, 0.04);
  --topbar-border: rgba(255, 255, 255, 0.62);
  --topbar-bg: rgba(252, 250, 244, 0.8);
  --topbar-bg-compact: rgba(252, 250, 244, 0.9);
  --panel-border: rgba(240, 242, 236, 0.65);
  --control-bg: rgba(255, 255, 255, 0.72);
  --timeline-text: #365148;
  --timeline-active-text: #0f312b;
  --timeline-active-bg: rgba(13, 116, 97, 0.14);
  --chapter-title: #153f37;
  --chapter-bg: rgba(13, 116, 97, 0.09);
  --link-hover: #07392f;
  --status-loading-color: #2c4c45;
  --status-loading-bg: rgba(13, 116, 97, 0.14);
  --status-error-color: #842525;
  --status-error-bg: rgba(179, 53, 53, 0.14);
  --to-top-bg: rgba(15, 87, 73, 0.93);
  --to-top-shadow: 0 6px 22px rgba(14, 26, 23, 0.26);
  --serif: "Fraunces", "Iowan Old Style", "Palatino Linotype", serif;
  --sans: "Manrope", "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
  --bg: #121920;
  --bg-alt: #0b1117;
  --surface: rgba(20, 27, 33, 0.82);
  --surface-strong: #141c22;
  --text: #e6efed;
  --muted: #9eaead;
  --line: #3a464d;
  --accent: #3fa790;
  --accent-strong: #66c8b1;
  --shadow: 0 14px 40px rgba(0, 0, 0, 0.45);
  --shadow-compact: 0 10px 28px rgba(0, 0, 0, 0.52);
  --bg-radial-1: rgba(63, 167, 144, 0.24);
  --bg-radial-2: rgba(220, 164, 84, 0.16);
  --noise-line: rgba(255, 255, 255, 0.05);
  --topbar-border: rgba(81, 97, 105, 0.72);
  --topbar-bg: rgba(18, 25, 31, 0.88);
  --topbar-bg-compact: rgba(15, 22, 28, 0.93);
  --panel-border: rgba(81, 97, 105, 0.6);
  --control-bg: rgba(15, 24, 30, 0.78);
  --timeline-text: #a9bcba;
  --timeline-active-text: #defaf4;
  --timeline-active-bg: rgba(63, 167, 144, 0.28);
  --chapter-title: #d4f1ea;
  --chapter-bg: rgba(63, 167, 144, 0.2);
  --link-hover: #a3e6d7;
  --status-loading-color: #c5efe4;
  --status-loading-bg: rgba(63, 167, 144, 0.22);
  --status-error-color: #ffc4c4;
  --status-error-bg: rgba(197, 78, 78, 0.24);
  --to-top-bg: rgba(39, 133, 113, 0.95);
  --to-top-shadow: 0 6px 22px rgba(0, 0, 0, 0.45);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  font-family: var(--sans);
  background:
    radial-gradient(circle at 12% 20%, var(--bg-radial-1), transparent 35%),
    radial-gradient(circle at 84% 0%, var(--bg-radial-2), transparent 42%),
    linear-gradient(145deg, var(--bg) 0%, var(--bg-alt) 100%);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.22;
  background-image: repeating-linear-gradient(
    -45deg,
    transparent,
    transparent 10px,
    var(--noise-line) 10px,
    var(--noise-line) 11px
  );
}

.shell {
  position: relative;
  z-index: 1;
  max-width: 1680px;
  margin: 0 auto;
  padding: 1rem 0.7rem 4rem;
}

.topbar {
  position: sticky;
  top: 0.75rem;
  z-index: 15;
  display: flex;
  gap: 1.5rem;
  justify-content: space-between;
  align-items: flex-end;
  padding: 1.2rem 1.3rem;
  border: 1px solid var(--topbar-border);
  border-radius: 20px;
  background: var(--topbar-bg);
  backdrop-filter: blur(10px);
  box-shadow: var(--shadow);
  transition:
    padding 0.22s ease,
    gap 0.22s ease,
    border-radius 0.22s ease,
    background 0.22s ease,
    box-shadow 0.22s ease;
}

.topbar .eyebrow,
.topbar .subtitle {
  transition:
    opacity 0.18s ease,
    max-height 0.18s ease,
    margin 0.18s ease,
    transform 0.18s ease;
  max-height: 120px;
  overflow: hidden;
}

.topbar h1 {
  transition: font-size 0.2s ease;
}

.topbar.is-compact {
  gap: 0.8rem;
  padding: 0.5rem 0.72rem;
  border-radius: 14px;
  background: var(--topbar-bg-compact);
  box-shadow: var(--shadow-compact);
}

.topbar.is-compact .eyebrow,
.topbar.is-compact .subtitle {
  opacity: 0;
  max-height: 0;
  margin: 0;
  transform: translateY(-6px);
}

.topbar.is-compact h1 {
  font-size: clamp(1rem, 0.75vw + 0.85rem, 1.18rem);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar.is-compact .controls {
  padding: 0.28rem;
}

.topbar.is-compact .control-label {
  display: none;
}

.topbar.is-compact .control-btn {
  padding: 0.34rem 0.62rem;
  font-size: 0.78rem;
}

.eyebrow {
  margin: 0 0 0.25rem;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  font-size: 0.72rem;
  color: var(--muted);
}

h1 {
  margin: 0;
  line-height: 1.1;
  font-family: var(--serif);
  font-size: clamp(1.45rem, 2vw + 1rem, 2.35rem);
  font-weight: 530;
}

.subtitle {
  margin: 0.4rem 0 0;
  max-width: 64ch;
  color: var(--muted);
  font-size: 0.95rem;
}

.control-cluster {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.controls {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.45rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--control-bg);
}

.control-label {
  padding: 0 0.4rem 0 0.6rem;
  color: var(--muted);
  font-size: 0.8rem;
}

.control-btn {
  border: 0;
  border-radius: 999px;
  padding: 0.45rem 0.8rem;
  font-size: 0.84rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease, transform 0.2s ease;
}

.control-btn:hover {
  transform: translateY(-1px);
  color: var(--accent-strong);
}

.control-btn.is-active {
  color: #fff;
  background: linear-gradient(140deg, var(--accent) 0%, var(--accent-strong) 100%);
}

.main-grid {
  display: grid;
  grid-template-columns: minmax(220px, 285px) minmax(0, 1fr);
  align-items: start;
  gap: 1.2rem;
  margin-top: 1rem;
}

.main-grid > * {
  min-width: 0;
}

.toc-panel {
  position: sticky;
  top: 7.75rem;
}

.panel-box {
  padding: 1.05rem;
  border-radius: 14px;
  border: 1px solid var(--panel-border);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.panel-box h2 {
  margin: 0 0 0.9rem;
  font-size: 0.9rem;
  letter-spacing: 0.11em;
  text-transform: uppercase;
  color: var(--muted);
}

#timeline {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  max-height: calc(100vh - 12rem);
  overflow: auto;
  padding-right: 0.25rem;
}

.legacy-link-row {
  margin: 0.55rem 0 0;
  text-align: right;
}

.legacy-link {
  color: var(--muted);
  font-size: 0.72rem;
  letter-spacing: 0.03em;
  text-decoration: none;
  opacity: 0.75;
  transition: opacity 0.2s ease, color 0.2s ease;
}

.legacy-link:hover {
  color: var(--accent-strong);
  opacity: 1;
}

#timeline a,
.timeline-empty {
  display: block;
  border-radius: 10px;
  padding: 0.42rem 0.58rem;
  font-size: 0.86rem;
  text-decoration: none;
  color: var(--timeline-text);
  transition: background 0.2s ease, color 0.2s ease;
}

#timeline a:hover,
#timeline a.is-current {
  color: var(--timeline-active-text);
  background: var(--timeline-active-bg);
}

.timeline-empty {
  color: var(--muted);
}

.content-panel {
  border: 1px solid var(--panel-border);
  background: var(--surface);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: clamp(1rem, 2.1vw, 2.1rem);
  min-width: 0;
}

#journal-content.is-rendered {
  animation: content-rise 0.45s ease both;
}

#journal-content {
  max-width: 100%;
  min-width: 0;
  width: 100%;
  overflow-x: hidden;
  overflow-wrap: break-word;
  word-break: normal;
}

#journal-content h2.chapter-heading {
  margin: 2.25rem 0 1rem;
  padding: 0.75rem 0.95rem;
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  color: var(--chapter-title);
  background: var(--chapter-bg);
  font-family: var(--sans);
  font-size: clamp(1.05rem, 0.3vw + 0.98rem, 1.2rem);
  letter-spacing: 0.03em;
  text-transform: none;
}

#journal-content h3.entry-date {
  margin: 1.65rem 0 0.75rem;
  color: var(--accent-strong);
  font-family: var(--sans);
  font-size: 0.85rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

#journal-content p,
#journal-content li,
#journal-content blockquote {
  margin: 0 0 0.86rem;
  max-width: none;
  font-family: var(--serif);
  font-size: clamp(1.04rem, 0.28vw + 1rem, 1.17rem);
  line-height: 1.76;
}

#journal-content p.media-paragraph {
  max-width: none;
}

#journal-content ol,
#journal-content ul {
  margin: 0 0 0.9rem 1.15rem;
  padding: 0;
}

#journal-content img {
  display: block;
  margin: 0.5rem auto 1.1rem;
  max-width: min(100%, 980px);
  height: auto;
  border-radius: 9px;
}

#journal-content iframe {
  display: block;
  width: min(100%, 960px);
  aspect-ratio: 16 / 9;
  height: auto;
  margin: 0.9rem auto 1.2rem;
  border: 0;
  border-radius: 9px;
}

#journal-content a {
  color: var(--accent-strong);
  text-underline-offset: 2px;
  overflow-wrap: anywhere;
  word-break: break-word;
}

#journal-content a:hover {
  color: var(--link-hover);
}

.status {
  display: inline-block;
  padding: 0.7rem 0.85rem;
  border-radius: 10px;
  font-size: 0.94rem;
}

.status-loading {
  color: var(--status-loading-color);
  background: var(--status-loading-bg);
}

.status-error {
  color: var(--status-error-color);
  background: var(--status-error-bg);
}

.to-top {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 18;
  border: 0;
  border-radius: 999px;
  padding: 0.68rem 0.92rem;
  background: var(--to-top-bg);
  color: #fff;
  font-family: var(--sans);
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  cursor: pointer;
  box-shadow: var(--to-top-shadow);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.to-top.is-visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

@keyframes content-rise {
  from {
    opacity: 0.2;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

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

  .toc-panel {
    position: static;
  }

  #timeline {
    max-height: none;
    max-width: 100%;
    white-space: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    flex-direction: row;
  }

  #timeline a,
  .timeline-empty {
    display: inline-block;
  }
}

@media (max-width: 760px) {
  .shell {
    padding: 0.8rem 0.75rem 3rem;
  }

  .topbar {
    top: 0.35rem;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.85rem;
    padding: 1rem;
  }

  .control-cluster {
    width: 100%;
    justify-content: flex-end;
  }

  .controls {
    width: auto;
    justify-content: flex-end;
  }

  .control-label {
    margin-right: 0;
  }

  .topbar.is-compact {
    padding: 0.44rem 0.6rem;
  }

  .topbar.is-compact h1 {
    font-size: 0.95rem;
  }
}
