:root {
  --bg: #F3EEDF;
  --panel: #FFFFFF;
  --panel-2: #EFE6D0;
  --ink: #19171A;
  --text: #19171A;
  --muted: #6B6558;
  --accent: #C8102E;
  --accent-dark: #7A0A1D;
  --yellow: #FFD23B;
  --blue: #2E6BE0;
  --danger: #C8102E;
  --radius: 8px;
  --display-font: 'Dela Gothic One', sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Noto Sans', system-ui, -apple-system, "Segoe UI", sans-serif;
  padding-bottom: 2rem;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.1rem 1.25rem 1.3rem;
  background: var(--accent);
  border-bottom: 4px solid var(--ink);
}

.topbar h1 {
  font-family: var(--display-font);
  font-size: 1.15rem;
  line-height: 1.25;
  margin: 0;
  color: #fff;
  text-shadow: 2px 2px 0 var(--ink);
}

.pill {
  font-size: 0.68rem;
  font-weight: 700;
  padding: 0.3rem 0.7rem;
  border-radius: 999px;
  background: var(--yellow);
  color: var(--ink);
  border: 2px solid var(--ink);
}

.categories {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.85rem 1rem;
}

.cat-tab {
  flex: none;
  padding: 0.5rem 1rem;
  border-radius: 999px;
  border: 2px solid var(--ink);
  background: var(--panel);
  color: var(--ink);
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.cat-tab.active {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}

.attribution {
  padding: 0 1rem;
  max-width: 720px;
  margin: -0.3rem auto 0.7rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.attribution a { color: var(--accent-dark); font-weight: 700; }

main { padding: 0 1rem; max-width: 720px; margin: 0 auto; }

.add-panel {
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 10px;
  padding: 1rem;
  margin-bottom: 1.5rem;
}

.add-actions { display: flex; gap: 0.6rem; }

.scan-hint {
  margin: 0.6rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.btn {
  flex: 1;
  padding: 0.75rem 1rem 0.95rem;
  border-radius: var(--radius);
  border: 2px solid var(--ink);
  background: var(--bg);
  color: var(--ink);
  font-size: 0.9rem;
  font-weight: 700;
  box-shadow: 0 3px 0 var(--ink);
  position: relative;
  top: 0;
}

.btn:active { box-shadow: 0 1px 0 var(--ink); top: 2px; }

.btn.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--ink);
  font-family: var(--display-font);
  font-size: 0.85rem;
  box-shadow: 0 3px 0 var(--accent-dark);
}

.btn.primary:active { box-shadow: 0 1px 0 var(--accent-dark); }

.btn.danger { background: var(--accent); color: #fff; border-color: var(--ink); box-shadow: 0 3px 0 var(--accent-dark); }

.scanner-wrap { margin-top: 1rem; }
#scanPreview {
  width: 100%;
  max-height: 220px;
  object-fit: contain;
  border-radius: var(--radius);
  background: var(--ink);
  border: 2px solid var(--ink);
}

.manual-form, .confirm-panel, .search-results { margin-top: 1rem; }

.search-results {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 320px;
  overflow-y: auto;
}

.search-result {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--panel);
  border: 2px solid var(--ink);
  border-radius: var(--radius);
  padding: 0.5rem;
  color: var(--text);
  text-align: left;
}

.search-result img {
  width: 44px;
  height: 60px;
  object-fit: cover;
  border-radius: 4px;
  background: var(--panel-2);
  border: 1px solid var(--ink);
  flex: none;
}

.search-result span { font-size: 0.88rem; font-weight: 500; }

.manual-form input {
  width: 100%;
  padding: 0.65rem 0.8rem;
  margin-bottom: 0.6rem;
  border-radius: var(--radius);
  border: 2px solid var(--ink);
  background: var(--panel);
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
}

.form-actions { display: flex; gap: 0.5rem; margin-top: 0.6rem; flex-wrap: wrap; }

