/* ============================================
   ASCII FORGE — Gothic Cathedral Edition
   ============================================ */

@import url('https://fonts.googleapis.com/css2?family=Cinzel:wght@400;500;600;700;800;900&family=Cinzel+Decorative:wght@400;700;900&family=Cormorant+Garamond:ital,wght@0,300;0,400;0,500;0,600;1,400&family=JetBrains+Mono:wght@400;500;700;800&display=swap');

:root,
[data-theme='light'] {
  --font-display: 'Cinzel', Georgia, serif;
  --font-ornate: 'Cinzel Decorative', Georgia, serif;
  --font-body: 'Cormorant Garamond', Georgia, serif;
  --font-mono: 'JetBrains Mono', 'Courier New', monospace;

  /* Gothic light mode — aged parchment */
  --bg: #e8e0d0;
  --surface: #f0e8d8;
  --surface-2: #e8dfcc;
  --surface-offset: #ddd3bd;
  --border: #c4b896;
  --divider: #d4c8a8;

  --text: #2a1f14;
  --text-muted: #6b5d44;
  --text-faint: #9a8e72;

  --accent: #8b1a1a;
  --accent-bright: #a52a2a;
  --accent-glow: rgba(139, 26, 26, 0.15);
  --accent-soft: rgba(139, 26, 26, 0.06);
  --gold: #b8860b;
  --gold-soft: rgba(184, 134, 11, 0.1);

  --danger: #8b0000;
  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 8px;

  --shadow-sm: 0 1px 3px rgba(42, 31, 20, 0.1);
  --shadow-md: 0 4px 16px rgba(42, 31, 20, 0.12);
  --shadow-lg: 0 12px 40px rgba(42, 31, 20, 0.18);

  --ease: cubic-bezier(0.16, 1, 0.3, 1);
  --dur: 200ms;

  --grid-line: rgba(139, 26, 26, 0.02);
  --texture-opacity: 0.03;
}

[data-theme='dark'] {
  /* Dark gray palette — cool neutral charcoal, no brown */
  --bg: #121214;
  --surface: #1a1a1e;
  --surface-2: #222226;
  --surface-offset: #2a2a2e;
  --border: #38383e;
  --divider: #2e2e32;

  --text: #d8d8dc;
  --text-muted: #88888f;
  --text-faint: #555560;

  --accent: #a52a2a;
  --accent-bright: #c44a4a;
  --accent-glow: rgba(165, 42, 42, 0.15);
  --accent-soft: rgba(165, 42, 42, 0.06);
  --gold: #c9a227;
  --gold-soft: rgba(201, 162, 39, 0.08);

  --danger: #c44;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.6);

  --grid-line: rgba(255, 255, 255, 0.015);
  --texture-opacity: 0.04;
}

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

html {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  scroll-behavior: smooth;
}

body {
  min-height: 100dvh;
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  overflow-x: hidden;
  position: relative;
}

/* Noise texture overlay for gothic feel */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  opacity: var(--texture-opacity);
  pointer-events: none;
  z-index: 0;
  mix-blend-mode: overlay;
}

::selection { background: var(--accent-glow); color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; border-radius: var(--radius-sm); }

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

.grid-bg {
  position: fixed; inset: 0;
  background-image: linear-gradient(var(--grid-line) 1px, transparent 1px), linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
  background-size: 40px 40px;
  pointer-events: none; z-index: 0;
}

/* Ornamental divider */
.ornament {
  display: flex; align-items: center; justify-content: center;
  gap: 0.5rem; margin: 0.25rem 0;
}
.ornament::before, .ornament::after {
  content: ''; flex: 1; height: 1px;
  background: linear-gradient(90deg, transparent, var(--border), transparent);
}
.ornament-icon { color: var(--accent); font-size: 0.8rem; opacity: 0.6; }

