:root {
  /* Warm "paper & ink" palette — light, calm, high-contrast */
  --bg: #f6f3ec;
  --surface: #fffefb;
  --surface-2: #efebe1;
  --border: #e0dacd;
  --border-strong: #cdc5b4;
  --text: #2c2a26;
  --muted: #76705f;
  --accent: #2f5d50;        /* deep muted green */
  --accent-strong: #244a40;
  --accent-soft: #e8efe9;
  --danger: #a23b3b;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(44, 42, 38, 0.06), 0 6px 20px rgba(44, 42, 38, 0.07);

  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

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

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

[hidden] { display: none !important; }

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  padding: 0.6rem 1.1rem;
  border-radius: var(--radius);
  font-size: 0.98rem;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
  letter-spacing: 0.2px;
}
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); color: #fff; }
.btn-primary:disabled { opacity: 0.55; cursor: default; }

/* ---------- Password gate ---------- */
.gate {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background: var(--bg);
}

.gate-card {
  width: min(400px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2.5rem 2rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  text-align: center;
}

.gate-title {
  margin: 0;
  font-family: var(--serif);
  font-weight: 600;
  font-size: 2.1rem;
  letter-spacing: 0.3px;
  color: var(--text);
}
.gate-sub { margin: 0 0 0.6rem; color: var(--muted); }

.gate-input, .add-input {
  width: 100%;
  padding: 0.7rem 0.85rem;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
}
.gate-input:focus, .add-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.gate-error { margin: 0.25rem 0 0; color: var(--danger); font-size: 0.9rem; }

/* ---------- Sync button + modal ---------- */
.sync-btn.connected {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: var(--accent);
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 1100;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background: rgba(20, 19, 26, 0.45);
  backdrop-filter: blur(3px);
}
.modal-card {
  width: min(520px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.75rem 1.75rem 1.5rem;
}
.modal-title {
  margin: 0 0 0.5rem;
  font-family: var(--serif);
  font-weight: 600;
  font-size: 1.5rem;
}
.modal-intro { margin: 0 0 1rem; color: var(--muted); }
.modal-steps { margin: 0 0 1.25rem; padding-left: 1.2rem; color: var(--muted); font-size: 0.92rem; }
.modal-steps li { margin-bottom: 0.35rem; }
.modal-steps code {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0 0.3rem;
  font-size: 0.85em;
}
.modal-label { display: block; margin-bottom: 0.4rem; font-weight: 600; font-size: 0.95rem; }
.modal-label + .gate-input { margin-bottom: 0.6rem; }
.modal-optional { font-weight: 400; color: var(--muted); font-size: 0.85em; }
.sync-status { margin: 0.75rem 0 0; min-height: 1.2em; font-size: 0.9rem; color: var(--muted); }
.sync-status.error { color: var(--danger); }
.sync-status.ok { color: var(--accent); }
.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 1rem;
}
.modal-note { margin: 1rem 0 0; font-size: 0.82rem; color: var(--muted); }

/* ---------- Chooser page ---------- */
.chooser {
  position: fixed;
  inset: 0;
  z-index: 900;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background: var(--bg);
}

.chooser-inner {
  width: min(680px, 100%);
  text-align: center;
}

.chooser-title {
  margin: 0;
  font-family: var(--serif);
  font-weight: 600;
  font-size: 2.5rem;
  letter-spacing: 0.3px;
  color: var(--text);
}
.chooser-sub { margin: 0.5rem 0 2.25rem; color: var(--muted); font-size: 1.1rem; }

.chooser-options {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1.25rem;
}
@media (max-width: 480px) {
  .chooser-options { grid-template-columns: 1fr; }
}

.choice-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  padding: 2.75rem 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}
.choice-card:hover {
  transform: translateY(-3px);
  border-color: var(--accent);
  box-shadow: 0 2px 4px rgba(44, 42, 38, 0.08), 0 12px 28px rgba(44, 42, 38, 0.1);
}
.choice-emoji { font-size: 2.6rem; line-height: 1; }
.choice-label { font-family: var(--serif); font-size: 1.5rem; font-weight: 600; }
.choice-desc { color: var(--muted); font-size: 0.95rem; }

