:root {
  --bg: #0b0b0f;
  --bg-panel: #111115;
  --bg-elevated: #16161c;
  --border: #1d1d24;
  --border-soft: #25252e;
  --text: #e6e6eb;
  --text-dim: #9a9aa5;
  --text-faint: #6b6b76;
  --accent: #c9c9d3;
  --accent-dim: #9c9ca7;
  --accent-bg: rgba(201, 201, 211, 0.09);
  --green: #4cd97b;
  --amber: #e0b45e;
  --red: #e0656b;
  --radius: 10px;
  --radius-sm: 6px;
  --font-ui: system-ui, -apple-system, "Segoe UI", Inter, Roboto, sans-serif;
  --font-read: system-ui, -apple-system, "Segoe UI", Inter, Georgia, serif;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
}

[data-theme="light"] {
  --bg: #f4f4f7;
  --bg-panel: #ececf1;
  --bg-elevated: #ffffff;
  --border: #d9d9e0;
  --border-soft: #c7c7d0;
  --text: #1c1c22;
  --text-dim: #565660;
  --text-faint: #8b8b95;
  --accent: #3a3a46;
  --accent-dim: #6a6a76;
  --accent-bg: rgba(58, 58, 70, 0.08);
  --green: #1e8e4a;
  --amber: #a97a1b;
  --red: #c23d45;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
}
[data-theme="light"] .tab.active { border-color: rgba(58, 58, 70, 0.35); }
[data-theme="light"] .paper-row.selected { border-color: rgba(58, 58, 70, 0.3); }
[data-theme="light"] .read-status.read { border-color: rgba(30, 142, 74, 0.5); }

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ---------- toolbar ---------- */
.toolbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 12px 22px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
  flex: 0 0 auto;
}

.wordmark {
  font-weight: 800;
  font-size: 1.15rem;
  letter-spacing: -0.02em;
  color: var(--text);
}
.wordmark::first-letter { color: var(--accent); }

.tabs { display: flex; gap: 4px; }

.tab {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  transition: color 0.15s, background 0.15s, border-color 0.15s;
}
.tab:hover { color: var(--text); background: var(--bg-elevated); }
.tab.active {
  color: var(--text);
  background: var(--accent-bg);
  border-color: rgba(201, 201, 211, 0.35);
}
.tab.active .badge { color: var(--accent-dim); }

.badge {
  font-size: 0.75rem;
  color: var(--text-faint);
  background: var(--bg-elevated);
  border-radius: 999px;
  padding: 0 7px;
  min-width: 18px;
  text-align: center;
}

.spinner-badge {
  width: 14px;
  height: 14px;
  border: 2px solid var(--border-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toolbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.hint { color: var(--text-faint); font-size: 0.8rem; }

.daynav { display: flex; align-items: center; gap: 6px; }

.daynav .btn { padding: 4px 10px; }

#day-input {
  font: inherit;
  font-size: 0.875rem;
  color: var(--text);
  background: var(--bg-elevated);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 5px 8px;
  color-scheme: dark;
}
[data-theme="light"] #day-input { color-scheme: light; }

.toolbar-search { display: flex; align-items: center; gap: 8px; }

#search-input {
  font: inherit;
  font-size: 0.875rem;
  color: var(--text);
  background: var(--bg-elevated);
  border: 1px solid var(--border-soft);
  border-radius: 999px;
  padding: 6px 16px;
  min-width: 260px;
  outline: none;
  transition: border-color 0.15s;
}
#search-input:focus { border-color: var(--accent); }
#search-input::placeholder { color: var(--text-faint); }

.list-head { flex: 0 0 auto; }

.list-title {
  max-width: 52rem;
  margin: 0 auto;
  padding: 18px 22px 0;
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-dim);
}

