:root {
  --bg: #f4f1ea;
  --card-bg: #ffffff;
  --text: #2b2620;
  --muted: #8a8378;
  --accent: #b8552f;
  --like: #3d9152;
  --superlike: #3273c4;
  --discard: #c0392b;
  --shadow: 0 8px 30px rgba(40, 30, 20, 0.18);
  --panel-bg: #fbf9f5;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171512;
    --card-bg: #262320;
    --text: #e8e2d8;
    --muted: #8f887c;
    --accent: #d97a51;
    --like: #5bb974;
    --superlike: #6aa3e8;
    --discard: #e06455;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
    --panel-bg: #1e1b18;
  }
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: Charter, Georgia, "Times New Roman", serif;
  display: flex;
  flex-direction: column;
  overscroll-behavior: none;
}

body > header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  /* Standalone on iOS draws under the status bar and home indicator. */
  padding: calc(0.7rem + env(safe-area-inset-top)) 1.1rem 0.7rem;
  flex: 0 0 auto;
}

body > header h1 {
  font-size: 1.05rem;
  margin: 0;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
}

#discarded-toggle {
  background: none;
  border: 1px solid var(--muted);
  color: var(--muted);
  border-radius: 999px;
  padding: 0.3rem 0.9rem;
  font-size: 0.8rem;
  cursor: pointer;
  font-family: inherit;
}
#discarded-toggle:hover {
  color: var(--text);
  border-color: var(--text);
}

/* --- Login --- */

body.login-page {
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.login {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  width: min(320px, 100%);
  background: var(--card-bg);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 2rem 1.6rem;
  text-align: center;
}

.login-icon {
  font-size: 2.6rem;
}
.login h1 {
  margin: 0 0 0.4rem;
  font-size: 1.1rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.login input[type="password"] {
  font: inherit;
  font-size: 1rem;
  padding: 0.7rem 0.9rem;
  border: 1px solid var(--muted);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
}
.login input[type="password"]:focus {
  outline: none;
  border-color: var(--accent);
}

.login button {
  font: inherit;
  font-size: 1rem;
  padding: 0.7rem;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}

.login-error {
  margin: 0;
  color: var(--discard);
  font-size: 0.85rem;
}

/* --- Card stack --- */

#stack-area {
  flex: 1 1 auto;
  position: relative;
  min-height: 0;
}

#stack {
  position: absolute;
  inset: 0.5rem 1rem 1rem;
  max-width: 480px;
  margin: 0 auto;
  touch-action: pan-y;
}

.card {
  position: absolute;
  inset: 0;
  background: var(--card-bg);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 1.4rem 1.5rem 1.2rem;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  will-change: transform;
  /* Text is selectable; selection is suppressed only while a swipe is underway. */
  user-select: text;
  -webkit-user-select: text;
  -webkit-user-drag: none;
  /*
   * Horizontal drag is the swipe, so the browser must not claim it: without
   * this it treats a sideways drag as its own gesture and fires pointercancel
   * mid-swipe. Vertical is left to the card body's scroller.
   */
  touch-action: pan-y;
}

.card.top {
  cursor: grab;
}
.card.top:active {
  cursor: grabbing;
}
/* An I-beam over the text is the hint that it can be selected. */
.card.top .card-body {
  cursor: auto;
}

/*
 * Once a drag has locked horizontal it is a swipe, not a selection: dragging
 * sideways through a paragraph must not paint it blue on the way out.
 */
.card.swiping,
.card.swiping .card-body {
  user-select: none;
  -webkit-user-select: none;
  cursor: grabbing;
}
.card.spring-back {
  transition: transform 0.3s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}
.card.fly-out {
  transition: transform 0.35s ease-in, opacity 0.35s ease-in;
  opacity: 0;
}
.card.behind {
  transition: transform 0.25s ease;
}

.card-head {
  flex: 0 0 auto;
  margin-bottom: 0.9rem;
  /* Room for the list icon in the corner, so a long title never runs under it. */
  padding-right: 2.2rem;
}

.card-book {
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
  font-weight: 600;
}

/* The title opens the note in Obsidian. Dotted underline rather than the browser default,
   to match the wikilinks in the body – and a hit area big enough for a thumb. */
