/* ==========================================================================
   DungeonMaker — estilos
   Paleta: blanco de fondo, naranja #d9542a (primario) y negro #1a1a1a
   para acentos dramáticos. Inspirada en el sello dragón del logo.
   ========================================================================== */

:root {
  --color-bg: #ffffff;
  --color-bg-subtle: #faf6f3;
  --color-surface: #ffffff;
  --color-text: #1a1a1a;
  --color-text-muted: #6b7280;
  --color-border: #ececec;
  --color-border-strong: #d4d4d4;

  --color-primary: #d9542a;
  --color-primary-dark: #a93f1c;
  --color-accent: #ee7a3d;
  --color-accent-soft: #fde4d3;

  --color-dark: #1a1a1a;
  --color-dark-soft: #2a2a2a;

  --color-error: #dc2626;
  --color-error-bg: #fef2f2;
  --color-error-border: #fca5a5;
  --color-error-text: #7f1d1d;

  --decoration-glow-1: rgba(238, 122, 61, 0.10);
  --decoration-glow-2: rgba(26, 26, 26, 0.04);

  --radius: 14px;
  --radius-sm: 8px;
  --radius-xs: 6px;

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 4px 16px rgba(15, 23, 42, 0.06);

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
}

/* ============ Dark theme overrides ============ */
[data-theme="dark"] {
  --color-bg: #0f0f0f;
  --color-bg-subtle: #161616;
  --color-surface: #1c1c1c;
  --color-text: #ededed;
  --color-text-muted: #9ca3af;
  --color-border: #2a2a2a;
  --color-border-strong: #3a3a3a;

  --color-primary: #ee7a3d;
  --color-primary-dark: #d9542a;
  --color-accent: #ff9457;
  --color-accent-soft: #3d2418;

  /* En modo oscuro, "dark" pasa a ser claro: mantiene el contraste
     dramático en gradientes (claro → naranja) que en claro era (negro → naranja). */
  --color-dark: #f5f5f5;
  --color-dark-soft: #d4d4d4;

  --color-error-bg: #2a1414;
  --color-error-border: #7f1d1d;
  --color-error-text: #fca5a5;

  --decoration-glow-1: rgba(238, 122, 61, 0.14);
  --decoration-glow-2: rgba(255, 148, 87, 0.06);

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }
*::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

body {
  font-family: var(--font-sans);
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.25s ease, color 0.25s ease;
}

/* Subtle decorative background */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  background:
    radial-gradient(800px 400px at 10% -10%, var(--decoration-glow-1), transparent 60%),
    radial-gradient(700px 400px at 110% 0%, var(--decoration-glow-2), transparent 60%);
  pointer-events: none;
  z-index: -1;
}

a {
  color: var(--color-primary);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color 0.15s ease;
}
a:hover { border-bottom-color: var(--color-primary); }

strong { color: var(--color-dark); }

/* ==========================================================================
   Header
   ========================================================================== */
.site-header {
  text-align: center;
  padding: 3rem 1.5rem 2rem;
  max-width: 900px;
  margin: 0 auto;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.9rem;
  margin-bottom: 0.5rem;
}

.brand-logo {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: block;
  box-shadow: 0 4px 14px rgba(217, 84, 42, 0.25);
  transition: transform 0.4s ease;
}

.brand:hover .brand-logo {
  transform: rotate(-8deg) scale(1.05);
}

.brand h1 {
  font-size: clamp(2rem, 4vw, 2.75rem);
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, var(--color-dark) 0%, var(--color-primary) 65%, var(--color-accent) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.tagline {
  color: var(--color-text-muted);
  font-size: 1.05rem;
  margin: 0;
}

/* ==========================================================================
   Layout
   ========================================================================== */
main {
  flex: 1;
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  padding: 0 1.5rem 3rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

form { display: contents; }

/* ==========================================================================
   Card
   ========================================================================== */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 1.75rem;
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.2s ease, border-color 0.2s ease,
              background-color 0.25s ease;
}

.card:hover {
  box-shadow: var(--shadow-md);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}

.card-header h2 {
  font-size: 1.15rem;
  font-weight: 600;
  margin: 0;
  color: var(--color-primary);
}

.step-num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--color-dark), var(--color-primary));
  color: white;
  font-size: 0.85rem;
  font-weight: 600;
  flex-shrink: 0;
  box-shadow: 0 2px 6px rgba(217, 84, 42, 0.3);
}

