:root {
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --primary-light: #818cf8;
  --primary-glow: rgba(99, 102, 241, 0.18);

  --bg: #f8fafc;
  --bg-card: #ffffff;
  --bg-elevated: rgba(255, 255, 255, 0.92);
  --bg-subtle: #f1f5f9;

  --text: #0f172a;
  --text-primary: #1e293b;
  --text-secondary: #475569;
  --text-muted: #94a3b8;

  --border: #e2e8f0;
  --shadow-card: 0 1px 2px rgba(0,0,0,0.04), 0 1px 4px rgba(0,0,0,0.04);
  --shadow-hover: 0 8px 28px rgba(0,0,0,0.08), 0 2px 8px rgba(0,0,0,0.04);
  --shadow-bar: 0 4px 20px rgba(0,0,0,0.06);

  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --radius-inp: 14px;
  --radius-card: 14px;
  --radius-sm: 8px;
  --radius-pill: 9999px;

  --transition: 0.22s cubic-bezier(0.4, 0, 0.2, 1);
}

[data-theme="dark"] {
  --bg: #0b0b14;
  --bg-card: #14141f;
  --bg-elevated: rgba(20, 20, 31, 0.94);
  --bg-subtle: #1a1a2e;

  --text: #f1f5f9;
  --text-primary: #e2e8f0;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;

  --border: #1e293b;
  --shadow-card: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-hover: 0 8px 28px rgba(0,0,0,0.4);
  --shadow-bar: 0 4px 20px rgba(0,0,0,0.4);

  --primary-glow: rgba(99, 102, 241, 0.12);
}

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text-primary);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  transition: background 0.35s ease, color 0.35s ease;
}
img { max-width: 100%; height: auto; display: block; }
a { text-decoration: none; color: inherit; }
button { cursor: pointer; font-family: var(--font); border: none; background: none; }
input, select { font-family: var(--font); }

:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
::selection { background: var(--primary); color: #fff; }

#loader {
  position: fixed; inset: 0; z-index: 9999;
  background: var(--bg);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  transition: opacity 0.4s ease, visibility 0.4s ease;
}
#loader.hidden { opacity: 0; visibility: hidden; pointer-events: none; }
.loader-spinner {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  animation: spin 0.7s linear infinite;
  margin-bottom: 14px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loader-text { font-size: 13px; color: var(--text-muted); }

/* ── HEADER (scrolls away) ── */
.top-header {
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
}
.header-inner {
  max-width: 1400px; margin: 0 auto;
  padding: 12px 24px;
  display: flex; align-items: center; justify-content: space-between;
}
.header-brand {
  display: flex; align-items: center; gap: 10px;
  font-weight: 700; font-size: 15px; color: var(--text);
}
.header-brand svg { flex-shrink: 0; }
.header-tools { display: flex; align-items: center; gap: 14px; }
.header-link { font-size: 13px; font-weight: 500; color: var(--text-secondary); transition: var(--transition); }
.header-link:hover { color: var(--primary); }

.theme-btn {
  display: flex; align-items: center; justify-content: center;
  width: 34px; height: 34px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-subtle);
  color: var(--text-secondary);
  transition: var(--transition);
}
.theme-btn:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-glow); }
.theme-btn .sun { display: block; }
.theme-btn .moon { display: none; }
[data-theme="dark"] .theme-btn .sun { display: none; }
[data-theme="dark"] .theme-btn .moon { display: block; }

