/* ==========================================================================
   Hacker News Reader — refined reading-focused design system
   ========================================================================== */

:root {
  --font-sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas,
    "Liberation Mono", monospace;

  --accent: #e8550d;
  --accent-strong: #c94a0b;
  --accent-soft: rgba(232, 85, 13, 0.1);
  --bg: #f7f5f1;
  --surface: #ffffff;
  --surface-sunken: #f0ede7;
  --text: #211d18;
  --text-secondary: #6f6a60;
  --text-faint: #9a948a;
  --border: #e4dfd6;
  --border-strong: #d4cdc0;
  --link: #2d5fa8;
  --danger: #b3261e;
  --shadow-sm: 0 1px 2px rgba(30, 25, 18, 0.06);
  --shadow-md: 0 4px 16px rgba(30, 25, 18, 0.08);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --header-h: 56px;
  --measure: 44rem;
  color-scheme: light;
}

[data-theme="dark"] {
  --accent: #ff8a4c;
  --accent-strong: #ffa06b;
  --accent-soft: rgba(255, 138, 76, 0.14);
  --bg: #16130f;
  --surface: #201b16;
  --surface-sunken: #191510;
  --text: #ece6dd;
  --text-secondary: #a89f92;
  --text-faint: #7c7365;
  --border: #332c24;
  --border-strong: #453c31;
  --link: #8db4e8;
  --danger: #e57373;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html {
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  scroll-padding-top: calc(var(--header-h) + 12px);
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

@media (prefers-reduced-motion: no-preference) {
  body.theme-animating,
  body.theme-animating * {
    transition: background-color 0.35s ease, color 0.35s ease,
      border-color 0.35s ease, fill 0.35s ease, stroke 0.35s ease !important;
  }
}

a {
  color: var(--link);
  text-decoration: none;
}
a:hover { text-decoration: underline; }
a:focus-visible,
button:focus-visible,
input:focus-visible,
[tabindex="-1"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: 12px;
  top: -48px;
  z-index: 100;
  background: var(--accent);
  color: #fff;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  transition: top 0.15s ease;
}
.skip-link:focus { top: 8px; text-decoration: none; }

/* ---------- Header ---------- */

.site-header {
  position: sticky;
  top: 0;
  z-index: 50;
  height: var(--header-h);
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--border);
}

.header-inner {
  max-width: 60rem;
  margin: 0 auto;
  height: 100%;
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 20px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  color: var(--text);
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
}
.brand:hover { text-decoration: none; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 800;
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s ease;
}
.brand:hover .brand-mark { transform: rotate(-6deg) scale(1.06); }

.feed-nav {
  display: flex;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
  flex: 1;
  min-width: 0;
}
.feed-nav::-webkit-scrollbar { display: none; }
.feed-nav a {
  position: relative;
  color: var(--text-secondary);
  font-size: 0.9rem;
  font-weight: 500;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  white-space: nowrap;
  transition: color 0.12s ease, background-color 0.12s ease;
}
.feed-nav a:hover {
  color: var(--text);
  background: var(--surface-sunken);
  text-decoration: none;
}
.feed-nav a[aria-current="page"] {
  color: var(--accent);
  font-weight: 650;
}
.feed-nav a[aria-current="page"]::after {
  content: "";
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 1px;
  height: 2px;
  border-radius: 2px;
  background: var(--accent);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.search-form input {
  width: 150px;
  padding: 6px 10px 6px 30px;
  font: inherit;
  font-size: 0.875rem;
  color: var(--text);
  background: var(--surface-sunken)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%239a948a' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='M21 21l-4.3-4.3'/%3E%3C/svg%3E")
    no-repeat 8px center / 15px;
  border: 1px solid transparent;
  border-radius: 999px;
  transition: width 0.2s ease, border-color 0.15s ease, background-color 0.15s ease;
}
.search-form input::placeholder { color: var(--text-faint); }
.search-form input:focus {
  width: 210px;
  outline: none;
  border-color: var(--accent);
  background-color: var(--surface);
}
[data-theme="dark"] .search-form input {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%237c7365' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='M21 21l-4.3-4.3'/%3E%3C/svg%3E");
}

.icon-button {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 0.12s ease, border-color 0.12s ease, transform 0.2s ease;
}
.icon-button:hover {
  color: var(--accent);
  border-color: var(--accent);
}
@media (prefers-reduced-motion: no-preference) {
  .icon-button:active { transform: rotate(20deg) scale(0.92); }
}
.icon-button .icon-moon { display: none; }
[data-theme="dark"] .icon-button .icon-moon { display: block; }
[data-theme="dark"] .icon-button .icon-sun { display: none; }

/* ---------- Layout ---------- */

main {
  flex: 1;
  width: 100%;
  max-width: 60rem;
  margin: 0 auto;
  padding: 20px 16px 48px;
}

/* ---------- Story list ---------- */

.feed-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin: 4px 2px 14px;
}
.feed-header h1 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 750;
  letter-spacing: -0.02em;
}
.feed-header .page-indicator {
  font-size: 0.8rem;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.story-list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.story-row {
  display: flex;
  gap: 12px;
  padding: 12px 14px 12px 10px;
  border-bottom: 1px solid var(--border);
  transition: background-color 0.1s ease;
}
.story-row:last-child { border-bottom: none; }
.story-row:hover { background: color-mix(in srgb, var(--accent-soft) 40%, var(--surface)); }

@media (prefers-reduced-motion: no-preference) {
  .story-row.entering {
    animation: row-in 0.3s ease both;
  }
  @keyframes row-in {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: none; }
  }
}

