*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f4f7fb;
  --surface: rgba(255, 255, 255, 0.92);
  --surface-strong: #ffffff;
  --surface-soft: #eef3fb;
  --text: #152033;
  --text-muted: #64748b;
  --border: rgba(148, 163, 184, 0.22);
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: rgba(37, 99, 235, 0.1);
  --danger: #dc2626;
  --danger-soft: rgba(220, 38, 38, 0.12);
  --success: #16a34a;
  --shadow: 0 18px 45px rgba(15, 23, 42, 0.08);
  --shadow-soft: 0 8px 18px rgba(15, 23, 42, 0.06);
  --radius-xl: 24px;
  --radius-lg: 18px;
  --radius-md: 14px;
  --radius-sm: 12px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}

[data-theme="dark"] {
  --bg: #09111f;
  --surface: rgba(18, 27, 46, 0.92);
  --surface-strong: #101a2d;
  --surface-soft: #16233c;
  --text: #e8eefc;
  --text-muted: #97a7c3;
  --border: rgba(148, 163, 184, 0.16);
  --primary: #7aa2ff;
  --primary-hover: #5f8eff;
  --primary-soft: rgba(122, 162, 255, 0.14);
  --danger: #f87171;
  --danger-soft: rgba(248, 113, 113, 0.16);
  --success: #4ade80;
  --shadow: 0 22px 48px rgba(0, 0, 0, 0.34);
  --shadow-soft: 0 12px 24px rgba(0, 0, 0, 0.24);
  color-scheme: dark;
}

html { height: 100%; -webkit-text-size-adjust: 100%; }
body {
  min-height: 100%;
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(37, 99, 235, 0.14), transparent 30%),
    radial-gradient(circle at top right, rgba(14, 165, 233, 0.10), transparent 28%),
    var(--bg);
  overflow: hidden;
}

[hidden] { display: none !important; }
.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;
}

.login-screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card,
.card,
.settings-card,
.hero-card,
.preview-wrap,
.bottom-nav {
  backdrop-filter: blur(14px);
}

.login-card {
  width: min(100%, 420px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 36px 30px;
  box-shadow: var(--shadow);
  text-align: center;
}

.login-logo { font-size: 46px; margin-bottom: 10px; }
.login-card h1 { font-size: 28px; margin-bottom: 6px; }
.login-subtitle { color: var(--text-muted); margin-bottom: 24px; }
.login-card form { display: grid; gap: 12px; }

.form-error {
  color: var(--danger);
  background: var(--danger-soft);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 13px;
}

.link-btn {
  margin-top: 16px;
  border: none;
  background: none;
  color: var(--primary);
  font-weight: 600;
  cursor: pointer;
}

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: calc(16px + var(--safe-t)) 20px 18px;
}

.top-nav {
  display: flex;
  gap: 10px;
  padding: 0 20px 18px;
  flex-wrap: wrap;
}

.app-title { font-size: 24px; font-weight: 800; letter-spacing: -0.02em; }
.app-subtitle { color: var(--text-muted); font-size: 13px; margin-top: 4px; }
.header-actions { display: flex; align-items: center; gap: 10px; }

.app-main {
  flex: 1;
  overflow-y: auto;
  padding: 0 20px 28px;
}

.section { display: none; animation: fadeIn .22s ease; }
.section.active { display: block; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.hero-card,
.card,
.settings-card,
.preview-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-soft);
}

.hero-card {
  padding: 22px;
  margin-bottom: 18px;
  display: grid;
  gap: 18px;
}

.hero-card.compact { padding: 20px 22px; }
.eyebrow {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .12em;
  color: var(--primary);
  font-weight: 700;
  margin-bottom: 8px;
}

.hero-card h2,
.card h2,
.settings-card h3,
.preview-header h2 {
  font-size: 21px;
  line-height: 1.2;
}

.hero-text,
.settings-card p,
.preview-note,
.field-help,
.status,
.template-meta,
.field-name {
  color: var(--text-muted);
}

.hero-actions {
  display: grid;
  gap: 10px;
  grid-template-columns: minmax(0, 1fr) auto;
}

.editor-grid {
  display: grid;
  gap: 16px;
  grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
  align-items: start;
}

.panel-card,
.card,
.settings-card,
.preview-wrap {
  padding: 18px;
}

.section-mini-header,
.preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.fields {
  display: grid;
  gap: 12px;
  max-height: 520px;
  overflow: auto;
  padding-right: 4px;
}