/* The confirm panel's row has three buttons (Add/Edit/Cancel) next to a
   fixed-width cover image, which is too tight for three equal flex:1
   buttons on a phone — let only the primary one claim extra space. */
.confirm-panel .form-actions .btn:not(.primary) { flex: 0 1 auto; }

.duplicate-warning {
  margin: -0.3rem 0 0.6rem;
  color: var(--accent-dark);
  font-size: 0.8rem;
  font-weight: 700;
}

.link-btn {
  display: block;
  margin-top: 0.6rem;
  background: none;
  border: none;
  padding: 0;
  color: var(--accent-dark);
  font-size: 0.85rem;
  font-weight: 700;
  text-decoration: underline;
}

.confirm-panel {
  display: flex;
  gap: 1rem;
  background: var(--panel);
  border: 2px solid var(--ink);
  border-radius: 10px;
  padding: 0.8rem;
  box-shadow: 4px 4px 0 var(--ink);
}

.confirm-panel img, #itemImage {
  width: 90px;
  height: 120px;
  object-fit: cover;
  border-radius: 6px;
  background: var(--panel-2);
  border: 2px solid var(--ink);
  flex: none;
}

.confirm-info { flex: 1; min-width: 0; }
.confirm-info h3 { margin: 0 0 0.25rem; font-size: 1rem; font-family: var(--display-font); font-weight: 400; }
.confirm-info p { margin: 0; color: var(--muted); font-size: 0.85rem; }

.status-msg { min-height: 1.2rem; color: var(--muted); font-size: 0.85rem; margin: 0.6rem 0 0; }

.collection h2 {
  font-family: var(--display-font);
  font-size: 1rem;
  color: var(--ink);
  font-weight: 400;
  margin-bottom: 0.75rem;
}

