/* Post Page - Enhanced Styles */

/* Post Header */
.post-header {
  margin-bottom: 32px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--border);
}

.post-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.post-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 700;
}

.post-date {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: var(--muted);
}

.post-date svg {
  width: 16px;
  height: 16px;
}

.post-title {
  font-size: 36px;
  font-weight: 800;
  line-height: 1.2;
  color: var(--text);
  margin: 0 0 12px;
}

.post-summary {
  font-size: 18px;
  color: var(--muted);
  line-height: 1.6;
  margin: 0;
}

/* Post Stats */
.post-stats {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.post-stat {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: var(--muted);
}

.post-stat svg {
  width: 16px;
  height: 16px;
}

.post-stat-value {
  font-weight: 600;
  color: var(--text);
}

/* Enhanced Prose */
.prose {
  font-size: 17px;
  line-height: 1.8;
  color: var(--text);
  /* Mobile safety: prevent long URLs/inline-code from breaking layout */
  overflow-wrap: anywhere;
  word-break: break-word;
}

.prose h2 {
  font-size: 28px;
  font-weight: 700;
  margin-top: 48px;
  margin-bottom: 20px;
  color: var(--text);
  position: relative;
  padding-left: 16px;
}

.prose h2::before {
  content: "";
  position: absolute;
  left: 0;
  top: 4px;
  bottom: 4px;
  width: 4px;
  background: linear-gradient(180deg, var(--primary), transparent);
  border-radius: 999px;
}

.prose h3 {
  font-size: 22px;
  font-weight: 600;
  margin-top: 36px;
  margin-bottom: 16px;
  color: var(--text);
}

.prose h4 {
  font-size: 18px;
  font-weight: 600;
  margin-top: 28px;
  margin-bottom: 12px;
  color: var(--text);
}

.prose p {
  margin: 16px 0;
}

.prose ul,
.prose ol {
  margin: 16px 0;
  padding-left: 28px;
}

.prose li {
  margin: 8px 0;
}

.prose blockquote {
  margin: 24px 0;
  padding: 16px 20px;
  border-left: 4px solid var(--primary);
  background: var(--surface-2);
  border-radius: 0 8px 8px 0;
  color: var(--muted);
  font-style: italic;
}

.prose blockquote p {
  margin: 8px 0;
}

.prose a {
  color: var(--primary);
  text-decoration: underline;
  text-decoration-color: color-mix(in srgb, var(--primary) 40%, transparent);
  text-underline-offset: 3px;
  transition: all 0.2s ease;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.prose a:hover {
  text-decoration-color: var(--primary);
  color: var(--primary-700);
}

.prose img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  margin: 24px 0;
  box-shadow: var(--shadow-soft);
}

.prose img:hover {
  box-shadow: var(--shadow);
}

.prose hr {
  margin: 40px 0;
  border: none;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--border), transparent);
}

/* Enhanced TOC */
.post-toc {
  position: sticky;
  top: 80px;
  padding: 18px;
  border-radius: 18px;
  border: 1px solid color-mix(in srgb, var(--border) 75%, transparent);
  background: linear-gradient(
    180deg,
    color-mix(in srgb, var(--surface) 88%, transparent),
    color-mix(in srgb, var(--surface) 72%, transparent)
  );
  backdrop-filter: blur(14px);
  box-shadow: 0 22px 48px rgba(0, 0, 0, 0.28);
}

.post-toc {
  max-height: calc(100vh - 96px);
  overflow: auto;
  scrollbar-width: thin;
  scroll-behavior: smooth;
}

.post-toc::-webkit-scrollbar {
  width: 10px;
}

.post-toc::-webkit-scrollbar-thumb {
  background: color-mix(in srgb, var(--border) 70%, transparent);
  border-radius: 999px;
  border: 2px solid transparent;
  background-clip: padding-box;
}

.post-toc::-webkit-scrollbar-thumb:hover {
  background: color-mix(in srgb, var(--primary) 25%, var(--border));
  background-clip: padding-box;
}

.post-toc:hover {
  transform: none;
}

.post-toc {
  --toc-accent: #10b981;
  --toc-accent-2: #34d399;
  --toc-active-text: #d1fae5;
  --toc-active-text-contrast: #052e1c;
}