.story-rank {
  flex: 0 0 2.1em;
  text-align: right;
  color: var(--text-faint);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  padding-top: 2px;
  user-select: none;
}

.story-main { flex: 1; min-width: 0; }

.story-title-line {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
}
.story-title {
  color: var(--text);
  font-size: 0.975rem;
  font-weight: 600;
  line-height: 1.35;
  letter-spacing: -0.005em;
  word-break: break-word;
}
.story-title:hover { color: var(--accent-strong); text-decoration: none; }
.story-title:visited { color: var(--text-faint); }

.domain-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.72rem;
  font-weight: 550;
  color: var(--text-secondary);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 8px;
  white-space: nowrap;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color 0.12s ease, border-color 0.12s ease;
}
.domain-chip:hover {
  color: var(--accent);
  border-color: var(--accent);
  text-decoration: none;
}

.story-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 10px;
  margin-top: 4px;
  font-size: 0.78rem;
  color: var(--text-secondary);
}
.story-meta a { color: var(--text-secondary); }
.story-meta a:hover { color: var(--accent); }

.score-badge {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  color: var(--accent);
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}
.score-badge svg { transition: transform 0.15s ease; }
.story-row:hover .score-badge svg { transform: translateY(-1px); }

.meta-dot { color: var(--text-faint); user-select: none; }

.comment-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-weight: 550;
}
.comment-link svg { opacity: 0.7; }

.job-tag {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 4px;
  padding: 1px 6px;
}

/* ---------- Skeletons ---------- */

.skeleton-row {
  display: flex;
  gap: 12px;
  padding: 14px;
  border-bottom: 1px solid var(--border);
}
.skeleton-row:last-child { border-bottom: none; }
.skel {
  border-radius: 5px;
  background: linear-gradient(
    100deg,
    var(--surface-sunken) 40%,
    color-mix(in srgb, var(--surface-sunken) 55%, var(--surface)) 50%,
    var(--surface-sunken) 60%
  );
  background-size: 200% 100%;
}
@media (prefers-reduced-motion: no-preference) {
  .skel { animation: shimmer 1.4s linear infinite; }
  @keyframes shimmer {
    from { background-position: 150% 0; }
    to { background-position: -50% 0; }
  }
}
.skel-rank { width: 2em; height: 0.9em; }
.skel-lines { flex: 1; display: grid; gap: 7px; }
.skel-title { height: 0.95em; width: 72%; }
.skel-meta { height: 0.75em; width: 44%; }

.skeleton-comment { padding: 14px 16px; display: grid; gap: 8px; }
.skel-cmeta { height: 0.75em; width: 32%; }
.skel-cbody { height: 0.9em; width: 92%; }
.skel-cbody.short { width: 58%; }

