:root {
  color-scheme: dark;
  --bg: #0b1220;
  --bg-elevated: #111c31;
  --bg-card: #15253f;
  --border: rgba(148, 163, 184, 0.2);
  --primary: #38bdf8;
  --primary-soft: rgba(56, 189, 248, 0.1);
  --text: #e2e8f0;
  --text-subtle: #94a3b8;
  --danger: #f87171;
  --radius-lg: 18px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --shadow-soft: 0 14px 40px rgba(15, 23, 42, 0.45);
  --transition: 180ms ease;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at 10% 10%, rgba(56, 189, 248, 0.12), transparent 35%),
    radial-gradient(circle at 90% 5%, rgba(168, 85, 247, 0.07), transparent 40%),
    var(--bg);
  color: var(--text);
  line-height: 1.6;
}

body.modal-open {
  overflow: hidden;
}

h1,
h2,
h3 {
  margin: 0;
  font-weight: 600;
  color: #f8fafc;
}

a {
  color: var(--primary);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: 0.2rem clamp(1.8rem, 4vw, 3rem);
  background: linear-gradient(180deg, rgba(15, 23, 42, 0.92) 0%, rgba(15, 23, 42, 0.8) 60%, transparent 100%);
  backdrop-filter: blur(18px);
  display: grid;
  grid-template-columns: 45% 55%; /* 40% left, 60% right */
  /*gap: 1rem;*/
  align-items: stretch;
}

.header-top {
  position: relative;
  width: 100%;
  max-width: none;
  margin-bottom: 0.4rem;
}

.header-top-close {
  position: absolute;
  top: 0.4rem;
  right: 0.4rem;
  border: none;
  background: rgba(148, 163, 184, 0.15);
  border-radius: 999px;
  width: 2rem;
  height: 2rem;
  color: var(--text);
  cursor: pointer;
  font-size: 0.95rem;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), transform var(--transition);
}

.header-top-close:hover {
  background: rgba(56, 189, 248, 0.35);
}

.header-top-close:active {
  transform: translateY(1px);
}

.tagline {
  margin: 0.8rem 0 0;
  color: var(--text-subtle);
  font-size: 1rem;
}

.control-bar {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 0.8rem;
  margin-bottom: 1rem;
}

/* Controls + Ad layout */
.controls-with-ad {
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(240px, 40vw, 810px);
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
  width: 100%;
}

/* Left area should grow, right ad stays fixed-ish */
.controls-with-ad .control-bar {
  min-width: 0;
  margin-bottom: 0; /* outer container handles spacing */
}

.right-media {
  flex: 0 1 auto;
  width: auto;
  max-width: 100%;
  min-width: 0;
}

.right-link {
  display: flex;
  align-items: center;
  justify-content: center;
  width: min(100%, 810px);
  aspect-ratio: 810 / 130; /* scale proportionally */
  height: auto;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: linear-gradient(135deg, rgba(56,189,248,.16), rgba(148,163,184,.08));
  color: var(--text-subtle);
  text-decoration: none;
}

.right-link img {
  width: 100%;
  height: auto;
  display: block;
  object-fit: contain;
  border-radius: var(--radius-md);
}

.search-field {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 0.9rem;
  flex: 1 1 360px;
  min-width: 240px;
  width: auto;
  max-width: 50%;
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  transition: border-color var(--transition), box-shadow var(--transition);
}

.search-field:focus-within {
  border-color: rgba(56, 189, 248, 0.6);
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.15);
}

.search-icon {
  font-size: 1rem;
  opacity: 0.6;
}

#searchInput {
  flex: 1;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 1rem;
  outline: none;
}

.ghost-btn {
  border: none;
  background: rgba(148, 163, 184, 0.15);
  color: var(--text-subtle);
  padding: 0.35rem 0.75rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition), color var(--transition), transform var(--transition);
}

.ghost-btn:hover {
  background: rgba(148, 163, 184, 0.3);
  color: var(--text);
}

.ghost-btn:active {
  transform: translateY(1px);
}

.clear-filter-btn {
  padding: 0.45rem 0.9rem;
  white-space: nowrap;
  flex: 0 0 auto;
}