.field-item { display: grid; gap: 6px; }
.field-name {
  font-size: 12px;
  font-weight: 600;
}

.preview-wrap {
  min-height: 560px;
  background: linear-gradient(180deg, var(--surface) 0%, var(--surface-soft) 100%);
}

.preview {
  min-height: 460px;
  overflow: auto;
  padding: 8px;
  border-radius: var(--radius-lg);
  background: rgba(255, 255, 255, 0.58);
}

[data-theme="dark"] .preview {
  background: rgba(9, 17, 31, 0.44);
}

.preview .ak-editable {
  outline: 2px dashed rgba(37, 99, 235, 0.45);
  border-radius: 8px;
  padding: 1px 4px;
  background: var(--primary-soft);
  color: var(--primary) !important;
  min-width: 28px;
  display: inline-block;
}

.preview .ak-editable:empty::before {
  content: attr(data-placeholder);
  color: var(--text-muted);
  opacity: 0.72;
}

.editor-actions {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}

.upload-card { display: grid; gap: 14px; margin-bottom: 18px; }
.upload-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.file-selection {
  min-height: 48px;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  border: 1px dashed var(--border);
  background: var(--surface-soft);
  color: var(--text-muted);
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.file-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--surface-strong);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
}

.card-list {
  display: grid;
  gap: 12px;
}

.template-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 14px;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  box-shadow: var(--shadow-soft);
}

.template-name { font-weight: 700; word-break: break-word; }
.template-meta { font-size: 13px; margin-top: 4px; }
.template-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.settings-list {
  display: grid;
  gap: 16px;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}

.settings-save { margin-top: 16px; width: 100%; }
.field-label {
  display: block;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 6px;
  margin-top: 10px;
}

.field-help {
  font-size: 13px;
  margin-top: 6px;
}

input[type="text"],
input[type="email"],
input[type="password"],
select {
  width: 100%;
  border: 1px solid transparent;
  background: var(--surface-strong);
  color: var(--text);
  padding: 13px 14px;
  border-radius: var(--radius-md);
  font-size: 15px;
  box-shadow: inset 0 0 0 1px var(--border);
  transition: .18s ease;
}

input:focus,
select:focus {
  outline: none;
  box-shadow: inset 0 0 0 2px var(--primary-soft), 0 0 0 3px rgba(37, 99, 235, 0.12);
  border-color: var(--primary);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  background: var(--surface-soft);
  color: var(--text);
  transition: transform .12s ease, background .18s ease, border-color .18s ease, box-shadow .18s ease;
}

.btn:hover { transform: translateY(-1px); }
.btn:disabled { opacity: .6; cursor: not-allowed; transform: none; }
.btn.primary {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 10px 22px rgba(37, 99, 235, 0.22);
}
.btn.primary:hover { background: var(--primary-hover); }
.btn.outline {
  background: transparent;
  border-color: var(--border);
}
.btn.danger {
  background: var(--danger);
  color: #fff;
}
.btn.small { min-height: 38px; padding: 0 12px; font-size: 13px; }
.btn.full { width: 100%; }

.icon-btn {
  width: 44px;
  height: 44px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  box-shadow: var(--shadow-soft);
}

.nav-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: var(--shadow-soft);
}

.nav-btn span { font-size: 13px; }
.nav-btn.active {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: rgba(37, 99, 235, 0.28);
}

@media (max-width: 980px) {
  .editor-grid { grid-template-columns: 1fr; }
  .preview-wrap { min-height: 420px; }
}

@media (max-width: 640px) {
  .app-header { padding-left: 16px; padding-right: 16px; }
  .top-nav { padding-left: 16px; padding-right: 16px; }
  .app-main { padding-left: 16px; padding-right: 16px; }
  .hero-actions { grid-template-columns: 1fr; }
  .template-row { grid-template-columns: 1fr; }
  .template-actions { width: 100%; }
  .template-actions .btn { flex: 1; }
}

@media print {
  .app-header,
  .top-nav,
  .editor-actions,
  #sec-plantillas,
  #sec-cuenta,
  .hero-card,
  .panel-card {
    display: none !important;
  }

  .app-main { padding: 0; }
  .section,
  .section.active { display: block !important; }
  .preview-wrap,
  .preview {
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    padding: 0 !important;
  }

  .preview .ak-editable {
    outline: none !important;
    background: transparent !important;
    color: inherit !important;
  }
}
