:root {
  --radius: clamp(8px, 0.8vw, 24px);
  --pad:    clamp(8px, 1vw, 24px);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: #fff;
  color: #000;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  min-height: 100vh;
}

header {
  margin: var(--pad) var(--pad) 0;
  padding: 12px 20px;
  border: 1px solid #e0e0e0;
  border-radius: var(--radius);
  display: flex;
  align-items: baseline;
  gap: 12px;
}

header h1 { font-size: 1rem; font-weight: 700; }
#image-count { font-size: 0.82rem; }

main { padding: var(--pad); }

/* height set by JS; position:relative makes it the containing block for cards */
#grid { position: relative; }

/* left/top/width/height are set by the row engine via inline style */
.card {
  overflow: hidden;
  cursor: pointer;
  border-radius: var(--radius);
}

/* The box carries the card's display aspect, which is deliberately not the
   image's own; cover crops it to fit from the centre rather than distort. */
.card img { width: 100%; height: 100%; display: block; object-fit: cover; }

.card-name, .card-tags {
  display: none;
  position: absolute;
  left: 0;
  right: 0;
}

.card-name {
  bottom: 0;
  padding: 8px 10px 12px;
  background: linear-gradient(transparent, rgba(0,0,0,0.55));
  color: #fff;
  font-size: 0.75rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border-radius: 0 0 var(--radius) var(--radius);
}

.card-tags {
  top: 0;
  padding: 8px;
  background: linear-gradient(rgba(0,0,0,0.5), transparent);
  border-radius: var(--radius) var(--radius) 0 0;
}

.card:hover .card-name { display: block; }
.card:hover .card-tags { display: flex; flex-wrap: wrap; gap: 3px; }

.card-tag {
  padding: 1px 6px;
  background: rgba(0,0,0,0.45);
  border-radius: 999px;
  font-size: 0.6rem;
  color: rgba(255,255,255,0.9);
  white-space: nowrap;
  cursor: pointer;
}
.card-tag:hover { background: rgba(0,0,0,0.7); }

.card.tag-hidden { display: none; }

#status {
  padding: 48px 0;
  text-align: center;
  font-size: 0.9rem;
}

.error-box {
  display: inline-block;
  border: 1px solid #000;
  padding: 16px 24px;
  max-width: 480px;
  text-align: left;
}

.error-box h2 { margin-bottom: 6px; font-size: 1rem; }
.error-box pre { font-size: 0.8rem; white-space: pre-wrap; word-break: break-word; }
.error-box p { margin-top: 10px; font-size: 0.8rem; }

/* -- Lightbox ------------------------------------------------------- */
#lightbox {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.95);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

#lightbox.open { display: flex; }

#lightbox-img {
  width: 100vw;
  height: 100vh;
  object-fit: contain;
}

#lightbox-caption {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 6px 16px 4px;
  background: linear-gradient(transparent, rgba(0,0,0,0.2));
  color: #fff;
  font-size: 0.7rem;
  text-align: center;
  user-select: text;
}

#lightbox-close {
  position: fixed;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  color: #fff;
  font-size: 2rem;
  cursor: pointer;
  line-height: 1;
  padding: 4px 8px;
}

#lightbox-nav {
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding: 0 12px;
  pointer-events: none;
}

.nav-btn {
  pointer-events: all;
  background: none;
  border: 1px solid #fff;
  color: #fff;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.nav-btn:hover { background: #fff; color: #000; }

/* -- Tag filter bar ------------------------------------------------- */
#tag-bar {
  margin: clamp(4px, 0.5vw, 8px) var(--pad) 0;
  padding: 8px 12px;
  border: 1px solid #e0e0e0;
  border-radius: var(--radius);
  display: none; /* shown once tags.json has loaded */
}

#tag-bar.ready { display: block; }

#tag-search-row {
  display: flex;
  align-items: center;
  gap: 8px;
  position: relative;
}

#tag-search {
  flex: 1;
  min-width: 0;
  border: 1px solid #ccc;
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 0.82rem;
  font-family: inherit;
  outline: none;
}
#tag-search:focus { border-color: #888; }

#tag-clear, #tag-browse-toggle {
  background: none;
  font-size: 0.78rem;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
}

#tag-clear {
  display: none;
  border: 1px solid #ccc;
  border-radius: 6px;
  padding: 4px 10px;
}
#tag-clear.visible { display: inline-block; }
#tag-clear:hover { background: #f0f0f0; }

#tag-browse-toggle { border: none; color: #666; padding: 4px 2px; }
#tag-browse-toggle:hover { color: #000; }

#tag-suggestions {
  display: none;
  position: absolute;
  z-index: 500;
  background: #fff;
  border: 1px solid #ccc;
  border-radius: 8px;
  max-height: 240px;
  overflow-y: auto;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  min-width: 180px;
}
#tag-suggestions.open { display: block; }

.tag-suggestion {
  padding: 6px 12px;
  font-size: 0.82rem;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  gap: 12px;
}
.tag-suggestion:hover { background: #f5f5f5; }
.tag-suggestion .count { color: #999; font-size: 0.75rem; }

#active-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}
#active-tags:empty { margin-top: 0; }

.tag-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  cursor: pointer;
  background: #333;
  color: #fff;
  line-height: 1.4;
}
.tag-pill:hover { background: #555; }
.tag-pill .remove { font-size: 0.85em; }

/* -- Tag browse panel ----------------------------------------------- */
#tag-browse {
  display: none;
  margin-top: 8px;
  max-height: 280px;
  overflow-y: auto;
  border-top: 1px solid #eee;
  padding-top: 8px;
}
#tag-browse.open { display: block; }

.tag-category { margin-bottom: 6px; }

.tag-category-label {
  font-size: 0.7rem;
  color: #999;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 3px;
}

.tag-category-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.browse-tag {
  display: inline-block;
  padding: 2px 8px;
  border: 1px solid #ddd;
  border-radius: 999px;
  font-size: 0.72rem;
  cursor: pointer;
  background: #fff;
  color: #333;
  line-height: 1.4;
}
.browse-tag:hover { background: #f0f0f0; border-color: #bbb; }
.browse-tag.active { background: #333; color: #fff; border-color: #333; }
.browse-tag .count { color: #aaa; font-size: 0.65rem; margin-left: 2px; }
.browse-tag.active .count { color: #ccc; }
