:root {
  --bg: oklch(0.22 0.06 255);
  --bg-elevated: oklch(0.27 0.055 255);
  --bg-hover: oklch(0.32 0.05 255);
  --border: oklch(0.4 0.04 255);
  --ink: oklch(0.97 0.01 255);
  --ink-muted: oklch(0.78 0.02 255);
  --accent: oklch(0.72 0.17 55);
  --accent-hover: oklch(0.78 0.16 55);
  --accent-ink: oklch(0.22 0.04 55);
  --danger-muted: oklch(0.55 0.02 255);
  --radius: 8px;
  --font: "IBM Plex Sans", system-ui, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
  --z-modal: 400;
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
}

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

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: var(--font);
  background:
    radial-gradient(1200px 600px at 10% -10%, oklch(0.3 0.08 255 / 0.55), transparent),
    radial-gradient(900px 500px at 90% 0%, oklch(0.28 0.07 240 / 0.4), transparent),
    var(--bg);
  color: var(--ink);
  line-height: 1.45;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 3rem;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem 1.5rem;
  align-items: flex-end;
  justify-content: space-between;
  margin-bottom: 1.25rem;
}

.brand h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  text-wrap: balance;
}

.subtitle {
  margin: 0.2rem 0 0;
  color: var(--ink-muted);
  font-size: 0.9rem;
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
}

.search input {
  width: min(320px, 70vw);
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
  color: var(--ink);
  font: inherit;
  font-size: 0.9rem;
}

.search input::placeholder {
  color: var(--ink-muted);
}

.search input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.view-toggle {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg-elevated);
}

.toggle {
  border: 0;
  background: transparent;
  color: var(--ink-muted);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 500;
  padding: 0.5rem 0.85rem;
  cursor: pointer;
}

.toggle:hover {
  color: var(--ink);
  background: var(--bg-hover);
}

.toggle.is-active {
  background: var(--accent);
  color: var(--accent-ink);
}

.toggle:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.sources {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.source-chip {
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--ink);
  border-radius: 999px;
  padding: 0.35rem 0.8rem;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.source-chip:hover:not(:disabled) {
  background: var(--bg-hover);
}

.source-chip.is-active {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.source-chip:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.source-chip:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.count {
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--ink-muted);
}

.source-chip.is-active .count {
  color: var(--accent);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.status {
  margin: 0;
  color: var(--ink-muted);
  font-size: 0.875rem;
}

.pagination {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.page-info {
  font-family: var(--mono);
  font-size: 0.8rem;
  color: var(--ink-muted);
  min-width: 7ch;
  text-align: center;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  border: 0;
  border-radius: var(--radius);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.45rem 0.75rem;
  cursor: pointer;
  text-decoration: none;
  transition: background 160ms var(--ease), color 160ms var(--ease), opacity 160ms var(--ease);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-ink);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-ghost {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--border);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--bg-hover);
}

.btn-sm {
  padding: 0.35rem 0.6rem;
  font-size: 0.75rem;
}

.content:empty::before {
  content: "Nenhum template encontrado.";
  display: block;
  color: var(--ink-muted);
  padding: 2rem 0;
}

/* Grid */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}

.card {
  display: flex;
  flex-direction: column;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  min-height: 100%;
  transition: border-color 160ms var(--ease), transform 180ms var(--ease);
}

.card:hover {
  border-color: oklch(0.55 0.08 255);
}

.card-thumb {
  aspect-ratio: 16 / 10;
  background: oklch(0.18 0.05 255);
  overflow: hidden;
  container-type: size;
}

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

.card-thumb .thumb-live {
  width: 100%;
  height: 100%;
  overflow: hidden;
  pointer-events: none;
  background: oklch(0.12 0.04 255);
}

.card-thumb .thumb-live iframe {
  width: 1280px;
  height: 800px;
  border: 0;
  transform: scale(calc(100cqw / 1280));
  transform-origin: top left;
  display: block;
}

.card-thumb .placeholder {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  color: var(--ink-muted);
  font-size: 0.8rem;
  font-family: var(--mono);
}

.card-body {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  padding: 0.85rem;
  flex: 1;
}

.card-source {
  font-family: var(--mono);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--accent);
}

.card-title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  line-height: 1.3;
}

.card-desc {
  margin: 0;
  color: var(--ink-muted);
  font-size: 0.8rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  flex: 1;
}

.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.35rem;
}

/* Table */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

th,
td {
  text-align: left;
  padding: 0.7rem 0.85rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

th {
  color: var(--ink-muted);
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: oklch(0.24 0.05 255);
}

tr:last-child td {
  border-bottom: 0;
}

tbody tr:hover td {
  background: var(--bg-hover);
}

.cell-title {
  font-weight: 600;
}

.cell-desc {
  color: var(--ink-muted);
  max-width: 36ch;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cell-id {
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--ink-muted);
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.empty {
  padding: 2.5rem 1rem;
  text-align: center;
  color: var(--ink-muted);
}

/* Modal */
.modal {
  width: min(1100px, calc(100vw - 2rem));
  height: min(860px, calc(100vh - 2rem));
  max-width: none;
  max-height: none;
  margin: auto;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
  box-shadow: 0 24px 80px oklch(0.1 0.04 255 / 0.55);
}

.modal::backdrop {
  background: oklch(0.12 0.04 255 / 0.72);
}

.modal-chrome {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
}

.modal-meta h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.modal-sub {
  margin: 0.15rem 0 0;
  font-size: 0.75rem;
  color: var(--ink-muted);
  font-family: var(--mono);
}

.modal-actions {
  display: flex;
  gap: 0.5rem;
}

.modal iframe {
  display: block;
  width: 100%;
  height: calc(100% - 64px);
  border: 0;
  background: white;
}

@media (max-width: 640px) {
  .app {
    padding: 1rem 0.85rem 2rem;
  }

  .search input {
    width: 100%;
  }

  .topbar-actions {
    width: 100%;
  }

  th.hide-sm,
  td.hide-sm {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition: none !important;
  }
}
