/* NFHN Styles */

/* =============================================================================
   Theme system using light-dark() CSS function (Baseline 2024)
   Enhanced with Relative Color Syntax (Baseline 2024)
   ============================================================================= */

/* =============================================================================
   Accent Color System using Relative Color Syntax
   All accent variations derived from a single base color using oklch()
   ============================================================================= */

:root {
  /* Base accent color - NFHN orange */
  --accent: #ff7a18;
  
  /* Derive accent variations using relative color syntax */
  --accent-light: oklch(from var(--accent) calc(l + 0.1) c h);
  --accent-dark: oklch(from var(--accent) calc(l - 0.15) c h);
  --accent-muted: oklch(from var(--accent) l calc(c * 0.6) h);
  --accent-subtle: oklch(from var(--accent) calc(l + 0.25) calc(c * 0.4) h);
  
  /* Interactive states derived from accent */
  --accent-hover: oklch(from var(--accent) calc(l + 0.05) c h);
  --accent-active: oklch(from var(--accent) calc(l - 0.1) c h);
  --accent-focus: oklch(from var(--accent) l c h / 0.5);
  
  /* Complementary and analogous colors */
  --accent-complement: oklch(from var(--accent) l c calc(h + 180));
  --accent-warm: oklch(from var(--accent) l c calc(h - 20));
  --accent-cool: oklch(from var(--accent) l c calc(h + 40));
}

/* Reserve the scrollbar's track at all times, so navigating between a page that
   overflows and one that does not cannot shift the centred layout sideways. */
:root {
  scrollbar-gutter: stable;
}

/* Light theme (default) */
:root,
:root[data-theme="light"] {
  color-scheme: light;
  --background: #f5f5f5;
  --background-elevated: #ffffff;
  --text-strong: #111827;
  --text-primary: #1f2937;
  --text-muted: #4b5563;
  --text-secondary: #374151;
  --border-color: rgba(0, 0, 0, 0.1);
  --border-subtle: rgba(0, 0, 0, 0.08);
  --divider-color: black;
  --badge-bg: rgba(0, 0, 0, 0.04);
  --comment-bg: rgba(0, 0, 0, 0.02);
  
  /* Light theme interactive overlays derived from accent */
  --hover-overlay: oklch(from var(--accent) l c h / 0.08);
  --active-overlay: oklch(from var(--accent) l c h / 0.15);
  --selected-bg: oklch(from var(--accent) calc(l + 0.35) calc(c * 0.3) h);
}

/* Dark theme */
:root[data-theme="dark"] {
  color-scheme: dark;
  --background: #0d1117;
  --background-elevated: #161b22;
  --text-strong: #f0f6fc;
  --text-primary: #c9d1d9;
  --text-muted: #8b949e;
  --text-secondary: #b1bac4;
  --border-color: rgba(255, 255, 255, 0.1);
  --border-subtle: rgba(255, 255, 255, 0.08);
  --divider-color: #30363d;
  --badge-bg: rgba(255, 255, 255, 0.06);
  --comment-bg: rgba(255, 255, 255, 0.03);
  
  /* Dark theme: lighter accent for better visibility */
  --accent-light: oklch(from var(--accent) calc(l + 0.15) c h);
  --accent-dark: oklch(from var(--accent) calc(l - 0.1) c h);
  
  /* Dark theme interactive overlays */
  --hover-overlay: oklch(from var(--accent) l c h / 0.12);
  --active-overlay: oklch(from var(--accent) l c h / 0.2);
  --selected-bg: oklch(from var(--accent) calc(l - 0.3) calc(c * 0.4) h);
}