#clearSearch {
  display: none;
  align-items: center;
  justify-content: center;
  width: 1.8rem;
  height: 1.8rem;
  font-size: 0.85rem;
}

.result-stats {
  color: var(--text-subtle);
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 0 1 28ch;
  max-width: 28ch;
}

.actions-row {
  display: inline-flex;
  align-items: center;
  gap: 0.8rem;
  flex: 0 0 auto;
  white-space: nowrap;
  min-width: 0;
  max-width: none;
}

.tag-filter {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  padding-right: 0.2rem;
  /* height is controlled by .tag-filter-container (collapsed/expanded) */
}

/* Tag filter collapse/expand container */
.tag-filter-container {
  /* nothing special, acts as a wrapper */
}

/* When collapsed, ensure 10px spacing from previous row.
   Grid row gap is 1rem; use negative margin to net 10px. */
.tag-filter-container.collapsed {
  margin-top: calc(10px - 1rem);
}

.tag-filter-container.collapsed .tag-filter {
  /* Collapsed to a single row; height measured in JS and set via --collapsed-height */
  max-height: var(--collapsed-height, 40px);
  overflow: hidden;
}

.tag-filter-container.expanded .tag-filter {
  /* Show all rows */
  max-height: none;
  overflow: visible;
}

.tag-toggle-btn {
  margin-left: auto;
  font-size: 0.86rem;
  white-space: nowrap;
  flex: 0 0 auto;
}

.tag-button {
  padding: 0.35rem 0.9rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.6);
  color: var(--text-subtle);
  font-size: 0.88rem;
  cursor: pointer;
  transition: transform var(--transition), border-color var(--transition), background var(--transition), color var(--transition);
}

.tag-button:hover {
  transform: translateY(-2px);
  border-color: rgba(56, 189, 248, 0.6);
  color: var(--text);
}

.tag-button.active {
  background: var(--primary-soft);
  border-color: rgba(56, 189, 248, 0.8);
  color: var(--primary);
}

main {
  padding: 0 clamp(1.4rem, 4vw, 3rem) 3rem;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 1.6rem;
}

.prompt-card {
  background: rgba(17, 25, 40, 0.8);
  border-radius: var(--radius-lg);
  border: 1px solid rgba(148, 163, 184, 0.08);
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.4);
  backdrop-filter: blur(10px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: transform var(--transition), border-color var(--transition);
  cursor: pointer;
}

.prompt-card:hover {
  transform: translateY(-6px);
  border-color: rgba(56, 189, 248, 0.4);
}

.prompt-card img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  background: rgba(148, 163, 184, 0.08);
}

.card-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  aspect-ratio: 4 / 3;
  background: rgba(148, 163, 184, 0.08);
  color: rgba(148, 163, 184, 0.7);
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
}

.card-body {
  padding: 1.1rem 1.1rem 0.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.card-title {
  margin: 0;
  font-size: 1.05rem;
  letter-spacing: 0.01em;
}

.card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.tag-chip {
  padding: 0.2rem 0.65rem;
  font-size: 0.75rem;
  border-radius: 999px;
  background: rgba(56, 189, 248, 0.12);
  color: #bae6fd;
}

.card-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--text-subtle);
  font-size: 0.82rem;
}

.card-meta span + span::before {
  content: '•';
  margin: 0 0.4rem;
  color: rgba(148, 163, 184, 0.4);
}

.empty-state {
  margin-top: 3rem;
  text-align: center;
  color: var(--text-subtle);
  font-size: 1rem;
}

.hidden {
  display: none !important;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(4, 7, 15, 0.72);
  backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(1.5rem, 4vw, 3rem);
  z-index: 999;
}

.modal-content {
  position: relative;
  width: min(960px, 100%);
  max-height: 90vh;
  padding: clamp(1.4rem, 2vw, 2.1rem);
  background: radial-gradient(circle at top right, rgba(56, 189, 248, 0.1), transparent 40%), rgba(8, 17, 35, 0.98);
  border-radius: var(--radius-lg);
  border: 1px solid rgba(148, 163, 184, 0.2);
  box-shadow: var(--shadow-soft);
  overflow-y: auto;
}