/* ── COMPACT HERO ── */
.hero-bar {
  padding: 40px 24px 24px;
  background: linear-gradient(135deg, #0f0f1a 0%, #1a1a2e 50%, #0f0f1a 100%);
  text-align: center;
}
.hero-bar-inner { max-width: 1400px; margin: 0 auto; }
.hero-bar h1 {
  font-size: clamp(24px, 3.5vw, 38px);
  font-weight: 800; color: #fff;
  line-height: 1.15; letter-spacing: -1px;
  margin-bottom: 8px;
}
.hero-bar h1 .gradient-text {
  background: linear-gradient(135deg, var(--primary-light), #a78bfa);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
  background-clip: text;
}
.hero-bar p {
  font-size: 14px; color: #94a3b8;
  max-width: 420px; margin: 0 auto 14px; line-height: 1.5;
}
.hero-tags { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.hero-tag {
  padding: 5px 12px;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: var(--radius-pill);
  font-size: 11px; color: #94a3b8;
}
.hero-tag strong { color: #fff; font-weight: 700; }

/* ── STICKY SEARCH ZONE ── */
.sticky-zone {
  position: sticky;
  top: 0;
  z-index: 100;
}
.sticky-bar {
  background: var(--bg-elevated);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-bar);
  transition: background 0.35s ease, box-shadow 0.35s ease;
}
.bar-inner {
  max-width: 1400px;
  margin: 0 auto;
  padding: 18px 24px 10px;
}

.bar-top {
  display: flex;
  gap: 12px;
  align-items: stretch;
  margin-bottom: 14px;
}

.search-box {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: var(--radius-inp);
  transition: var(--transition);
}
.search-box:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px var(--primary-glow);
}
.search-ico { flex-shrink: 0; color: var(--text-muted); }
.search-box input {
  flex: 1;
  border: none; background: transparent;
  font-size: 15px; color: var(--text-primary);
  outline: none;
  padding: 14px 0;
  min-width: 0;
}
.search-box input::placeholder { color: var(--text-muted); }

.clear-btn {
  display: none;
  align-items: center; justify-content: center;
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--bg-subtle);
  color: var(--text-muted);
  transition: var(--transition);
  flex-shrink: 0;
}
.clear-btn.show { display: flex; }
.clear-btn:hover { background: var(--border); color: var(--text-secondary); }

.bar-controls {
  display: flex;
  gap: 8px;
  align-items: stretch;
}

.sort-sel {
  padding: 0 34px 0 14px;
  border: 2px solid var(--border);
  border-radius: var(--radius-inp);
  background: var(--bg);
  color: var(--text-secondary);
  font-size: 13px; font-weight: 500;
  cursor: pointer; outline: none;
  transition: var(--transition);
  appearance: none; -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2.5'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}
.sort-sel:focus { border-color: var(--primary); box-shadow: 0 0 0 4px var(--primary-glow); }

.view-toggle {
  display: flex;
  border: 2px solid var(--border);
  border-radius: var(--radius-inp);
  overflow: hidden;
}
.view-btn {
  display: flex; align-items: center; justify-content: center;
  width: 40px;
  padding: 8px 0;
  background: var(--bg);
  color: var(--text-muted);
  transition: var(--transition);
}
.view-btn.active { background: var(--primary); color: #fff; }
.view-btn:not(.active):hover { background: var(--bg-subtle); color: var(--text-secondary); }

.reset-btn {
  padding: 0 16px;
  border: 2px solid var(--border);
  border-radius: var(--radius-inp);
  background: var(--bg);
  color: var(--text-secondary);
  font-size: 12px; font-weight: 600;
  transition: var(--transition);
  white-space: nowrap;
}
.reset-btn:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-glow); }

.bar-mid {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 6px;
}
.filter-chips {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
  flex: 1;
}
.chip {
  padding: 5px 12px;
  border-radius: var(--radius-pill);
  border: 1.5px solid var(--border);
  background: var(--bg);
  font-size: 11px; font-weight: 500;
  color: var(--text-secondary);
  transition: var(--transition);
  white-space: nowrap;
}
.chip:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-glow); }
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; box-shadow: 0 2px 8px rgba(99,102,241,0.3); }

.result-count { font-size: 12px; font-weight: 500; color: var(--text-muted); white-space: nowrap; }

.bar-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
  min-height: 20px;
}
.meta-count { font-size: 12px; color: var(--text-muted); }
.meta-count strong { color: var(--text-secondary); font-weight: 600; }
.meta-dot { color: var(--border); font-size: 12px; }