/* Auto theme - follows system preference */
:root[data-theme="auto"] {
  color-scheme: light dark;
  --background: light-dark(#f5f5f5, #0d1117);
  --background-elevated: light-dark(#ffffff, #161b22);
  --text-strong: light-dark(#111827, #f0f6fc);
  --text-primary: light-dark(#1f2937, #c9d1d9);
  --text-muted: light-dark(#4b5563, #8b949e);
  --text-secondary: light-dark(#374151, #b1bac4);
  --border-color: light-dark(rgba(0, 0, 0, 0.1), rgba(255, 255, 255, 0.1));
  --border-subtle: light-dark(rgba(0, 0, 0, 0.08), rgba(255, 255, 255, 0.08));
  --divider-color: light-dark(black, #30363d);
  --badge-bg: light-dark(rgba(0, 0, 0, 0.04), rgba(255, 255, 255, 0.06));
  --comment-bg: light-dark(rgba(0, 0, 0, 0.02), rgba(255, 255, 255, 0.03));
  
  /* Auto theme interactive overlays - derived from accent */
  --hover-overlay: oklch(from var(--accent) l c h / 0.1);
  --active-overlay: oklch(from var(--accent) l c h / 0.18);
}

/* Theme toggle styles */
.theme-toggle {
  display: flex;
  align-items: center;
  gap: 0.5em;
  font-size: 0.85em;
  color: var(--text-muted);
}

.theme-toggle fieldset {
  display: flex;
  gap: 0.25em;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 0.2em;
  margin: 0;
  background: var(--badge-bg);
}

.theme-toggle legend {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.theme-toggle input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.theme-toggle label {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.35em 0.6em;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.15s ease;
  min-width: 32px;
  min-height: 32px;
}

.theme-toggle label:hover {
  background: var(--border-color);
}

.theme-toggle input[type="radio"]:checked + label {
  background: var(--background-elevated);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.theme-toggle input[type="radio"]:focus-visible + label {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.theme-toggle svg {
  width: 16px;
  height: 16px;
  fill: currentColor;
}

/* Theme toggle tooltips using CSS Anchor Positioning */
@supports (anchor-name: --theme-label) {
  .theme-toggle label {
    anchor-name: --theme-label;
    position: relative;
  }

  .theme-toggle label::after {
    content: attr(title);
    position: fixed;
    position-anchor: --theme-label;
    
    top: anchor(bottom);
    left: anchor(center);
    translate: -50% 0.35rem;
    
    /* Use solid dark background for good contrast in both light and dark modes */
    background: oklch(0.25 0 0);
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    white-space: nowrap;
    pointer-events: none;
    z-index: 1000;
    
    opacity: 0;
    transition: opacity 0.15s ease;
    
    position-try-fallbacks: --theme-tooltip-above;
  }

  @position-try --theme-tooltip-above {
    top: auto;
    bottom: anchor(top);
    translate: -50% -0.35rem;
  }

  .theme-toggle label:hover::after {
    opacity: 1;
  }
}

.header-bar {
  display: flex;
  align-items: center;
  gap: 0.75em;
  margin-bottom: 1em;
}

/* Skip link for keyboard accessibility */
.skip-link {
  position: absolute;
  top: -100px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text-strong);
  color: var(--background);
  padding: 0.75em 1.5em;
  border-radius: 0 0 8px 8px;
  text-decoration: none;
  font-weight: 600;
  z-index: 1000;
  transition: top 0.2s ease;
}

.skip-link:focus {
  top: 0;
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Keyboard hint button */
.keyboard-hint {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--badge-bg);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 0.2em 0.4em;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.85em;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.keyboard-hint:hover,
.keyboard-hint:focus {
  background: var(--border-color);
  border-color: var(--text-muted);
  outline: none;
}

.keyboard-hint kbd {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  font-size: 0.9em;
  padding: 0;
  background: transparent;
  border: none;
  box-shadow: none;
}

* {
  box-sizing: border-box;
}

body {
  font-family: system-ui, sans-serif;
  background-color: var(--background);
  margin: 40px auto;
  max-width: 80ch;
  line-height: 1.6;
  font-size: 18px;
  color: var(--text-strong);
  padding: 0 10px;
}

main {
  display: block;
}

h1,
h2,
h3 {
  line-height: 1.2;
  font-size: x-large;
  margin: 0;
}

ul {
  list-style: none;
  padding-left: 0;
}

article ul {
  padding-left: 1em;
}

ol {
  list-style-type: none;
  counter-reset: section;
  padding: 0;
}

ol > li {
  position: relative;
  display: grid;
  grid-template-columns: 0fr 1fr;
  grid-template-areas:
    ". main"
    ". footer"
    ". .";
  gap: 1em;
  margin-bottom: 1em;
}

ol > li:before {
  counter-increment: section;
  content: counter(section);
  font-size: 1.6em;
  position: absolute;
}

ol > li:after {
  content: "";
  background: var(--divider-color);
  position: absolute;
  bottom: 0;
  left: 3em;
  width: calc(100% - 3em);
  height: 1px;
}

ol > li > * {
  margin-left: 2em;
}

.title {
  grid-area: main;
}

.comments {
  grid-area: footer;
}

.title,
.comments {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 0.25em;
}

a {
  text-decoration: none;
  color: inherit;
}

article a:hover {
  text-decoration: underline;
}

a:hover .story-title-text {
  text-decoration: underline;
}

a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
  border-radius: 4px;
}

.badge {
  display: inline-block;
  padding: 0.1em 0.4em;
  border-radius: 999px;
  font-size: 0.7em;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-right: 0.5em;
  border: 1px solid var(--border-color);
  background: var(--badge-bg);
  vertical-align: middle;
}

/* Badge colors using Relative Color Syntax (Baseline 2024)
   Each badge type defines a base color, then derives background/border from it */

.badge-link {
  /* Blue - hue ~240 in oklch */
  --badge-color: oklch(0.55 0.2 250);
  background: oklch(from var(--badge-color) l c h / 0.1);
  border-color: oklch(from var(--badge-color) l c h / 0.3);
  color: var(--badge-color);
}

.badge-ask {
  /* Red - hue ~25 in oklch */
  --badge-color: oklch(0.55 0.2 25);
  background: oklch(from var(--badge-color) l c h / 0.1);
  border-color: oklch(from var(--badge-color) l c h / 0.3);
  color: var(--badge-color);
}

.badge-show {
  /* Green - hue ~145 in oklch */
  --badge-color: oklch(0.55 0.18 145);
  background: oklch(from var(--badge-color) l c h / 0.1);
  border-color: oklch(from var(--badge-color) l c h / 0.3);
  color: var(--badge-color);
}

.badge-tell {
  /* Blue - hue ~240 in oklch */
  --badge-color: oklch(0.55 0.18 240);
  background: oklch(from var(--badge-color) l c h / 0.1);
  border-color: oklch(from var(--badge-color) l c h / 0.3);
  color: var(--badge-color);
}

.badge-job {
  /* Orange/Gold - hue ~75 in oklch */
  --badge-color: oklch(0.6 0.18 75);
  background: oklch(from var(--badge-color) l c h / 0.1);
  border-color: oklch(from var(--badge-color) l c h / 0.3);
  color: var(--badge-color);
}

.badge-default {
  background: var(--badge-bg);
  border-color: var(--border-color);
}

.story-title-text {
  font-weight: 500;
}

.story-meta {
  font-size: 0.85em;
  color: var(--text-muted);
}

.nav-feeds {
  display: flex;
  gap: 0.75em;
  padding: 1.5em 0;
  font-size: 0.9em;
  color: var(--text-primary);
}

.nav-feeds a {
  text-decoration: none;
  color: var(--text-primary);
  font-weight: 600;
}

.nav-feeds a.active {
  color: var(--text-strong);
  text-decoration: underline;
}

hr {
  border: none;
  border-bottom: 1px solid var(--border-subtle);
  margin: 1.5em 0;
}

article {
  padding-left: 0.5em;
}

small {
  padding-top: 0.35em;
}

p {
  padding-block: 0.5em;
  margin: 0;
}

.meta-line {
  font-size: 0.9em;
  color: var(--text-secondary);
}

.more-link {
  display: block;
  text-align: center;
  margin-top: 1.5em;
}

.story-heading {
  display: inline-block;
}

details {
  background-color: var(--background);
  margin: 40px auto;
  max-width: 650px;
  line-height: 1.6;
  font-size: 18px;
  color: var(--text-primary);
}

summary {
  font-weight: bold;
  margin: -0.5em -0.5em 0;
  padding: 0.75em;
  display: flex;
  align-items: center;
  gap: 0.5em;
  min-height: 48px;
}

details[open] summary {
  border-bottom: 1px solid var(--border-color);
}

.comment-meta {
  display: flex;
  align-items: center;
  gap: 0.5em;
  flex-wrap: wrap;
}

.comment-user {
  font-weight: 700;
  color: var(--text-strong);
}

.comment-permalink {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.45em 0.7em;
  min-height: 44px;
  min-width: 44px;
  border-radius: 8px;
  text-decoration: none;
  color: var(--text-primary);
  background-color: var(--comment-bg);
}

.comment-permalink:hover {
  text-decoration: underline;
}

.comment-permalink:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

pre {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* Long unbreakable strings (e.g. URLs) in user-generated content must wrap
   instead of forcing the page into horizontal scroll. Applied to descendants
   too because some elements (flex/inline-block children, nested blocks) can
   establish their own min-content width that defeats inherited wrapping. */
details > div,
details > div *,
article,
article * {
  overflow-wrap: anywhere;
  min-width: 0;
}

/* Constrain embedded media in user-generated content so wide images or
   tables don't push the page into horizontal scroll. */
details > div img,
details > div video,
article img,
article video {
  max-width: 100%;
  height: auto;
}

details > div table,
article table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
}

/* User profile page */
.user-profile h1 {
  margin-bottom: 0.5em;
}

.user-stats {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.25em 1em;
  margin: 1em 0;
  padding: 1em;
  background: var(--comment-bg);
  border-radius: 8px;
}

.user-stats dt {
  font-weight: 600;
  color: var(--text-muted);
}

.user-stats dd {
  margin: 0;
  color: var(--text-strong);
}

.user-about {
  margin: 1.5em 0;
}

.user-about h2 {
  font-size: 1em;
  color: var(--text-muted);
  margin-bottom: 0.5em;
}

.user-links {
  margin-top: 1.5em;
}

.user-links a {
  color: var(--text-primary);
  text-decoration: underline;
}

/* OP badge for comment author */
.comment-user.is-op {
  color: var(--badge-show);
  font-weight: 600;
}

.op-badge {
  font-size: 0.75em;
  /* Use a darker green for good contrast with white text */
  background: oklch(0.45 0.15 145);
  color: white;
  padding: 0.1em 0.4em;
  border-radius: 3px;
  margin-left: 0.3em;
  vertical-align: middle;
  text-decoration: none;
}

/* User links in comments and stories */
.user-link {
  color: var(--text-muted);
  text-decoration: none;
}

.user-link:hover {
  text-decoration: underline;
  color: var(--text-primary);
}

.comment-user .user-link {
  color: inherit;
}

/* User submissions section */
.user-submissions {
  margin-top: 2em;
  padding-top: 1em;
  border-top: 1px solid var(--border-color);
}

.user-submissions h2 {
  font-size: 1em;
  color: var(--text-muted);
  margin-bottom: 1em;
}

.user-submissions .stories {
  padding-left: 0;
}

.link-sep {
  margin: 0 0.5em;
  color: var(--text-muted);
}

/* Keyboard navigation focus styles */
.kbd-focus {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
  background: rgba(255, 122, 24, 0.05);
  border-radius: 4px;
}

.kbd-focus.comment {
  padding-left: calc(var(--indent-level, 0) * var(--indent-unit, 16px) + 4px);
  margin-left: calc(var(--indent-level, 0) * var(--indent-unit, 16px) * -1 - 4px);
}

/* Visually hidden class for screen reader text */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Keyboard shortcuts modal */
.shortcuts-modal {
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 1.5em;
  max-width: 400px;
  background: var(--background-elevated);
  color: var(--text-primary);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.shortcuts-modal::backdrop {
  background: rgba(0, 0, 0, 0.5);
}

.shortcuts-modal h2 {
  margin: 0 0 1em;
  font-size: 1.2em;
  color: var(--text-strong);
}

.shortcuts-list {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.5em 1em;
  margin: 0;
}

.shortcuts-list dt {
  font-weight: normal;
}

.shortcuts-list dd {
  margin: 0;
  color: var(--text-muted);
}

.shortcuts-list kbd {
  display: inline-block;
  padding: 0.2em 0.5em;
  font-family: inherit;
  font-size: 0.85em;
  background: var(--badge-bg);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.modal-close {
  position: absolute;
  top: 0.5em;
  right: 0.5em;
  width: 2em;
  height: 2em;
  padding: 0;
  border: none;
  background: transparent;
  font-size: 1.2em;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 4px;
}

.modal-close:hover {
  background: var(--badge-bg);
  color: var(--text-primary);
}

/* Settings menu button */
.settings-menu-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--badge-bg);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 0.3em;
  cursor: pointer;
  color: var(--text-muted);
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.settings-menu-btn:hover,
.settings-menu-btn:focus {
  background: var(--border-color);
  border-color: var(--text-muted);
  outline: none;
}

.settings-menu-btn svg {
  fill: currentColor;
}

/* Settings menu dialog */
.settings-menu {
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 1.5em;
  max-width: 300px;
  background: var(--background-elevated);
  color: var(--text-primary);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.settings-menu::backdrop {
  background: rgba(0, 0, 0, 0.5);
}

.settings-menu h2 {
  margin: 0 0 1em;
  font-size: 1.2em;
  color: var(--text-strong);
}

.settings-menu .theme-toggle {
  margin-bottom: 1em;
}

.settings-menu .keyboard-hint {
  width: 100%;
  justify-content: flex-start;
  gap: 0.5em;
  padding: 0.5em 0.75em;
}

/* External link indicators */
.external-link {
  position: relative;
}

.external-link::after {
  content: "↗";
  display: inline-block;
  font-size: 0.7em;
  margin-left: 0.2em;
  opacity: 0.6;
  vertical-align: super;
}

.story-link.external-link::after {
  display: none;
}

/* Mobile improvements - larger touch targets */
@media (max-width: 600px) {
  /* Reduce base font size slightly for more content */
  body {
    font-size: 16px;
    padding: 0 12px;
    margin: 16px auto;
  }

  /* Improve story list layout for narrow screens */
  ol > li {
    display: block;
    padding-left: 2.5em;
    margin-bottom: 1.25em;
    padding-bottom: 1em;
  }

  ol > li:before {
    font-size: 1.3em;
    left: 0;
    top: 0.1em;
  }

  ol > li:after {
    left: 0;
    width: 100%;
  }

  ol > li > * {
    margin-left: 0;
  }

  /* Stack title and meta for better readability */
  .title {
    margin-bottom: 0.5em;
  }

  .story-meta {
    font-size: 0.8em;
    line-height: 1.5;
  }

  /* Compact badges on mobile */
  .badge {
    font-size: 0.65em;
    padding: 0.15em 0.35em;
  }

  /* Better header layout */
  .header-bar {
    flex-direction: row;
    align-items: center;
    gap: 0.75em;
  }

  .theme-toggle {
    width: auto;
    justify-content: center;
  }

  /* Scrollable nav with visual indicators */
  .nav-feeds {
    margin: 0 -12px;
    padding: 0 12px;
    gap: 0.4em;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  .nav-feeds::-webkit-scrollbar {
    display: none;
  }

  .nav-feeds a {
    scroll-snap-align: start;
    padding: 0.4em 0.6em;
    min-height: 40px;
    font-size: 0.85em;
    display: flex;
    align-items: center;
    white-space: nowrap;
    /* background: var(--badge-bg); */
    border-radius: 6px;
  }

  .nav-feeds a.active {
    background: var(--background-elevated);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  }

  /* Reduce heading sizes */
  h1, h2, h3 {
    font-size: 1.25em;
  }

  .page-controls {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5em;
  }

  /* Optimize comment layout */
  details {
    margin: 20px auto;
    font-size: 16px;
  }

  summary {
    min-height: 56px;
    padding: 0.75em 0.5em;
    gap: 0.4em;
  }

  .comment-meta {
    gap: 0.35em;
    font-size: 0.85em;
  }

  .comment-permalink {
    min-height: 48px;
    min-width: 48px;
  }

  /* Reduce comment indentation on mobile */
  .comment {
    --indent-unit: 12px;
  }

  /* Full-width pagination button */
  .more-link {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 1em -12px 0;
    padding: 1em;
    background: var(--badge-bg);
    border-radius: 0;
    text-align: center;
    font-weight: 600;
    min-height: 48px;
  }

  /* Share buttons - horizontal scroll */
  .share-buttons {
    gap: 0.4em;
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 0.5em;
  }

  .share-btn {
    flex-shrink: 0;
    width: 2.5em;
    height: 2.5em;
  }

  /* User profile adjustments */
  .user-stats {
    gap: 0.2em 0.75em;
    padding: 0.75em;
  }

  /* Modal adjustments */
  .shortcuts-modal,
  .settings-menu {
    max-width: calc(100vw - 2em);
    padding: 1em;
  }

  .font-btn {
    min-width: 44px;
    min-height: 44px;
  }
}

/* Extra small screens (< 400px) */
@media (max-width: 400px) {
  body {
    font-size: 15px;
    padding: 0 10px;
  }

  ol > li {
    padding-left: 2em;
  }

  ol > li:before {
    font-size: 1.1em;
  }

  .nav-feeds a {
    padding: 0.35em 0.5em;
    font-size: 0.8em;
  }

  .badge {
    font-size: 0.6em;
  }

  /* Stack metadata on very small screens */
  .story-meta {
    display: flex;
    flex-direction: column;
    gap: 0.25em;
  }

  .link-sep {
    display: none;
  }
}

/* Landscape phone orientation */
@media (max-width: 600px) and (orientation: landscape) {
  body {
    margin: 10px auto;
  }

  .header-bar {
    flex-direction: row;
    flex-wrap: wrap;
  }

  summary {
    min-height: 48px;
  }
}

/* Code syntax highlighting for dark mode */
:root[data-theme="dark"] pre,
:root[data-theme="dark"] code {
  background: #1e1e1e;
  border-radius: 4px;
}

:root[data-theme="dark"] code {
  padding: 0.1em 0.3em;
}

:root[data-theme="dark"] pre code {
  padding: 0;
  background: transparent;
}

:root[data-theme="dark"] pre {
  padding: 1em;
  overflow-x: auto;
  border: 1px solid var(--border-color);
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] pre,
  :root[data-theme="auto"] code {
    background: #1e1e1e;
    border-radius: 4px;
  }
  
  :root[data-theme="auto"] code {
    padding: 0.1em 0.3em;
  }
  
  :root[data-theme="auto"] pre code {
    padding: 0;
    background: transparent;
  }
  
  :root[data-theme="auto"] pre {
    padding: 1em;
    overflow-x: auto;
    border: 1px solid var(--border-color);
  }
}

/* Light mode code blocks */
:root[data-theme="light"] pre,
:root[data-theme="light"] code {
  background: #f6f8fa;
  border-radius: 4px;
}

:root[data-theme="light"] code {
  padding: 0.1em 0.3em;
}

:root[data-theme="light"] pre code {
  padding: 0;
  background: transparent;
}

:root[data-theme="light"] pre {
  padding: 1em;
  overflow-x: auto;
  border: 1px solid var(--border-color);
}

/* Auto theme in light mode (when system prefers light) */
@media (prefers-color-scheme: light) {
  :root[data-theme="auto"] pre,
  :root[data-theme="auto"] code {
    background: #f6f8fa;
    border-radius: 4px;
  }
  
  :root[data-theme="auto"] code {
    padding: 0.1em 0.3em;
  }
  
  :root[data-theme="auto"] pre code {
    padding: 0;
    background: transparent;
  }
  
  :root[data-theme="auto"] pre {
    padding: 1em;
    overflow-x: auto;
    border: 1px solid var(--border-color);
  }
}

/* Improved contrast for dark mode badges using Relative Color Syntax */
:root[data-theme="dark"] .badge {
  font-weight: 500;
}

:root[data-theme="dark"] .badge-link {
  --badge-color: oklch(0.75 0.15 250);
  background: oklch(from var(--badge-color) l c h / 0.15);
  border-color: oklch(from var(--badge-color) l c h / 0.4);
  color: var(--badge-color);
}

:root[data-theme="dark"] .badge-ask {
  --badge-color: oklch(0.75 0.15 25);
  background: oklch(from var(--badge-color) l c h / 0.15);
  border-color: oklch(from var(--badge-color) l c h / 0.4);
  color: var(--badge-color);
}

:root[data-theme="dark"] .badge-show {
  --badge-color: oklch(0.75 0.14 145);
  background: oklch(from var(--badge-color) l c h / 0.15);
  border-color: oklch(from var(--badge-color) l c h / 0.4);
  color: var(--badge-color);
}

:root[data-theme="dark"] .badge-tell {
  --badge-color: oklch(0.75 0.14 240);
  background: oklch(from var(--badge-color) l c h / 0.15);
  border-color: oklch(from var(--badge-color) l c h / 0.4);
  color: var(--badge-color);
}

:root[data-theme="dark"] .badge-job {
  --badge-color: oklch(0.8 0.14 75);
  background: oklch(from var(--badge-color) l c h / 0.15);
  border-color: oklch(from var(--badge-color) l c h / 0.4);
  color: var(--badge-color);
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] .badge {
    font-weight: 500;
  }
  
  :root[data-theme="auto"] .badge-link {
    --badge-color: oklch(0.75 0.15 250);
    background: oklch(from var(--badge-color) l c h / 0.15);
    border-color: oklch(from var(--badge-color) l c h / 0.4);
    color: var(--badge-color);
  }
  
  :root[data-theme="auto"] .badge-ask {
    --badge-color: oklch(0.75 0.15 25);
    background: oklch(from var(--badge-color) l c h / 0.15);
    border-color: oklch(from var(--badge-color) l c h / 0.4);
    color: var(--badge-color);
  }
  
  :root[data-theme="auto"] .badge-show {
    --badge-color: oklch(0.75 0.14 145);
    background: oklch(from var(--badge-color) l c h / 0.15);
    border-color: oklch(from var(--badge-color) l c h / 0.4);
    color: var(--badge-color);
  }
  
  :root[data-theme="auto"] .badge-tell {
    --badge-color: oklch(0.75 0.14 240);
    background: oklch(from var(--badge-color) l c h / 0.15);
    border-color: oklch(from var(--badge-color) l c h / 0.4);
    color: var(--badge-color);
  }
  
  :root[data-theme="auto"] .badge-job {
    --badge-color: oklch(0.8 0.14 75);
    background: oklch(from var(--badge-color) l c h / 0.15);
    border-color: oklch(from var(--badge-color) l c h / 0.4);
    color: var(--badge-color);
  }
}

/* Bookmark/Save button */
.bookmark-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.2em;
  height: 2.2em;
  padding: 0;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background: var(--badge-bg);
  color: var(--text-muted);
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s, transform 0.1s;
  flex-shrink: 0;
}

.bookmark-btn:hover {
  background: var(--background-elevated);
  color: var(--text-primary);
  transform: scale(1.05);
}

.bookmark-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.bookmark-btn svg {
  fill: currentColor;
}

.bookmark-btn .bookmark-icon-filled {
  display: none;
}

.bookmark-btn .bookmark-icon-outline {
  display: block;
}

.bookmark-btn[aria-pressed="true"] {
  background: oklch(from var(--accent) l c h / 0.15);
  border-color: oklch(from var(--accent) l c h / 0.5);
  color: var(--accent);
}

.bookmark-btn[aria-pressed="true"] .bookmark-icon-filled {
  display: block;
}

.bookmark-btn[aria-pressed="true"] .bookmark-icon-outline {
  display: none;
}

.bookmark-btn[aria-pressed="true"]:hover {
  background: oklch(from var(--accent) l c h / 0.25);
}

/* Story actions container */
.story-actions {
  grid-area: footer;
  display: flex;
  align-items: center;
  gap: 0.75em;
}

/* Article actions (bookmark on article page) */
.article-actions {
  display: flex;
  align-items: center;
  gap: 0.5em;
  margin: 0.5em 0;
}

.article-actions .bookmark-btn {
  width: 2em;
  height: 2em;
}

/* Reader mode link */
.reader-mode-link {
  display: inline-flex;
  align-items: center;
  gap: 0.35em;
  padding: 0.1em 0.5em;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background: var(--badge-bg);
  color: var(--text-muted);
  font-size: 0.85em;
  text-decoration: none;
  transition: background-color 0.15s, color 0.15s;
}

.reader-mode-link:hover {
  background: var(--background-elevated);
  color: var(--text-primary);
  text-decoration: none;
}

.reader-mode-link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.reader-mode-link:active {
  transform: scale(0.95);
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.reader-mode-link svg {
  fill: currentColor;
  flex-shrink: 0;
}

/* Picture-in-Picture reader button */
.pip-reader-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35em;
  padding: 0.1em 0.5em;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background: var(--badge-bg);
  color: var(--text-muted);
  font-size: 0.85em;
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s;
}

.pip-reader-btn:hover {
  background: var(--background-elevated);
  color: var(--text-primary);
}

.pip-reader-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.pip-reader-btn:active {
  transform: scale(0.95);
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.pip-reader-btn svg {
  fill: currentColor;
  flex-shrink: 0;
}

.pip-reader-btn .pip-label {
  font-weight: 500;
}

/* Hide PiP button when not supported */
.no-pip .pip-reader-btn {
  display: none;
}

/* Saved stories page */
.saved-header {
  margin-bottom: 1.5em;
  padding-left: 0.5em;
}

.saved-header h1 {
  margin: 0 0 0.25em;
}

.saved-description {
  font-size: 0.9em;
  color: var(--text-muted);
  margin: 0;
}

/* Export/Import actions */
.saved-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5em;
  margin-top: 1em;
}

.saved-action-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4em;
  padding: 0.5em 0.9em;
  font-size: 0.85em;
  font-weight: 500;
  color: var(--text-main);
  background: var(--surface-elevated);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.saved-action-btn:hover {
  background: var(--surface-hover);
  border-color: var(--accent);
}

.saved-action-btn:active {
  transform: scale(0.97);
}

.saved-action-btn:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.saved-action-btn svg {
  flex-shrink: 0;
}

.saved-action-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Toast notifications */
.toast-notification {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(100%);
  padding: 0.75rem 1.25rem;
  background: oklch(0.25 0 0);
  color: #fff;
  font-size: 0.9em;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  z-index: 1000;
  opacity: 0;
  transition: transform 0.3s ease, opacity 0.3s ease;
}

.toast-notification.toast-visible {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.toast-notification.toast-error {
  background: oklch(0.45 0.15 30);
}

.saved-count {
  font-size: 0.85em;
  color: var(--text-muted);
  margin-bottom: 0.5em;
  padding-left: 0.5em;
}

.empty-saved {
  text-align: center;
  padding: 3em 1em;
  color: var(--text-muted);
}

.empty-saved p {
  margin: 0.5em 0;
}

.offline-message {
  text-align: center;
  padding: 3em 1em;
}

.offline-message h1 {
  color: var(--text-strong);
  margin-bottom: 0.5em;
}

.offline-message p {
  margin: 0.75em 0;
  color: var(--text-muted);
}

.offline-message a {
  color: var(--accent);
}

.offline-message .more-link {
  margin-top: 1.5em;
}

.loading-message {
  text-align: center;
  padding: 2em 1em;
  color: var(--text-muted);
}

/* Mobile touch target improvements for bookmark */
@media (max-width: 600px) {
  .bookmark-btn {
    width: 2em;
    height: 2em;
  }
  
  .story-actions {
    gap: 0.5em;
  }
}

/* =============================================================================
   Print Stylesheet - Optimized for article pages
   ============================================================================= */

@media print {
  /* Reset colors to black and white for printing */
  :root,
  :root[data-theme="light"],
  :root[data-theme="dark"],
  :root[data-theme="auto"] {
    --background: #ffffff;
    --background-elevated: #ffffff;
    --text-strong: #000000;
    --text-primary: #000000;
    --text-muted: #333333;
    --text-secondary: #333333;
    --border-color: #cccccc;
    --border-subtle: #dddddd;
    --divider-color: #000000;
    --badge-bg: #f0f0f0;
    --comment-bg: #f5f5f5;
    color-scheme: light;
  }
  
  /* Page setup */
  @page {
    margin: 1.5cm;
    size: auto;
  }
  
  /* Basic print resets */
  body {
    font-size: 12pt;
    line-height: 1.5;
    max-width: none;
    margin: 0;
    padding: 0;
    background: white;
    color: black;
  }
  
  /* Hide non-essential UI elements */
  .nav-feeds,
  .header-bar,
  .theme-toggle,
  .skip-link,
  .share-buttons,
  .bookmark-btn,
  .reader-mode-link,
  .story-actions .bookmark-btn,
  .article-actions .bookmark-btn,
  .article-actions .reader-mode-link,
  .more-link,
  .shortcuts-modal,
  .settings-menu,
  .saved-header,
  .saved-description,
  .saved-count,
  #aria-live {
    display: none !important;
  }
  
  /* Show URLs for links */
  article a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-size: 0.85em;
    color: #666;
    word-break: break-all;
  }
  
  /* Don't show URL for internal links */
  article a[href^="/"]::after,
  article a[href^="#"]::after {
    content: none;
  }
  
  /* Style the main article heading */
  .story-heading {
    font-size: 18pt;
    margin: 0 0 0.5em;
    display: block;
  }
  
  /* Badge styling for print */
  .badge {
    border: 1px solid #999;
    background: #f0f0f0;
    padding: 0.1em 0.4em;
    font-size: 8pt;
    color: #333;
  }
  
  /* Meta line styling */
  .meta-line {
    font-size: 10pt;
    color: #333;
    margin-bottom: 1em;
  }
  
  /* Comments section */
  details {
    margin: 1em 0;
    max-width: none;
    page-break-inside: avoid;
  }
  
  details[open] {
    page-break-inside: auto;
  }
  
  summary {
    font-size: 10pt;
    padding: 0.5em 0;
    min-height: auto;
  }
  
  .comment-meta {
    font-size: 9pt;
  }
  
  .comment-permalink {
    background: none;
    padding: 0;
    min-height: auto;
    min-width: auto;
  }
  
  /* Nested comment indentation for print */
  article ul {
    padding-left: 1.5em;
    border-left: 1px solid #ccc;
    margin-left: 0.5em;
  }
  
  /* HR styling */
  hr {
    border: none;
    border-top: 1px solid #000;
    margin: 1em 0;
  }
  
  /* Code blocks */
  pre, code {
    background: #f5f5f5 !important;
    border: 1px solid #ddd;
    font-size: 9pt;
  }
  
  pre {
    padding: 0.5em;
    white-space: pre-wrap;
    word-wrap: break-word;
    page-break-inside: avoid;
  }
  
  /* User profile page */
  .user-profile h1 {
    font-size: 16pt;
  }
  
  .user-stats {
    background: #f5f5f5;
    padding: 0.5em;
    page-break-inside: avoid;
  }
  
  /* Story list (feed pages) */
  ol.stories {
    list-style: decimal;
    padding-left: 2em;
  }
  
  ol.stories > li {
    display: list-item;
    margin-bottom: 0.75em;
    page-break-inside: avoid;
  }
  
  ol.stories > li::before {
    display: none;
  }
  
  ol.stories > li::after {
    display: none;
  }
  
  .title {
    display: inline;
  }
  
  .comments {
    display: inline;
    font-size: 10pt;
    color: #666;
  }
  
  .comments::before {
    content: " — ";
  }
  
  /* External link indicator - hide in print */
  .external-link::after {
    display: none !important;
  }
  
  /* OP badge */
  .op-badge {
    border: 1px solid #666;
    background: #eee;
    color: #333;
  }
  
  /* Avoid page breaks in awkward places */
  h1, h2, h3 {
    page-break-after: avoid;
  }
  
  article {
    page-break-before: avoid;
  }
  
  /* Print footer with source URL */
  body::after {
    content: "Printed from NFHN (hn.jakechampion.name)";
    display: block;
    text-align: center;
    font-size: 9pt;
    color: #666;
    margin-top: 2em;
    padding-top: 1em;
    border-top: 1px solid #ccc;
  }
}

/* =============================================================================
   View Transitions API - Smooth page navigation
   ============================================================================= */

/* Opt-in to cross-document view transitions */
@view-transition {
  navigation: auto;
}

/* Main content transition - slides in/out */
main {
  view-transition-name: main-content;
}

/* Header bar persists across pages */
.header-bar {
  view-transition-name: header;
}

/* Navigation stays fixed during transition */
.nav-feeds {
  view-transition-name: nav;
}

/* Customize the default crossfade animations */
::view-transition-old(main-content) {
  animation: 150ms ease-out both fade-out,
             150ms ease-out both slide-to-left;
}

::view-transition-new(main-content) {
  animation: 150ms ease-in 50ms both fade-in,
             150ms ease-in 50ms both slide-from-right;
}

/* Header and nav should crossfade quickly */
::view-transition-old(header),
::view-transition-old(nav) {
  animation: 100ms ease-out both fade-out;
}

::view-transition-new(header),
::view-transition-new(nav) {
  animation: 100ms ease-in both fade-in;
}

/* Animation keyframes */
@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes fade-out {
  from { opacity: 1; }
  to { opacity: 0; }
}

@keyframes slide-from-right {
  from { transform: translateX(30px); }
  to { transform: translateX(0); }
}

@keyframes slide-to-left {
  from { transform: translateX(0); }
  to { transform: translateX(-30px); }
}

/* Reverse animation for back navigation */
:root:active-view-transition-type(backward) {
  &::view-transition-old(main-content) {
    animation: 150ms ease-out both fade-out,
               150ms ease-out both slide-to-right;
  }

  &::view-transition-new(main-content) {
    animation: 150ms ease-in 50ms both fade-in,
               150ms ease-in 50ms both slide-from-left;
  }
}

/* Feed -> story and back. The story title carries a matching
   view-transition-name on both pages (emitted per-story by
   components.ts), so it morphs from the list row into the article
   heading. The page itself only crossfades, so the travelling title is
   the thing the eye follows rather than competing with a slide. */
:root:active-view-transition-type(drill-in),
:root:active-view-transition-type(drill-out) {
  &::view-transition-old(main-content) {
    animation: 120ms ease-out both fade-out;
  }

  &::view-transition-new(main-content) {
    animation: 180ms ease-in 60ms both fade-in;
  }
}

/* The shared title element. Each story title is given
   `view-transition-class: story-title` alongside its unique
   view-transition-name, so one rule can slow the morph slightly without
   naming every story id. */
::view-transition-group(.story-title) {
  animation-duration: 250ms;
  animation-timing-function: ease-in-out;
}

@keyframes slide-from-left {
  from { transform: translateX(-30px); }
  to { transform: translateX(0); }
}

@keyframes slide-to-right {
  from { transform: translateX(0); }
  to { transform: translateX(30px); }
}

/* Reduce motion preference - disable animations */
@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*),
  ::view-transition-old(*),
  ::view-transition-new(*) {
    animation: none !important;
  }
}

/* Comment thread controls (Invoker Commands) */
.comment-controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1rem 0 0.5rem;
  flex-wrap: wrap;
}

.comment-controls button {
  font: inherit;
  font-size: 0.85rem;
  padding: 0.25rem 0.6rem;
  border: 1px solid var(--border, currentColor);
  border-radius: 0.35rem;
  background: transparent;
  color: inherit;
  cursor: pointer;
}

.comment-controls button:hover {
  background: color-mix(in oklch, currentColor 8%, transparent);
}

.comment-summary {
  margin: 0 0 1rem;
  padding: 0.75rem 1rem;
  border-inline-start: 3px solid var(--accent, currentColor);
  background: color-mix(in oklch, currentColor 5%, transparent);
  border-radius: 0 0.35rem 0.35rem 0;
  white-space: pre-wrap;
}

.comment-summary-note {
  margin: 0.5rem 0 0;
  font-size: 0.75rem;
  opacity: 0.65;
}

.comment-controls .comment-count {
  font-size: 0.85rem;
  opacity: 0.7;
}

/* =============================================================================
   @scope - keep comment styling from leaking down the tree
   (Chromium 118+, Safari 17.4+; Firefox in progress)
   ============================================================================= */

/* Comment threads nest arbitrarily deep, so every descendant selector aimed at
   a comment also matches every comment inside it. @scope sets a lower boundary:
   these rules stop at the next nested comment rather than cascading all the way
   down. Wrapped in @supports so browsers without it keep today's behaviour. */
@supports at-rule(@scope) {
  @scope ([aria-label="Comments"] details) to (details) {
    :scope > summary .comment-meta {
      opacity: 0.85;
    }
  }
}

/* =============================================================================
   Styling the no-JavaScript state
   ============================================================================= */

/* Nearly everything here is progressive enhancement, but until now there was no
   way to style the fallback state - controls that only do something once app.js
   runs were shown regardless. `scripting: none` covers both "JS disabled" and
   "JS not yet supported", which is exactly the audience that should not see a
   button that cannot work. */
@media (scripting: none) {
  .comment-controls,
  .back-to-top,
  .share-button,
  .bookmark-button {
    display: none;
  }
}

/* =============================================================================
   ::target-text - highlight the sentence a link points at
   ============================================================================= */

/* Comment permalinks scroll to a comment; a text fragment (#:~:text=...) can
   point at the exact sentence being quoted. This styles that match with the
   site's own accent instead of the browser default yellow, so it reads as part
   of the page in both themes. */
::target-text {
  background-color: color-mix(in oklch, var(--accent, Highlight) 30%, transparent);
  color: inherit;
}

/* =============================================================================
   content-visibility - skip rendering off-screen comment subtrees
   (Baseline: Chromium 85+, Firefox 125+, Safari 18+)
   See docs/api-proposals/12-content-visibility.md
   ============================================================================= */

/* A busy HN thread is 1,000+ comments and several thousand DOM nodes, all of
   which are laid out, styled and painted before the page is interactive - to
   show about eight of them. content-visibility: auto lets the browser skip that
   work for subtrees that are off-screen and do it lazily on scroll.

   contain-intrinsic-size uses the `auto <length>` form deliberately: comment
   heights vary enormously (a one-line "this" next to a forty-line code block),
   so a fixed estimate would make the scrollbar lurch every time it was wrong.
   `auto` remembers each element's last-rendered size, so any given subtree is
   mis-estimated at most once.

   Find-in-page and fragment navigation both still work: the browser forces
   rendering of a skipped subtree to search it, and scrolls to the match. */
[aria-label="Comments"] > details,
[aria-label="Comments"] ul > li {
  content-visibility: auto;
  contain-intrinsic-size: auto 8rem;
}

/* NOT applied to `.stories > li`, however tempting the free win looks.
   `content-visibility: auto` implies *style* containment, and style containment
   scopes `counter-increment` and `counter-set` to the contained element's own
   subtree. `ol > li:before` increments `section` to number the story rows, so
   containing each row makes every one of them start a fresh counter and render
   as "1". That is spec'd behaviour, not a browser bug, so it breaks everywhere.
   Comment subtrees above are safe because they are `ul > li` and carry no
   counters. 30 rows was never worth the risk. */

/* =============================================================================
   Animated comment collapse - ::details-content + interpolate-size
   (Chromium 129+/131+, Safari 18.4+; Firefox in progress)
   See docs/api-proposals/13-details-content-animation.md
   ============================================================================= */

/* Animating height from 0 to auto has been the canonical impossible CSS problem.
   interpolate-size: allow-keywords makes transitions to intrinsic keywords legal,
   ::details-content exposes the previously-unstylable wrapper inside <details>,
   and allow-discrete keeps the content present while it closes.

   Doing this in CSS rather than JS matters here specifically: the CSP is
   script-src 'self' plus a single pinned hash, so every interaction that stays
   in CSS is one that never needs a handler or a hash update. */
@supports (interpolate-size: allow-keywords) {
  :root {
    interpolate-size: allow-keywords;
  }

  @media (prefers-reduced-motion: no-preference) {
    [aria-label="Comments"] details::details-content {
      block-size: 0;
      overflow: clip;
      transition:
        block-size 250ms ease,
        content-visibility 250ms allow-discrete;
    }

    [aria-label="Comments"] details[open]::details-content {
      block-size: auto;
    }
  }
}

/* =============================================================================
   text-wrap: balance & pretty - Better text wrapping (Baseline 2024)
   ============================================================================= */

/* Balance short headings for better visual appearance */
h1, h2, .story-title-text {
  text-wrap: balance;
}

/* Pretty wrap for paragraphs - avoids orphans/widows */
p, .comment-content, .story-meta {
  text-wrap: pretty;
}

/* =============================================================================
   text-box-trim - optical vertical rhythm
   (Safari 18.2+, Chromium 133+; Firefox in progress)
   See docs/api-proposals/17-typography-and-i18n.md
   ============================================================================= */

/* Text carries invisible leading above the cap height and below the baseline,
   which is why a heading in a padded box never looks vertically centred and why
   the gap under a <summary> never quite matches the designed value. Trimming to
   cap height and baseline means declared spacing is the spacing you see.

   This matters more here than on most sites: the comment list is dense and
   deeply nested, so every level currently accumulates a little optical drift. */
@supports (text-box: trim-both cap alphabetic) {
  h1,
  h2,
  .story-title-text,
  .story-heading,
  .comment-meta {
    text-box: trim-both cap alphabetic;
  }
}

/* =============================================================================
   Popover API styles (Baseline 2024)
   ============================================================================= */

/* Popover backdrop */
[popover]::backdrop {
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(2px);
}

/* Popover open state animations */
[popover] {
  border: none;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  background: var(--background-elevated);
  color: var(--text-primary);
  padding: 1.5em;
  max-width: min(90vw, 400px);
  
  /* Entry animation */
  opacity: 0;
  transform: scale(0.95) translateY(-10px);
  transition: 
    opacity 150ms ease-out,
    transform 150ms ease-out,
    overlay 150ms ease-out allow-discrete,
    display 150ms ease-out allow-discrete;
}

[popover]:popover-open {
  opacity: 1;
  transform: scale(1) translateY(0);
}

/* Starting style for entry animation */
@starting-style {
  [popover]:popover-open {
    opacity: 0;
    transform: scale(0.95) translateY(-10px);
  }
}

/* --- Live thread updates ---
   Shown by app.js when the SSE stream reports the thread has moved. Sits
   between the story and the comments, where the reader's eye is already
   heading. */

.live-updates {
  margin: 1em 0;
  padding: 0;
}

.live-updates-link {
  display: inline-block;
  padding: 0.4em 0.85em;
  border: 1px solid var(--accent-muted);
  border-radius: 999px;
  background: var(--hover-overlay);
  color: var(--text-strong);
  font-size: 0.9rem;
  text-decoration: none;
}

.live-updates-link:hover,
.live-updates-link:focus-visible {
  background: var(--active-overlay);
}

/* It appears while the reader is mid-page, so it arrives rather than blinking
   into existence - but only for readers who have not asked otherwise. */
@media (prefers-reduced-motion: no-preference) {
  .live-updates-link {
    animation: live-updates-in 200ms ease-out;
  }

  @keyframes live-updates-in {
    from {
      opacity: 0;
      transform: translateY(-0.25em);
    }
  }
}

/* --- Story preview hint (interest invokers) ---
   Opened by hovering, long-pressing or focusing a story title. The browser
   owns when it opens and closes; this owns where it sits and what it looks
   like. See the module in app.js. */

#story-preview {
  /* Interest invokers make the link the popover's implicit anchor, which is
     what `position-area` resolves against here - no anchor-name needed, and
     no per-row names for thirty rows. */
  position: fixed;
  position-area: block-end span-inline-end;
  /* A row near the bottom of the viewport flips the card above the link
     rather than letting it hang off the screen. */
  position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
  margin-block-start: 0.35rem;

  max-width: min(90vw, 36rem);
  padding: 0.85em 1em;
  font-size: 0.95rem;
  /* Lighter than the modal shadow: this is a hint, not a layer above the page. */
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.14);
  border: 1px solid var(--border-color);
}

/* A hint takes no backdrop - it does not block the page behind it. */
#story-preview::backdrop {
  background: none;
  backdrop-filter: none;
}

#story-preview p {
  margin: 0;
}

.story-preview-title {
  font-weight: 600;
  color: var(--text-strong);
}

.story-preview-excerpt {
  margin-block-start: 0.5em !important;
  color: var(--text-primary);
  /* Long excerpts are clipped server-side; this is the belt to that braces,
     and keeps a card from growing taller than the row it describes. */
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 4;
  overflow: hidden;
}

.story-preview-meta {
  margin-block-start: 0.5em !important;
  font-size: 0.85em;
  color: var(--text-muted);
}

/* Without popover support the element is left `hidden` by app.js and never
   reaches this, but a browser that supports popover and not interest invokers
   would keep a card nothing can open. Belt and braces. */
@supports not selector(:popover-open) {
  #story-preview {
    display: none;
  }
}

