/* ═══════════════════════════════════════════════════════════
   MAD Story Studio — Writer's Notebook Aesthetic
   Fonts: Caveat (handwriting), Lora (body), Playfair Display (accent)
   ═══════════════════════════════════════════════════════════ */

:root {
  --ink: #2a2118;
  --ink-soft: #5c4f42;
  --ink-faint: #8a7b6b;
  --cream: #f5efe6;
  --cream-warm: #ede4d6;
  --cream-deep: #e2d6c4;
  --parchment: #faf6ef;
  --red-ink: #a33b2e;
  --red-ink-soft: rgba(163, 59, 46, 0.15);
  --blue-ink: #2e5a7a;
  --blue-ink-soft: rgba(46, 90, 122, 0.12);
  --pencil: #6b6259;
  --margin-line: rgba(163, 59, 46, 0.18);
  --rule-line: rgba(46, 90, 122, 0.09);
  --shadow-warm: rgba(90, 65, 35, 0.12);
  --shadow-deep: rgba(60, 40, 20, 0.22);
}

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

/* ── Page background ─────────────────────────────────────── */
html {
  min-height: 100%;
  background:
    linear-gradient(rgba(255, 255, 255, 0.4), rgba(255, 255, 255, 0.4)),
    url('bg.jpg') center / cover fixed no-repeat;
  background-color: #d4c8b5;
}

body {
  min-height: 100vh;
  color: var(--ink);
  font-family: "Lora", "Georgia", serif;
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* paper grain noise overlay */
.paper-grain {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9999;
  opacity: 0.035;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  background-size: 200px;
}

/* ── Shell / outer notebook ──────────────────────────────── */
.shell {
  max-width: 1100px;
  margin: 0 auto;
  padding: 32px 20px 48px;
  position: relative;
}

/* ── Notebook spine ──────────────────────────────────────── */
.spine {
  display: none;
}

@media (min-width: 860px) {
  .spine {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 48px;
    position: absolute;
    left: 50%;
    top: 180px;
    bottom: 80px;
    transform: translateX(-50%);
    z-index: 3;
    pointer-events: none;
  }
  .spine-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--cream-deep);
    border: 2px solid rgba(140, 120, 95, 0.35);
    box-shadow:
      inset 0 1px 2px rgba(0,0,0,0.15),
      0 1px 3px var(--shadow-warm);
  }
}

/* ── Hero header ─────────────────────────────────────────── */
.hero {
  text-align: center;
  padding: 12px 20px 28px;
  position: relative;
  margin-bottom: 24px;
}

.kicker {
  font-family: "Lora", serif;
  font-size: 0.72rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 4px;
}

.title {
  font-family: "Caveat", cursive;
  font-weight: 700;
  font-size: clamp(3rem, 7vw, 5rem);
  color: var(--red-ink);
  line-height: 1;
  position: relative;
  display: inline-block;
}

/* underline scribble effect */
.title::after {
  content: "";
  position: absolute;
  left: -5%;
  right: -5%;
  bottom: -2px;
  height: 8px;
  background: var(--red-ink);
  opacity: 0.12;
  border-radius: 40% 60% 50% 40% / 80% 50% 80% 50%;
  transform: rotate(-0.5deg);
}

.subtitle {
  font-family: "Caveat", cursive;
  font-size: 1.25rem;
  color: var(--ink-soft);
  margin-top: 6px;
}

.ink-splatter {
  position: absolute;
  top: 8px;
  right: 12%;
  width: 28px;
  height: 28px;
  background: radial-gradient(circle, var(--red-ink) 30%, transparent 70%);
  opacity: 0.06;
  border-radius: 40% 60% 55% 45%;
  transform: rotate(25deg);
}

/* ── Notebook body (two-page spread) ─────────────────────── */
.notebook-body {
  display: grid;
  gap: 0;
  grid-template-columns: 1fr;
}

@media (min-width: 860px) {
  .notebook-body {
    grid-template-columns: 1fr 24px 1fr;
  }
}

.gutter {
  display: none;
}

@media (min-width: 860px) {
  .gutter {
    display: block;
    position: relative;
    background: linear-gradient(
      90deg,
      rgba(0,0,0,0.04) 0%,
      rgba(0,0,0,0.08) 40%,
      rgba(0,0,0,0.08) 60%,
      rgba(0,0,0,0.04) 100%
    );
  }
  .gutter::before {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(
      180deg,
      transparent 0%,
      rgba(180, 160, 130, 0.15) 20%,
      rgba(180, 160, 130, 0.15) 80%,
      transparent 100%
    );
  }
}

/* ── Page panels ─────────────────────────────────────────── */
.page {
  background: var(--parchment);
  padding: 28px 24px 32px;
  border-radius: 3px;
  position: relative;
  box-shadow:
    0 1px 3px var(--shadow-warm),
    0 8px 28px -8px var(--shadow-deep);
  /* faint ruled lines */
  background-image:
    repeating-linear-gradient(
      transparent 0px,
      transparent 27px,
      var(--rule-line) 27px,
      var(--rule-line) 28px
    );
  background-size: 100% 28px;
  background-position: 0 14px;
  margin-bottom: 16px;
}