.book-link {
  color: inherit;
  text-decoration: none;
  border-bottom: 1px dotted currentColor;
  padding-bottom: 2px;
}

.card-author {
  font-size: 0.82rem;
  color: var(--muted);
  font-style: italic;
  margin-top: 0.15rem;
}

.card-weight {
  color: var(--muted);
  font-weight: 400;
  letter-spacing: normal;
  text-transform: none;
  float: right;
}

.card-body {
  flex: 1 1 auto;
  overflow-y: auto;
  /*
   * Nothing may ever overflow sideways: a horizontal scroller inside the card
   * swallows the horizontal drag and the card can no longer be swiped. Long
   * unbroken tokens (image URLs, links) wrap rather than widen the box.
   */
  overflow-x: hidden;
  overflow-wrap: anywhere;
  overscroll-behavior: contain;
  /* Vertical drag scrolls this pane; horizontal drag is the swipe. */
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  font-size: 1.15rem;
  line-height: 1.55;
  padding-bottom: 0.5rem;
}

/* Hairline fade hinting that a long card continues below. */
.card-body::after {
  content: "";
  display: block;
  height: 0.25rem;
}

.card-body img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0.6rem 0;
  border-radius: 6px;
}

/*
 * The captions the item hangs under. Quieter and smaller than the item itself: they are
 * there so the item reads as what it is, not to compete with it for attention.
 */
.card-lead {
  font-size: 0.92rem;
  line-height: 1.4;
  color: var(--muted);
  margin-bottom: 0.5rem;
  padding-left: 0.7rem;
  border-left: 2px solid rgba(128, 128, 128, 0.28);
}
.card-lead + .card-text {
  margin-top: 0.8rem;
}

/*
 * My own note on a highlight. Set apart from the highlight rather than merely under it: the
 * card shows two voices, and it should be obvious which one is the author's.
 */
.card-note {
  margin-top: 0.9rem;
  padding-top: 0.7rem;
  border-top: 1px dashed rgba(128, 128, 128, 0.3);
  font-size: 0.95rem;
  line-height: 1.5;
  color: var(--muted);
}

/* The corner icon: this item is one of a list, and the list is a tap away. */
.card-list {
  position: absolute;
  top: 0.9rem;
  right: 0.9rem;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Small mark, thumb-sized target. */
  width: 34px;
  height: 34px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: none;
  color: var(--muted);
  opacity: 0.55;
  cursor: pointer;
}
.card-list:hover {
  opacity: 1;
  background: rgba(128, 128, 128, 0.12);
}
.card-list svg {
  fill: currentColor;
}

.wikilink {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px dotted var(--accent);
}

.tag {
  color: var(--muted);
  font-size: 0.85em;
}

/* --- Swipe stamps --- */

.stamp {
  position: absolute;
  top: 2.2rem;
  padding: 0.3rem 0.9rem;
  border: 3px solid;
  border-radius: 8px;
  font-family: system-ui, sans-serif;
  font-weight: 800;
  font-size: 1.5rem;
  letter-spacing: 0.1em;
  opacity: 0;
  pointer-events: none;
}
.stamp-right {
  left: 1.5rem;
  color: var(--like);
  border-color: var(--like);
  transform: rotate(-14deg);
}
.stamp-left {
  right: 1.5rem;
  color: var(--discard);
  border-color: var(--discard);
  transform: rotate(14deg);
}

/* --- Empty state --- */

#empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 2rem;
}
#empty-state p {
  font-size: 1.2rem;
  margin: 0.3rem;
}
.hint {
  color: var(--muted);
  font-size: 0.9rem !important;
}

/* --- Action buttons --- */

#actions {
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
  gap: 1.3rem;
  padding: 0.9rem 0;
}

.action {
  width: 58px;
  height: 58px;
  border-radius: 50%;
  border: none;
  background: var(--card-bg);
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.18);
  font-size: 1.5rem;
  cursor: pointer;
  transition: transform 0.12s ease;
  color: var(--text);
}
.action:hover {
  transform: scale(1.1);
}
.action:active {
  transform: scale(0.95);
}
.action.superlike {
  color: var(--superlike);
}
.action.pass {
  color: var(--muted);
  font-size: 1.25rem;
  width: 48px;
  height: 48px;
  align-self: center;
}
.action.undo {
  color: var(--muted);
  font-size: 1.3rem;
  width: 48px;
  height: 48px;
  align-self: center;
}