/* ---------- States (empty / error) ---------- */

.state-box {
  text-align: center;
  padding: 56px 24px;
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-lg);
  color: var(--text-secondary);
}
.state-box .state-emoji {
  font-size: 2rem;
  display: block;
  margin-bottom: 10px;
}
.state-box h2 {
  margin: 0 0 6px;
  font-size: 1.05rem;
  color: var(--text);
}
.state-box p { margin: 0 auto 16px; max-width: 34em; font-size: 0.9rem; }

.button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  font: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 999px;
  cursor: pointer;
  transition: background-color 0.12s ease, transform 0.1s ease;
}
.button:hover { background: var(--accent-strong); text-decoration: none; color: #fff; }
.button:active { transform: scale(0.97); }
.button.ghost {
  background: var(--surface);
  color: var(--text-secondary);
  border: 1px solid var(--border-strong);
}
.button.ghost:hover { color: var(--accent); border-color: var(--accent); background: var(--surface); }
.button[disabled] { opacity: 0.55; cursor: default; }

.more-row {
  display: flex;
  justify-content: center;
  margin-top: 18px;
}

/* ---------- Item page ---------- */

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 14px;
}
.back-link:hover { color: var(--accent); text-decoration: none; }

.story-detail {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px 24px;
  box-shadow: var(--shadow-sm);
  margin-bottom: 18px;
}
.story-detail h1 {
  margin: 0 0 10px;
  font-size: 1.4rem;
  line-height: 1.3;
  font-weight: 750;
  letter-spacing: -0.015em;
}
.story-detail h1 a { color: var(--text); }
.story-detail h1 a:hover { color: var(--accent-strong); text-decoration: none; }
.story-detail .story-meta { font-size: 0.85rem; margin-top: 0; }
.detail-actions { display: flex; gap: 10px; margin-top: 14px; flex-wrap: wrap; }

.story-text {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  font-size: 0.95rem;
  line-height: 1.65;
}

/* ---------- Comments ---------- */

.comments-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 0 2px 10px;
}
.comments-toolbar h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}
.comments-toolbar .toolbar-actions { display: flex; gap: 8px; align-items: center; }
.root-jump { display: inline-flex; gap: 2px; }

.thread { display: grid; gap: 10px; }

.comment {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
}
.comment.depth-0 { box-shadow: var(--shadow-sm); }

.comment-inner { padding: 12px 14px; }

.comment-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.78rem;
  color: var(--text-secondary);
  flex-wrap: wrap;
}
.collapse-toggle {
  display: inline-grid;
  place-items: center;
  width: 20px;
  height: 20px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface-sunken);
  color: var(--text-secondary);
  font-size: 0.7rem;
  font-family: var(--font-mono);
  cursor: pointer;
  transition: color 0.12s ease, border-color 0.12s ease;
}
.collapse-toggle:hover { color: var(--accent); border-color: var(--accent); }

.comment-author { color: var(--text-secondary); font-weight: 600; }
.comment-author:hover { color: var(--accent); }
.op-badge {
  font-size: 0.64rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: #fff;
  background: var(--accent);
  border-radius: 4px;
  padding: 1px 5px;
}
.fresh-dot {
  font-size: 0.64rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 0 6px;
}
.comment.collapsed .comment-inner { padding: 8px 14px; }
.comment.collapsed { opacity: 0.85; }
.hidden-count { color: var(--text-faint); font-variant-numeric: tabular-nums; }

.comment-body {
  margin-top: 8px;
  font-size: 0.92rem;
  line-height: 1.62;
  overflow-wrap: break-word;
}
.comment-body p { margin: 0 0 0.7em; }
.comment-body p:last-child { margin-bottom: 0; }
.comment-body a { text-decoration: underline; text-underline-offset: 2px; word-break: break-all; }
.comment-body pre {
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.82rem;
  line-height: 1.5;
  margin: 0.6em 0;
}
.comment-body code { font-family: var(--font-mono); font-size: 0.86em; }
.comment-body blockquote {
  margin: 0.6em 0;
  padding: 2px 0 2px 12px;
  border-left: 3px solid var(--border-strong);
  color: var(--text-secondary);
}
.comment-body ul, .comment-body ol { margin: 0.5em 0; padding-left: 1.4em; }