/* Header */
.header {
  position: relative; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: 1.2rem 2rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.header::after {
  content: '';
  position: absolute; bottom: -1px; left: 50%; transform: translateX(-50%);
  width: 60%; height: 1px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  opacity: 0.4;
}

.logo { display: flex; align-items: center; gap: 0.65rem; color: var(--accent); }
.logo-text { font-family: var(--font-ornate); font-weight: 900; font-size: 1.25rem; letter-spacing: 0.02em; color: var(--text); }
.logo-accent { color: var(--accent); }

.theme-toggle {
  display: flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border-radius: var(--radius-sm);
  color: var(--text-muted); cursor: pointer; border: 1px solid var(--border);
  background: var(--surface-2);
  transition: color var(--dur) var(--ease), background var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
.theme-toggle:hover { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }

/* Main */
.main { position: relative; z-index: 1; max-width: 1500px; margin: 0 auto; padding: 2rem 2rem 4rem; }

.hero { text-align: center; margin-bottom: 2rem; padding-top: 1rem; }
.hero-title { font-family: var(--font-ornate); font-size: clamp(2rem, 1rem + 4vw, 3.5rem); font-weight: 900; letter-spacing: 0.01em; line-height: 1.15; margin-bottom: 0.3rem; }
.hero-title .accent { color: var(--accent); }
.hero-subtitle { font-family: var(--font-body); font-size: 1.1rem; font-style: italic; color: var(--text-muted); max-width: 440px; margin: 0 auto; }

/* Workspace */
.workspace { display: grid; grid-template-columns: 350px 1fr; gap: 1.5rem; align-items: start; }
@media (max-width: 960px) { .workspace { grid-template-columns: 1fr; } }

/* Controls Panel — gothic frame */
.controls-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  display: flex; flex-direction: column; gap: 1.5rem;
  box-shadow: var(--shadow-md);
  position: sticky; top: 1rem;
  max-height: calc(100vh - 2rem); overflow-y: auto;
}
@media (max-width: 960px) { .controls-panel { position: static; max-height: none; } }

.controls-panel::-webkit-scrollbar { width: 5px; }
.controls-panel::-webkit-scrollbar-track { background: transparent; }
.controls-panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.control-section { display: flex; flex-direction: column; gap: 0.6rem; }

.section-header { display: flex; align-items: center; justify-content: space-between; }
.section-label { font-family: var(--font-display); font-size: 0.72rem; font-weight: 600; letter-spacing: 0.12em; text-transform: uppercase; color: var(--text-faint); margin-bottom: 0.2rem; }

/* Reset Button */
.reset-btn {
  display: flex; align-items: center; justify-content: center;
  width: 22px; height: 22px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text-faint); cursor: pointer; font-size: 0.8rem; line-height: 1;
  transition: all var(--dur) var(--ease); flex-shrink: 0;
}
.reset-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.reset-btn:active { transform: scale(0.9); }

/* Dropzone — gothic arch feel */
.dropzone {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 0.5rem; padding: 2rem 1rem;
  border: 2px dashed var(--border); border-radius: var(--radius-md);
  color: var(--text-muted); text-align: center; cursor: pointer;
  transition: border-color var(--dur) var(--ease), background var(--dur) var(--ease), color var(--dur) var(--ease);
}
.dropzone:hover, .dropzone.drag-over { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.dropzone:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.dropzone-text { font-family: var(--font-body); font-size: 1rem; font-weight: 500; line-height: 1.4; }
.dropzone-text span { color: var(--text-faint); font-size: 0.85rem; }

/* Charset */
.charset-presets { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.preset-btn {
  font-family: var(--font-mono); font-size: 0.72rem; font-weight: 600;
  padding: 0.38rem 0.65rem; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text-muted);
  transition: all var(--dur) var(--ease); cursor: pointer;
}
.preset-btn:hover { border-color: var(--accent); color: var(--accent); }
.preset-btn.active { background: var(--accent); border-color: var(--accent); color: var(--bg); }

.charset-input {
  font-family: var(--font-mono); font-size: 0.82rem;
  padding: 0.5rem 0.65rem; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  width: 100%; letter-spacing: 0.05em;
  transition: border-color var(--dur) var(--ease);
}
.charset-input:focus { border-color: var(--accent); outline: none; }

/* Color */
.color-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.6rem; }
.color-label { display: flex; flex-direction: column; gap: 0.3rem; font-family: var(--font-body); font-size: 0.9rem; color: var(--text-muted); font-weight: 500; }
.color-label input[type="color"] { width: 100%; height: 38px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2); cursor: pointer; padding: 3px; }
.color-label input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
.color-label input[type="color"]::-webkit-color-swatch { border: none; border-radius: 3px; }

