/* ==========================================================================
   Tokens
   ========================================================================== */
:root {
  --bg: #0b0d0b;
  --surface: #111411;
  --surface-2: #171b17;
  --art-bg: #070807; /* identique au fond du PNG généré */
  --line: rgba(220, 240, 215, 0.08);
  --line-strong: rgba(220, 240, 215, 0.16);
  --text: #e8ece6;
  --muted: #8b958a;
  --faint: #5c665b;
  --accent: #6cd45e;
  --accent-hover: #7fdc72;
  --accent-ink: #081207;
  --accent-soft: rgba(108, 212, 94, 0.12);
  --danger: #ef8b78;

  --font-sans: 'Geist', 'Segoe UI', system-ui, sans-serif;
  --font-mono: 'Geist Mono', 'Cascadia Mono', 'Courier New', monospace;

  --radius-lg: 18px;
  --radius-md: 12px;
  --radius-sm: 8px;

  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);

  --z-grain: 1;
  --z-content: 2;
}

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

html {
  color-scheme: dark;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  font-family: var(--font-sans);
  font-size: 1rem;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

img {
  display: block;
  max-width: 100%;
}

button,
input {
  font: inherit;
  color: inherit;
}

.mono {
  font-family: var(--font-mono);
}

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

.skip-link {
  position: absolute;
  left: 1rem;
  top: -3rem;
  z-index: 10;
  padding: 0.5rem 0.875rem;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  text-decoration: none;
}

.skip-link:focus {
  top: 1rem;
}

/* Fond : dégradé profond, halos lumineux, trame de points et vignettage */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
  background:
    radial-gradient(120% 80% at 50% 0%, #101410 0%, var(--bg) 55%, #060706 100%);
}

/* Trame de points, comme une grille de caractères, estompée vers les bords */
.backdrop::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image: radial-gradient(circle, rgba(220, 240, 215, 0.075) 1px, transparent 1.5px);
  background-size: 22px 22px;
  background-position: center top;
  -webkit-mask-image: radial-gradient(ellipse 70% 60% at 60% 30%, #000 10%, transparent 75%);
  mask-image: radial-gradient(ellipse 70% 60% at 60% 30%, #000 10%, transparent 75%);
}

/* Vignettage : assombrit les bords pour concentrer le regard */
.backdrop::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse 85% 75% at 50% 40%, transparent 55%, rgba(0, 0, 0, 0.55) 100%);
}

.glow {
  position: absolute;
  border-radius: 50%;
  filter: blur(90px);
  will-change: transform;
}

/* Halo principal, derrière le cadre de prévisualisation */
.glow-a {
  top: -18rem;
  right: -6rem;
  width: 52rem;
  height: 36rem;
  background: radial-gradient(closest-side, rgba(108, 212, 94, 0.2), rgba(108, 212, 94, 0.06) 60%, transparent);
  animation: drift-a 26s ease-in-out infinite alternate;
}

/* Contre-halo discret, en bas à gauche */
.glow-b {
  bottom: -20rem;
  left: -12rem;
  width: 44rem;
  height: 32rem;
  background: radial-gradient(closest-side, rgba(70, 160, 120, 0.12), transparent);
  animation: drift-b 32s ease-in-out infinite alternate;
}

@keyframes drift-a {
  to { transform: translate3d(-6rem, 4rem, 0) scale(1.08); }
}

@keyframes drift-b {
  to { transform: translate3d(5rem, -3rem, 0) scale(1.05); }
}

/* Grain : casse l'aplat numérique du fond */
.grain {
  position: fixed;
  inset: 0;
  z-index: var(--z-grain);
  pointer-events: none;
  opacity: 0.06;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* ==========================================================================
   En-tête et pied de page
   ========================================================================== */
.topbar,
.layout,
.footer {
  position: relative;
  z-index: var(--z-content);
  width: 100%;
  max-width: 1240px;
  margin-inline: auto;
  padding-inline: clamp(1rem, 4vw, 2.5rem);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-block: 1.25rem;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  font-family: var(--font-mono);
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text);
  text-decoration: none;
}

.brand-arrow {
  color: var(--accent);
  margin-inline: 0.15em;
}

