:root {
  --tv-bg: #f3f4f6; --tv-surface: #ffffff; --tv-ink: #16181d; --tv-ink-soft: #5b606b;
  --tv-line: #e8e9ee; --tv-line-strong: #d9dbe2; --tv-accent: #3b5bff;
  --tv-radius: 16px; --tv-radius-sm: 11px;
  --tv-shadow: 0 1px 2px rgba(16,18,23,.04), 0 12px 32px -16px rgba(16,18,23,.18);
  --bs-font-sans-serif: 'Instrument Sans', sans-serif;
  --bs-body-font-family: var(--bs-font-sans-serif);
  --bs-body-bg: var(--tv-bg); --bs-body-color: var(--tv-ink);
  --bs-border-color: var(--tv-line);
  --bs-border-radius: 0; --bs-border-radius-sm: 0; --bs-border-radius-lg: 0;
  --bs-primary: var(--tv-accent); --bs-primary-rgb: 59,91,255;
  --muted: var(--tv-ink-soft);
}
body { -webkit-font-smoothing: antialiased; }

/* .tv-brand-mark / .tv-ver-pill / .tv-btn-ink / .tv-btn-ghost / header layout
   now come from /styles.css (the editor's own stylesheet, loaded above) —
   this used to duplicate them locally, which is exactly how this page's
   header drifted out of sync with the editor's. Only page-specific rules
   (not header ones) belong in this file from here on. */

html.templates-page,
body.templates-page {
  overflow: auto;
  height: auto;
  min-height: 100dvh;
  overscroll-behavior: auto;
  -webkit-overflow-scrolling: touch;
}

.templates-main {
  /* No top-offset needed: the shared header (styles.css) is position:sticky,
     not fixed, so it occupies normal document flow — unlike the old
     page-local .tv-topbar this replaced, which was position:fixed. */
  padding: 28px 24px 48px;
  padding-bottom: max(48px, env(safe-area-inset-bottom, 0px));
}

.templates-hero {
  max-width: 640px;
  margin: 0 auto 36px;
}
.templates-hero > p:first-child {
  font-size: .72rem; letter-spacing: .08em; font-weight: 700; color: var(--tv-ink-soft); text-transform: uppercase;
}

.templates-title {
  font-size: clamp(1.65rem, 3.8vw, 2.15rem);
  font-weight: 600;
  letter-spacing: -0.025em;
  line-height: 1.15;
  margin-bottom: 12px;
}

.templates-grid-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
  overflow: hidden;
}

.templates-grid {
  display: grid;
  grid-template-columns: repeat(3, calc(600px * 9 / 16));
  gap: 40px 32px;
  justify-content: center;
  align-items: stretch;
  width: fit-content;
  max-width: 100%;
  padding: 0 24px;
  box-sizing: border-box;
}

.template-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
  height: 100%;
}

.template-preview-host {
  position: relative;
  width: calc(600px * 9 / 16);
  aspect-ratio: 9 / 16;
  overflow: hidden;
  flex-shrink: 0;
  background: #1a1918;
  line-height: 0;
  border-radius: 16px;
  box-shadow: var(--tv-shadow);
}

.template-preview-host iframe {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease;
}

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

.template-preview-host.is-ready iframe { opacity: 1; }

.template-preview-host.is-loading::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 2;
  background: rgba(0,0,0,.35);
}

.template-preview-host.is-loading::before {
  content: '';
  position: absolute;
  top: 50%; left: 50%;
  z-index: 3;
  width: 28px; height: 28px;
  margin: -14px 0 0 -14px;
  border: 2px solid rgba(255,255,255,.25);
  border-top-color: #fff;
  border-radius: 50%;
  animation: template-spin .7s linear infinite;
}

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

.template-item.is-previewing .template-preview-host { box-shadow: 0 0 0 2px var(--tv-accent); }

.template-meta {
  display: flex;
  flex-direction: column;
  flex: 1;
  gap: 6px;
  margin-top: 10px;
  width: calc(600px * 9 / 16);
  max-width: 100%;
}

.template-meta h2 {
  margin: 0;
  font-size: .9375rem;
  font-weight: 600;
}

.template-meta p {
  margin: 0;
  font-size: .875rem;
  line-height: 1.45;
  color: var(--muted);
  min-height: calc(.875rem * 1.45 * 2);
}

.template-actions { margin-top: auto; padding-top: 6px; }
.action-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.action-row .btn { width: 100%; }

.templates-empty, .templates-error {
  grid-column: 1 / -1;
  max-width: 520px;
  margin: 0 auto;
  padding: 24px;
  text-align: center;
  color: var(--muted);
}

@media (max-width: 1100px) {
  .templates-grid {
    grid-template-columns: repeat(2, 1fr);
    width: 100%;
    max-width: calc(2 * (600px * 9 / 16) + 32px + 48px);
  }
  .template-preview-host { width: 100%; max-width: calc(600px * 9 / 16); }
  .template-meta { width: 100%; max-width: calc(600px * 9 / 16); }
}

@media (max-width: 720px) {
  .templates-main { padding: calc(56px + 22px) 16px 36px; }
  .templates-grid { grid-template-columns: 1fr; width: 100%; padding: 0; }
  .template-preview-host { width: min(calc(600px * 9 / 16), 100vw - 32px); height: auto; max-width: none; }
  .template-meta { width: min(calc(600px * 9 / 16), 100vw - 32px); max-width: none; }
}