/* =============================================================================
   CSS Anchor Positioning (Baseline 2024)
   Position popovers, tooltips, and menus relative to their trigger elements
   ============================================================================= */

/* --- Settings Menu Anchor Positioning ---
   Anchor the settings popover to the settings button */

@supports (anchor-name: --settings-btn) {
  /* Define the anchor on the settings button */
  .settings-menu-btn {
    anchor-name: --settings-btn;
  }

  /* Position the settings menu relative to the button */
  .settings-menu {
    position: fixed;
    position-anchor: --settings-btn;
    
    /* Default: position below and aligned to start */
    inset: auto;
    top: anchor(bottom);
    left: anchor(left);
    margin-top: 0.5rem;
    
    /* Override default centering */
    translate: 0;
    
    /* Fallback positions when there's not enough space */
    position-try-fallbacks: 
      --settings-below-right,
      --settings-above-left,
      --settings-above-right;
  }

  /* Fallback: below, aligned to right edge */
  @position-try --settings-below-right {
    top: anchor(bottom);
    left: auto;
    right: anchor(right);
    bottom: auto;
  }

  /* Fallback: above, aligned to left edge */
  @position-try --settings-above-left {
    top: auto;
    bottom: anchor(top);
    left: anchor(left);
    margin-top: 0;
    margin-bottom: 0.5rem;
  }

  /* Fallback: above, aligned to right edge */
  @position-try --settings-above-right {
    top: auto;
    bottom: anchor(top);
    left: auto;
    right: anchor(right);
    margin-top: 0;
    margin-bottom: 0.5rem;
  }
}