.footer {
  padding-block: 2rem 2.5rem;
  font-size: 0.8125rem;
  color: var(--faint);
}

/* ==========================================================================
   Mise en page
   ========================================================================== */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 5fr) minmax(0, 6fr);
  gap: clamp(2rem, 5vw, 4.5rem);
  align-items: start;
  padding-block: clamp(1.5rem, 5vw, 4rem) 2rem;
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
  }
}

/* ==========================================================================
   Introduction et formulaire
   ========================================================================== */
h1 {
  font-size: clamp(2.5rem, 5.4vw, 4.5rem);
  font-weight: 600;
  line-height: 1.02;
  letter-spacing: -0.035em;
  text-wrap: balance;
}

.lede {
  max-width: 44ch;
  margin-top: 1.25rem;
  color: var(--muted);
  font-size: 1.0625rem;
  text-wrap: pretty;
}

.form {
  display: grid;
  gap: 1.75rem;
  margin-top: 2.5rem;
}

.field > label,
.option legend {
  display: block;
  margin-bottom: 0.625rem;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text);
}

.option {
  border: 0;
  min-width: 0;
}

.input-row {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.375rem;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-md);
  transition: border-color 200ms var(--ease-out), box-shadow 200ms var(--ease-out);
}

.input-row:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-soft);
}

.input-row[data-invalid] {
  border-color: var(--danger);
}

#spotifyUrl {
  flex: 1;
  min-width: 0;
  padding: 0.625rem 0.5rem 0.625rem 0.625rem;
  background: transparent;
  border: 0;
  font-family: var(--font-mono);
  font-size: 0.9375rem;
}

#spotifyUrl:focus {
  outline: none;
}

#spotifyUrl::placeholder {
  color: var(--faint);
}

.input-action {
  flex-shrink: 0;
  padding: 0.5rem 0.875rem;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: color 200ms var(--ease-out), background-color 200ms var(--ease-out), transform 160ms var(--ease-out);
}

.input-action:hover {
  color: var(--text);
  background: #1f241f;
}

.input-action:active {
  transform: scale(0.97);
}

.hint {
  margin-top: 0.625rem;
  font-size: 0.8125rem;
  color: var(--faint);
}

.form-error {
  margin-top: 0.625rem;
  font-size: 0.875rem;
  color: var(--danger);
}

/* Contrôle segmenté (boutons radio) */
.segmented {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.25rem;
  padding: 0.25rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
}

.segmented label {
  position: relative;
  cursor: pointer;
}

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

.segmented span {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: center;
  column-gap: 0.4em;
  padding: 0.55rem 0.5rem;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--muted);
  text-align: center;
  transition: color 200ms var(--ease-out), background-color 200ms var(--ease-out);
}

.segmented small {
  font-size: 0.75rem;
  font-weight: 400;
  color: var(--faint);
}

.segmented label:hover span {
  color: var(--text);
}

.segmented input:checked + span {
  background: var(--surface-2);
  color: var(--text);
  box-shadow: inset 0 0 0 1px var(--line-strong);
}

.segmented input:checked + span small {
  color: var(--accent);
}

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

.swatch {
  align-self: center;
  width: 0.625rem;
  height: 0.625rem;
  border-radius: 3px;
}