@media (min-width: 860px) {
  .page { margin-bottom: 0; }
  .page-left {
    border-radius: 3px 0 0 3px;
    /* red margin line on right edge */
    border-right: 2px solid var(--margin-line);
  }
  .page-right {
    border-radius: 0 3px 3px 0;
    /* red margin line on left edge */
    border-left: 2px solid var(--margin-line);
  }
}

/* page corner fold */
.page-fold {
  position: absolute;
  top: 0;
  width: 24px;
  height: 24px;
  z-index: 2;
}

.page-left .page-fold {
  right: 0;
  background: linear-gradient(225deg, var(--cream-deep) 50%, transparent 50%);
}

.page-right .page-fold {
  left: 0;
  background: linear-gradient(135deg, var(--cream-deep) 50%, transparent 50%);
}

/* ── Section titles ──────────────────────────────────────── */
.section-title {
  font-family: "Caveat", cursive;
  font-weight: 700;
  font-size: 1.65rem;
  color: var(--blue-ink);
  margin-bottom: 18px;
  display: flex;
  align-items: baseline;
  gap: 10px;
}

.section-number {
  font-family: "Playfair Display", serif;
  font-style: italic;
  font-size: 0.85rem;
  color: var(--ink-faint);
  opacity: 0.5;
}

/* ── Form elements ───────────────────────────────────────── */
.field-label {
  display: block;
  font-size: 0.8rem;
  color: var(--ink-faint);
  letter-spacing: 0.04em;
  margin-bottom: 6px;
}

textarea {
  width: 100%;
  border: none;
  border-bottom: 1.5px solid var(--cream-deep);
  background: transparent;
  padding: 8px 4px;
  font-family: "Caveat", cursive;
  font-size: 1.15rem;
  color: var(--ink);
  outline: none;
  resize: vertical;
  min-height: 72px;
  line-height: 1.5;
  transition: border-color 0.2s;
}

textarea::placeholder {
  color: var(--ink-faint);
  opacity: 0.5;
  font-style: italic;
}

textarea:focus {
  border-color: var(--blue-ink);
  border-bottom-width: 2px;
}


/* ── Buttons ─────────────────────────────────────────────── */
.actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin: 16px 0;
}

.action-divider {
  font-family: "Caveat", cursive;
  font-size: 1.05rem;
  color: var(--ink-faint);
}

.btn {
  border: none;
  cursor: pointer;
  padding: 9px 18px;
  font-family: "Caveat", cursive;
  font-weight: 600;
  font-size: 1.1rem;
  border-radius: 3px;
  transition: all 0.2s;
  position: relative;
}

.btn-ink {
  color: var(--parchment);
  background: var(--ink);
  box-shadow: 0 2px 8px rgba(42, 33, 24, 0.25);
}

.btn-ink:hover:not(:disabled) {
  background: var(--ink-soft);
  transform: translateY(-1px);
  box-shadow: 0 4px 14px rgba(42, 33, 24, 0.3);
}

.btn-pencil {
  color: var(--ink-soft);
  background: transparent;
  border: 1.5px dashed var(--cream-deep);
}

.btn-pencil:hover:not(:disabled) {
  border-color: var(--ink-faint);
  background: var(--red-ink-soft);
  color: var(--red-ink);
}

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

/* ── Topic list ──────────────────────────────────────────── */
.topics-wrap {
  margin-top: 18px;
}

.topics-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.topics-header h3 {
  font-family: "Caveat", cursive;
  font-weight: 600;
  font-size: 1.15rem;
  color: var(--ink-soft);
}

.count-badge {
  font-family: "Playfair Display", serif;
  font-style: italic;
  font-size: 0.8rem;
  color: var(--parchment);
  background: var(--ink-faint);
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.topic-list {
  list-style: none;
  display: grid;
  gap: 8px;
  max-height: 320px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--cream-deep) transparent;
}

.topic-item {
  padding: 10px 14px;
  border-left: 3px solid transparent;
  background: rgba(255, 252, 248, 0.5);
  border-radius: 2px;
  cursor: pointer;
  font-family: "Caveat", cursive;
  font-size: 1.08rem;
  line-height: 1.4;
  color: var(--ink-soft);
  transition: all 0.15s;
}

.topic-item:hover {
  background: var(--blue-ink-soft);
  border-left-color: var(--blue-ink);
  color: var(--ink);
}

.topic-item.selected {
  background: var(--red-ink-soft);
  border-left-color: var(--red-ink);
  color: var(--ink);
}

/* ── Selected topic display ──────────────────────────────── */
.selected-topic {
  min-height: 56px;
  padding: 10px 14px;
  border: none;
  border-bottom: 1.5px dashed var(--cream-deep);
  background: transparent;
  font-family: "Caveat", cursive;
  font-size: 1.1rem;
  line-height: 1.5;
  color: var(--ink);
}