/* --- Share Button Tooltip ---
   Show "Share" tooltip on hover for share buttons */

@supports (anchor-name: --share-btn) {
  .share-btn {
    anchor-name: --share-btn;
    position: relative;
  }

  /* Tooltip using ::after pseudo-element */
  .can-share .share-btn::after {
    content: attr(title);
    position: fixed;
    position-anchor: --share-btn;
    
    /* Position below the button */
    top: anchor(bottom);
    left: anchor(center);
    translate: -50% 0.25rem;
    
    /* Use solid dark background for good contrast in both light and dark modes */
    background: oklch(0.25 0 0);
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    white-space: nowrap;
    pointer-events: none;
    
    /* Hidden by default */
    opacity: 0;
    transition: opacity 0.15s ease;
    
    /* Fallback above if not enough space */
    position-try-fallbacks: --tooltip-above;
  }

  @position-try --tooltip-above {
    top: auto;
    bottom: anchor(top);
    translate: -50% -0.25rem;
  }

  .can-share .share-btn:hover::after,
  .can-share .share-btn:focus::after {
    opacity: 1;
  }
}

/* --- Bookmark Button Tooltip ---
   Show "Save"/"Saved" tooltip on hover */

@supports (anchor-name: --bookmark-btn) {
  .bookmark-btn {
    anchor-name: --bookmark-btn;
    position: relative;
  }

  .bookmark-btn::after {
    content: "Save";
    position: fixed;
    position-anchor: --bookmark-btn;
    
    top: anchor(bottom);
    left: anchor(center);
    translate: -50% 0.25rem;
    
    /* Use solid dark background for good contrast in both light and dark modes */
    background: oklch(0.25 0 0);
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    white-space: nowrap;
    pointer-events: none;
    
    opacity: 0;
    transition: opacity 0.15s ease;
    
    position-try-fallbacks: --bookmark-tooltip-above;
  }

  @position-try --bookmark-tooltip-above {
    top: auto;
    bottom: anchor(top);
    translate: -50% -0.25rem;
  }

  .bookmark-btn[aria-pressed="true"]::after {
    content: "Saved";
  }

  .bookmark-btn:hover::after,
  .bookmark-btn:focus::after {
    opacity: 1;
  }
}