.btn {
  font: inherit;
  font-size: 0.875rem;
  border-radius: var(--radius-sm);
  padding: 6px 14px;
  cursor: pointer;
  border: 1px solid var(--border-soft);
  background: var(--bg-elevated);
  color: var(--text);
  transition: border-color 0.15s, background 0.15s, color 0.15s, opacity 0.15s;
}
.btn:hover { border-color: var(--accent); color: var(--accent-dim); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #0b0b0f; font-weight: 600; }
.btn.primary:hover { background: var(--accent-dim); color: #0b0b0f; }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn.small { padding: 3px 10px; font-size: 0.8rem; }

/* ---------- views ---------- */
.view { overflow: auto; flex: 1 1 auto; }

/* ---------- library ---------- */
.papers { max-width: 52rem; margin: 0 auto; padding: 20px 22px 40px; }

.paper-row {
  padding: 14px 16px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}
.paper-row:hover { background: var(--bg-panel); }
.paper-row.selected { background: var(--accent-bg); border-color: rgba(201, 201, 211, 0.3); }

.paper-title { font-weight: 600; font-size: 1.02rem; margin-bottom: 4px; }
.paper-sub { color: var(--text-dim); font-size: 0.85rem; display: flex; gap: 10px; flex-wrap: wrap; }
.paper-sub .arxiv { color: var(--accent-dim); }

.empty {
  max-width: 40rem;
  margin: 12vh auto 0;
  text-align: center;
  color: var(--text-dim);
  padding: 30px;
}
.empty .pulse {
  width: 26px; height: 26px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin: 0 auto 18px;
  animation: spin 1s linear infinite;
}
.empty .eyes { animation: pulse-soft 1.6s ease-in-out infinite; }
@keyframes pulse-soft { 0%,100% { opacity: 1; } 50% { opacity: 0.35; } }
.empty .title { color: var(--text); font-weight: 600; margin-bottom: 8px; }
.empty p { margin: 6px 0; font-size: 0.95rem; }

/* ---------- reader ---------- */
.reader-view { display: flex; flex-direction: column; }

.reader-topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
}
.reader-meta { display: flex; align-items: center; gap: 14px; font-size: 0.85rem; color: var(--text-dim); }
.reader-actions { display: flex; align-items: center; gap: 8px; }
.read-status { padding: 3px 10px; border-radius: 999px; font-size: 0.75rem; border: 1px solid var(--border-soft); }
.read-status.read { color: var(--green); border-color: rgba(76, 217, 123, 0.4); }
.read-status.unread { color: var(--text-dim); }

.reader-body { flex: 1 1 auto; overflow: auto; }

.block {
  max-width: 48rem;
  margin: 0 auto;
  padding: 40px 24px 200px;
  opacity: 1;
  transform: translateY(0);
  transition: opacity 0.22s ease, transform 0.22s ease;
}
.block.fade-out { opacity: 0; transform: translateY(8px); }
.block.fade-in { opacity: 0; transform: translateY(-8px); }

/* heading */
.heading {
  display: block;
  border-left: 3px solid var(--accent);
  padding-left: 14px;
  margin: 26px 0 18px;
  color: var(--text);
  font-size: 1.3rem;
  font-weight: 650;
}
.heading .label { color: var(--text-faint); font-size: 0.8rem; font-weight: 500; display: block; margin-bottom: 4px; letter-spacing: 0.03em; }

/* title block */
.title-block .paper-title {
  font-size: 1.9rem;
  line-height: 1.25;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin-bottom: 14px;
}
.authors { color: var(--text-dim); font-size: 1rem; margin-bottom: 10px; }
.affiliations {
  color: var(--text-faint);
  font-size: 0.9rem;
  margin: 0 0 18px;
  line-height: 1.5;
}
.affiliations .affiliation-list {
  list-style: none;
  padding: 0;
  margin: 0;
}
.affiliations .affiliation-list li {
  margin: 2px 0;
}
.affiliations .affiliation-list li::before {
  content: "";
}
.abstract {
  color: var(--text);
  font-family: var(--font-read);
  font-size: 1.05rem;
  line-height: 1.65;
  border-left: 3px solid var(--border-soft);
  padding-left: 16px;
  margin-bottom: 22px;
}
.abstract .lbl { color: var(--text-faint); font-size: 0.75rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; display: block; margin-bottom: 6px; }
.arxiv-link {
  color: var(--accent-dim);
  text-decoration: none;
  border: 1px solid var(--border-soft);
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 0.85rem;
  display: inline-block;
}
.arxiv-link:hover { border-color: var(--accent); background: var(--accent-bg); }

/* text block (markdown) */
.md {
  font-family: var(--font-read);
  font-size: 1.1rem;
  line-height: 1.7;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 0.85em; }
.md h1, .md h2, .md h3, .md h4 {
  color: var(--text);
  font-family: var(--font-ui);
  line-height: 1.3;
  margin: 1.1em 0 0.45em;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.md h1 { font-size: 1.35em; }
.md h2 { font-size: 1.2em; }
.md h3 { font-size: 1.08em; }
.md h4 { font-size: 1em; color: var(--text-dim); }
.md ul, .md ol { padding-left: 1.4em; margin: 0 0 0.85em; }
.md li { margin: 0.25em 0; }
.md li > p { margin: 0.25em 0; }
.md li > ul, .md li > ol { margin-bottom: 0.25em; }
.md hr {
  border: none;
  border-top: 1px solid var(--border-soft);
  margin: 1em 0;
}
.md code {
  background: var(--bg-elevated);
  border: 1px solid var(--border-soft);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.9em;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
}
.md pre {
  background: var(--bg-elevated);
  border: 1px solid var(--border-soft);
  padding: 14px;
  border-radius: var(--radius);
  overflow: auto;
  margin: 0 0 0.85em;
}
.md pre code { background: transparent; border: none; padding: 0; }
.md blockquote {
  border-left: 3px solid var(--border-soft);
  margin: 0 0 0.85em;
  padding: 0.15em 0 0.15em 14px;
  color: var(--text-dim);
}
.md a { color: var(--accent-dim); }
.md a:hover { color: var(--accent); }
.md table {
  border-collapse: collapse;
  width: 100%;
  margin: 0 0 0.85em;
  font-size: 0.95em;
  display: block;
  overflow-x: auto;
}
.md th, .md td {
  border: 1px solid var(--border-soft);
  padding: 6px 10px;
  text-align: left;
}
.md th { background: var(--bg-elevated); font-weight: 600; }
.md del {
  /* Real ~~strikethrough~~ only; keep subtle so it never looks like a bug. */
  text-decoration: line-through;
  color: var(--text-dim);
  text-decoration-thickness: 1px;
}

/* Chat answers: slightly tighter type so long explanations scan better */
.chat-msg.assistant .bubble .md {
  font-size: 0.98rem;
  line-height: 1.65;
}
.chat-msg.assistant .bubble .md h1,
.chat-msg.assistant .bubble .md h2,
.chat-msg.assistant .bubble .md h3 {
  margin-top: 0.9em;
}
.chat-msg.assistant .bubble .md p { margin-bottom: 0.7em; }
.chat-msg.assistant .bubble .md ul,
.chat-msg.assistant .bubble .md ol { margin-bottom: 0.7em; }

/* equation block */
.equation {
  margin: 34px 0;
  padding: 20px 0;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  text-align: center;
  font-size: 1.1rem;
  overflow-x: auto;
}
.equation .katex-display { margin: 6px 0; }

/* figure block */
.figure {
  margin: 26px 0;
  text-align: center;
}
.figure img,
.md img {
  max-width: 100%;
  max-height: 62vh;
  object-fit: contain;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  cursor: zoom-in;
}
.figure .fig-pdf {
  display: block;
  width: 100%;
  height: 60vh;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  background: var(--bg-elevated);
}
.figure .fig-caption { color: var(--text-dim); font-size: 0.9rem; margin-top: 12px; }
.figure .fig-fallback {
  border: 1px dashed var(--border-soft);
  border-radius: var(--radius);
  padding: 40px;
  color: var(--text-faint);
}

.text-section-label {
  color: var(--text-faint);
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin: 10px 0 4px;
}

/* ---------- prompt & chat ---------- */
.prompt-zone {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  background: var(--bg-panel);
  padding-bottom: 14px;
}

.chat-pane {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  height: 40vh;
  max-width: 52rem;
  margin: 0 auto;
  padding: 0 22px;
}

.chat-resize {
  flex: 0 0 auto;
  height: 12px;
  margin: 0 -22px;
  cursor: row-resize;
  position: relative;
  touch-action: none;
}
.chat-resize::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 6px;
  transform: translateX(-50%);
  width: 44px;
  height: 3px;
  border-radius: 999px;
  background: var(--border-soft);
}
.chat-resize:hover::after, .chat-resize.dragging::after { background: var(--accent); }

.chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 0 8px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 12px;
}
.chat-title { font-weight: 600; font-size: 0.9rem; }

