/* Doomscholar components. Load after tokens.css and src/app.css.
   Owned by the UI/UX thread. Uses only variables from tokens.css.
   Looks for the likes + reader features live in design/overrides.css,
   which loads after features/reader.css. */

/* ---------- Base ---------- */
html, body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: var(--radius-sm); }
::selection { background: var(--accent-soft); }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.icon { width: 24px; height: 24px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* Press feedback stands in for haptics, which iOS web apps can't trigger. */
.action-btn, .btn, .chip, .tab, .icon-btn, .card__more {
  transition: transform var(--dur-press) var(--ease-out), opacity var(--dur-press) var(--ease-out),
              background-color var(--dur-fast), color var(--dur-fast), border-color var(--dur-fast);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
.action-btn:active, .btn:active, .chip:active, .tab:active, .icon-btn:active { transform: scale(0.94); }
.card__more:active { opacity: 0.6; }

/* ---------- Top bar + category chips ---------- */
.topbar {
  padding: calc(var(--safe-top) + var(--space-2)) 0 var(--space-4);
  background: linear-gradient(to bottom, var(--bg) 0%, var(--bg) 65%, transparent 100%);
}
.topbar__brand { display: flex; align-items: center; gap: var(--space-2); height: 36px; padding: 0 var(--gutter); margin-bottom: var(--space-1); }
.brand__mark { width: 22px; height: 22px; flex: none; }
.brand__name { margin: 0; font-size: var(--fs-headline); font-weight: 700; letter-spacing: -0.015em; color: var(--text); }
.topbar__chips { padding: 0 var(--gutter); gap: var(--space-2); scroll-padding-inline: var(--gutter); }
.chip {
  height: 34px; padding: 0 14px;
  display: inline-flex; align-items: center;
  border: 1px solid var(--line-strong); background: var(--bar);
  color: var(--text-2);
  font-size: 14px; font-weight: 500; white-space: nowrap;
}
.chip--active, .chip[aria-pressed="true"] { background: var(--inverse); color: var(--on-inverse); border-color: transparent; font-weight: 600; }

/* ---------- Feed card ---------- */
.feed { scrollbar-width: none; }
.feed::-webkit-scrollbar { display: none; }

.card__inner {
  gap: 14px;
  padding:
    calc(var(--safe-top) + 112px)
    calc(var(--gutter) + var(--safe-right))
    calc(var(--safe-bottom) + var(--tabbar-h) + var(--space-3))
    calc(var(--gutter) + var(--safe-left));
}
.card__meta { flex-wrap: wrap; gap: 6px 10px; font-size: var(--fs-meta); color: var(--text-2); }
.card__meta time { font-variant-numeric: tabular-nums; }
.card__id { font-family: var(--font-mono); font-size: 12px; color: var(--text-3); }

.card__title {
  font-size: var(--fs-title); line-height: var(--lh-tight); font-weight: 700; letter-spacing: -0.025em;
  color: var(--text);
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden;
  text-wrap: balance;
  flex: none;
}
@media (max-height: 700px) { .card__title { font-size: 25px; -webkit-line-clamp: 3; } }

.card__authors {
  font-size: 14px; line-height: 1.4; color: var(--text-2);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  flex: none;
}
.card__abstract {
  margin-top: var(--space-1);
  font-family: var(--font-serif); font-size: var(--fs-reading); line-height: var(--lh-reading);
  color: var(--text-body);
  text-wrap: pretty; hyphens: manual;
  -webkit-mask-image: linear-gradient(to bottom, #000 72%, transparent);
  mask-image: linear-gradient(to bottom, #000 72%, transparent);
  outline: none;
}
.card__abstract .katex { font-size: 1em; }
.card__more {
  align-self: flex-start; min-height: 32px; margin-top: calc(var(--space-2) * -1);
  color: var(--accent); font-size: 14px; font-weight: 600;
}

.card__tags { gap: 6px; flex: none; }
.tag {
  padding: 3px 8px; border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text-2);
  font-size: 12px; font-weight: 500; line-height: 1.3;
}
.tag--primary { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* Action row: icon buttons left, primary "Read paper" pill right. */
.card__actions { gap: var(--space-1); margin: 0 calc(var(--space-2) * -1); flex: none; }
.card__actions .action-btn {
  min-width: 52px; min-height: 52px; padding: 0;
  border: 0; background: transparent; color: var(--text);
  border-radius: var(--radius-pill);
}
.card__actions .action-btn svg { width: 28px; height: 28px; }
.card__actions .action-btn__label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.card__actions .action-btn--link {
  min-width: 0; min-height: 44px; padding: 0 var(--space-3);
  color: var(--text-2); font-size: 14px; font-weight: 600;
}
.card__actions .btn { margin-left: auto; }

.btn {
  min-height: 48px; padding: 0 20px 0 18px; border-radius: var(--radius-pill);
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  border: 1px solid var(--line-strong); background: transparent; color: var(--text);
  font-size: 16px; font-weight: 600; text-decoration: none; white-space: nowrap;
}
.btn svg { width: 20px; height: 20px; }
.btn--primary { background: var(--inverse); color: var(--on-inverse); border-color: transparent; }

.icon-btn {
  width: var(--tap); height: var(--tap); border-radius: var(--radius-pill);
  display: inline-flex; align-items: center; justify-content: center;
  border: 0; background: transparent; color: var(--text); padding: 0;
}

/* ---------- Loading, error, end ---------- */
.feed__status { color: var(--text-2); font-size: var(--fs-callout); line-height: 1.5; text-align: center; }
.feed__status p { margin: 0; max-width: 28ch; }
.feed__status[data-state="loading"]::before {
  content: ""; width: 28px; height: 28px; border-radius: 50%;
  border: 2.5px solid var(--line-strong); border-top-color: var(--like);
  animation: spin 0.8s linear infinite;
}
.feed__status .action-btn { padding: 0 20px; background: var(--inverse); color: var(--on-inverse); border: 0; font-weight: 600; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 2.5px solid var(--line-strong); border-top-color: var(--like); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.empty-state { display: flex; flex-direction: column; align-items: center; gap: var(--space-3); padding: var(--space-12) var(--gutter); text-align: center; color: var(--text-2); font-size: var(--fs-callout); line-height: 1.5; }
.empty-state svg { width: 44px; height: 44px; color: var(--text-3); }
.empty-state h2 { margin: 0; color: var(--text); font-size: 20px; }

/* ---------- Bottom tab bar (Feed / Liked) ---------- */
.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 20;
  display: flex;
  padding: 6px var(--safe-right) var(--safe-bottom) var(--safe-left);
  min-height: calc(var(--tabbar-h) + var(--safe-bottom));
  background: var(--bar);
  -webkit-backdrop-filter: blur(20px) saturate(1.4);
  backdrop-filter: blur(20px) saturate(1.4);
  border-top: 1px solid var(--line);
}
.tab {
  flex: 1; min-height: var(--tap);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  border: 0; background: transparent; padding: 0;
  color: var(--text-3); font-size: var(--fs-caption); font-weight: 500; text-decoration: none;
}
.tab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.tab[aria-selected="true"], .tab[aria-current="page"], .tab.is-active { color: var(--text); font-weight: 600; }
.tab--liked[aria-selected="true"], .tab--liked[aria-current="page"], .tab--liked.is-active { color: var(--like); }
.tab--liked[aria-selected="true"] svg, .tab--liked[aria-current="page"] svg, .tab--liked.is-active svg { fill: currentColor; }
.tab__badge { position: absolute; margin: -18px 0 0 22px; min-width: 16px; height: 16px; padding: 0 4px; border-radius: 8px; background: var(--like); color: #fff; font-size: 10px; font-weight: 700; line-height: 16px; text-align: center; }

/* A full-screen page under the top/tab bars, e.g. the Liked view */
.page {
  height: 100dvh; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding: calc(var(--safe-top) + var(--space-6)) calc(var(--gutter) + var(--safe-right)) calc(var(--safe-bottom) + var(--tabbar-h) + var(--space-6)) calc(var(--gutter) + var(--safe-left));
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; z-index: 1100; left: 50%;
  top: calc(var(--safe-top) + var(--space-3));
  transform: translateX(-50%);
  padding: 10px 16px; border-radius: var(--radius-pill);
  background: var(--inverse); color: var(--on-inverse);
  font-size: 14px; font-weight: 600; box-shadow: var(--shadow-float);
  animation: toast-in var(--dur-med) var(--ease-spring);
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, -12px); } to { opacity: 1; transform: translate(-50%, 0); } }

.page__title { margin: 0; font-size: var(--fs-large-title); font-weight: 700; letter-spacing: -0.025em; line-height: 1.1; }
.page__sub { margin: var(--space-1) 0 0; font-size: var(--fs-callout); color: var(--text-2); }

/* ---------- Like ---------- */
.like-btn svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; transition: color var(--dur-fast); }
.like-btn[aria-pressed="true"] { color: var(--like); }
.like-btn[aria-pressed="true"] svg { fill: var(--like); stroke: var(--like); animation: heart-pop var(--dur-med) var(--ease-spring); }
@keyframes heart-pop {
  0% { transform: scale(1); }
  30% { transform: scale(0.8); }
  65% { transform: scale(1.24); }
  100% { transform: scale(1); }
}

/* Double-tap burst:
   <div class="heart-burst" aria-hidden="true"> svg.heart-burst__heart, span.heart-burst__ring, 6 × span.heart-burst__spark </div>
   inside .card, --x/--y = tap point relative to the card, .is-bursting on the card for ~750ms. */
.heart-burst {
  position: absolute; z-index: 5; pointer-events: none;
  left: var(--x, 50%); top: var(--y, 50%);
  width: 120px; height: 120px; margin: -60px 0 0 -60px;
  opacity: 0;
}
.heart-burst__heart { position: absolute; left: 10px; top: 10px; width: 100px; height: 100px; fill: var(--like); stroke: none; filter: drop-shadow(0 10px 24px var(--like-glow)); }
.heart-burst__ring { position: absolute; inset: -16px; border-radius: 50%; border: 2px solid var(--like); opacity: 0; }
.heart-burst__spark { position: absolute; left: 56px; top: 56px; width: 8px; height: 8px; border-radius: 50%; background: var(--like); opacity: 0; }
.heart-burst__spark:nth-child(odd) { background: var(--like-2); }
.heart-burst__spark:nth-child(3) { --a: 0deg; }
.heart-burst__spark:nth-child(4) { --a: 60deg; }
.heart-burst__spark:nth-child(5) { --a: 120deg; }
.heart-burst__spark:nth-child(6) { --a: 180deg; }
.heart-burst__spark:nth-child(7) { --a: 240deg; }
.heart-burst__spark:nth-child(8) { --a: 300deg; }

.is-bursting > .heart-burst { animation: burst-fade var(--dur-burst) var(--ease-out) forwards; }
.is-bursting > .heart-burst .heart-burst__heart { animation: burst-heart var(--dur-burst) var(--ease-spring) forwards; }
.is-bursting > .heart-burst .heart-burst__ring { animation: burst-ring var(--dur-burst) var(--ease-out) forwards; }
.is-bursting > .heart-burst .heart-burst__spark { animation: burst-spark var(--dur-burst) var(--ease-out) forwards; }

@keyframes burst-fade { 0% { opacity: 1; } 75% { opacity: 1; } 100% { opacity: 0; } }
@keyframes burst-heart {
  0% { transform: scale(0) rotate(-14deg); }
  40% { transform: scale(1.15) rotate(4deg); }
  58% { transform: scale(0.95) rotate(0); }
  80% { transform: scale(1) translateY(0); }
  100% { transform: scale(0.9) translateY(-36px); }
}
@keyframes burst-ring { 0% { transform: scale(0.35); opacity: 0.7; } 55% { opacity: 0; transform: scale(1.2); } 100% { opacity: 0; } }
@keyframes burst-spark {
  0% { opacity: 0; transform: rotate(var(--a)) translateY(0) scale(1); }
  20% { opacity: 1; }
  70% { opacity: 1; transform: rotate(var(--a)) translateY(-84px) scale(0.6); }
  100% { opacity: 0; transform: rotate(var(--a)) translateY(-96px) scale(0); }
}

@media (prefers-reduced-motion: reduce) {
  .like-btn[aria-pressed="true"] svg { animation: none; }
  .is-bursting > .heart-burst .heart-burst__heart { animation: none; }
  .heart-burst__ring, .heart-burst__spark { display: none; }
}

/* ---------- Liked page ---------- */
.liked-list { list-style: none; margin: var(--space-4) 0 0; padding: 0; }
.liked-item {
  display: flex; align-items: center; gap: var(--space-2);
  padding: var(--space-3) 0; border-bottom: 1px solid var(--line);
}
.liked-item__body { transition: opacity var(--dur-press); padding: var(--space-1) 0 !important; }
.liked-item__body:active { opacity: 0.6; }
.liked-item__meta { display: flex; align-items: center; gap: var(--space-2); font-size: 12px; color: var(--text-2); }
.liked-item__title { font-size: var(--fs-headline); font-weight: 600; line-height: 1.28; letter-spacing: -0.01em; color: var(--text); }
.liked-item__authors { font-size: var(--fs-meta); color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.liked-item .like-btn { width: var(--tap); height: var(--tap); border: 0; background: none; padding: 0; display: inline-flex; align-items: center; justify-content: center; color: var(--text-3); }
.liked-item .like-btn svg { width: 24px; height: 24px; }
.liked-item .like-btn[aria-pressed="true"] { color: var(--like); }
.liked-item .like-btn[aria-pressed="true"] svg { animation: none; }

/* ---------- Reader (full paper) ---------- */
.reader {
  position: fixed; inset: 0; z-index: 1000;
  background: var(--bg); color: var(--text);
  overflow-y: auto; overflow-x: hidden; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  animation: sheet-up var(--dur-med) var(--ease-out);
}
.reader.is-closing { animation: sheet-down var(--dur-fast) ease-in forwards; }
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: none; } }
@keyframes sheet-down { to { transform: translateY(100%); } }
@media (prefers-reduced-motion: reduce) {
  .reader { animation-name: fade-in; }
  .reader.is-closing { animation-name: fade-out; }
}
@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }

.reader__header {
  position: sticky; top: 0; z-index: 2;
  padding: var(--safe-top) calc(var(--space-2) + var(--safe-right)) 0 calc(var(--space-2) + var(--safe-left));
  background: var(--bar);
  -webkit-backdrop-filter: blur(20px) saturate(1.4);
  backdrop-filter: blur(20px) saturate(1.4);
  border-bottom: 1px solid var(--line);
}
.reader__bar { min-height: var(--tap); display: flex; align-items: center; gap: var(--space-1); padding-right: var(--space-2); }
.reader__heading { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.reader__title { font-size: var(--fs-callout); font-weight: 600; line-height: 1.25; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader__sub { font-size: 12px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader__progress { height: 2px; margin: 6px calc((var(--space-2) + var(--safe-left)) * -1) 0 calc((var(--space-2) + var(--safe-left)) * -1); background: var(--line); }
.reader__progress::after {
  content: ""; display: block; height: 100%;
  background: var(--like); border-radius: 0 2px 2px 0;
  transform-origin: left; transform: scaleX(var(--progress, 0));
  transition: transform 80ms linear;
}

.reader__body {
  max-width: 680px; margin: 0 auto;
  padding: var(--space-6) calc(var(--space-6) + var(--safe-right)) calc(var(--safe-bottom) + 120px) calc(var(--space-6) + var(--safe-left));
  font-family: var(--font-serif); font-size: var(--reader-size, var(--fs-reading)); line-height: var(--lh-reading); color: var(--text-body);
  text-wrap: pretty; hyphens: auto; overflow-wrap: break-word;
}
.reader__meta { font-family: var(--font-sans); font-size: 12px; font-weight: 600; letter-spacing: 0.02em; color: var(--accent); }
.reader__meta a { color: var(--text-3); font-family: var(--font-mono); font-weight: 500; text-decoration: none; }
.reader__abstract { border: 1px solid var(--line); }
.reader__abstract summary { letter-spacing: 0.08em; text-transform: uppercase; }
.reader__abstract p { font-size: 17px; line-height: 1.55; }
.reader__body h2, .reader__body h3 {
  font-family: var(--font-sans); color: var(--accent);
  font-size: var(--fs-meta); font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; line-height: 1.35;
  margin: var(--space-8) 0 var(--space-3);
}
.reader__body h3 { color: var(--text-2); }
.reader__body p { margin: 0 0 1em; }
.reader__body a { color: var(--accent); }
.reader__body img, .reader__body video { max-width: 100%; height: auto; border-radius: var(--radius-sm); background: #fff; }
.reader__body svg:not(.icon):not(math svg) { max-width: 100%; height: auto; }
.reader__body table { max-width: 100%; border-collapse: collapse; font-family: var(--font-sans); font-size: 13px; }
.reader__body td, .reader__body th { padding: 4px 8px; }
.reader__body pre, .reader__body code { font-family: var(--font-mono); font-size: 0.82em; }
.reader__body pre { overflow-x: auto; padding: var(--space-4); border-radius: var(--radius-md); background: var(--surface); }
.reader__body blockquote { margin: var(--space-4) 0; padding-left: var(--space-4); border-left: 2px solid var(--line-strong); color: var(--text-2); }
.reader__body .katex-display { overflow-x: auto; overflow-y: hidden; padding: var(--space-1) 0; }
.reader__loading .spinner, .reader__loading::before { border-top-color: var(--like); }

/* Floating dock at the bottom of the reader */
.reader__dock {
  position: fixed; z-index: 1001;
  left: calc(var(--space-4) + var(--safe-left)); right: calc(var(--space-4) + var(--safe-right));
  bottom: calc(var(--safe-bottom) + var(--space-2));
  max-width: 560px; margin: 0 auto;
  height: 60px; padding: 0 6px; border-radius: 30px;
  display: flex; align-items: center; gap: var(--space-1);
  background: var(--bar); border: 1px solid var(--line);
  -webkit-backdrop-filter: blur(20px) saturate(1.4); backdrop-filter: blur(20px) saturate(1.4);
  box-shadow: var(--shadow-float);
}
.reader__dock { animation: dock-in var(--dur-med) var(--ease-out); }
@keyframes dock-in { from { opacity: 0; transform: translateY(24px); } }
.reader__dock .spacer { flex: 1; }
.reader__dock .btn { min-height: 44px; font-size: 15px; padding: 0 16px; }
.reader__dock .action-btn, .reader__dock .like-btn { width: 48px; height: 48px; min-height: 0; padding: 0; border: 0; background: none; display: inline-flex; align-items: center; justify-content: center; color: var(--text); border-radius: var(--radius-pill); }
.reader__dock .like-btn svg { width: 26px; height: 26px; }
.reader__dock .like-btn[aria-pressed="true"] { color: var(--like); }