[data-theme="light"] .post-toc {
  --toc-active-text: #052e1c;
  --toc-active-text-contrast: #052e1c;
}

.toc-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toc-group-head {
  display: flex;
  align-items: center;
  gap: 8px;
}

.toc-group-head .toc-link {
  flex: 1 1 auto;
  min-width: 0;
}

.toc-toggle {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: 14px;
  border: none;
  background: color-mix(in srgb, var(--surface) 68%, transparent);
  color: color-mix(in srgb, var(--text) 70%, var(--muted));
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 180ms ease, transform 180ms ease;
}

.toc-toggle:hover {
  background: color-mix(in srgb, var(--surface-2) 72%, transparent);
  transform: translateY(-1px);
}

.toc-toggle:focus-visible {
  outline: none;
  box-shadow: var(--focus);
}

.toc-toggle::before {
  content: "";
  width: 10px;
  height: 10px;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(-45deg);
  opacity: 0.75;
  transition: transform 180ms ease, opacity 180ms ease;
}

.toc-group.is-open .toc-toggle::before {
  transform: rotate(45deg);
  opacity: 0.95;
}

.toc-children {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-left: 6px;
  padding-left: 10px;
  border-left: 1px solid color-mix(in srgb, var(--border) 60%, transparent);
  overflow: hidden;
  max-height: 0;
  opacity: 0;
  transform: translateY(-2px);
  transition: max-height 260ms ease, opacity 240ms ease, transform 240ms ease;
}

.toc-group.is-open .toc-children {
  max-height: 680px;
  opacity: 1;
  transform: translateY(0);
}

.sidebar-card-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  margin: 0 0 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-card-title svg {
  width: 18px;
  height: 18px;
  display: block;
  color: var(--primary);
  flex: 0 0 auto;
}

.toc-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  margin: 0 0 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding-bottom: 12px;
  border-bottom: 1px solid color-mix(in srgb, var(--border) 70%, transparent);
}

.post-toc > summary.toc-title {
  cursor: pointer;
  list-style: none;
  margin: 0;
}

.post-toc > summary.toc-title::-webkit-details-marker {
  display: none;
}

.post-toc > summary.toc-title::marker {
  content: "";
}

.post-toc > summary.toc-title::after {
  content: "";
  margin-left: auto;
  width: 10px;
  height: 10px;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(-45deg);
  opacity: 0.65;
  transition: transform 180ms ease, opacity 180ms ease;
}

details.post-toc[open] > summary.toc-title::after {
  transform: rotate(45deg);
  opacity: 0.9;
}

details.post-toc[open] > summary.toc-title {
  margin: 0 0 14px;
}

.toc-title svg {
  width: 18px;
  height: 18px;
  color: var(--primary);
}

.toc-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.post-toc .toc-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 14px;
  font-size: 13px;
  line-height: 1.35;
  color: color-mix(in srgb, var(--text) 84%, var(--muted));
  text-decoration: none;
  border: 1px solid transparent;
  background: color-mix(in srgb, var(--surface) 72%, transparent);
  transition: background 180ms ease, border-color 180ms ease, box-shadow 180ms ease, color 180ms ease;
}

.post-toc .toc-link:hover {
  color: var(--text);
  border-color: color-mix(in srgb, var(--toc-accent) 28%, var(--border));
  background: linear-gradient(
    90deg,
    color-mix(in srgb, var(--toc-accent) 14%, transparent),
    color-mix(in srgb, var(--surface) 80%, transparent)
  );
  box-shadow: 0 10px 24px rgba(16, 185, 129, 0.12);
}

.post-toc .toc-link.active {
  color: var(--toc-active-text);
  border-color: color-mix(in srgb, var(--toc-accent-2) 40%, var(--border));
  background: linear-gradient(
    90deg,
    rgba(16, 185, 129, 0.32),
    rgba(16, 185, 129, 0.1)
  );
  box-shadow:
    0 14px 34px rgba(16, 185, 129, 0.18),
    inset 0 0 0 1px rgba(52, 211, 153, 0.14);
  font-weight: 650;
}