.action.lock {
  font-size: 1.25rem;
  width: 48px;
  height: 48px;
  align-self: center;
  filter: grayscale(1);
  opacity: 0.55;
}
.action.lock:hover {
  opacity: 0.9;
}
.action.lock.active {
  filter: none;
  opacity: 1;
  box-shadow: 0 0 0 2px var(--accent), 0 3px 12px rgba(0, 0, 0, 0.18);
}

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

footer {
  flex: 0 0 auto;
  text-align: center;
  color: var(--muted);
  font-size: 0.78rem;
  /* Clears the iPhone home indicator when running from the home screen. */
  padding: 0 0 calc(0.7rem + env(safe-area-inset-bottom));
  font-family: system-ui, sans-serif;
  transition: color 0.2s ease;
}

footer.flash {
  color: var(--accent);
  font-weight: 600;
}

/* --- Discarded panel --- */

#discarded-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(420px, 100vw);
  background: var(--panel-bg);
  box-shadow: -6px 0 24px rgba(0, 0, 0, 0.25);
  z-index: 50;
  display: flex;
  flex-direction: column;
  padding: 1rem 1.2rem;
  overflow: hidden;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex: 0 0 auto;
}
.panel-header h2 {
  font-size: 1rem;
  margin: 0;
}
.panel-header button {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1.1rem;
  cursor: pointer;
}

#discarded-list {
  list-style: none;
  margin: 0.8rem 0 0;
  padding: 0;
  overflow-y: auto;
  flex: 1 1 auto;
}

#discarded-list li {
  border-bottom: 1px solid rgba(128, 128, 128, 0.2);
  padding: 0.7rem 0;
  display: flex;
  gap: 0.8rem;
  align-items: flex-start;
}

.discarded-text {
  flex: 1 1 auto;
  font-size: 0.88rem;
  line-height: 1.4;
}
.discarded-book {
  display: block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  margin-bottom: 0.2rem;
}

.restore-btn {
  flex: 0 0 auto;
  background: none;
  border: 1px solid var(--like);
  color: var(--like);
  border-radius: 999px;
  padding: 0.25rem 0.7rem;
  font-size: 0.75rem;
  cursor: pointer;
}

/* --- The list a card came out of --- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(1rem + env(safe-area-inset-top)) 1rem calc(1rem + env(safe-area-inset-bottom));
  background: rgba(20, 16, 12, 0.55);
  backdrop-filter: blur(2px);
}

.modal-sheet {
  display: flex;
  flex-direction: column;
  width: min(520px, 100%);
  max-height: 100%;
  background: var(--panel-bg);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 1rem 1.2rem 1.2rem;
  overflow: hidden;
}

.modal-sheet .panel-header h2 {
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}

.list-items {
  list-style: none;
  margin: 0.8rem 0 0;
  padding: 0;
  overflow-y: auto;
  overflow-wrap: anywhere;
  flex: 1 1 auto;
  font-size: 0.95rem;
  line-height: 1.5;
}

.list-items li {
  position: relative;
  padding: 0.35rem 0.5rem 0.35rem 0.9rem;
  border-radius: 6px;
}
/* A dash for every item, indented by its depth in the note. */
.list-items li::before {
  content: "–";
  position: absolute;
  left: 0;
  color: var(--muted);
}
.list-items li[data-depth="1"] {
  margin-left: 1rem;
}
.list-items li[data-depth="2"] {
  margin-left: 2rem;
}
.list-items li[data-depth="3"] {
  margin-left: 3rem;
}
.list-items li[data-depth="4"] {
  margin-left: 4rem;
}

/* The item you are holding. */
.list-items li.current {
  background: rgba(184, 85, 47, 0.12);
  box-shadow: inset 3px 0 0 var(--accent);
  font-weight: 600;
}

.list-items img {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: 6px;
}

@media (max-width: 600px) {
  #stack {
    inset: 0.3rem 0.7rem 0.7rem;
  }
  .card {
    padding: 1.1rem 1.2rem 1rem;
  }
  .card-body {
    font-size: 1.05rem;
  }
  .action {
    width: 52px;
    height: 52px;
  }
}