.chat-messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding-bottom: 12px;
}
.chat-msg { display: flex; gap: 12px; }
.chat-msg .who { flex: 0 0 auto; width: 26px; height: 26px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 0.85rem; font-weight: 700; }
.chat-msg.user .who { background: var(--accent-bg); color: var(--accent-dim); border: 1px solid rgba(201,201,211,0.4); }
.chat-msg.assistant .who { background: var(--bg-elevated); color: var(--text-dim); border: 1px solid var(--border-soft); }
.chat-msg .bubble { flex: 1 1 auto; max-width: 100%; }
.chat-msg.assistant .bubble .md { font-size: 1rem; }
.chat-msg.error .who { color: var(--red); }
.cursor-blink {
  display: inline-block;
  width: 8px; height: 15px;
  background: var(--accent);
  vertical-align: text-bottom;
  animation: blink 1s step-start infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.chat-thinking {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text-faint);
  font-style: italic;
  font-size: 0.95rem;
  user-select: none;
}
.chat-thinking .thinking-label { letter-spacing: 0.01em; }
.chat-thinking .thinking-timer {
  font-style: normal;
  font-variant-numeric: tabular-nums;
  color: var(--text-faint);
  opacity: 0.75;
  font-size: 0.85rem;
  min-width: 1.5em;
}
.chat-thinking .thinking-dots {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 1em;
}
.chat-thinking .thinking-dots span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--text-faint);
  opacity: 0.35;
  animation: thinking-bounce 1.2s ease-in-out infinite;
}
.chat-thinking .thinking-dots span:nth-child(2) { animation-delay: 0.15s; }
.chat-thinking .thinking-dots span:nth-child(3) { animation-delay: 0.3s; }
.chat-thinking.is-reasoning .thinking-dots span {
  background: var(--accent-dim);
}
@keyframes thinking-bounce {
  0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

.prompt-bar {
  max-width: 52rem;
  margin: 10px auto 0;
  padding: 0 22px;
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-soft);
  border-radius: 14px;
  padding: 9px 16px;
  box-shadow: var(--shadow);
  transition: border-color 0.15s;
}
.prompt-bar:focus-within { border-color: var(--accent); }
.prompt-glyph { color: var(--accent-dim); font-weight: 700; font-size: 1rem; }
#prompt-input {
  flex: 1 1 auto;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font: inherit;
  font-size: 1rem;
}
#prompt-input::placeholder { color: var(--text-faint); }