.post-toc .toc-badge {
  width: 26px;
  height: 26px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 9px;
  flex: 0 0 auto;
  font-weight: 800;
  font-size: 12px;
  letter-spacing: 0.2px;
  color: color-mix(in srgb, var(--toc-accent-2) 90%, white 10%);
  background: rgba(16, 185, 129, 0.16);
  border: 1px solid rgba(16, 185, 129, 0.22);
  box-shadow: inset 0 0 0 1px rgba(16, 185, 129, 0.08);
}

.post-toc .toc-link.active .toc-badge {
  color: var(--toc-active-text-contrast);
  background: linear-gradient(180deg, rgba(52, 211, 153, 1), rgba(16, 185, 129, 0.9));
  border-color: rgba(52, 211, 153, 0.75);
  box-shadow: 0 10px 24px rgba(16, 185, 129, 0.28);
}

.post-toc .toc-bullet {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  flex: 0 0 auto;
  background: color-mix(in srgb, var(--muted) 40%, transparent);
  box-shadow: inset 0 0 0 2px rgba(2, 6, 23, 0.35);
}

.post-toc .toc-link.active .toc-bullet {
  background: rgba(52, 211, 153, 0.9);
  box-shadow: 0 10px 22px rgba(16, 185, 129, 0.2);
}

.post-toc .toc-text {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.post-toc .toc-link[data-level="3"] {
  padding-left: 16px;
  font-size: 12.5px;
  opacity: 0.96;
}

.toc a[data-level="4"] {
  padding-left: 36px;
  font-size: 12px;
}

.prose h2,
.prose h3 {
  scroll-margin-top: 86px;
}

body[data-page="post"] .layout > aside {
  align-self: stretch;
}

@media (prefers-reduced-motion: reduce) {
  .post-toc .toc-link {
    transition: none;
  }
  .toc-children {
    transition: none;
  }
}

/* Post Actions */
.post-actions {
  display: flex;
  gap: 12px;
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
}

.post-action-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  text-decoration: none;
}

.post-action-btn:hover {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(75, 107, 255, 0.3);
}

.post-action-btn svg {
  width: 16px;
  height: 16px;
}

/* Related Posts */
.related-posts {
  margin-top: 60px;
  padding-top: 40px;
  border-top: 2px solid var(--border);
}

.related-posts-title {
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 24px;
  color: var(--text);
}

.related-posts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
}

.related-post-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow-soft);
  transition: all 0.3s ease;
  text-decoration: none;
  display: block;
}

.related-post-card:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow);
  border-color: color-mix(in srgb, var(--primary) 40%, var(--border));
}

.related-post-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 8px;
  line-height: 1.4;
}

.related-post-summary {
  font-size: 14px;
  color: var(--muted);
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Post Navigation */
.post-nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-top: 40px;
}

.post-nav-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow-soft);
  transition: all 0.3s ease;
  text-decoration: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.post-nav-item:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow);
  border-color: color-mix(in srgb, var(--primary) 40%, var(--border));
}

.post-nav-item.is-disabled {
  pointer-events: none;
  opacity: 0.55;
}

.post-nav-label {
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}

.post-nav-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.4;
}

.post-nav-item.prev {
  text-align: left;
}

.post-nav-item.next {
  text-align: right;
}

/* Share Buttons */
.post-share {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 32px;
  padding: 20px;
  background: var(--surface-2);
  border-radius: var(--radius);
}

.post-share-label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.post-share-buttons {
  display: flex;
  gap: 8px;
}

.share-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  cursor: pointer;
  transition: all 0.2s ease;
}

.share-btn:hover {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
  transform: translateY(-2px);
}

.share-btn svg {
  width: 18px;
  height: 18px;
}

/* Code Enhancements */
.prose pre {
  margin: 24px 0;
}

.prose :not(pre) > code {
  padding: 3px 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.9em;
  color: var(--primary-700);
  font-weight: 500;
  overflow-wrap: anywhere;
  word-break: break-word;
  white-space: break-spaces;
}

/* Table Enhancements */
.prose table {
  width: 100%;
  border-collapse: collapse;
  margin: 24px 0;
  font-size: 15px;
}

.prose table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.prose thead {
  background: var(--surface-2);
}

.prose th {
  padding: 12px 16px;
  text-align: left;
  font-weight: 600;
  color: var(--text);
  border: 1px solid var(--border);
}

.prose td {
  padding: 12px 16px;
  border: 1px solid var(--border);
  color: var(--text);
}