.color-presets { display: flex; gap: 0.45rem; flex-wrap: wrap; }
.color-preset { width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--border); background: var(--c); cursor: pointer; transition: transform var(--dur) var(--ease), border-color var(--dur) var(--ease); }
.color-preset:hover { transform: scale(1.15); border-color: var(--text); }

/* Checkbox */
.checkbox-row {
  display: flex; align-items: center; gap: 0.5rem;
  font-family: var(--font-body); font-size: 0.95rem; color: var(--text-muted); cursor: pointer; user-select: none; margin-top: 0.2rem;
}
.checkbox-row input[type="checkbox"] { width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer; }
.checkbox-row .reset-btn { margin-left: auto; }

/* Sliders */
.slider-row { display: flex; justify-content: space-between; align-items: center; font-family: var(--font-body); font-size: 0.9rem; color: var(--text-muted); font-weight: 500; }
.slider-controls { display: flex; align-items: center; gap: 0.35rem; }
.slider-value { font-family: var(--font-mono); font-size: 0.78rem; color: var(--accent); font-weight: 600; }

.slider {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 3px; background: var(--border);
  border-radius: 2px; outline: none; cursor: pointer; margin-bottom: 0.6rem;
}
.slider::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 14px; height: 14px; border-radius: 50%; background: var(--accent); cursor: pointer; border: 2px solid var(--surface); box-shadow: var(--shadow-sm); transition: transform var(--dur) var(--ease); }
.slider::-webkit-slider-thumb:hover { transform: scale(1.2); }
.slider::-moz-range-thumb { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); cursor: pointer; border: 2px solid var(--surface); box-shadow: var(--shadow-sm); }

/* Buttons */
.btn { display: flex; align-items: center; justify-content: center; gap: 0.4rem; font-family: var(--font-display); font-size: 0.78rem; font-weight: 600; padding: 0.55rem 0.8rem; border-radius: var(--radius-sm); border: 1px solid var(--border); cursor: pointer; transition: all var(--dur) var(--ease); flex: 1; letter-spacing: 0.03em; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--bg); }
.btn-primary:hover { filter: brightness(1.15); transform: translateY(-1px); box-shadow: 0 4px 16px var(--accent-glow); }
.btn-secondary { background: var(--surface-2); color: var(--text-muted); }
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.export-buttons { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.export-buttons .btn { min-width: 70px; }

/* Custom noise colors */
.custom-noise-colors { padding: 0.2rem 0; }

/* CRT Preset */
.crt-preset-btn { font-family: var(--font-display); font-size: 0.78rem; font-weight: 700; padding: 0.5rem; border-radius: var(--radius-sm); border: 1px solid var(--accent); background: var(--accent-soft); color: var(--accent); cursor: pointer; transition: all var(--dur) var(--ease); width: 100%; margin-bottom: 0.4rem; letter-spacing: 0.04em; }
.crt-preset-btn:hover { background: var(--accent); color: var(--bg); }
.crt-preset-btn.active { background: var(--accent); color: var(--bg); }

/* Glow shape selector */
.glow-shape-row { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.glow-shape-btn { font-family: var(--font-mono); font-size: 0.68rem; font-weight: 600; padding: 0.3rem 0.5rem; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface-2); color: var(--text-muted); cursor: pointer; transition: all var(--dur) var(--ease); }
.glow-shape-btn:hover { border-color: var(--accent); color: var(--accent); }
.glow-shape-btn.active { background: var(--accent); border-color: var(--accent); color: var(--bg); }

/* Noise palettes */
.noise-palette-row { display: flex; flex-direction: column; gap: 0.35rem; margin-top: 0.2rem; }
.noise-label { font-family: var(--font-body); font-size: 0.9rem; color: var(--text-muted); font-weight: 500; }
.noise-presets { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.noise-btn { font-family: var(--font-mono); font-size: 0.68rem; font-weight: 600; padding: 0.3rem 0.55rem; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface-2); color: var(--text-muted); cursor: pointer; transition: all var(--dur) var(--ease); }
.noise-btn:hover { border-color: var(--accent); color: var(--accent); }
.noise-btn.active { background: var(--accent); border-color: var(--accent); color: var(--bg); }

/* Resolution selector (Export) */
.resolution-row { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.resolution-btn { font-family: var(--font-mono); font-size: 0.68rem; font-weight: 600; padding: 0.3rem 0.55rem; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface-2); color: var(--text-muted); cursor: pointer; transition: all var(--dur) var(--ease); }
.resolution-btn:hover { border-color: var(--accent); color: var(--accent); }
.resolution-btn.active { background: var(--accent); border-color: var(--accent); color: var(--bg); }

/* Output Panel */
.output-panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); overflow: hidden; box-shadow: var(--shadow-md); min-height: 500px; display: flex; flex-direction: column; }

