/* Self-hosted fonts (latin subset; Thai glyphs fall back to the system font). Licence: fonts/OFL.txt */
@font-face {
  font-family: 'Fraunces';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url(../fonts/fraunces-latin-var.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: 'IBM Plex Sans';
  font-style: normal;
  font-weight: 400 600;
  font-display: swap;
  src: url(../fonts/ibm-plex-sans-latin-var.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: 'IBM Plex Mono';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url(../fonts/ibm-plex-mono-latin-400.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

:root {
  --bg: #14161f;
  --bg-elevated: #1c1f2b;
  --bg-card: #1f2230;
  --text: #f0ede4;
  --text-dim: #9a9db0;
  --text-faint: #6b6e80;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.14);

  --accent: #d9a441;       /* amber — primary action / stale marker */
  --accent-ink: #1a1608;
  --teal: #4fb0a5;         /* watching */
  --green: #7ab88a;        /* completed */
  --rust: #d9634a;         /* dropped */
  --violet: #8b7fd6;       /* movie tag */
  --gray: #6b6e80;         /* plan-to */

  --radius: 14px;
  --radius-sm: 8px;

  font-size: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'IBM Plex Sans', system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100dvh;
  padding-bottom: 48px;
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* ---------- Topbar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(20, 22, 31, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  padding: env(safe-area-inset-top) 0 0 0;
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px 10px;
}

.wordmark {
  font-family: 'Fraunces', serif;
  font-weight: 700;
  font-size: 1.6rem;
  letter-spacing: -0.01em;
  margin: 0;
}
.wordmark-dot { color: var(--accent); }

.btn-add {
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: 999px;
  padding: 9px 16px;
  font-weight: 600;
  font-size: 0.9rem;
  font-family: inherit;
  cursor: pointer;
}
.btn-add:active { transform: scale(0.97); }

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn-outline {
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
  border-radius: var(--radius-sm);
  width: 38px;
  height: 38px;
  font-size: 1.1rem;
  cursor: pointer;
}

.view-tabs {
  display: flex;
  gap: 8px;
  padding: 0 18px 10px;
}
.view-tab {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-family: 'Fraunces', serif;
  font-size: 1rem;
  font-weight: 600;
  padding: 4px 2px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
}
.view-tab.is-active {
  color: var(--text);
  border-bottom-color: var(--accent);
}

.filter-row {
  display: flex;
  align-items: center;
  padding: 0 18px 10px;
  overflow-x: auto;
  scrollbar-width: none;
}
.filter-row::-webkit-scrollbar { display: none; }
.filter-row[hidden] { display: none; }

.sort-row {
  display: flex;
  justify-content: flex-end;
  padding: 0 18px 14px;
}
.sort-row[hidden] { display: none; }

.chip-group {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
}
.chip-group::-webkit-scrollbar { display: none; }

.chip {
  flex: none;
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 6px 13px;
  font-size: 0.82rem;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
}
.chip.is-active {
  background: var(--text);
  color: var(--bg);
  border-color: var(--text);
  font-weight: 500;
}

.sort-select {
  flex: none;
  width: auto; /* beat the generic `select { width: 100% }` */
  background: var(--bg-elevated);
  color: var(--text-dim);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  font-size: 0.78rem;
  font-family: inherit;
}

/* ---------- Home: new-chapter check ---------- */

.home-toolbar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 0 18px 14px;
}
.home-toolbar[hidden] { display: none; }

.home-toolbar-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.failed-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.failed-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: rgba(217, 99, 74, 0.1);
  border: 1px solid rgba(217, 99, 74, 0.35);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
}
.failed-title {
  min-width: 0;
  font-size: 0.8rem;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.failed-link-btn {
  flex: none;
  font-size: 0.76rem;
  color: var(--rust);
  background: none;
  border: 1px solid var(--rust);
  border-radius: var(--radius-sm);
  padding: 3px 8px;
  text-decoration: none;
  white-space: nowrap;
}

.btn-check {
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 14px;
  font-size: 0.85rem;
  font-family: inherit;
  cursor: pointer;
}
.btn-check:disabled { opacity: 0.6; }
.check-status {
  font-size: 0.78rem;
  color: var(--text-dim);
}

.new-chapter-badge {
  display: inline-block;
  flex: none;
  background: var(--green);
  color: var(--accent-ink);
  font-size: 0.72rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  margin-left: 8px;
}
.mark-read-btn {
  margin-top: 10px;
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  font-size: 0.8rem;
  font-family: inherit;
  cursor: pointer;
}
.home-empty {
  padding: 40px 18px;
  text-align: center;
  color: var(--text-dim);
  font-size: 0.9rem;
}

/* ---------- List / Cards ---------- */

.list-root {
  padding: 14px 14px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 640px;
  margin: 0 auto;
}

.empty-state {
  text-align: center;
  color: var(--text-faint);
  font-size: 0.9rem;
  margin-top: 60px;
  padding: 0 30px;
}

.card {
  position: relative;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  overflow: hidden;
  cursor: pointer;
}
.card:active { background: var(--bg-elevated); }

/* Signature element: dog-eared fold for entries gone stale (30+ days untouched) */
.card.is-stale::before {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  width: 26px;
  height: 26px;
  background: linear-gradient(135deg, transparent 50%, var(--accent) 50%);
  opacity: 0.85;
}
.card.is-stale::after {
  content: "";
  position: absolute;
  inset: 0;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  opacity: 0.28;
  pointer-events: none;
}

.card-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.card-title {
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 1.08rem;
  margin: 0;
  line-height: 1.25;
}

.tag {
  flex: none;
  font-size: 0.68rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  padding: 2px 8px;
  border-radius: 999px;
  color: var(--bg);
}

.card-rating {
  margin-top: 4px;
  font-size: 0.9rem;
  color: var(--accent);
  letter-spacing: 1px;
}

.card-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  font-size: 0.82rem;
  color: var(--text-dim);
}

.progress-pill {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.78rem;
  color: var(--text);
  background: rgba(255,255,255,0.06);
  padding: 2px 8px;
  border-radius: var(--radius-sm);
}

.uptodate-pill {
  font-size: 0.78rem;
  color: var(--green);
  background: rgba(122, 184, 138, 0.12);
  padding: 2px 8px;
  border-radius: var(--radius-sm);
}

.link-pill {
  font-size: 0.78rem;
  color: var(--accent);
  background: rgba(255,255,255,0.06);
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  text-decoration: none;
  border: 1px solid var(--accent);
}

.link-pill:active {
  background: rgba(255,255,255,0.12);
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex: none;
}

.card-synopsis {
  margin: 8px 0 0;
  font-size: 0.85rem;
  color: var(--text-dim);
  line-height: 1.45;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.stale-note {
  margin-top: 8px;
  font-size: 0.74rem;
  color: var(--accent);
  font-weight: 500;
}

/* ---------- Modal ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 11, 16, 0.7);
  display: flex;
  align-items: flex-end;
  z-index: 50;
}

.modal-overlay[hidden] {
  display: none;
}

.modal-card {
  width: 100%;
  max-height: 92dvh;
  overflow-y: auto;
  background: var(--bg-elevated);
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  padding: 18px 18px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
  border-top: 1px solid var(--border-strong);
}

@media (min-width: 640px) {
  .modal-overlay { align-items: center; justify-content: center; }
  .modal-card { max-width: 480px; border-radius: 18px; }
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-head h2 {
  font-family: 'Fraunces', serif;
  font-size: 1.2rem;
  margin: 0;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 4px 8px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  flex: 1;
}
.field-label {
  font-size: 0.76rem;
  color: var(--text-dim);
  font-weight: 500;
}
.link-fetch-status {
  font-size: 0.76rem;
  color: var(--text-faint);
}
.field-row {
  display: flex;
  gap: 10px;
}

.star-picker {
  display: flex;
  align-items: center;
  gap: 2px;
}
.star-btn {
  background: none;
  border: none;
  font-size: 1.6rem;
  line-height: 1;
  color: var(--border-strong);
  cursor: pointer;
  padding: 4px 2px;
}
.star-btn.is-filled { color: var(--accent); }
.star-clear {
  background: none;
  border: none;
  color: var(--text-faint);
  font-size: 0.78rem;
  text-decoration: underline;
  cursor: pointer;
  padding: 4px 8px;
  margin-left: 6px;
}

input[type="text"], input[type="url"], select, textarea {
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 10px 11px;
  font-size: 0.95rem;
  font-family: inherit;
  width: 100%;
}
textarea { resize: vertical; line-height: 1.4; }
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 4px;
}

.btn-primary {
  flex: 1;
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px;
  font-weight: 600;
  font-size: 0.95rem;
  font-family: inherit;
  cursor: pointer;
}
.btn-danger {
  background: transparent;
  color: var(--rust);
  border: 1px solid var(--rust);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-family: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}

/* ---------- Settings ---------- */

.settings-section { margin-top: 4px; }
.settings-hint {
  font-size: 0.78rem;
  color: var(--text-dim);
  line-height: 1.45;
  margin: 0 0 10px;
}
.settings-heading {
  font-family: 'Fraunces', serif;
  font-size: 1rem;
  margin: 0 0 10px;
}
.settings-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.settings-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
}
.settings-row-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: none;
}
.settings-row-label {
  flex: 1;
  min-width: 0;
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.settings-row-input {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 4px 8px;
  font-size: 0.9rem;
  font-family: inherit;
}
.settings-row-btn {
  flex: none;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 0.95rem;
  cursor: pointer;
  padding: 4px 6px;
}
.settings-row-btn.danger { color: var(--rust); }
.settings-add-row {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}
.settings-add-row input { flex: 1; min-width: 0; }
.settings-backup-row { flex-wrap: wrap; }

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 32px);
  z-index: 60;
  pointer-events: none; /* never block the buttons underneath (e.g. modal Save) */
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  background: var(--text);
  color: var(--bg);
  font-size: 0.88rem;
  line-height: 1.4;
}
.toast[hidden] { display: none; }