.prose tbody tr:hover {
  background: var(--surface-2);
}

/* Responsive */
@media (max-width: 980px) {
  .post-toc {
    position: static;
  }

  .post-nav {
    grid-template-columns: 1fr;
  }

  .post-nav-item.next {
    text-align: left;
  }

  .related-posts-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .post-title {
    font-size: 28px;
  }

  .post-summary {
    font-size: 16px;
  }

  .prose {
    font-size: 16px;
  }

  .prose h2 {
    font-size: 24px;
    margin-top: 36px;
  }

  .prose h3 {
    font-size: 20px;
    margin-top: 28px;
  }

  .post-actions {
    flex-direction: column;
  }

  .post-action-btn {
    width: 100%;
    justify-content: center;
  }

  .post-share {
    flex-direction: column;
    align-items: flex-start;
  }

  .post-share-buttons {
    width: 100%;
    justify-content: space-around;
  }
}

/* Dark Mode */
[data-theme="dark"] .prose blockquote {
  background: rgba(30, 41, 59, 0.5);
}

[data-theme="dark"] .prose img {
  border-color: var(--border);
}

[data-theme="dark"] .related-post-card,
[data-theme="dark"] .post-nav-item {
  background: var(--surface);
  border-color: var(--border);
}

/* Print Styles */
@media print {
  .post-toc,
  .post-actions,
  .post-share,
  .post-nav,
  .related-posts {
    display: none;
  }

  .prose {
    font-size: 12pt;
    line-height: 1.6;
  }

  .prose a {
    color: #000;
    text-decoration: underline;
  }

  .prose img {
    max-width: 100%;
    page-break-inside: avoid;
  }
}

/* Animations */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.post-header,
.prose,
.post-actions {
  animation: fadeIn 0.6s ease;
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
  .post-header,
  .prose,
  .post-actions {
    animation: none;
  }

  .related-post-card:hover,
  .post-nav-item:hover,
  .post-action-btn:hover,
  .share-btn:hover,
  .toc-toggle:hover {
    transform: none;
  }
}

/* Reading Progress Bar */
.reading-progress {
  position: fixed;
  top: 64px;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--surface-2);
  z-index: 100;
}

.reading-progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), #7da2ff);
  width: 0%;
  transition: width 0.1s ease;
}

/* Scroll to Top Button */
.scroll-to-top {
  position: fixed;
  bottom: 32px;
  right: 32px;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  padding: 0;
  appearance: none;
  -webkit-appearance: none;
  background: var(--primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(75, 107, 255, 0.3);
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
  z-index: 100;
  overflow: visible;
}

.scroll-to-top-ring {
  position: absolute;
  /* Slightly overlap the button edge to avoid showing page background as a "black ring". */
  inset: -4px;
  width: calc(100% + 8px);
  height: calc(100% + 8px);
  pointer-events: none;
  transform: rotate(-90deg);
  filter: none;
}

.scroll-to-top-ring-track {
  stroke: color-mix(in srgb, var(--primary) 22%, transparent);
}

.scroll-to-top-ring-indicator {
  stroke: var(--primary);
  transition: stroke-dashoffset 120ms linear;
}

/* Keep the arrow icon above the ring (avoid affecting the injected ring SVG). */
.scroll-to-top > svg:not(.scroll-to-top-ring) {
  position: relative;
  z-index: 1;
}

.scroll-to-top.visible {
  opacity: 1;
  visibility: visible;
}

.scroll-to-top:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(75, 107, 255, 0.4);
}

.scroll-to-top:focus-visible {
  outline: none;
  box-shadow: var(--focus), 0 8px 20px rgba(75, 107, 255, 0.4);
}

.scroll-to-top > svg:not(.scroll-to-top-ring) {
  width: 24px;
  height: 24px;
}

@media (max-width: 860px) {
  /* Leave room for the mobile command palette FAB ("?") at bottom-right. */
  .scroll-to-top {
    bottom: calc(76px + env(safe-area-inset-bottom));
  }
}

@media (max-width: 640px) {
  .scroll-to-top {
    bottom: calc(76px + env(safe-area-inset-bottom));
    right: 20px;
    width: 44px;
    height: 44px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .scroll-to-top-ring-indicator {
    transition: none;
  }
}
