/* ===================== Base & Tema ===================== */
:root {
  --c-bg: #f6f7fb;
  --c-surface: #ffffff;
  --c-input-bg: #ffffff;
  --c-text: #1f2333;
  --c-muted: #5d6478;
  --c-border: #e3e6ef;
  --c-primary: #4f46e5;
  --c-primary-hover: #4338ca;
  --c-secondary-bg: #eef0ff;
  --c-error: #e53935;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 4px 16px rgba(16, 24, 40, .06);
  --max-w: 1200px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

body.dark-theme {
  --c-bg: #0f172a;
  --c-surface: #1e293b;
  --c-input-bg: #0f172a;
  --c-text: #f1f5f9;
  --c-muted: #94a3b8;
  --c-border: #334155;
  --c-primary: #6366f1;
  --c-primary-hover: #818cf8;
  --c-secondary-bg: #312e81;
  --c-error: #ff5252;
  --shadow: 0 2px 8px rgba(0, 0, 0, .35), 0 8px 24px rgba(0, 0, 0, .4);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--c-bg);
  color: var(--c-text);
  line-height: 1.55;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background-color .2s, color .2s;
}

a { color: var(--c-primary); }

.skip-link {
  position: absolute;
  left: -999px;
  top: 8px;
  background: var(--c-primary);
  color: #fff;
  padding: 8px 12px;
  border-radius: 8px;
  z-index: 100;
}
.skip-link:focus { left: 8px; }

/* ===================== Header / Nav ===================== */
.site-header {
  background: var(--c-surface);
  border-bottom: 1px solid var(--c-border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.nav {
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 10px 20px;
  display: flex;
  align-items: center;
  gap: 20px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: var(--c-text);
  font-weight: 700;
  font-size: 1.15rem;
  white-space: nowrap;
}

.nav__links {
  display: flex;
  gap: 18px;
  list-style: none;
  margin: 0;
  padding: 0;
}
.nav__links a {
  color: var(--c-muted);
  text-decoration: none;
  font-weight: 500;
}
.nav__links a:hover { color: var(--c-primary); }

.nav__search {
  margin-left: auto;
  flex: 0 1 320px;
  min-width: 180px;
}

.theme-toggle {
  padding: 8px 14px;
  font-size: .88rem;
  gap: 6px;
  white-space: nowrap;
}

/* Ajustes ligeros del widget de Google PSE */
.nav__search .gsc-control-searchbox-only,
.nav__search .gsc-search-box { margin: 0 !important; }
.nav__search table.gsc-search-box td.gsc-input { padding-right: 4px !important; }
.nav__search .gsc-input-box { border-radius: 8px 0 0 8px; }
.nav__search .gsc-search-button-v2 {
  border-radius: 0 8px 8px 0;
  background: var(--c-primary);
  border-color: var(--c-primary);
  padding: 9px 14px;
}

.search-placeholder {
  display: flex;
  align-items: center;
  padding: 7px 12px;
  border: 1px dashed var(--c-border);
  border-radius: 8px;
  background: var(--c-input-bg);
  color: var(--c-muted);
  font-size: .85rem;
  user-select: none;
}

/* ===================== Layout ===================== */
.layout {
  flex: 1;
  width: 100%;
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 24px 20px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 24px;
  align-items: start;
}

.content { display: grid; gap: 24px; }
.search-results:not(:has(.gsc-results-wrapper-visible)) { display: none; }

.card {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px;
}

h1 { font-size: 1.85rem; line-height: 1.2; margin: 0 0 8px; }
h2 { font-size: 1.35rem; margin: 0 0 12px; }
.lead { color: var(--c-muted); margin: 0 0 24px; }

/* ===================== Formulario ===================== */
.tts-form { display: grid; gap: 18px; }

.field { display: grid; gap: 6px; }
.field__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.field__actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.preset-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin: 2px 0 4px;
}
.preset-bar__label {
  font-size: .82rem;
  font-weight: 600;
  color: var(--c-muted);
  margin-right: 2px;
}
.preset-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font: inherit;
  font-size: .82rem;
  font-weight: 600;
  color: var(--c-text);
  background: var(--c-secondary-bg);
  border: 1px solid var(--c-border);
  border-radius: 999px;
  padding: 5px 13px;
  cursor: pointer;
  user-select: none;
  transition: background .15s ease, color .15s ease, border-color .15s ease, transform .1s ease, box-shadow .15s ease;
}
.preset-chip:hover {
  border-color: var(--c-primary);
  color: var(--c-primary);
  background: var(--c-surface);
  box-shadow: 0 2px 6px rgba(79, 70, 229, .12);
}
.preset-chip:active {
  transform: translateY(1px) scale(.98);
}
.preset-chip:focus-visible {
  outline: none;
  border-color: var(--c-primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, .22);
}
.preset-chip--active {
  background: var(--c-primary);
  color: #fff;
  border-color: var(--c-primary);
}
.preset-chip--active:hover {
  background: var(--c-primary-hover);
  color: #fff;
}
.field label { font-weight: 600; font-size: .95rem; display: flex; align-items: center; gap: 8px; }
.field__hint { color: var(--c-muted); font-size: .85rem; text-align: right; transition: color .15s; }
.field__hint--error,
.char-count--error {
  color: var(--c-error) !important;
  font-weight: 700;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

textarea, select {
  width: 100%;
  font: inherit;
  color: var(--c-text);
  background: var(--c-input-bg);
  border: 1px solid var(--c-border);
  border-radius: 10px;
  padding: 12px 14px;
  transition: border-color .15s, box-shadow .15s;
}
textarea { resize: vertical; min-height: 160px; }
textarea:focus, select:focus {
  outline: none;
  border-color: var(--c-primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, .22);
}

/* Slider de velocidad */
input[type="range"] {
  width: 100%;
  accent-color: var(--c-primary);
  cursor: pointer;
  margin: 4px 0 0;
}

.rate-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--c-secondary-bg);
  color: var(--c-primary);
  font-weight: 700;
  font-size: .85rem;
}