/* ---------- App layout ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem clamp(1rem, 4vw, 2.5rem);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: rgba(246, 243, 236, 0.9);
  backdrop-filter: blur(8px);
  z-index: 10;
}

.brand {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 1.35rem;
  letter-spacing: 0.3px;
  color: var(--text);
}

.brand-btn {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}
.brand-btn:hover { color: var(--accent); }

.modes { display: flex; gap: 0.5rem; }

.mode-btn {
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--muted);
  padding: 0.45rem 0.95rem;
  border-radius: 999px;
  font-size: 0.92rem;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s ease;
}
.mode-btn:hover { color: var(--accent); border-color: var(--accent); }
.mode-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.content {
  max-width: 1080px;
  margin: 0 auto;
  padding: clamp(1.5rem, 4vw, 2.75rem);
}

.collection-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.collection-title {
  margin: 0;
  font-family: var(--serif);
  font-weight: 600;
  font-size: 1.7rem;
}

/* View toggle (grid / list) */
.view-toggle {
  display: inline-flex;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  overflow: hidden;
}
.view-btn {
  border: none;
  background: var(--surface);
  color: var(--muted);
  padding: 0.45rem 0.7rem;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.view-btn + .view-btn { border-left: 1px solid var(--border-strong); }
.view-btn:hover { color: var(--accent); }
.view-btn.active { background: var(--accent); color: #fff; }

.add-form { display: flex; gap: 0.6rem; margin-bottom: 1rem; align-items: flex-start; flex-wrap: wrap; }
.add-input-wrap { position: relative; flex: 1; min-width: 220px; }
.add-input-wrap .add-input { width: 100%; }

.search-field {
  padding: 0.7rem 0.85rem;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  cursor: pointer;
}
.search-field:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* Type-ahead dropdown */
.suggestions {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 50;
  margin: 0;
  padding: 0.25rem;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  max-height: 360px;
  overflow-y: auto;
}
.suggestion {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.5rem 0.6rem;
  border-radius: 8px;
  cursor: pointer;
}
.suggestion.active { background: var(--accent-soft); }
.suggestion-thumb {
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  border-radius: 5px;
  object-fit: cover;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.suggestion-text { display: flex; flex-direction: column; min-width: 0; }
.suggestion-title {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.suggestion-sub {
  color: var(--muted);
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status { margin: 0 0 1.25rem; color: var(--muted); min-height: 1.2em; font-size: 0.95rem; }
.status.error { color: var(--danger); }

.empty { color: var(--muted); padding: 2.5rem 0; text-align: center; font-style: italic; }

/* ---------- Card grid ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1.25rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  position: relative;
  box-shadow: var(--shadow);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.card:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 4px rgba(44, 42, 38, 0.08), 0 12px 26px rgba(44, 42, 38, 0.1);
}

.card-cover {
  aspect-ratio: 1 / 1;
  width: 100%;
  object-fit: cover;
  background: var(--surface-2);
  display: block;
  border-bottom: 1px solid var(--border);
}
.card-cover.book { aspect-ratio: 2 / 3; }

/* ---------- List view ---------- */
.grid.list-view {
  grid-template-columns: 1fr;
  gap: 0.75rem;
}
.grid.list-view .card {
  flex-direction: row;
  align-items: stretch;
}
.grid.list-view .card:hover { transform: none; }
.grid.list-view .card-cover {
  width: 84px;
  flex: 0 0 84px;
  aspect-ratio: auto;
  height: auto;
  border-bottom: none;
  border-right: 1px solid var(--border);
}
.grid.list-view .card-cover.book { aspect-ratio: auto; }
.grid.list-view .card-body { flex: 1; padding: 0.75rem 1rem; }
.grid.list-view .card-meta { display: flex; flex-wrap: wrap; gap: 0 1.25rem; }
.grid.list-view .card-meta li { border-top: none; padding: 0.1rem 0; }
.grid.list-view .card-remove { opacity: 1; }

.card-body { padding: 0.95rem 1.05rem 1.15rem; }
.card-title { margin: 0 0 0.2rem; font-family: var(--serif); font-weight: 600; font-size: 1.1rem; line-height: 1.3; }
.card-sub { margin: 0 0 0.75rem; color: var(--accent); font-size: 0.92rem; font-weight: 500; }

.card-meta { list-style: none; margin: 0; padding: 0; font-size: 0.85rem; color: var(--muted); }
.card-meta li {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.22rem 0;
  border-top: 1px solid var(--border);
}
.card-meta li:first-child { border-top: none; }
.card-meta li span:last-child { color: var(--text); text-align: right; }

.card-link {
  display: inline-block;
  margin-top: 0.7rem;
  font-size: 0.85rem;
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}
.card-link:hover { text-decoration: underline; }

.card-remove {
  position: absolute;
  top: 0.55rem;
  right: 0.55rem;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: rgba(255, 254, 251, 0.92);
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s ease, background 0.15s ease, color 0.15s ease;
}
.card:hover .card-remove { opacity: 1; }
.card-remove:hover { background: var(--danger); border-color: var(--danger); color: #fff; }