/* --- Back to Top Tooltip ---
   Show tooltip for back-to-top button */

@supports (anchor-name: --back-to-top) {
  .back-to-top {
    anchor-name: --back-to-top;
  }

  .back-to-top::before {
    content: "Back to top";
    position: fixed;
    position-anchor: --back-to-top;
    
    top: anchor(center);
    right: anchor(left);
    translate: -0.5rem -50%;
    
    /* Use solid dark background for good contrast in both light and dark modes */
    background: oklch(0.25 0 0);
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    white-space: nowrap;
    pointer-events: none;
    
    opacity: 0;
    transition: opacity 0.15s ease;
  }

  .back-to-top:hover::before {
    opacity: 1;
  }
}

/* =============================================================================
   Share button styles
   ============================================================================= */

.share-btn {
  display: none; /* Hidden by default, shown via JS if Web Share API available */
  appearance: none;
  align-items: center;
  justify-content: center;
  width: 2.2em;
  height: 2.2em;
  padding: 0;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background: var(--badge-bg);
  color: var(--text-muted);
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s, transform 0.1s;
  flex-shrink: 0;
}

.share-btn:hover {
  background: var(--background-elevated);
  color: var(--text-primary);
  transform: scale(1.05);
}

.share-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.share-btn svg {
  display: block;
  fill: currentColor;
}