.active-tags {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  min-height: 0;
  overflow: hidden;
  transition: var(--transition);
}
.active-tags.has-items { margin-bottom: 4px; }
.active-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  background: var(--primary-glow);
  border: 1px solid var(--primary);
  border-radius: var(--radius-pill);
  font-size: 11px; font-weight: 500;
  color: var(--primary);
}
.active-tag button {
  display: flex; align-items: center; justify-content: center;
  width: 14px; height: 14px;
  border: none; background: transparent;
  color: var(--primary); font-size: 14px;
  line-height: 1; padding: 0;
}

/* ── MAIN AREA ── */
.main-area {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px 24px 50px;
}

/* ── TEMPLATE GRID ── */
.template-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 20px;
}

/* ── LIST VIEW ── */
.template-grid.list-view {
  grid-template-columns: 1fr;
}
.template-grid.list-view .template-card {
  flex-direction: row;
}
.template-grid.list-view .card-preview {
  width: 240px;
  aspect-ratio: 4 / 3;
  flex-shrink: 0;
}
.template-grid.list-view .card-top {
  margin-bottom: 4px;
}
.template-grid.list-view .card-desc {
  -webkit-line-clamp: 1;
}

/* ── EMPTY STATE ── */
.empty-state {
  grid-column: 1 / -1;
  text-align: center; padding: 60px 20px;
}
.empty-state svg { width: 44px; height: 44px; color: var(--text-muted); margin-bottom: 10px; }
.empty-state h3 { font-size: 17px; color: var(--text-primary); margin-bottom: 4px; }
.empty-state p { font-size: 13px; color: var(--text-muted); }

/* ── TEMPLATE CARD ── */
.template-card {
  background: var(--bg-card);
  border-radius: var(--radius-card);
  border: 1px solid var(--border);
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease, border-color 0.3s ease;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-card);
  animation: cardIn 0.35s ease forwards;
  opacity: 0;
}
.template-card:hover {
  transform: translateY(-6px);
  box-shadow: var(--shadow-hover);
  border-color: transparent;
}

.card-preview {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  background: var(--bg-subtle);
  overflow: hidden;
}
.card-preview img {
  width: 100%; height: 100%;
  object-fit: cover;
  transition: transform 0.4s ease;
}
.template-card:hover .card-preview img {
  transform: scale(1.06);
}

.card-preview .fallback {
  position: absolute; inset: 0;
  display: none;
  align-items: center; justify-content: center;
  flex-direction: column; gap: 6px;
}
.card-preview .fallback-icon { font-size: 30px; }
.card-preview .fallback-label { font-size: 13px; font-weight: 600; color: rgba(255,255,255,0.85); }

.preview-overlay {
  position: absolute; inset: 0;
  background: rgba(15,15,26,0.55);
  display: flex;
  align-items: center; justify-content: center;
  gap: 8px;
  opacity: 0;
  transition: opacity 0.25s ease;
}
.template-card:hover .preview-overlay { opacity: 1; }

