:root {
  /* Page background: royal purple */
  --bg: #3B1D66;
  --bg-deep: #2C1450;

  /* Card background: lilac "pages" that sit on the purple */
  --panel: #E8DCF6;
  --panel-input: #F3ECFB;

  /* Text used directly on the purple background */
  --ink-dark-bg: #F5F0FC;
  --ink-dark-bg-soft: #C9B3E8;
  --line-dark-bg: rgba(245, 240, 252, 0.22);

  /* Text used inside the lilac cards */
  --ink-light-bg: #2A1846;
  --ink-light-bg-soft: #5E4A82;
  --line-light-bg: #CBB9E3;

  /* Accents */
  --accent-royal: #6633A8;
  --accent-gold: #D6A73C;
  --accent-gold-soft: #E4C06B;

  --success: #1F6B41;
  --success-bg: rgba(31, 107, 65, 0.12);
  --success-line: rgba(31, 107, 65, 0.35);

  --error: #A02E48;
  --error-bg: rgba(160, 46, 72, 0.1);
  --error-line: rgba(160, 46, 72, 0.32);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink-dark-bg);
  font-family: "Source Serif 4", Georgia, serif;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

.page {
  max-width: 640px;
  margin: 0 auto;
  padding: 4rem 1.5rem 5rem;
}

/* Masthead */

.masthead {
  text-align: center;
  margin-bottom: 2rem;
}

.masthead h1 {
  font-family: "Fraunces", Georgia, serif;
  font-optical-sizing: auto;
  font-weight: 600;
  font-size: 3rem;
  letter-spacing: -0.01em;
  margin: 0 0 0.5rem;
  color: var(--ink-dark-bg);
}

.tagline {
  font-style: italic;
  color: var(--ink-dark-bg-soft);
  font-size: 1.05rem;
  max-width: 34ch;
  margin: 0 auto;
}

/* Premise / consistent blurb */

.premise {
  background: rgba(245, 240, 252, 0.06);
  border: 1px solid var(--line-dark-bg);
  border-radius: 2px;
  padding: 1.25rem 1.5rem;
  margin-bottom: 2rem;
}

.premise-label {
  font-family: "Fraunces", Georgia, serif;
  font-style: italic;
  font-weight: 500;
  font-size: 0.95rem;
  color: var(--accent-gold-soft);
  margin: 0 0 0.4rem;
}

.premise-text {
  color: var(--ink-dark-bg-soft);
  font-size: 0.95rem;
  margin: 0;
}

.rule {
  border: none;
  border-top: 1px solid var(--line-dark-bg);
  margin: 0 0 2.5rem;
}

/* Book spread: manuscript (previous page) + write/waiting side by side on desktop */

.spread {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0;
}

.spread > section {
  background: var(--panel);
  color: var(--ink-light-bg);
  border-radius: 2px;
  box-shadow: 0 6px 18px rgba(20, 8, 40, 0.28);
}

.spread > .manuscript {
  padding: 2.25rem 2rem;
}

.spread > .write,
.spread > .waiting {
  padding: 2.25rem 2rem;
  margin-top: 1.75rem;
}

@media (min-width: 860px) {
  .page { max-width: 1040px; }

  .masthead, .premise, .colophon {
    max-width: 640px;
    margin-left: auto;
    margin-right: auto;
  }

  .spread {
    grid-template-columns: 1fr 1fr;
    align-items: stretch;
    position: relative;
  }

  .spread > .manuscript {
    border-radius: 3px 0 0 3px;
  }

  .spread > .write,
  .spread > .waiting {
    border-radius: 0 3px 3px 0;
    margin-top: 0;
  }

  /* A soft seam down the middle, like the gutter of an open book */
  .spread::after {
    content: "";
    position: absolute;
    top: 1.5rem;
    bottom: 1.5rem;
    left: 50%;
    width: 10px;
    margin-left: -5px;
    background: linear-gradient(
      to right,
      rgba(20, 8, 40, 0.16),
      rgba(20, 8, 40, 0.04) 40%,
      rgba(20, 8, 40, 0.04) 60%,
      rgba(20, 8, 40, 0.16)
    );
    pointer-events: none;
  }
}

/* Manuscript (previous page) */

.manuscript-meta {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--line-light-bg);
}

.page-number {
  font-family: "Fraunces", Georgia, serif;
  font-weight: 600;
  font-style: italic;
  font-size: 1.15rem;
  color: var(--accent-royal);
}

.written-by {
  color: var(--ink-light-bg-soft);
  font-size: 0.95rem;
}

.manuscript-text {
  font-size: 1.08rem;
  white-space: pre-wrap;
  text-align: justify;
  hyphens: auto;
  margin: 0;
}

.manuscript-text::first-letter {
  font-family: "Fraunces", Georgia, serif;
  font-size: 2.6rem;
  font-weight: 600;
  float: left;
  line-height: 0.85;
  padding-right: 0.1em;
  padding-top: 0.08em;
  color: var(--accent-royal);
}

.manuscript-empty {
  color: var(--ink-light-bg-soft);
  font-style: italic;
  margin: 0;
}

/* Write section */

.write-heading-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.write h2 {
  font-family: "Fraunces", Georgia, serif;
  font-weight: 600;
  font-size: 1.6rem;
  margin: 0;
  color: var(--ink-light-bg);
}

