:root {
  color-scheme: light dark;
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-2: #f0eefc;
  --text: #1c1c1e;
  --muted: #6b6b70;
  --border: #e2e2e5;
  --accent: #7c3aed;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --spine: #c9c5db;
  --marker: #7c3aed;
  --container-fill: #ede9fe;
  --container-border: #7c3aed;
  --claude-badge: #d97706;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121214;
    --surface: #1c1c1e;
    --surface-2: #241f36;
    --text: #f2f2f3;
    --muted: #9a9a9e;
    --border: #303033;
    --accent: #a78bfa;
    --accent-text: #1c1c1e;
    --spine: #423b5c;
    --marker: #a78bfa;
    --container-fill: #2c2447;
    --container-border: #a78bfa;
    --claude-badge: #f59e0b;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; height: 100%; }
body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
a, button, summary { touch-action: manipulation; }
h1, h2 { margin: 0; }
button {
  font: inherit;
  border-radius: 0.6rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  min-height: 2.6rem;
  padding: 0.4rem 0.9rem;
  cursor: pointer;
}
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
button.danger { color: var(--danger); border-color: var(--danger); }
input, textarea, select {
  font: inherit;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 0.6rem;
  padding: 0.55rem 0.7rem;
  min-height: 2.6rem;
}
textarea { min-height: 4.5rem; resize: vertical; }

/* Login / settings (server-rendered pages) */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.login-card, .settings-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 1.5rem;
  width: 100%;
  max-width: 360px;
}
.login-card h1 { font-size: 1.6rem; margin-bottom: 0.25rem; }
.login-card form, .settings-card form { display: flex; flex-direction: column; gap: 0.75rem; margin-top: 1rem; }
.login-card input { text-align: center; font-size: 1.4rem; letter-spacing: 0.3rem; }
.error { color: var(--danger); margin: 0; }
.success { color: #16a34a; margin: 0; }
.topbar {
  padding: max(1rem, env(safe-area-inset-top)) 1rem 0.5rem;
  max-width: 720px;
  margin: 0 auto;
}
.title-row { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; margin-bottom: 0.5rem; }
.settings-link {
  display: inline-flex; align-items: center; justify-content: center;
  width: 2.75rem; height: 2.75rem; color: var(--muted); text-decoration: none;
  font-size: 1.3rem; line-height: 1; opacity: 0.7; flex-shrink: 0;
}
.settings-link:hover { opacity: 1; }
.settings-main { max-width: 480px; margin: 0 auto; padding: 0 1rem 3rem; display: flex; flex-direction: column; gap: 1rem; }
.settings-back { color: var(--accent); text-decoration: none; font-weight: 500; }
.form-row { display: flex; flex-direction: column; gap: 0.5rem; }
.form-row-symmetric { flex-direction: row; gap: 0.75rem; }
.form-row-symmetric .field { flex: 1; }
.field { display: flex; flex-direction: column; gap: 0.3rem; }
.field-label { font-size: 0.85rem; color: var(--muted); }

/* App shell */
.app-root { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.app-topbar { max-width: none; width: 100%; padding: max(0.75rem, env(safe-area-inset-top)) 1rem 0.5rem; flex-shrink: 0; }
.breadcrumb { display: flex; flex-wrap: wrap; align-items: center; gap: 0.25rem; font-size: 1rem; min-height: 2.2rem; }
.breadcrumb button {
  border: none; background: none; padding: 0.15rem 0.3rem; min-height: auto;
  color: var(--accent); font-weight: 600; cursor: pointer;
}
.breadcrumb button:disabled { color: var(--text); font-weight: 700; cursor: default; }
.breadcrumb .sep { color: var(--muted); }

.search-row { display: flex; gap: 0.5rem; }
#search-input { flex: 1; }
.search-results {
  margin-top: 0.6rem; display: flex; flex-direction: column; gap: 0.5rem;
  max-height: 40vh; overflow-y: auto; background: var(--surface); border: 1px solid var(--border);
  border-radius: 0.75rem; padding: 0.6rem;
}
.search-result {
  border: 1px solid var(--border); border-radius: 0.6rem; padding: 0.6rem 0.7rem;
  display: flex; justify-content: space-between; align-items: flex-start; gap: 0.6rem;
}
.search-result .sr-body { flex: 1; min-width: 0; }
.search-result .sr-title { font-weight: 600; }
.search-result .sr-date { color: var(--muted); font-size: 0.85rem; }
.search-result .sr-desc { font-size: 0.9rem; margin-top: 0.15rem; }
.search-result.added { opacity: 0.55; }
.search-status { color: var(--muted); font-size: 0.9rem; padding: 0.3rem; }

.timeline-main { flex: 1; display: flex; flex-direction: column; min-height: 0; position: relative; }
.timeline-toolbar {
  display: flex; gap: 0.5rem; padding: 0.5rem 1rem; align-items: center; flex-shrink: 0;
  max-width: 720px; width: 100%; margin: 0 auto; box-sizing: border-box;
}
.timeline-toolbar .primary { margin-left: auto; }
#zoom-in, #zoom-out { width: 2.6rem; font-size: 1.2rem; font-weight: 700; padding: 0; }

.timeline-scroll {
  flex: 1; overflow-x: auto; overflow-y: hidden; position: relative;
  -webkit-overflow-scrolling: touch; cursor: grab;
}
.timeline-scroll.dragging { cursor: grabbing; user-select: none; }
.timeline-track {
  position: relative; height: 100%; min-width: 100%;
}
.timeline-spine {
  position: absolute; left: 0; right: 0; top: 50%; height: 3px;
  background: var(--spine); transform: translateY(-1.5px);
}

.tl-marker {
  position: absolute; top: 50%; transform: translate(-50%, -50%);
  display: flex; flex-direction: column; align-items: center; cursor: pointer; z-index: 2;
}
.tl-dot {
  width: 24px; height: 24px; border-radius: 50%; background: var(--marker);
  border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--marker);
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; line-height: 1;
}
.tl-marker.has-children .tl-dot { width: 28px; height: 28px; font-size: 14px; background: var(--container-border); }
.tl-label {
  position: absolute; white-space: nowrap; font-size: 0.8rem; font-weight: 600;
  background: var(--surface); padding: 0.1rem 0.4rem; border-radius: 0.4rem; border: 1px solid var(--border);
  max-width: 40vw; overflow: hidden; text-overflow: ellipsis; transform: translateX(-50%);
  cursor: pointer; z-index: 2;
}
.tl-label.above { bottom: calc(50% + 14px); }
.tl-label.below { top: calc(50% + 14px); }
.tl-dot.claude-dot { position: relative; }
.tl-dot.claude-dot::after { content: "✦"; font-size: 0.55rem; color: var(--claude-badge); position: absolute; top: -7px; right: -7px; }

.tl-range {
  position: absolute; top: 50%; height: 26px; transform: translateY(-50%);
  background: var(--container-fill); border: 2px solid var(--container-border); border-radius: 999px;
  cursor: pointer; display: flex; align-items: center; z-index: 1; min-width: 12px;
}
.tl-range-icon { font-size: 0.85rem; margin-left: 0.5rem; flex-shrink: 0; line-height: 1; }
.tl-range .tl-range-label {
  font-size: 0.78rem; font-weight: 600; padding: 0 0.6rem; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; color: var(--text); min-width: 0;
}
.tl-range.claude-dot { position: relative; }
.tl-range.claude-dot::after { content: "✦"; font-size: 0.6rem; color: var(--claude-badge); position: absolute; top: -8px; right: -6px; }
.tl-range.point-only { display: none; }

.tl-year-ticks { position: absolute; inset: 0; pointer-events: none; }
.tl-tick { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--border); opacity: 0.6; }
.tl-tick-label {
  position: absolute; top: 6px; font-size: 0.72rem; color: var(--muted); transform: translateX(-50%);
}