.rate-ticks {
  display: flex;
  justify-content: space-between;
  font-size: .78rem;
  color: var(--c-muted);
}

.actions { display: flex; gap: 12px; flex-wrap: wrap; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font: inherit;
  font-weight: 600;
  border-radius: 10px;
  padding: 11px 22px;
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  transition: background .15s, opacity .15s;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn--primary { background: var(--c-primary); color: #fff; }
.btn--primary:hover:not(:disabled) { background: var(--c-primary-hover); }
.btn--secondary { background: var(--c-secondary-bg); color: var(--c-primary); }
.btn--ghost { background: transparent; border-color: var(--c-border); color: var(--c-text); }
.btn--ghost:hover:not(:disabled) { border-color: var(--c-primary); color: var(--c-primary); }
.btn--sm {
  padding: 6px 12px;
  font-size: .82rem;
  border-radius: 8px;
}

/* ===================== Estado / reproductor ===================== */
.progress {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 18px;
}
.progress progress { flex: 1; height: 8px; accent-color: var(--c-primary); }
.progress span { font-size: .85rem; color: var(--c-muted); min-width: 120px; text-align: right; }

.status { min-height: 1.5em; margin: 14px 0 0; color: var(--c-muted); }
.status--error { color: var(--c-error); }

.player {
  margin-top: 18px;
  padding: 18px;
  border: 1px dashed var(--c-border);
  border-radius: 10px;
  display: grid;
  gap: 14px;
}
.player audio { width: 100%; }
.player .btn { justify-self: start; }

.audio-meta {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 4px 16px;
  margin: 0;
  font-size: .9rem;
}
.audio-meta dt { color: var(--c-muted); }
.audio-meta dd { margin: 0; }

/* ===================== Historial de Audios Recientes (IndexedDB) ===================== */
.history {
  margin-top: 22px;
  border: 1px solid var(--c-border);
  border-radius: 10px;
  background: var(--c-bg);
  overflow: hidden;
}

.history__summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  font-weight: 600;
  background: var(--c-surface);
  user-select: none;
}

.history__title {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.history__badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 7px;
  border-radius: 999px;
  background: var(--c-secondary-bg);
  color: var(--c-primary);
  font-size: .78rem;
  font-weight: 700;
}

.history__body {
  padding: 12px 16px;
  border-top: 1px solid var(--c-border);
}

.history__empty {
  margin: 0;
  color: var(--c-muted);
  font-size: .88rem;
}

.history__list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--c-border);
  border-radius: 8px;
  background: var(--c-surface);
  flex-wrap: wrap;
}