.swatch-cover {
  background: conic-gradient(#ef8b78, #e8c86a, #6cd45e, #6ab4e8, #b48be8, #ef8b78);
}

.swatch-green {
  background: var(--accent);
}

.swatch-white {
  background: var(--text);
}

/* ==========================================================================
   Boutons
   ========================================================================== */
.primary {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.625rem 1rem 0.625rem 1.125rem;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 0.9375rem;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  box-shadow: 0 8px 24px -10px rgba(108, 212, 94, 0.6);
  transition: background-color 200ms var(--ease-out), transform 160ms var(--ease-out), opacity 200ms;
}

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

.primary:active {
  transform: scale(0.97);
}

.primary-arrow {
  transition: transform 200ms var(--ease-out);
}

.primary:hover .primary-arrow {
  transform: translateX(2px);
}

.primary[aria-busy='true'] {
  opacity: 0.7;
  cursor: progress;
}

.secondary {
  padding: 0.7rem 1.1rem;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 200ms var(--ease-out), border-color 200ms var(--ease-out), transform 160ms var(--ease-out), opacity 200ms;
}

.secondary:hover:not(:disabled) {
  background: var(--surface-2);
  border-color: rgba(220, 240, 215, 0.24);
}

.secondary:active:not(:disabled) {
  transform: scale(0.97);
}

.link {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: inherit;
  text-decoration: underline;
  text-decoration-color: rgba(108, 212, 94, 0.35);
  text-underline-offset: 0.2em;
  cursor: pointer;
  transition: text-decoration-color 200ms var(--ease-out), opacity 200ms;
}

.link:hover:not(:disabled) {
  text-decoration-color: currentColor;
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ==========================================================================
   Scène : cadre de prévisualisation
   ========================================================================== */
.stage {
  position: sticky;
  top: 1.5rem;
}

@media (max-width: 900px) {
  .stage {
    position: static;
  }
}

.frame {
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: 0 30px 80px -40px rgba(0, 0, 0, 0.8), 0 0 0 1px rgba(0, 0, 0, 0.3);
}

.frame-bar {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--line);
  font-size: 0.75rem;
  color: var(--muted);
}

.frame-meta {
  color: var(--faint);
  font-variant-numeric: tabular-nums;
}

.frame[data-state='result'] .frame-meta {
  color: var(--accent);
}

.frame-body {
  display: grid;
  min-height: clamp(18rem, 42vw, 32rem);
  background: var(--art-bg);
}

/* Les trois états se superposent dans la même cellule */
.state {
  grid-area: 1 / 1;
  display: none;
  place-items: center;
  min-width: 0;
}

.frame[data-state='empty'] .state-empty,
.frame[data-state='loading'] .state-loading,
.frame[data-state='result'] .state-result {
  display: grid;
}

.state-empty {
  align-content: center;
  gap: 1.25rem;
  padding: 2rem;
}

.state-empty img {
  width: min(16rem, 60%);
  opacity: 0.22;
  animation: breathe 5s ease-in-out infinite;
}

.state-empty p {
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  color: var(--faint);
}

@keyframes breathe {
  50% { opacity: 0.32; }
}

.state-loading {
  overflow: hidden;
  padding: 1rem;
}

#scramble {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  line-height: 1.15;
  color: var(--accent);
  opacity: 0.55;
  user-select: none;
}

.state-result {
  padding: clamp(0.75rem, 2vw, 1.25rem);
}

#asciiImage {
  width: 100%;
  height: auto;
  image-rendering: auto;
}

/* Révélation ligne par ligne, comme une impression */
.frame[data-state='result'] #asciiImage {
  animation: print 900ms var(--ease-out) both;
}

@keyframes print {
  from { clip-path: inset(0 0 100% 0); }
  to { clip-path: inset(0 0 0 0); }
}

.frame-caption {
  padding: 0.875rem 1rem;
  border-top: 1px solid var(--line);
  font-size: 0.875rem;
  color: var(--muted);
}

.frame-caption:empty {
  display: none;
}

.frame-caption strong {
  color: var(--text);
  font-weight: 500;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.625rem 1rem;
  margin-top: 1rem;
}

.actions .link {
  margin-left: auto;
  font-size: 0.875rem;
}

.status {
  min-height: 1.5em;
  margin-top: 0.75rem;
  font-size: 0.8125rem;
  color: var(--muted);
}

.status[data-tone='error'] {
  color: var(--danger);
}

/* ==========================================================================
   Entrée de page
   ========================================================================== */
.intro > *,
.stage {
  animation: rise 700ms var(--ease-out) both;
}

.intro > :nth-child(2) { animation-delay: 60ms; }
.intro > :nth-child(3) { animation-delay: 120ms; }
.stage { animation-delay: 220ms; }

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
}

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

@media (max-width: 480px) {
  .segmented span {
    flex-direction: column;
    align-items: center;
  }

  .input-row {
    flex-wrap: wrap;
  }

  #spotifyUrl {
    flex-basis: 100%;
  }

  .input-action,
  .primary {
    flex: 1;
  }

  .actions .link {
    margin-left: 0;
  }
}