.floor-badge {
  font-size: 0.78rem;
  color: var(--success);
  border: 1px solid var(--success-line);
  background: var(--success-bg);
  padding: 0.25rem 0.6rem;
  border-radius: 2px;
  white-space: nowrap;
}

.floor-note {
  color: var(--ink-light-bg-soft);
  font-size: 0.9rem;
  margin: 0.5rem 0 1.25rem;
}

form {
  display: flex;
  flex-direction: column;
}

label {
  font-size: 0.9rem;
  color: var(--ink-light-bg-soft);
  margin-bottom: 0.35rem;
  margin-top: 1.1rem;
}

label:first-of-type {
  margin-top: 0;
}

input[type="text"],
textarea {
  font-family: "Source Serif 4", Georgia, serif;
  font-size: 1rem;
  color: var(--ink-light-bg);
  background: var(--panel-input);
  border: 1px solid var(--line-light-bg);
  border-radius: 2px;
  padding: 0.7rem 0.85rem;
  width: 100%;
  resize: vertical;
}

input[type="text"]::placeholder,
textarea::placeholder {
  color: var(--ink-light-bg-soft);
  opacity: 0.75;
}

input[type="text"]:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent-royal);
  box-shadow: 0 0 0 3px rgba(102, 51, 168, 0.15);
}

textarea {
  min-height: 220px;
  line-height: 1.6;
}

.story-label-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-top: 1.1rem;
  margin-bottom: 0.35rem;
}

.story-label-row label {
  margin: 0;
}

.char-count {
  font-size: 0.85rem;
  color: var(--ink-light-bg-soft);
  font-variant-numeric: tabular-nums;
}

.char-count-warning {
  color: #7A4E19;
}

.char-count-limit {
  color: var(--error);
  font-weight: 600;
}

.char-progress-track {
  height: 4px;
  background: var(--line-light-bg);
  border-radius: 2px;
  margin-top: 0.5rem;
  overflow: hidden;
}

.char-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent-royal);
  border-radius: 2px;
  transition: width 0.1s linear, background 0.15s ease;
}

.char-progress-warning {
  background: #B4791F;
}

.char-progress-limit {
  background: var(--error);
}

button[type="submit"] {
  align-self: flex-start;
  margin-top: 1.75rem;
  background: var(--accent-gold);
  color: var(--bg-deep);
  border: none;
  border-radius: 2px;
  padding: 0.8rem 1.6rem;
  font-family: "Source Serif 4", Georgia, serif;
  font-weight: 600;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.15s ease;
}

button[type="submit"]:hover {
  background: var(--accent-gold-soft);
}

button[type="submit"]:focus-visible {
  outline: 2px solid var(--accent-royal);
  outline-offset: 2px;
}

.release-link {
  display: block;
  margin: 1.25rem auto 0;
  background: none;
  border: none;
  color: var(--ink-light-bg-soft);
  font-family: "Source Serif 4", Georgia, serif;
  font-size: 0.85rem;
  font-style: italic;
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
  padding: 0.25rem;
}

.release-link:hover {
  color: var(--accent-royal);
}

/* Waiting screen */

.waiting {
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.waiting h2 {
  font-family: "Fraunces", Georgia, serif;
  font-weight: 600;
  font-style: italic;
  font-size: 1.6rem;
  margin: 0 0 0.75rem;
  color: var(--accent-royal);
}

.waiting-text {
  color: var(--ink-light-bg-soft);
  max-width: 40ch;
  margin: 0 auto 1.5rem;
}

.waiting-indicator {
  display: flex;
  justify-content: center;
  gap: 0.4rem;
  margin-bottom: 1.75rem;
}

.waiting-indicator span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent-royal);
  animation: waiting-pulse 1.2s ease-in-out infinite;
}

.waiting-indicator span:nth-child(2) { animation-delay: 0.2s; }
.waiting-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes waiting-pulse {
  0%, 80%, 100% { opacity: 0.25; transform: scale(0.85); }
  40% { opacity: 1; transform: scale(1); }
}

@media (prefers-reduced-motion: reduce) {
  .waiting-indicator span { animation: none; opacity: 0.7; }
}

.check-again {
  background: var(--panel-input);
  border: 1px solid var(--line-light-bg);
  color: var(--ink-light-bg);
  font-family: "Source Serif 4", Georgia, serif;
  font-size: 0.95rem;
  padding: 0.65rem 1.4rem;
  border-radius: 2px;
  cursor: pointer;
  align-self: center;
}

.check-again:hover {
  border-color: var(--accent-royal);
}

/* Flash messages */

.flash-stack {
  margin-bottom: 0.5rem;
}

.flash {
  font-size: 0.92rem;
  padding: 0.65rem 0.85rem;
  border-radius: 2px;
  margin: 0 0 0.5rem;
}

.flash-error {
  background: var(--error-bg);
  color: var(--error);
  border: 1px solid var(--error-line);
}

.flash-success {
  background: var(--success-bg);
  color: var(--success);
  border: 1px solid var(--success-line);
}

/* Colophon */

.colophon {
  text-align: center;
  margin-top: 3rem;
  color: var(--ink-dark-bg-soft);
  font-size: 0.85rem;
  font-style: italic;
}

/* Responsive */

@media (max-width: 480px) {
  .masthead h1 { font-size: 2.3rem; }
  .spread > section { padding: 1.5rem 1.25rem; }
  .page { padding: 2.5rem 1.1rem 3.5rem; }
}

/* Reduced motion */

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