.history-item__info {
  flex: 1 1 240px;
  min-width: 0;
}

.history-item__snippet {
  font-weight: 600;
  font-size: .92rem;
  color: var(--c-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-item__meta {
  font-size: .78rem;
  color: var(--c-muted);
  margin-top: 2px;
}

.history-item__actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.history-item__delete:hover {
  border-color: var(--c-error) !important;
  color: var(--c-error) !important;
}

/* ===================== FAQ ===================== */
.faq details { border-top: 1px solid var(--c-border); padding: 12px 0; }
.faq details:first-of-type { border-top: 0; }
.faq summary { cursor: pointer; font-weight: 600; }
.faq p { margin: 8px 0 0; color: var(--c-muted); }

/* ===================== Publicidad ===================== */
.sidebar { position: sticky; top: 84px; }

.ad-slot {
  overflow: hidden;
  text-align: center;
}
.ad-slot--sidebar { width: 300px; min-height: 600px; }
.ad-slot--bottom {
  width: 100%;
  max-width: var(--max-w);
  min-height: 90px;
  margin: 0 auto 24px;
  padding: 0 20px;
}

/* Marcador visible mientras no haya un ID de editor configurado */
.ad-slot--placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px dashed var(--c-border);
  border-radius: var(--radius);
  background: repeating-linear-gradient(45deg, var(--c-surface), var(--c-surface) 10px, var(--c-bg) 10px, var(--c-bg) 20px);
  color: var(--c-muted);
  font-size: .85rem;
}
.ad-slot--placeholder::before { content: "Espacio publicitario"; }
.ad-slot--placeholder.ad-slot--bottom { width: calc(100% - 40px); }
.ad-slot--placeholder ins { display: none !important; }

/* ===================== Footer & Páginas Legales ===================== */
.site-footer {
  border-top: 1px solid var(--c-border);
  background: var(--c-surface);
  color: var(--c-muted);
  font-size: .9rem;
  padding: 20px;
}
.site-footer__inner {
  max-width: var(--max-w);
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 24px;
}
.site-footer p { margin: 0; }

.footer-nav__list {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
  list-style: none;
  margin: 0;
  padding: 0;
}
.footer-nav__list a {
  color: var(--c-muted);
  text-decoration: none;
  font-weight: 500;
}
.footer-nav__list a:hover,
.footer-nav__list a[aria-current="page"] {
  color: var(--c-primary);
  text-decoration: underline;
}

.layout--single {
  flex: 1;
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
  padding: 28px 20px;
}

.legal-card h1 { margin-bottom: 6px; }
.legal-card .legal-updated { color: var(--c-muted); font-size: .88rem; margin: 0 0 22px; }
.legal-card h2 { font-size: 1.2rem; margin: 24px 0 8px; }
.legal-card p, .legal-card li { color: var(--c-text); }
.legal-card ul { padding-left: 20px; }

/* ===================== Responsive ===================== */
@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; }
  .ad-slot--sidebar { width: 100%; min-height: 250px; }
}

@media (max-width: 640px) {
  .nav { flex-wrap: wrap; gap: 12px; }
  .nav__links { display: none; }
  .nav__search { flex: 1 1 100%; margin-left: 0; }
  .field-row { grid-template-columns: 1fr; }
  .card { padding: 20px; }
  h1 { font-size: 1.5rem; }
}

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