.status-tabs {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.status-tab {
  flex: 1;
  padding: 0.5rem 0.6rem;
  border-radius: 999px;
  border: 2px solid var(--ink);
  background: var(--panel);
  color: var(--ink);
  font-size: 0.8rem;
  font-weight: 700;
}

.status-tab.active[data-status="have"] { background: var(--accent); color: #fff; }
.status-tab.active[data-status="want"] { background: var(--yellow); color: var(--ink); }
.status-tab.active[data-status="read"] { background: var(--blue); color: #fff; }

.sort-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.sort-select {
  flex: 1;
  padding: 0.55rem 0.7rem;
  border-radius: var(--radius);
  border: 2px solid var(--ink);
  background: var(--panel);
  color: var(--text);
  font-size: 0.85rem;
  font-weight: 700;
  font-family: inherit;
}

.sort-row .btn {
  flex: none;
  width: 48px;
  padding: 0.5rem;
  font-size: 1.1rem;
  line-height: 1;
}

.collection-search {
  display: block;
  width: 100%;
  padding: 0.6rem 0.8rem;
  margin-bottom: 0.9rem;
  border-radius: var(--radius);
  border: 2px solid var(--ink);
  background: var(--panel);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 0.75rem;
}

.grid-item {
  background: var(--panel);
  border: 2.5px solid var(--ink);
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  text-align: left;
  padding: 0;
  color: var(--text);
  box-shadow: 3px 3px 0 var(--ink);
}

.grid-item img {
  width: 100%;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  background: var(--panel-2);
  display: block;
}

.grid-item .label {
  padding: 0.4rem 0.5rem;
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1.2;
  background: var(--ink);
  color: #fff;
  text-align: center;
}

.empty-msg { color: var(--muted); font-size: 0.9rem; }

.hidden { display: none !important; }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(25,23,26,0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 10;
}

.overlay-card {
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 10px;
  padding: 1.25rem;
  max-width: 360px;
  width: 100%;
  text-align: center;
  box-shadow: 5px 5px 0 var(--ink);
}

.overlay-card img { width: 140px; height: 190px; margin: 0 auto 0.75rem; display: block; border: 2px solid var(--ink); border-radius: 4px; }
.overlay-card h3 { margin: 0 0 0.3rem; font-family: var(--display-font); font-weight: 400; font-size: 1.05rem; }
.overlay-card p { color: var(--muted); font-size: 0.85rem; margin: 0.2rem 0; }
.overlay-card .form-actions { justify-content: center; }

.status-row {
  display: flex;
  gap: 0.4rem;
  justify-content: center;
  margin: 0.6rem 0;
  flex-wrap: wrap;
}

.confirm-info .status-row { justify-content: flex-start; }

.status-chip {
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  border: 2px solid var(--ink);
  background: var(--bg);
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.status-chip.active[data-status="have"] { background: var(--accent); border-color: var(--ink); color: #fff; }
.status-chip.active[data-status="want"] { background: var(--yellow); border-color: var(--ink); color: var(--ink); }
.status-chip.active[data-status="read"] { background: var(--blue); border-color: var(--ink); color: #fff; }

.series-card { max-width: 480px; text-align: left; }
.series-card h3 { text-align: center; margin-bottom: 0.75rem; }
.series-card .grid { max-height: 50vh; overflow-y: auto; }

.series-tile .badge {
  position: absolute;
  top: 0.3rem;
  right: 0.3rem;
  background: var(--yellow);
  color: var(--ink);
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0.1rem 0.45rem;
  border: 2px solid var(--ink);
  border-radius: 999px;
}

.grid-item { position: relative; }

.status-dot {
  position: absolute;
  top: 0.3rem;
  left: 0.3rem;
  display: flex;
  gap: 0.2rem;
}

.status-dot span {
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  display: block;
  border: 1.5px solid var(--ink);
}

.status-dot .have { background: var(--accent); }
.status-dot .want { background: var(--yellow); }
.status-dot .read { background: var(--blue); }

.subkind-row {
  display: flex;
  gap: 0.5rem;
  margin: 0 0 0.6rem;
}

.subkind-chip {
  flex: 1;
  padding: 0.45rem 0.6rem;
  border-radius: 999px;
  border: 2px solid var(--ink);
  background: var(--bg);
  color: var(--ink);
  font-size: 0.8rem;
  font-weight: 700;
}

.subkind-chip.active {
  background: var(--ink);
  color: #fff;
}

/* Books & Manga: a packed shelf instead of a grid — covers sit edge to edge
   on a wood-plank row, grouped into a Books section and a Manga section
   (see item.subKind), each wood plank built from the row below it. */
.shelf-grid { display: block; }

.shelf-section { margin-bottom: 0.5rem; }

.shelf-section-label {
  font-family: var(--display-font);
  font-size: 0.85rem;
  color: var(--ink);
  margin: 0.9rem 0 0.4rem;
}

.shelf-row {
  display: flex;
}

/* When a section wraps to more than one row, leave a gap above every row
   after the first — otherwise the plank from one row sits flush against
   the covers on the row below it. */
.shelf-row + .shelf-row {
  margin-top: 14px;
}

.shelf-item {
  flex: none;
  width: 92px;
  touch-action: none;
  position: relative;
  cursor: pointer;
  user-select: none;
}

.shelf-item:first-child .shelf-cover {
  border-left-width: 2.5px;
}

.shelf-cover {
  height: 142px;
  border: 2.5px solid var(--ink);
  border-right-width: 1.25px;
  border-left-width: 1.25px;
  overflow: hidden;
  background: var(--panel-2);
  position: relative;
}

.shelf-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.shelf-plank {
  height: 18px;
  background: #8B5A2B;
  border-top: 4px solid #5C3A1E;
  border-bottom: 5px solid #3E2714;
  background-image: repeating-linear-gradient(90deg, rgba(0,0,0,0.1) 0 2px, transparent 2px 15px);
}

.shelf-item.dragging {
  opacity: 0.3;
}

.shelf-drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 50;
  opacity: 0.92;
  transform: scale(1.06);
  box-shadow: 4px 6px 0 rgba(0,0,0,0.35);
}