.output-header { display: flex; align-items: center; justify-content: space-between; padding: 0.7rem 1rem; background: var(--surface-2); border-bottom: 1px solid var(--border); }
.output-tabs { display: flex; align-items: center; gap: 0.7rem; }
.terminal-dots { display: flex; gap: 0.3rem; }
.dot { width: 10px; height: 10px; border-radius: 50%; }
.dot-red { background: #a52a2a; } .dot-yellow { background: #b8860b; } .dot-green { background: #4a7c3a; }
.output-label { font-family: var(--font-mono); font-size: 0.75rem; color: var(--text-muted); }
.output-header-right { display: flex; align-items: center; gap: 0.6rem; }
.char-counter { font-family: var(--font-mono); font-size: 0.7rem; color: var(--text-faint); }

.preview-toggle { display: flex; align-items: center; justify-content: center; width: 28px; height: 28px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface-2); color: var(--text-muted); cursor: pointer; transition: all var(--dur) var(--ease); }
.preview-toggle:hover { border-color: var(--accent); color: var(--accent); }
.preview-toggle[data-active="true"] { background: var(--accent); border-color: var(--accent); color: var(--bg); }

.output-container { position: relative; flex: 1; overflow: auto; padding: 1rem; display: flex; align-items: center; justify-content: center; min-height: 400px; }

.image-preview {
  position: absolute; top: 12px; right: 12px; z-index: 20;
  max-width: 200px; max-height: 200px;
  border: 2px solid var(--accent); border-radius: var(--radius-md);
  overflow: hidden; box-shadow: var(--shadow-lg);
  pointer-events: none;
}
.image-preview img { width: 100%; height: 100%; object-fit: contain; display: block; }

/* ASCII canvas — the single source of truth for preview AND export */
.ascii-canvas {
  display: block;
  max-width: 100%;
  height: auto;
  position: relative; z-index: 4;
}

/* Flicker — CSS animation applied directly to the canvas element */
@keyframes crt-flicker {
  0%, 100% { opacity: 1; }
  3% { opacity: 0.88; } 6% { opacity: 1; } 7% { opacity: 0.92; } 8% { opacity: 1; }
  50% { opacity: 0.97; } 51% { opacity: 1; }
  73% { opacity: 0.93; } 74% { opacity: 1; }
}
.ascii-canvas.flicker { animation: crt-flicker 4s infinite; }

/* Empty state */
.empty-state { text-align: center; color: var(--text-faint); }
.empty-icon { color: var(--text-faint); opacity: 0.25; margin-bottom: 1rem; }
.empty-hint { font-family: var(--font-body); font-size: 1.05rem; font-style: italic; }

/* Image status — sits below the output container */
.image-status {
  font-family: var(--font-mono); font-size: 0.8rem; color: var(--text-faint);
  text-align: center; margin-top: 0.8rem; margin-bottom: 0.5rem; min-height: 1.2em;
  position: relative; z-index: 2;
}

/* Footer */
.footer { position: relative; z-index: 1; text-align: center; padding: 1.5rem 2rem; font-family: var(--font-body); font-size: 0.9rem; font-style: italic; color: var(--text-faint); border-top: 1px solid var(--border); }

/* Toast */
.toast {
  position: fixed; bottom: 2rem; left: 50%; transform: translateX(-50%) translateY(120%);
  background: var(--surface-offset); border: 1px solid var(--accent); color: var(--accent);
  font-family: var(--font-display); font-size: 0.82rem; font-weight: 600;
  padding: 0.65rem 1.3rem; border-radius: 50px; box-shadow: var(--shadow-lg);
  z-index: 100; transition: transform 300ms var(--ease); pointer-events: none;
  letter-spacing: 0.03em;
}
.toast.show { transform: translateX(-50%) translateY(0); }

.hint { font-family: var(--font-body); font-size: 0.82rem; color: var(--text-faint); line-height: 1.4; font-style: italic; }