.optional {
  font-weight: 400;
  font-size: 0.9rem;
  color: var(--color-text-muted);
}

.hint {
  color: var(--color-text-muted);
  font-size: 0.92rem;
  margin: 0 0 1.25rem;
}

/* ==========================================================================
   API key section
   ========================================================================== */
.api-key-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.input-with-icon {
  position: relative;
  flex: 1;
}

.input-with-icon input {
  width: 100%;
  padding-right: 3rem;
}

.input-with-icon .icon-btn {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
}

.checkbox-row {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  color: var(--color-text-muted);
  cursor: pointer;
  user-select: none;
}

.checkbox-row input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--color-primary);
  cursor: pointer;
}

/* ==========================================================================
   Inputs
   ========================================================================== */
input[type="text"],
input[type="password"],
textarea,
select {
  width: 100%;
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-xs);
  font-size: 0.95rem;
  font-family: inherit;
  color: var(--color-text);
  background: var(--color-surface);
  transition: border-color 0.15s ease, box-shadow 0.15s ease,
              background-color 0.25s ease;
}

textarea {
  resize: vertical;
  min-height: 60px;
}

input:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px rgba(238, 122, 61, 0.18);
}

input::placeholder,
textarea::placeholder {
  color: #9ca3af;
}

select {
  appearance: none;
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23d9542a'%3E%3Cpath d='M8 11 3 6h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  background-size: 14px;
  padding-right: 2.25rem;
}

/* ==========================================================================
   Theme grid
   ========================================================================== */
.theme-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 0.6rem;
}

.theme-card {
  position: relative;
  cursor: pointer;
  user-select: none;
}

.theme-card input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.theme-card span {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.85rem 0.5rem;
  border: 2px solid var(--color-border);
  border-radius: var(--radius-sm);
  text-align: center;
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--color-text);
  background: var(--color-surface);
  transition: all 0.15s ease;
  min-height: 56px;
}

.theme-card:hover span {
  border-color: var(--color-accent);
  color: var(--color-primary);
  transform: translateY(-1px);
}

.theme-card input:checked + span {
  border-color: var(--color-primary);
  background: linear-gradient(135deg, var(--color-primary), var(--color-accent));
  color: white;
  box-shadow: 0 4px 12px rgba(217, 84, 42, 0.3);
}

.theme-card input:focus-visible + span {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

/* ==========================================================================
   Custom fields & config row
   ========================================================================== */
.custom-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.field-label {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--color-text);
}

.field-required {
  display: inline-block;
  margin-left: 0.4rem;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  background: var(--color-accent-soft);
  color: var(--color-primary);
  font-size: 0.7rem;
  font-weight: 500;
  font-style: italic;
  letter-spacing: 0.01em;
  vertical-align: middle;
}

.field.full-width {
  grid-column: 1 / -1;
}

.config-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

@media (max-width: 600px) {
  .custom-fields,
  .config-row {
    grid-template-columns: 1fr;
  }
}

/* ==========================================================================
   Buttons
   ========================================================================== */
.primary-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  width: 100%;
  padding: 1rem 1.5rem;
  background: linear-gradient(135deg, var(--color-dark) 0%, var(--color-primary) 60%, var(--color-accent) 100%);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 1.05rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease;
  box-shadow: 0 4px 14px rgba(217, 84, 42, 0.35);
}

.primary-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 6px 20px rgba(217, 84, 42, 0.45);
}

.primary-btn:active:not(:disabled) {
  transform: translateY(0);
}

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

.primary-btn svg {
  width: 22px;
  height: 22px;
  fill: currentColor;
}

.ghost-btn {
  padding: 0.5rem 0.9rem;
  background: var(--color-surface);
  color: var(--color-primary);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-xs);
  font-size: 0.88rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s ease;
}

.ghost-btn:hover {
  border-color: var(--color-accent);
  color: var(--color-primary-dark);
  background: var(--color-accent-soft);
}

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

.ghost-btn.primary-ghost {
  border-color: var(--color-primary);
  color: var(--color-primary);
  font-weight: 600;
}

.ghost-btn.primary-ghost:hover:not(:disabled) {
  background: var(--color-accent-soft);
  border-color: var(--color-primary-dark);
  color: var(--color-primary-dark);
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  background: transparent;
  border: none;
  border-radius: var(--radius-xs);
  cursor: pointer;
  transition: background 0.15s ease;
}

