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

:root {
  --bg: #0a0a14;
  --bg2: #10101f;
  --card-bg: #161628;
  --card-border: #1e1e3a;
  --text: #eaeaf5;
  --text2: #8888a8;
  --text3: #5a5a78;
  --accent: #7c3aed;
  --accent2: #a855f7;
  --accent-glow: rgba(124, 58, 237, 0.3);
  --green: #22c55e;
  --radius: 14px;
  --radius-sm: 10px;
}

html, body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

/* === Header === */
.header {
  padding: 16px 20px 12px;
  text-align: center;
}

.header h1 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 4px;
}

.header p {
  font-size: 13px;
  color: var(--text2);
}

/* === Categories === */
.categories {
  display: flex;
  gap: 8px;
  padding: 8px 16px 12px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.categories::-webkit-scrollbar {
  display: none;
}

.cat-btn {
  flex-shrink: 0;
  padding: 8px 16px;
  border-radius: 20px;
  border: 1px solid var(--card-border);
  background: var(--card-bg);
  color: var(--text2);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}

.cat-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  box-shadow: 0 2px 12px var(--accent-glow);
}

.cat-btn:active {
  transform: scale(0.95);
}

/* === Templates Grid === */
.templates-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  padding: 4px 16px 100px;
}

/* === Template Card === */
.template-card {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  position: relative;
  animation: fadeInUp 0.3s ease both;
}

.template-card:active {
  transform: scale(0.97);
  border-color: var(--accent);
}

.template-card .video-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 9 / 16;
  background: var(--bg2);
  overflow: hidden;
}

.template-card video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.template-card .play-icon {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 40px;
  height: 40px;
  background: rgba(0,0,0,0.5);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  transition: opacity 0.2s;
}

.template-card .play-icon svg {
  width: 18px;
  height: 18px;
  fill: #fff;
  margin-left: 2px;
}

.template-card.playing .play-icon {
  opacity: 0;
}

.template-card .card-info {
  padding: 10px 12px;
}

.template-card .card-name {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.template-card .card-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text2);
}

.template-card .card-cost {
  color: var(--green);
  font-weight: 600;
}

.template-card .card-duration {
  color: var(--text3);
}

.template-card:nth-child(1) { animation-delay: 0.02s; }
.template-card:nth-child(2) { animation-delay: 0.06s; }
.template-card:nth-child(3) { animation-delay: 0.10s; }
.template-card:nth-child(4) { animation-delay: 0.14s; }
.template-card:nth-child(5) { animation-delay: 0.18s; }
.template-card:nth-child(6) { animation-delay: 0.22s; }

/* === Preview Page === */
.preview-page {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 200;
  display: flex;
  flex-direction: column;
  transition: opacity 0.25s, transform 0.25s;
}

.preview-page.hidden {
  opacity: 0;
  transform: translateY(20px);
  pointer-events: none;
}

.preview-close {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 210;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: rgba(0,0,0,0.5);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

.preview-close:active {
  transform: scale(0.9);
}

.preview-title {
  text-align: center;
  padding: 16px 48px;
  font-size: 17px;
  font-weight: 600;
  font-family: monospace, 'Courier New', monospace;
  letter-spacing: 0.5px;
}

.preview-video-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 16px;
  overflow: hidden;
}

.preview-video-wrap video {
  width: 100%;
  max-height: 100%;
  border-radius: var(--radius);
  background: #000;
}

.preview-select-btn {
  margin: 16px 20px;
  padding: 16px;
  border-radius: var(--radius);
  border: none;
  background: #fff;
  color: #000;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: all 0.15s;
}

.preview-select-btn:active {
  transform: scale(0.97);
  background: #e0e0e0;
}

@supports (margin-bottom: env(safe-area-inset-bottom)) {
  .preview-select-btn {
    margin-bottom: calc(16px + env(safe-area-inset-bottom));
  }
}

/* === Empty state === */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text3);
  grid-column: 1 / -1;
}

.empty-state .emoji {
  font-size: 48px;
  margin-bottom: 12px;
}

.empty-state p {
  font-size: 14px;
}

/* === Animations === */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* === Loading overlay === */
.loading {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 300;
  transition: opacity 0.3s;
}

.loading.hidden {
  opacity: 0;
  pointer-events: none;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--card-border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* === Safe area === */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .templates-grid {
    padding-bottom: calc(100px + env(safe-area-inset-bottom));
  }
}