.placeholder-text {
  color: var(--ink-faint);
  opacity: 0.5;
  font-style: italic;
}

/* ── Status ──────────────────────────────────────────────── */
.status {
  margin-top: 8px;
  min-height: 20px;
  font-size: 0.82rem;
  color: var(--ink-faint);
  font-style: italic;
}

/* ── Story output ────────────────────────────────────────── */
.story {
  margin-top: 14px;
  min-height: 280px;
  position: relative;
}

.debate-strip {
  margin-bottom: 18px;
  padding: 14px 14px 6px;
  border: 1.5px dashed var(--cream-deep);
  border-radius: 14px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.72), rgba(237, 228, 214, 0.7));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.55);
  animation: fadeWrite 0.2s ease-out;
}

.debate-strip-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.debate-kicker {
  font-family: "Playfair Display", serif;
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--red-ink);
}

.debate-phase {
  font-family: "Caveat", cursive;
  font-size: 1.1rem;
  color: var(--ink-soft);
  text-align: right;
}

.debate-bubbles {
  display: grid;
  gap: 10px;
}

.debate-bubble {
  position: relative;
  max-width: 88%;
  padding: 12px 14px;
  border-radius: 18px 18px 18px 6px;
  box-shadow: 0 8px 18px -14px var(--shadow-deep);
  transform: rotate(var(--bubble-tilt, 0deg));
  transform-origin: left bottom;
  animation: bubblePop 0.18s cubic-bezier(.18,.89,.32,1.28);
}

.debate-bubble::after {
  content: "";
  position: absolute;
  left: 14px;
  bottom: -8px;
  width: 16px;
  height: 16px;
  background: inherit;
  clip-path: polygon(0 0, 100% 0, 20% 100%);
}

.debate-bubble p {
  font-family: "Caveat", cursive;
  font-size: 1.2rem;
  line-height: 1.25;
  color: var(--ink);
}

.bubble-badge {
  margin-bottom: 4px;
  font-family: "Playfair Display", serif;
  font-size: 0.72rem;
  font-style: italic;
  letter-spacing: 0.04em;
  color: var(--ink-faint);
}

.writer-a {
  justify-self: start;
  background: #f8e8d5;
  border: 1px solid rgba(163, 59, 46, 0.16);
}

.writer-b {
  justify-self: end;
  background: #ddeaf1;
  border: 1px solid rgba(46, 90, 122, 0.18);
  border-radius: 18px 18px 6px 18px;
}

.writer-b::after {
  left: auto;
  right: 14px;
  clip-path: polygon(0 0, 100% 0, 80% 100%);
}

.judge,
.judge-final {
  justify-self: center;
  background: #efe6d3;
  border: 1px solid rgba(107, 98, 89, 0.2);
  border-radius: 18px;
}

.judge::after,
.judge-final::after {
  left: calc(50% - 8px);
}

.is-success {
  box-shadow: 0 0 0 2px rgba(163, 59, 46, 0.14), 0 10px 18px -14px var(--shadow-deep);
}

.story-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  color: var(--cream-deep);
}

.quill-icon {
  width: 36px;
  height: 36px;
  margin-bottom: 10px;
  opacity: 0.5;
}

.story-placeholder p {
  font-family: "Caveat", cursive;
  font-size: 1.15rem;
  opacity: 0.5;
}

/* story text when populated */
.story-text {
  font-family: "Lora", "Georgia", serif;
  font-size: 0.95rem;
  line-height: 1.75;
  color: var(--ink);
  white-space: pre-wrap;
  animation: fadeWrite 0.6s ease-out;
}

.share-wrap {
  margin-top: 12px;
}

.hidden {
  display: none !important;
}

@keyframes fadeWrite {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes bubblePop {
  from { opacity: 0; transform: scale(0.92) rotate(var(--bubble-tilt, 0deg)); }
  to { opacity: 1; transform: scale(1) rotate(var(--bubble-tilt, 0deg)); }
}

/* ── Footer ──────────────────────────────────────────────── */
.colophon {
  text-align: center;
  margin-top: 28px;
  padding-top: 16px;
}

.colophon p {
  font-family: "Lora", serif;
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-faint);
  opacity: 0.5;
}

/* ── Loading animation ───────────────────────────────────── */
.writing-anim {
  display: inline-block;
  font-family: "Caveat", cursive;
  font-size: 1.1rem;
  color: var(--ink-faint);
}

.writing-anim::after {
  content: "";
  animation: writingDots 1.5s steps(4, end) infinite;
}

@keyframes writingDots {
  0%  { content: ""; }
  25% { content: "."; }
  50% { content: ".."; }
  75% { content: "..."; }
}

/* ── Responsive ──────────────────────────────────────────── */
@media (max-width: 859px) {
  .shell { padding: 16px 12px 32px; }
  .page { padding: 20px 16px 24px; }
  .page-fold { display: none; }
  .title { font-size: 2.8rem; }
  .debate-strip-header { align-items: flex-start; flex-direction: column; }
  .debate-phase { text-align: left; }
  .debate-bubble { max-width: 100%; }
}