.comment-children {
  display: grid;
  gap: 10px;
  margin: 10px 0 2px;
  padding-left: 14px;
  border-left: 2px solid var(--border);
  transition: border-color 0.2s ease;
}
.comment-children:hover { border-left-color: var(--border-strong); }

.collapsible {
  display: grid;
  grid-template-rows: 1fr;
  transition: grid-template-rows 0.25s ease;
}
.collapsible > .collapsible-content { overflow: hidden; min-height: 0; }
.collapsible.closed { grid-template-rows: 0fr; }
@media (prefers-reduced-motion: reduce) {
  .collapsible { transition: none; }
}

.tombstone {
  font-size: 0.85rem;
  color: var(--text-faint);
  font-style: italic;
  padding: 10px 14px;
  background: var(--surface-sunken);
  border: 1px dashed var(--border);
  border-radius: var(--radius-md);
}
.tombstone .tombstone-children {
  font-style: normal;
  margin-top: 10px;
}

.comment.flash {
  animation: flash 1.2s ease;
}
@keyframes flash {
  0% { box-shadow: 0 0 0 2px var(--accent); }
  100% { box-shadow: 0 0 0 0 transparent; }
}

/* ---------- User page ---------- */

.user-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px 24px;
  box-shadow: var(--shadow-sm);
  margin-bottom: 18px;
}
.user-card h1 {
  margin: 0 0 4px;
  font-size: 1.35rem;
  letter-spacing: -0.015em;
}
.user-stats {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
  margin: 12px 0;
}
.user-stat .stat-value {
  font-size: 1.3rem;
  font-weight: 750;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.user-stat .stat-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
}
.user-about { font-size: 0.92rem; line-height: 1.6; color: var(--text); }
.user-about p { margin: 0 0 0.6em; }

.submission-row {
  display: flex;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
  align-items: baseline;
}
.submission-row:last-child { border-bottom: none; }
.submission-kind {
  flex: 0 0 auto;
  font-size: 0.66rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-faint);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0 5px;
}
.submission-title { color: var(--text); font-weight: 550; }
.submission-title:hover { color: var(--accent-strong); }
.submission-meta { font-size: 0.75rem; color: var(--text-faint); white-space: nowrap; margin-left: auto; }

/* ---------- Search ---------- */

.search-hero { margin-bottom: 18px; }
.search-hero h1 { margin: 0 0 12px; font-size: 1.25rem; }
.search-big {
  display: flex;
  gap: 8px;
}
.search-big input {
  flex: 1;
  padding: 10px 14px;
  font: inherit;
  font-size: 1rem;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
}
.search-big input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.search-meta { font-size: 0.78rem; color: var(--text-faint); margin-top: 8px; }

/* ---------- Footer ---------- */

.site-footer {
  border-top: 1px solid var(--border);
  padding: 20px 16px 28px;
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-faint);
}
.site-footer p { margin: 4px 0; }
.site-footer a { color: var(--text-secondary); }
.footer-links { display: flex; gap: 14px; justify-content: center; }

/* ---------- Responsive ---------- */

@media (max-width: 640px) {
  :root { --header-h: 52px; }
  .header-inner { gap: 10px; padding: 0 12px; }
  .brand-name { display: none; }
  .feed-nav a { padding: 6px 8px; font-size: 0.85rem; }
  .search-form input { width: 34px; padding-left: 30px; color: transparent; cursor: pointer; }
  .search-form input:focus { width: 140px; color: var(--text); cursor: text; background-color: var(--surface); }
  main { padding: 14px 10px 40px; }
  .story-row { padding: 11px 10px; gap: 9px; }
  .story-rank { flex-basis: 1.7em; font-size: 0.8rem; }
  .story-detail { padding: 16px; }
  .story-detail h1 { font-size: 1.15rem; }
  .comment-inner { padding: 10px 12px; }
  .comment-children { padding-left: 10px; }
  .comments-toolbar { flex-wrap: wrap; }
}