/* Show share button when Web Share API is available */
.can-share .share-btn {
  display: inline-flex;
}

/* =============================================================================
   CSS :has() Selectors (Baseline 2023)
   Replace JavaScript state management with pure CSS where possible
   ============================================================================= */

/* --- Bookmark/Save button using aria-pressed --- */
/* When bookmark button has aria-pressed="true", show saved state */
.bookmark-btn[aria-pressed="true"] {
  background: oklch(from var(--accent) l c h / 0.15);
  border-color: oklch(from var(--accent) l c h / 0.5);
  color: var(--accent);
}

.bookmark-btn[aria-pressed="true"] .bookmark-icon-filled {
  display: block;
}

.bookmark-btn[aria-pressed="true"] .bookmark-icon-outline {
  display: none;
}

.bookmark-btn[aria-pressed="true"]:hover {
  background: oklch(from var(--accent) l c h / 0.25);
}

/* --- Story item styling based on saved state --- */
/* Style story items that contain a saved bookmark */
li:has(.bookmark-btn[aria-pressed="true"]) {
  background: linear-gradient(
    to right,
    oklch(from var(--accent) l c h / 0.08) 0%,
    transparent 100%
  );
}

/* --- Keyboard navigation mode detection --- */
/* When any element has focus-visible, show keyboard hints */
body:has(:focus-visible) .kbd-hint {
  opacity: 1;
}

