/* NBERok — TikTok-style feed of NBER working papers */

* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #0b0e14;
  --card: #131826;
  --text: #f2f4f8;
  --muted: #9aa3b5;
  --accent: #4f8cff;
  --like: #ff3b5c;
  /* fallback chain for full viewport height on iOS Safari */
  --vh100: 100vh;
}
@supports (height: 100dvh) {
  :root { --vh100: 100dvh; }
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior: none;
}

.hidden { display: none !important; }

/* ============ Onboarding ============ */

.onboarding {
  position: fixed;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(79, 140, 255, 0.25), transparent),
    radial-gradient(900px 500px at -10% 110%, rgba(255, 59, 92, 0.18), transparent),
    var(--bg);
  z-index: 50;
}
.onboarding-inner {
  max-width: 640px;
  margin: 0 auto;
  padding: max(24px, env(safe-area-inset-top)) 20px calc(32px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.logo {
  font-size: 44px;
  font-weight: 800;
  letter-spacing: -1px;
}
.logo span, .topbar-logo span { color: var(--accent); }
.tagline { color: var(--muted); margin-bottom: 18px; }
.onboarding h2 { font-size: 22px; }
.hint { color: var(--muted); font-size: 14px; margin-bottom: 8px; }

.program-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
  margin: 8px 0 20px;
}
.grid-section {
  grid-column: 1 / -1;
  font-size: 15px;
  font-weight: 700;
  margin-top: 10px;
}
.grid-section:first-child { margin-top: 0; }
.grid-subsection {
  grid-column: 1 / -1;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  color: var(--muted);
  margin-top: 8px;
}
.grid-toggle {
  grid-column: 1 / -1;
  background: none;
  border: 1px dashed rgba(255, 255, 255, 0.22);
  color: var(--muted);
  border-radius: 12px;
  font-size: 13px;
  padding: 9px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.program-chip {
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  border-radius: 14px;
  padding: 12px 10px;
  font-size: 13.5px;
  line-height: 1.25;
  text-align: left;
  cursor: pointer;
  transition: transform 0.06s ease, background 0.15s ease, border-color 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}
.program-chip:active { transform: scale(0.97); }
.program-chip .code {
  display: block;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  margin-bottom: 3px;
  letter-spacing: 0.5px;
}
.program-chip.selected {
  background: rgba(79, 140, 255, 0.22);
  border-color: var(--accent);
}
.program-chip.selected .code { color: var(--accent); }

/* Recency segmented control (onboarding, above the interest grid) */
.recency-seg { display: flex; gap: 8px; margin: 4px 0 6px; }
.recency-btn {
  flex: 1;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  border-radius: 14px;
  padding: 10px 6px;
  font-size: 13.5px;
  cursor: pointer;
  transition: transform 0.06s ease, background 0.15s ease, border-color 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}
.recency-btn:active { transform: scale(0.97); }
.recency-btn.selected {
  background: rgba(79, 140, 255, 0.22);
  border-color: var(--accent);
}

.primary-btn {
  position: sticky;
  bottom: calc(12px + env(safe-area-inset-bottom));
  padding: 15px;
  border: none;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(79, 140, 255, 0.35);
}
.primary-btn:disabled { opacity: 0.4; cursor: not-allowed; box-shadow: none; }

/* ============ Top bar ============ */

.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  z-index: 20;
  pointer-events: none;
  background: linear-gradient(rgba(11, 14, 20, 0.85), transparent);
}
.topbar-logo { font-weight: 800; font-size: 18px; letter-spacing: -0.5px; }
.topbar-actions { display: flex; gap: 8px; }
.topbar .icon-btn { pointer-events: auto; }

/* Pull-to-refresh indicator: a fixed chip below the topbar, moved/rotated by
   JS in proportion to the pull so we never transform the scroller itself */
.pull-hint {
  position: fixed;
  top: calc(10px + env(safe-area-inset-top));
  left: 50%;
  transform: translate(-50%, 0);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.14);
  color: var(--text);
  font-size: 18px;
  line-height: 38px;
  text-align: center;
  z-index: 30;
  pointer-events: none;
}
.pull-hint.ready { background: rgba(79, 140, 255, 0.85); color: #fff; }

@keyframes spin { to { transform: rotate(360deg); } }
.icon-btn.spinning { animation: spin 0.8s linear infinite; }

.icon-btn {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: var(--text);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  font-size: 18px;
  line-height: 38px;
  text-align: center;
  cursor: pointer;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
.icon-btn:active { background: rgba(255, 255, 255, 0.22); }

/* ============ Feed ============ */

.feed {
  position: fixed;
  inset: 0;
  height: var(--vh100);
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  overscroll-behavior: none; /* prevent Android Chrome native PTR on the scroll container */
}
.feed::-webkit-scrollbar { display: none; }

.paper-card {
  position: relative;
  height: var(--vh100);
  scroll-snap-align: start;
  scroll-snap-stop: always;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  /* clear the bottom tab bar */
  padding: 0 16px calc(78px + env(safe-area-inset-bottom));
  overflow: hidden;
  touch-action: pan-y;
}

.card-bg {
  position: absolute;
  inset: 0;
  z-index: -1;
}
.card-photo {
  position: absolute;
  inset: 0;
  z-index: -1;
  background-size: cover;
  background-position: center 25%;
  opacity: 0.5;
  -webkit-mask-image: linear-gradient(#000 30%, transparent 85%);
  mask-image: linear-gradient(#000 30%, transparent 85%);
}

.card-content {
  max-width: 660px;
  width: 100%;
  margin: 0 auto;
  padding-right: 64px; /* room for the action rail */
}

.tag-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.tag {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.4px;
  padding: 4px 9px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  color: var(--text);
}
.tag.new { background: var(--like); }

.paper-title {
  font-size: clamp(19px, 4.6vw, 26px);
  font-weight: 800;
  line-height: 1.22;
  margin-bottom: 8px;
  text-wrap: balance;
}
.paper-authors {
  font-size: 14px;
  color: var(--text);
  opacity: 0.92;
  margin-bottom: 2px;
}
.paper-date { font-size: 12.5px; color: var(--muted); margin-bottom: 10px; }
.paper-abstract {
  font-size: 14.5px;
  line-height: 1.5;
  color: rgba(242, 244, 248, 0.88);
  display: -webkit-box;
  -webkit-line-clamp: 7;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.paper-card.expanded .paper-abstract {
  /* full abstracts can be 1500+ chars: un-clamp and scroll within the card */
  display: block;
  max-height: 38vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.read-pdf-hint {
  margin-top: 14px;
  font-size: 12.5px;
  color: var(--muted);
}

/* Action rail (right side, TikTok style) */
.action-rail {
  position: absolute;
  right: 10px;
  bottom: calc(140px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 18px;
  align-items: center;
  z-index: 5;
}
.rail-btn {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: var(--text);
  width: 48px;
  height: 48px;
  border-radius: 50%;
  font-size: 21px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.08s ease;
}
.rail-btn:active { transform: scale(0.9); }
.rail-btn.liked { color: var(--like); background: rgba(255, 59, 92, 0.18); }
.rail-label {
  font-size: 10px;
  color: var(--muted);
  margin-top: -12px;
  user-select: none;
  -webkit-user-select: none;
}

.feed-status {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  color: var(--muted);
  font-size: 15px;
  z-index: 10;
  pointer-events: none;
  white-space: pre-line;
}

/* ============ Bottom tab bar ============ */

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-around;
  padding: 6px 8px calc(8px + env(safe-area-inset-bottom));
  background: rgba(11, 14, 20, 0.82);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  z-index: 25;
}
.tab {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
  padding: 4px 10px; /* 4 tabs must fit a 320px screen */
  border-radius: 12px;
  -webkit-tap-highlight-color: transparent;
}
.tab span { font-size: 10.5px; font-weight: 600; letter-spacing: 0.3px; }
.tab.active { color: var(--text); }
.tab.active span { color: var(--accent); }

.howto {
  position: fixed;
  bottom: calc(78px + env(safe-area-inset-bottom));
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  z-index: 15;
  pointer-events: none;
}
.howto div {
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: var(--muted);
  font-size: 12px;
  padding: 7px 14px;
  border-radius: 999px;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

/* ============ PDF overlay ============ */

.pdf-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: #1a1d24;
  display: flex;
  flex-direction: column;
}
.pdf-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: var(--bg);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.pdf-title {
  flex: 1;
  font-size: 13.5px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pdf-pages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 0 calc(20px + env(safe-area-inset-bottom));
}
.pdf-page {
  position: relative;
  margin: 0 auto 10px;
  max-width: 100%;
  background: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
}
.pdf-page canvas {
  display: block;
  width: 100%;
  height: auto;
}
/* invisible selectable text positioned over the canvas (pdf.js text layer):
   makes native find-on-page and copy work inside the reader */
.textLayer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  line-height: 1;
  transform-origin: 0 0;
  forced-color-adjust: none;
}
.textLayer span,
.textLayer br {
  color: transparent;
  position: absolute;
  white-space: pre;
  cursor: text;
  transform-origin: 0% 0%;
}
.textLayer ::selection {
  background: rgba(79, 140, 255, 0.45);
  color: transparent;
}
.pdf-message {
  position: absolute;
  inset: 60px 0 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  color: var(--muted);
}
.pdf-message a {
  color: #fff;
  background: var(--accent);
  padding: 12px 22px;
  border-radius: 12px;
  text-decoration: none;
  font-weight: 700;
}

/* ============ Save-to-collection sheet ============ */

.save-sheet { position: fixed; inset: 0; z-index: 55; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.55); }
.sheet-panel {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 640px;
  background: var(--card);
  border-radius: 18px 18px 0 0;
  padding: 10px 18px calc(16px + env(safe-area-inset-bottom));
  animation: sheet-up 0.22s ease;
}
@keyframes sheet-up {
  from { transform: translate(-50%, 40%); opacity: 0.4; }
  to { transform: translate(-50%, 0); opacity: 1; }
}
.sheet-grab {
  width: 38px;
  height: 4px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.25);
  margin: 2px auto 12px;
}
.sheet-title { font-size: 17px; margin-bottom: 10px; }
.sheet-list { max-height: 38vh; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.sheet-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  background: none;
  border: none;
  color: var(--text);
  font-size: 15px;
  text-align: left;
  padding: 12px 4px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.sheet-row-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-row-count { color: var(--muted); font-size: 12.5px; }
.sheet-row-check { color: var(--accent); font-weight: 700; visibility: hidden; }
.sheet-row.checked .sheet-row-check { visibility: visible; }
.sheet-new { display: flex; gap: 8px; margin: 12px 0; }
.sheet-new input {
  flex: 1;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 10px;
  color: var(--text);
  font-size: 15px;
  padding: 10px 12px;
}
.sheet-add {
  width: 44px;
  border: none;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.12);
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
}
.sheet-panel .primary-btn { position: static; width: 100%; }

/* ============ Saved library ============ */

.saved-view {
  position: fixed;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: calc(58px + env(safe-area-inset-top)) 16px calc(84px + env(safe-area-inset-bottom));
  background: var(--bg);
  z-index: 5;
}
.saved-chips {
  display: flex;
  gap: 8px;
  align-items: center;
  overflow-x: auto;
  scrollbar-width: none;
  max-width: 660px;
  margin: 0 auto 14px;
  padding: 4px 0;
}
.saved-chips::-webkit-scrollbar { display: none; }
.saved-chip {
  flex-shrink: 0;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: var(--text);
  border-radius: 999px;
  font-size: 13px;
  padding: 7px 14px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.saved-chip.active { background: rgba(79, 140, 255, 0.25); border-color: var(--accent); }
.chip-count { color: var(--muted); font-size: 11.5px; }
.chip-tool {
  flex-shrink: 0;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: var(--muted);
  width: 30px;
  height: 30px;
  border-radius: 50%;
  font-size: 14px;
  cursor: pointer;
}
.saved-list { max-width: 660px; margin: 0 auto; display: flex; flex-direction: column; gap: 10px; }
.saved-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  background: var(--card);
  border-radius: 14px;
  padding: 13px 14px;
}
.saved-row-main { flex: 1; cursor: pointer; min-width: 0; }
.saved-row-title { font-size: 15px; font-weight: 700; line-height: 1.3; margin-bottom: 4px; }
.saved-row-meta { font-size: 12.5px; color: var(--muted); margin-bottom: 6px; }
.saved-row-tags { display: flex; flex-wrap: wrap; gap: 5px; }
.saved-tag {
  font-size: 10.5px;
  font-weight: 600;
  background: rgba(79, 140, 255, 0.18);
  color: var(--text);
  border-radius: 999px;
  padding: 2px 8px;
}
.saved-row-remove {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 16px;
  padding: 2px 6px;
  cursor: pointer;
}
.saved-empty {
  text-align: center;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.6;
  padding-top: 18vh;
}

/* ============ Sync section (settings) ============ */

.sync-section {
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  margin: 6px 0 18px;
  padding-top: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.sync-section h2 { font-size: 18px; }
.sync-btn {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: var(--text);
  border-radius: 10px;
  font-size: 14px;
  padding: 11px 14px;
  cursor: pointer;
}
.sync-btn.danger { color: #ff8095; border-color: rgba(255, 59, 92, 0.4); }
.sync-connect { display: flex; gap: 8px; margin-top: 8px; }
.sync-connect input {
  flex: 1;
  min-width: 0;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 10px;
  color: var(--text);
  font-size: 14px;
  padding: 10px 12px;
}
.sync-key-row { display: flex; align-items: center; gap: 8px; }
.sync-key-row code {
  flex: 1;
  background: var(--card);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  word-break: break-all;
  user-select: all;
  -webkit-user-select: all;
}
#syncStatus { min-height: 1em; }

/* ============ Like burst animation ============ */

.like-burst {
  position: fixed;
  z-index: 70;
  font-size: 90px;
  color: var(--like);
  pointer-events: none;
  transform: translate(-50%, -50%) scale(0.4);
  opacity: 0;
}
.like-burst.bursting {
  animation: burst 0.7s ease forwards;
}
@keyframes burst {
  20% { opacity: 1; transform: translate(-50%, -50%) scale(1.25) rotate(-8deg); }
  100% { opacity: 0; transform: translate(-50%, -80%) scale(1); }
}

/* Larger screens: keep the phone-like column but let the card breathe */
@media (min-width: 900px) {
  .card-content { padding-right: 80px; }
  .paper-abstract { -webkit-line-clamp: 10; font-size: 15.5px; }
}