.ov-btn {
  padding: 9px 20px;
  background: #fff;
  color: #0f172a;
  font-size: 12px; font-weight: 600;
  border-radius: var(--radius-sm);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.ov-btn:hover { transform: scale(1.06); box-shadow: 0 4px 14px rgba(0,0,0,0.2); }
.ov-btn-sub {
  background: rgba(255,255,255,0.15);
  backdrop-filter: blur(4px);
  border: 1px solid rgba(255,255,255,0.25);
  color: #fff;
  padding: 9px 14px;
  font-size: 11px;
}

.card-body {
  padding: 12px 14px 14px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
  gap: 6px;
}

.card-cat {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 9px;
  border-radius: var(--radius-pill);
  font-size: 9px; font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  white-space: nowrap;
}

.card-badges {
  display: flex;
  gap: 3px;
  flex-shrink: 0;
}
.badge {
  padding: 2px 5px;
  border-radius: 3px;
  font-size: 7px; font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.2px;
}
.badge-r { background: #dcfce7; color: #166534; }
.badge-e { background: #dbeafe; color: #1e40af; }
.badge-p { background: #fef3c7; color: #92400e; }

.card-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 3px;
  line-height: 1.3;
}

.card-desc {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.5;
  margin-bottom: 10px;
  flex: 1;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-actions {
  display: flex;
  gap: 5px;
}
.card-actions .btn-sm {
  display: inline-flex;
  align-items: center; justify-content: center;
  gap: 4px;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  font-size: 11px;
  font-weight: 600;
  transition: var(--transition);
  flex: 1;
}
.btn-sm-p {
  background: var(--primary);
  color: #fff;
}
.btn-sm-p:hover { background: var(--primary-dark); }
.btn-sm-o {
  background: transparent;
  border: 1.5px solid var(--border);
  color: var(--text-secondary);
}
.btn-sm-o:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-glow); }

/* ── FOOTER ── */
.page-footer {
  background: #0f0f1a;
  padding: 36px 24px 20px;
  border-top: 1px solid rgba(255,255,255,0.06);
}
.footer-inner { max-width: 1400px; margin: 0 auto; }
.footer-brand { margin-bottom: 16px; }
.footer-brand h3 { font-size: 15px; font-weight: 700; color: #fff; margin-bottom: 4px; }
.footer-brand p { font-size: 12px; color: #64748b; line-height: 1.6; max-width: 440px; }
.footer-links { display: flex; gap: 18px; margin-bottom: 16px; flex-wrap: wrap; }
.footer-links a { font-size: 12px; color: #94a3b8; transition: var(--transition); }
.footer-links a:hover { color: var(--primary-light); }
.footer-bottom {
  display: flex; flex-wrap: wrap;
  justify-content: space-between; align-items: center;
  gap: 10px;
  padding-top: 16px;
  border-top: 1px solid rgba(255,255,255,0.06);
  font-size: 11px; color: #64748b;
}
.footer-bottom a { color: #94a3b8; transition: var(--transition); }
.footer-bottom a:hover { color: var(--primary-light); }

/* ── ANIMATIONS ── */
@keyframes cardIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
.template-card:nth-child(1) { animation-delay: 0.02s; }
.template-card:nth-child(2) { animation-delay: 0.04s; }
.template-card:nth-child(3) { animation-delay: 0.06s; }
.template-card:nth-child(4) { animation-delay: 0.08s; }
.template-card:nth-child(5) { animation-delay: 0.10s; }
.template-card:nth-child(6) { animation-delay: 0.12s; }

/* ── SCROLLBAR ── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-secondary); }

/* ── RESPONSIVE ── */
@media (min-width: 1400px) {
  .template-grid { grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 24px; }
}
@media (max-width: 1200px) {
  .template-grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 18px; }
}
@media (max-width: 1024px) {
  .template-grid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px; }
}
@media (max-width: 768px) {
  .hero-bar { padding: 30px 16px 18px; }
  .hero-bar h1 { font-size: 22px; }
  .hero-bar p { font-size: 13px; }
  .header-inner { padding: 10px 16px; }
  .header-link { display: none; }

  .bar-inner { padding: 14px 16px 8px; }
  .bar-top { flex-direction: column; gap: 8px; }
  .bar-controls { width: 100%; }
  .sort-sel { flex: 1; min-width: 0; padding: 10px 34px 10px 12px; }
  .search-box input { padding: 12px 0; }
  .bar-mid { flex-direction: column; align-items: stretch; }
  .result-count { text-align: right; }

  .template-grid { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 14px; }
  .template-grid.list-view .card-preview { width: 160px; }

  .main-area { padding: 14px 16px 36px; }
}
@media (max-width: 480px) {
  .template-grid { grid-template-columns: 1fr; }
  .template-grid.list-view .template-card { flex-direction: column; }
  .template-grid.list-view .card-preview { width: 100%; }
  .card-preview { aspect-ratio: 16 / 9; }
  .filter-chips { gap: 4px; }
  .chip { padding: 4px 10px; font-size: 10px; }
  .main-area { padding: 10px 12px 28px; }
  .bar-inner { padding: 12px 12px 6px; }
}