/* --- Popover/Modal open states --- */
/* Dim main content when any popover is open */
/* Note: pointer-events not disabled because popover may be inside main */
/* The story preview is deliberately exempt: it is a hint, opened by hovering a
   row in the very list this rule would dim, and dimming the page behind a
   hover card makes the page flicker as the pointer moves down it. */
body:has([popover]:popover-open:not(#story-preview)) main {
  opacity: 0.6;
  transition: opacity 0.15s ease;
}

/* Prevent body scroll when modal is open */
body:has(#shortcuts-modal:popover-open),
body:has(#settings-menu:popover-open) {
  overflow: hidden;
}

/* --- Form validation states --- */
/* Style form when it has invalid inputs */
form:has(:invalid) .submit-btn {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Style label when its input is invalid */
label:has(+ input:invalid),
label:has(+ select:invalid) {
  color: var(--error, #dc2626);
}

/* --- Comment thread states --- */
/* Style parent comment when any child is focused */
.comment:has(.comment:focus-within) {
  border-left-color: var(--brand);
}

/* Comments with replies get stronger border */
.comment:has(.comment) {
  border-left-width: 3px;
}

/* Collapsed comments (details not open) */
details.comment:not([open]) {
  opacity: 0.7;
}

/* Highlight entire comment thread on hover */
.comment:has(:hover) {
  background: var(--comment-bg);
}

/* --- Story list states --- */
/* Empty state for saved stories */
#saved-stories-container:not(:has(.stories li)) .empty-saved {
  display: block;
}

#saved-stories-container:has(.stories li) .empty-saved {
  display: none;
}

/* --- Feed navigation current state --- */
/* Style the nav link container when it has current page */
.nav-links:has([aria-current="page"]) a:not([aria-current="page"]) {
  opacity: 0.8;
}

/* --- External links indicator --- */
/* Style links that go external (have target="_blank") */
a[target="_blank"]:not(.no-external-icon)::after {
  content: "";
  display: inline-block;
  width: 0.7em;
  height: 0.7em;
  margin-left: 0.2em;
  background: currentColor;
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M19 19H5V5h7V3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14c1.1 0 2-.9 2-2v-7h-2v7zM14 3v2h3.59l-9.83 9.83 1.41 1.41L19 6.41V10h2V3h-7z'/%3E%3C/svg%3E");
  mask-size: contain;
  vertical-align: middle;
  opacity: 0.6;
}

/* Don't show external icon on reader mode links */
.reader-mode-link::after {
  display: none;
}

/* --- Details/Summary states --- */
/* Style summary when details is open */
details[open] > summary {
  margin-bottom: 0.5em;
  font-weight: 600;
}

/* --- Story with different content types --- */
/* Page with loading indicator */
main:has(.loading) {
  min-height: 50vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* --- Quantity-based styling --- */
/* Stories list with many items - compact layout */
.stories:has(li:nth-child(20)) li {
  padding-block: 0.6em;
}

/* --- Sibling-based styling --- */
/* Last item in list (no following sibling) */
.stories li:not(:has(+ li)) {
  border-bottom: none;
}

/* First story after pagination */
.pagination + .stories li:first-child {
  border-top: 2px solid var(--border-color);
}

/* --- Reduced motion preference with :has() --- */
@media (prefers-reduced-motion: reduce) {
  /* Disable transitions when user prefers reduced motion */
  body:has(:focus-visible) *,
  body:has([popover]:popover-open) * {
    transition-duration: 0.01ms !important;
  }

  /* Same reason as the dimming rule: a hint that fades in as the pointer
     crosses each row is motion nobody asked for. */
  #story-preview {
    transition: none;
    transform: none;
  }
}

/* =============================================================================
   Comment Depth Coloring using Relative Color Syntax (Baseline 2024)
   Each nesting level shifts hue for visual distinction
   ============================================================================= */

/* Base comment border - uses accent as starting point */
.comment,
details.comment {
  --comment-border: oklch(from var(--accent) 0.65 0.15 h);
  border-left: 2px solid var(--comment-border);
}

/* Nested comments shift hue by 40 degrees per level */
.comment .comment,
details.comment details.comment {
  --comment-border: oklch(from var(--accent) 0.65 0.15 calc(h + 40));
}

.comment .comment .comment,
details.comment details.comment details.comment {
  --comment-border: oklch(from var(--accent) 0.65 0.15 calc(h + 80));
}

.comment .comment .comment .comment,
details.comment details.comment details.comment details.comment {
  --comment-border: oklch(from var(--accent) 0.65 0.15 calc(h + 120));
}

.comment .comment .comment .comment .comment,
details.comment details.comment details.comment details.comment details.comment {
  --comment-border: oklch(from var(--accent) 0.65 0.15 calc(h + 160));
}

/* 6+ levels cycle back but with reduced chroma */
.comment .comment .comment .comment .comment .comment,
details.comment details.comment details.comment details.comment details.comment details.comment {
  --comment-border: oklch(from var(--accent) 0.65 0.1 calc(h + 200));
}

/* Dark mode: lighter border colors for better visibility */
:root[data-theme="dark"] .comment,
:root[data-theme="dark"] details.comment {
  --comment-border: oklch(from var(--accent) 0.7 0.12 h);
}

:root[data-theme="dark"] .comment .comment,
:root[data-theme="dark"] details.comment details.comment {
  --comment-border: oklch(from var(--accent) 0.7 0.12 calc(h + 40));
}

:root[data-theme="dark"] .comment .comment .comment,
:root[data-theme="dark"] details.comment details.comment details.comment {
  --comment-border: oklch(from var(--accent) 0.7 0.12 calc(h + 80));
}

:root[data-theme="dark"] .comment .comment .comment .comment,
:root[data-theme="dark"] details.comment details.comment details.comment details.comment {
  --comment-border: oklch(from var(--accent) 0.7 0.12 calc(h + 120));
}

/* =============================================================================
   Container Queries (Baseline 2023)
   Components adapt to their container size, not viewport
   ============================================================================= */

/* --- Define Container Contexts --- */

/* Main content area as a container */
main {
  container-type: inline-size;
  container-name: main-content;
}

/* Note: ol.stories cannot have container-type as it breaks CSS counters.
   Use main-content container for story list responsive styling instead.
   The main element already has container-type: inline-size. */

/* Comment section as a container */
article {
  container-type: inline-size;
  container-name: article-content;
}

/* Individual comments as containers */
details {
  container-type: inline-size;
  container-name: comment;
}

/* --- Story List Container Queries --- */
/* Uses main-content container since ol cannot have container-type (breaks counters) */

/* Narrow container: compact layout */
@container main-content (max-width: 400px) {
  .stories li {
    padding-left: 1.5em;
  }

  .stories li::before {
    font-size: 1.1em;
  }

  .story-meta {
    font-size: 0.75em;
  }

  .badge {
    font-size: 0.55em;
    padding: 0.1em 0.3em;
  }

  /* Hide domain on very narrow containers */
  .story-meta .link-sep,
  .story-meta .link-sep + * {
    display: none;
  }
}

/* Standard container: balanced layout */
@container main-content (min-width: 400px) and (max-width: 600px) {
  .stories li {
    padding-left: 2.5em;
  }

  .story-meta {
    font-size: 0.8em;
  }
}

/* Wide container: full layout with more details */
@container main-content (min-width: 600px) {
  .stories li {
    padding-left: 3em;
  }

  .stories li::before {
    font-size: 1.6em;
  }

  .story-meta {
    font-size: 0.85em;
  }

  /* More prominent actions on wide containers */
  .story-actions {
    gap: 1em;
  }
}

/* Very wide container: grid layout for story actions */
@container main-content (min-width: 800px) {
  .story-actions {
    gap: 1.25em;
  }

  .bookmark-btn {
    width: 2.5em;
    height: 2.5em;
  }
}

/* --- Comment Container Queries --- */

/* Narrow comments: reduce indentation */
@container article-content (max-width: 400px) {
  details {
    margin: 20px auto;
    font-size: 15px;
  }

  summary {
    min-height: 48px;
    padding: 0.5em;
  }

  .comment-meta {
    font-size: 0.8em;
    gap: 0.3em;
  }

  /* Tighter nested comment indentation */
  article ul {
    padding-left: 0.5em;
  }
}

/* Standard comments */
@container article-content (min-width: 400px) and (max-width: 650px) {
  details {
    font-size: 16px;
  }

  article ul {
    padding-left: 0.75em;
  }
}

/* Wide comments: more breathing room */
@container article-content (min-width: 650px) {
  details {
    font-size: 18px;
  }

  summary {
    min-height: 56px;
    padding: 0.75em;
  }

  .comment-meta {
    gap: 0.5em;
  }

  article ul {
    padding-left: 1em;
  }
}

/* --- Individual Comment Depth Queries --- */
/* As comments get nested deeper (narrower containers), adapt */

@container comment (max-width: 300px) {
  /* Very narrow nested comment */
  summary {
    font-size: 0.9em;
    min-height: 44px;
  }

  .comment-permalink {
    padding: 0.3em 0.5em;
    min-height: 40px;
    min-width: 40px;
    font-size: 0.85em;
  }
}

@container comment (max-width: 200px) {
  /* Extremely narrow - collapse actions */
  .comment-permalink {
    padding: 0.25em;
    min-width: auto;
  }

  /* Truncate long usernames */
  .comment-user {
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

/* --- Main Content Container Queries --- */
/* For when main is resized (e.g., in a split view or PiP) */

@container main-content (max-width: 500px) {
  /* Compact header bar */
  .header-bar {
    gap: 0.5em;
  }

  .nav-feeds {
    gap: 0.3em;
    font-size: 0.85em;
  }

  .nav-feeds a {
    padding: 0.3em 0.5em;
  }

  /* Compact pagination */
  .more-link {
    padding: 0.75em;
    font-size: 0.9em;
  }
}

@container main-content (min-width: 700px) {
  /* More spacious layout for wide containers */
  .header-bar {
    gap: 1em;
  }

  .nav-feeds {
    gap: 1em;
  }

  .more-link {
    padding: 1.25em;
    font-size: 1em;
  }
}

/* --- Container Query Units --- */
/* Use container query units for fluid typography */

@supports (font-size: 1cqi) {
  /* Fluid story title based on container width */
  .story-title-text {
    font-size: clamp(0.9rem, 2.5cqi + 0.5rem, 1.2rem);
  }

  /* Fluid meta text */
  .story-meta {
    font-size: clamp(0.7rem, 1.5cqi + 0.4rem, 0.9rem);
  }
}

/* --- Saved Stories Container --- */
/* The saved stories container rendered by JS */

#saved-stories-container {
  container-type: inline-size;
  container-name: saved-stories;
}

@container saved-stories (max-width: 400px) {
  .saved-count {
    font-size: 0.8em;
    padding-left: 0.25em;
  }

  .empty-saved {
    padding: 2em 0.5em;
  }

  .empty-saved p {
    font-size: 0.9em;
  }
}

/* --- User Profile Container Queries --- */

.user-profile {
  container-type: inline-size;
  container-name: user-profile;
}

@container user-profile (max-width: 400px) {
  .user-stats {
    grid-template-columns: 1fr;
    gap: 0.5em;
  }

  .user-stats dt {
    font-size: 0.8em;
  }

  .user-about {
    margin: 1em 0;
  }
}

@container user-profile (min-width: 500px) {
  .user-stats {
    grid-template-columns: auto 1fr auto 1fr;
    gap: 0.5em 1.5em;
  }
}

/* =============================================================================
   Scroll-Driven Animations (Baseline 2024)
   ============================================================================= */

/* --- Reading Progress Indicator ---
   A thin progress bar at the top showing scroll progress through the page */

.reading-progress {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background: transparent;
  z-index: 1001; /* Above header */
  pointer-events: none;
}

.reading-progress::after {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--accent);
  transform-origin: left;
  transform: scaleX(0);

  /* Link to scroll progress on the document */
  animation: progress-grow linear;
  animation-timeline: scroll(root);
}

@keyframes progress-grow {
  from { transform: scaleX(0); }
  to { transform: scaleX(1); }
}

/* --- Story Item Reveal Animation ---
   Stories fade in and slide up as they scroll into view */

@supports (animation-timeline: view()) {
  .stories > li {
    animation: story-reveal linear both;
    animation-timeline: view();
    animation-range: entry 0% entry 25%;
  }

  @keyframes story-reveal {
    from {
      opacity: 0.4;
      transform: translateY(10px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  /* --- Comment Reveal Animation ---
     Comments subtly fade in as they scroll into view */

  details.comment {
    animation: comment-reveal linear both;
    animation-timeline: view();
    animation-range: entry 0% entry 20%;
  }

  @keyframes comment-reveal {
    from {
      opacity: 0.5;
      transform: translateX(-5px);
    }
    to {
      opacity: 1;
      transform: translateX(0);
    }
  }

  /* Nested comments have slightly delayed animations */
  details.comment details.comment {
    animation-range: entry 5% entry 25%;
  }

  details.comment details.comment details.comment {
    animation-range: entry 10% entry 30%;
  }
}

/* --- Back to Top Button ---
   Appears after scrolling down, controlled by scroll position */

.back-to-top {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  /* Use darker accent for good contrast with white icon */
  background: var(--accent-dark);
  color: white;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  text-decoration: none;
  z-index: 100;
  
  /* Start hidden, animate based on scroll */
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px);
  
  animation: show-back-to-top linear both;
  animation-timeline: scroll(root);
  animation-range: 300px 500px;
  
  transition: background-color 0.15s ease, transform 0.15s ease;
}

.back-to-top:hover {
  background: var(--accent);
  transform: translateY(-2px);
}

.back-to-top:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.back-to-top svg {
  width: 24px;
  height: 24px;
}

@keyframes show-back-to-top {
  from {
    opacity: 0;
    pointer-events: none;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
  }
}

/* --- Reduced Motion ---
   Disable scroll-driven animations for users who prefer reduced motion */

@media (prefers-reduced-motion: reduce) {
  .reading-progress::after,
  .stories > li,
  details.comment,
  .back-to-top {
    animation: none !important;
  }
  
  /* Ensure back-to-top is always visible when reduced motion is preferred */
  .back-to-top {
    opacity: 1;
    pointer-events: auto;
    transform: none;
  }
  
  .stories > li {
    opacity: 1;
    transform: none;
  }
  
  details.comment {
    opacity: 1;
    transform: none;
  }
}

/* =============================================================================
   Forced colours mode (Windows High Contrast and friends)

   In forced-colours mode the browser replaces the palette with the user's own,
   so anything whose boundary came only from a background colour or a shadow
   flattens into the canvas. Semantic elements - real <button>, <a>, <details> -
   keep their affordances automatically; these rules repair the custom surfaces
   that do not, using system colour keywords so the result tracks whichever
   palette the user actually chose.
   ============================================================================= */

@media (forced-colors: active) {
  /* Scroll progress is a bare coloured bar: without a forced system colour it
     is painted in the forced background and disappears entirely. */
  .reading-progress::after {
    background: Highlight;
  }

  /* Badges, toasts and popovers are defined by background fill alone. */
  .badge,
  .toast,
  .settings-menu,
  .shortcuts-modal {
    border: 1px solid CanvasText;
  }

  /* Icon buttons rely on hover/elevation backgrounds for their bounds. */
  .settings-menu-btn,
  .share-btn,
  .bookmark-btn,
  .pip-reader-btn,
  .saved-action-btn,
  .back-to-top {
    border: 1px solid ButtonText;
  }

  .reader-mode-link {
    border: 1px solid LinkText;
  }

  /* Keyboard selection must stay legible against the forced palette. */
  .kbd-focus {
    outline: 2px solid Highlight;
    outline-offset: 2px;
  }
}

/* Users who asked for more contrast without a forced palette are a different
   audience: keep our own colours, but firm up the boundaries that are
   deliberately faint in the default theme. */
@media (prefers-contrast: more) {
  .badge,
  .settings-menu,
  .shortcuts-modal,
  .toast {
    border: 1px solid var(--text-primary);
  }
}