.icon-btn:hover { background: var(--color-bg-subtle); }

.icon-btn svg {
  width: 18px;
  height: 18px;
  fill: var(--color-text-muted);
}

/* ==========================================================================
   Loading
   ========================================================================== */
.loading-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  text-align: center;
  padding: 2.5rem 1.5rem;
}

.d20-spinner svg {
  width: 56px;
  height: 56px;
  fill: var(--color-primary);
  animation: roll 1.6s cubic-bezier(0.5, 0.05, 0.5, 0.95) infinite;
}

@keyframes roll {
  0%   { transform: rotate(0deg) scale(1);   }
  25%  { transform: rotate(180deg) scale(1.1);}
  50%  { transform: rotate(360deg) scale(1); }
  75%  { transform: rotate(540deg) scale(1.1);}
  100% { transform: rotate(720deg) scale(1); }
}

.loading-text {
  color: var(--color-text-muted);
  font-style: italic;
  margin: 0;
}

/* ==========================================================================
   Error
   ========================================================================== */
.error-card {
  border-color: var(--color-error-border);
  background: var(--color-error-bg);
}

.error-card h3 {
  margin: 0 0 0.5rem;
  color: var(--color-error);
  font-size: 1rem;
}

.error-card p {
  margin: 0;
  color: var(--color-error-text);
  font-size: 0.92rem;
}

/* ==========================================================================
   Story output
   ========================================================================== */
.story-card {
  border-left: 4px solid var(--color-primary);
}

.story-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--color-border);
}

.story-header h2 {
  margin: 0;
  color: var(--color-primary);
  font-size: 1.25rem;
}

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

.story-content {
  font-family: var(--font-serif);
  font-size: 1.05rem;
  line-height: 1.75;
  color: var(--color-text);
}

.story-content h1,
.story-content h2,
.story-content h3 {
  font-family: var(--font-sans);
  color: var(--color-primary);
  margin: 1.75rem 0 0.75rem;
  line-height: 1.3;
}

.story-content h1 { font-size: 1.6rem; }
.story-content h2 { font-size: 1.3rem; }
.story-content h3 { font-size: 1.1rem; }

.story-content h1:first-child,
.story-content h2:first-child {
  margin-top: 0;
}

.story-content p { margin: 0 0 1rem; }
.story-content ul,
.story-content ol { margin: 0 0 1rem 1.5rem; }
.story-content li { margin-bottom: 0.4rem; }
.story-content strong { color: var(--color-primary-dark); }
.story-content em { color: var(--color-text-muted); }

.story-content::after {
  content: "▌";
  display: inline;
  color: var(--color-accent);
  animation: blink 1s steps(2) infinite;
}
.story-content.done::after { content: ""; }

/* Inline empty/loading states for the characters section */
.chars-empty {
  margin: 0;
  padding: 0.5rem 0;
  color: var(--color-text-muted);
  font-size: 0.95rem;
  line-height: 1.6;
  font-style: italic;
}

.chars-loading-inline {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 1.5rem 0 0.5rem;
}
.chars-loading-inline .d20-spinner svg {
  width: 40px;
  height: 40px;
}

@keyframes blink {
  50% { opacity: 0; }
}

/* ==========================================================================
   Footer
   ========================================================================== */
.site-footer {
  text-align: center;
  padding: 2rem 1.5rem;
  color: var(--color-text-muted);
  font-size: 0.88rem;
  border-top: 1px solid var(--color-border);
  margin-top: auto;
}

.site-footer p { margin: 0; }

/* ==========================================================================
   Theme toggle (top-right)
   ========================================================================== */
.theme-toggle {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 100;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--color-primary);
  box-shadow: var(--shadow-md);
  transition: transform 0.2s ease, background-color 0.25s ease,
              border-color 0.25s ease, color 0.25s ease;
}

.theme-toggle:hover {
  transform: scale(1.06) rotate(-12deg);
  border-color: var(--color-primary);
}

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

.theme-toggle svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
  transition: opacity 0.25s ease, transform 0.4s ease;
}

.theme-toggle .icon-moon { display: none; }
[data-theme="dark"] .theme-toggle .icon-sun { display: none; }
[data-theme="dark"] .theme-toggle .icon-moon { display: inline-block; }