.modal-close {
  position: absolute;
  top: 1.1rem;
  right: 1.1rem;
  border: none;
  background: rgba(15, 23, 42, 0.6);
  border-radius: 999px;
  width: 2.2rem;
  height: 2.2rem;
  color: var(--text);
  cursor: pointer;
  font-size: 1rem;
  transition: background var(--transition);
}

.modal-close:hover {
  background: rgba(56, 189, 248, 0.7);
}

.modal-header {
  margin-bottom: 1rem;
}

.modal-source {
  margin-top: 0.4rem;
  font-size: 0.9rem;
  color: var(--text-subtle);
}

.modal-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1.4rem;
}

.modal-tags .tag-chip {
  background: rgba(56, 189, 248, 0.18);
  color: #e0f2fe;
}

.modal-section {
  margin-bottom: 1.6rem;
}

.modal-section h3 {
  margin-bottom: 0.8rem;
  font-size: 1rem;
  letter-spacing: 0.02em;
}

.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 0.9rem;
}

.image-grid img {
  width: 100%;
  border-radius: var(--radius-md);
  border: 1px solid rgba(148, 163, 184, 0.18);
  object-fit: cover;
  background: rgba(148, 163, 184, 0.08);
}

.prompt-block {
  position: relative;
  padding: 1rem;
  background: rgba(15, 23, 42, 0.7);
  border-radius: var(--radius-md);
  border: 1px solid rgba(148, 163, 184, 0.18);
}

.prompt-block + .prompt-block {
  margin-top: 1rem;
}

.prompt-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.6rem;
  color: var(--text-subtle);
  font-size: 0.85rem;
}

.copy-btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.35rem 0.75rem;
  background: rgba(56, 189, 248, 0.12);
  color: #bae6fd;
  cursor: pointer;
  transition: background var(--transition), color var(--transition), transform var(--transition);
}

.copy-btn:hover {
  background: rgba(56, 189, 248, 0.25);
  color: #f0f9ff;
  transform: translateY(-1px);
}

pre {
  margin: 0;
  white-space: pre-wrap;
  font-family: 'JetBrains Mono', 'Fira Code', 'SFMono-Regular', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
  font-size: 0.9rem;
  color: #f8fafc;
}

.note-list {
  padding-left: 1.2rem;
  margin: 0.3rem 0 0;
  color: var(--text-subtle);
}

.description-block {
  color: var(--text-subtle);
  background: rgba(148, 163, 184, 0.08);
  border-radius: var(--radius-md);
  padding: 1rem;
  border: 1px solid rgba(148, 163, 184, 0.14);
}

.toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.85);
  color: #f8fafc;
  padding: 0.7rem 1.2rem;
  border-radius: var(--radius-md);
  border: 1px solid rgba(56, 189, 248, 0.35);
  box-shadow: 0 15px 40px rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(10px);
  font-size: 0.95rem;
  z-index: 1000;
}

.toast.error {
  border-color: rgba(248, 113, 113, 0.65);
  color: #fee2e2;
}

@media (max-width: 720px) {
  .app-header {
    position: static;
    padding-top: 1.8rem;
  }

  /* Keep ad visible on small screens; layout may scroll horizontally if too narrow */

  .control-bar {
    flex-direction: column;
    align-items: stretch;
  }

  .search-field {
    width: 100%;
  }

  .result-stats {
    margin-left: 0;
  }

  .gallery {
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  }

  .modal-content {
    max-height: 88vh;
  }
}

/* Stack left/right when extremely narrow */
@media (max-width: 0px) {
  .app-header {
    grid-template-columns: 1fr;
  }
  .controls-with-left,
  .controls-with-right,
  .tag-filter-container {
    grid-column: 1;
  }
  .controls-with-right {
    justify-self: stretch;
  }
  .right-media {
    width: 100%;
    max-width: 100%;
    margin: 0;
  }
  .right-link {
    width: 100%;
  }
  .actions-row {
    max-width: none;
    width: 100%;
  }
}
/* New left/right header layout */
.controls-with-left {
  grid-column: 1;
  min-width: 0;
  align-self: stretch;
}

.controls-with-right {
  grid-column: 2;
  align-self: stretch;
  min-width: 0;
  display: flex;
  align-items: center; /* vertical center */
  justify-content: flex-end; /* right align */
}

.tag-filter-container {
  grid-column: 1 / -1;
}