.empty-hint { text-align: center; color: var(--muted); margin: 2rem 1rem; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0); display: flex;
  align-items: flex-end; justify-content: center; z-index: 10; padding: 0;
  transition: background-color 200ms ease;
}
.modal-backdrop.visible { background: rgba(0,0,0,0.45); }
@media (min-width: 640px) {
  .modal-backdrop { align-items: center; padding: 1rem; }
}
.modal-card {
  background: var(--surface); border-radius: 1rem 1rem 0 0; padding: 1.25rem;
  width: 100%; max-width: 480px; max-height: 90vh; overflow-y: auto;
  padding-bottom: max(1.25rem, env(safe-area-inset-bottom));
  transition: transform 260ms cubic-bezier(0.22, 1, 0.36, 1), opacity 200ms ease, max-width 260ms cubic-bezier(0.22, 1, 0.36, 1);
}
.modal-card.expanded { max-width: 640px; }
@media (min-width: 640px) {
  .modal-card { border-radius: 1rem; }
}
.link-btn {
  background: none; border: none; color: var(--accent); font-weight: 600;
  padding: 0.2rem 0; min-height: auto; align-self: flex-start; cursor: pointer;
}
.modal-card h2 { margin-bottom: 0.75rem; }
.modal-card form { display: flex; flex-direction: column; gap: 0.75rem; }
.date-fields { display: flex; gap: 0.5rem; }
.date-fields input { width: 0; flex: 1; }
.date-group-label { font-size: 0.85rem; color: var(--muted); margin-bottom: -0.3rem; }
.modal-actions { display: flex; gap: 0.5rem; justify-content: flex-end; flex-wrap: wrap; margin-top: 0.25rem; }
.modal-actions .spacer { flex: 1; }
.detail-desc { color: var(--text); white-space: pre-wrap; }
.detail-date { color: var(--muted); margin-top: -0.4rem; }
.detail-source { font-size: 0.8rem; color: var(--claude-badge); }
.detail-facts { background: var(--surface-2); border-radius: 0.6rem; padding: 0.6rem 0.9rem; }
.detail-facts h3 { font-size: 0.85rem; color: var(--muted); margin: 0 0 0.4rem; text-transform: uppercase; letter-spacing: 0.03em; }
.detail-facts ul { margin: 0; padding-left: 1.1rem; display: flex; flex-direction: column; gap: 0.3rem; }
.detail-facts li { font-size: 0.92rem; }