/* ---------- cheatsheet ---------- */
.cheatsheet {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.cheatsheet-card {
  width: 860px;
  max-width: 94vw;
  max-height: 90vh;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.cheatsheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  flex: 0 0 auto;
}
.cheatsheet-head kbd {
  margin-left: 4px;
  font-weight: 500;
}
.cheatsheet-body {
  display: flex;
  gap: 28px;
  padding: 20px 22px 8px;
  overflow: auto;
  flex: 1 1 auto;
}
@media (max-width: 720px) {
  .cheatsheet-body { flex-direction: column; gap: 8px; }
}
.cs-col { flex: 1 1 0; min-width: 0; }
.cs-col h4 {
  margin: 0 0 8px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-faint);
}
.cs-col ul { list-style: none; padding: 0; margin: 0 0 18px; }
.cs-col li {
  margin: 7px 0;
  font-size: 0.88rem;
  color: var(--text-dim);
  line-height: 1.45;
}
kbd {
  display: inline-block;
  background: var(--bg-elevated);
  border: 1px solid var(--border-soft);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 6px;
  font-family: inherit;
  font-size: 0.78rem;
  color: var(--text);
  white-space: nowrap;
}

/* ---------- lightbox (figure viewer) ---------- */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.88);
  display: flex;
  flex-direction: column;
}
.lightbox-toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  color: #eee;
}
.lightbox-toolbar .lb-title { font-weight: 600; }
.lightbox-toolbar .lb-spacer { flex: 1 1 auto; }
.lb-zoom { font-size: 0.85rem; color: #bbb; min-width: 44px; text-align: right; }
.lightbox-toolbar .btn {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.18);
  color: #eee;
}
.lightbox-toolbar .btn:hover { border-color: #fff; color: #fff; }
.lightbox-stage {
  flex: 1 1 auto;
  position: relative;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
}
.lightbox-stage.grabbing { cursor: grabbing; }
.lightbox-stage .lb-hint {
  position: absolute;
  left: 50%;
  top: 14px;
  transform: translateX(-50%);
  color: rgba(255, 255, 255, 0.55);
  font-size: 0.85rem;
  pointer-events: none;
  text-align: center;
}
#lb-img {
  position: absolute;
  left: 50%;
  top: 50%;
  max-width: none;
  max-height: none;
  transform: translate(-50%, -50%);
  transform-origin: center center;
  border-radius: var(--radius);
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.6);
  user-select: none;
  will-change: transform;
}
.lightbox-caption {
  flex: 0 0 auto;
  padding: 10px 16px;
  color: #ccc;
  font-size: 0.9rem;
  text-align: center;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border-soft); border-radius: 6px; }
::-webkit-scrollbar-track { background: transparent; }

[hidden] { display: none !important; }

@media (max-width: 640px) {
  .toolbar { flex-wrap: wrap; gap: 10px; }
  .block { padding: 20px 16px 140px; }
}