:root {
  --paper: #fffdf8;
  --ink: #263238;
  --soft-ink: #5d676b;
  --accent: #295f57;
  --accent-dark: #1f4943;
  --border: #d8d4ca;
  --user: #eef4f2;
}

* {
  box-sizing: border-box;
}

html {
  font-size: 18px;
}

body {
  margin: 0;
  color: var(--ink);
  background: #f5f6f3;
  font-family: Arial, Helvetica, sans-serif;
  line-height: 1.5;
}

button,
input,
textarea {
  font: inherit;
}

.site-header {
  padding: .8rem 2rem;
  background: var(--paper);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.brand {
  color: var(--accent-dark);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.25rem;
  font-weight: 700;
  text-decoration: none;
}

.header-logout {
  display: flex;
  align-items: center;
  gap: .6rem;
  margin: 0;
}

.header-user {
  color: var(--ink-soft, #555);
  font-size: .95rem;
}

.link-button {
  background: none;
  border: none;
  padding: 0;
  color: var(--accent-dark);
  text-decoration: underline;
  cursor: pointer;
  font-size: .95rem;
}

.page-shell {
  width: min(1400px, calc(100% - 2rem));
  margin: 1.25rem auto 4rem;
}

.stationery-page,
.card {
  background: var(--paper);
  border: 1px solid var(--border);
  box-shadow: 0 8px 30px rgba(39, 48, 46, .08);
}

.stationery-page {
  width: 100%;
  padding: clamp(1.5rem, 3vw, 2.75rem);
}

.card {
  border-radius: 16px;
  padding: 1.5rem;
}

.narrow {
  max-width: 520px;
  margin: 3rem auto;
}

h1,
h2 {
  margin-top: 0;
}

h1 {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 2rem;
}

.lead {
  font-size: 1.12rem;
}

.stack {
  display: grid;
  gap: .8rem;
}

.card-section {
  border: 1px solid var(--border);
  border-radius: .5rem;
  padding: 1rem;
  display: grid;
  gap: .8rem;
}

.card-section h2 {
  margin-top: 0;
  font-size: 1.05rem;
}

.hint-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1rem;
  margin: 1rem 0;
}

.hints-editor {
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .95rem;
  padding: .75rem;
  border: 1px solid var(--border);
  border-radius: .4rem;
  resize: vertical;
}

.field-hint {
  color: var(--soft-ink, #555);
  font-size: .9rem;
}

input {
  width: 100%;
  padding: .75rem;
  border: 1px solid #aaa;
  border-radius: 8px;
}

.prompt-section {
  width: 100%;
  margin-bottom: 1.4rem;
  padding: 1rem 0 1.15rem;
  border-top: 1px solid #cfc8ba;
  border-bottom: 1px solid #cfc8ba;
}

.prompt-intro {
  margin: 0 0 .4rem;
  color: var(--accent);
  font-size: .9rem;
  font-weight: 700;
}

.current-question {
  max-width: 48em;
  font-size: 1.05rem;
  font-weight: 400;
  line-height: 1.55;
}

.writing-section {
  display: block;
  width: 100%;
  margin: 0;
  padding: 0;
}

.writing-section label {
  display: block;
  margin-bottom: .55rem;
  font-size: 1rem;
  font-weight: 600;
}

#answer {
  display: block;
  width: 100%;
  min-width: 100%;
  max-width: 100%;
  min-height: 280px;
  margin: 0;
  padding: 1rem;
  color: var(--ink);
  background: #fff;
  border: 1px solid #aeb8b5;
  border-radius: 8px;
  outline: none;
  box-shadow: inset 0 1px 3px rgba(39, 48, 46, .08);
  font-size: 1rem;
  line-height: 1.55;
  resize: vertical;
}

#answer:focus,
input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(41, 95, 87, .15);
}

#answer::placeholder {
  color: #777;
}

.answer-actions {
  display: flex;
  justify-content: space-between;
  width: 100%;
  gap: .75rem;
  margin-top: 1rem;
}

button,
.button {
  min-height: 42px;
  padding: .55rem 1rem;
  border: 1px solid var(--accent);
  border-radius: 8px;
  cursor: pointer;
  font-size: .95rem;
  font-weight: 700;
  text-decoration: none;
}

.primary {
  color: #fff;
  background: var(--accent);
}

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

.secondary,
.history-toggle {
  color: var(--accent-dark);
  background: transparent;
}

button:disabled {
  cursor: wait;
  opacity: .6;
}

.gentle-reminder,
.gentle-note,
.status {
  color: var(--soft-ink);
  font-size: .9rem;
  line-height: 1.45;
}

.gentle-reminder {
  margin: .9rem 0 0;
}

.status {
  min-height: 1.3em;
  margin: .35rem 0 0;
}

.history-section {
  width: 100%;
  margin-top: 1.5rem;
  padding-top: 1.15rem;
  border-top: 1px solid #cfc8ba;
  text-align: center;
}

.conversation {
  display: grid;
  gap: 1rem;
  margin-top: 1.5rem;
  text-align: left;
}

.message {
  max-width: 90%;
}

.message.user {
  margin-left: auto;
}

.speaker {
  margin: 0 0 .2rem .25rem;
  color: #6d7476;
  font-size: .72rem;
  font-weight: 700;
  text-transform: uppercase;
}

.message.user .speaker {
  margin-right: .25rem;
  text-align: right;
}

.bubble {
  padding: .8rem 1rem;
  background: #faf7f0;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: .95rem;
  line-height: 1.5;
  white-space: pre-wrap;
}

.message.user .bubble {
  background: var(--user);
}

.stopped-panel {
  margin-top: 1.5rem;
  text-align: center;
}

.choice-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin: 1.5rem 0;
}

.choice-grid form {
  display: flex;
}

.choice {
  width: 100%;
  min-height: 80px;
}

.flash {
  margin-bottom: 1rem;
  padding: .8rem 1rem;
  border-radius: 8px;
}

.flash.error {
  background: #ffe8e3;
}

.flash.success {
  background: #e3f3ec;
}

.hidden {
  display: none !important;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.photos-section {
  margin-top: 1.5rem;
}

.photos-heading {
  font-size: 1.05rem;
  margin-bottom: .5rem;
}

.photo-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: .75rem;
}

.photo-thumb {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: .5rem;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--user);
}

.photo-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.photo-delete {
  position: absolute;
  top: .25rem;
  right: .25rem;
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}

.photo-thumb.is-uploading img {
  opacity: .5;
}

.photo-thumb.just-added {
  animation: photo-added-flash 1.6s ease-out;
}

@keyframes photo-added-flash {
  0% {
    box-shadow: 0 0 0 4px var(--accent);
    transform: scale(1.04);
  }
  100% {
    box-shadow: 0 0 0 0 transparent;
    transform: scale(1);
  }
}

.photo-status-success {
  color: var(--accent-dark);
  font-weight: 600;
}

.photo-status-error {
  color: #a33;
  font-weight: 600;
}

@media (max-width: 700px) {
  .site-header {
    padding: .7rem 1rem;
  }

  .page-shell {
    width: calc(100% - 1rem);
    margin-top: .5rem;
  }

  .stationery-page {
    padding: 1rem;
  }

  .current-question {
    font-size: 1rem;
  }

  #answer {
    min-height: 230px;
  }

  .choice-grid {
    grid-template-columns: 1fr;
  }

  .answer-actions {
    flex-direction: column-reverse;
  }

  .answer-actions button {
    width: 100%;
  }
}


.send-actions {
  display: flex;
  gap: .75rem;
  margin-left: auto;
}

#nothing-else-button {
  white-space: nowrap;
}

@media (max-width: 700px) {
  .send-actions {
    width: 100%;
    flex-direction: column;
  }

  .send-actions button {
    width: 100%;
  }
}


.story-actions {
  display: flex;
  justify-content: center;
  gap: .85rem;
  margin-top: 1.35rem;
  padding-top: 1.2rem;
  border-top: 1px solid var(--border);
}

.finish-button {
  color: #fff;
  background: #6b5543;
  border-color: #6b5543;
}

.finish-button:hover {
  background: #574536;
}

@media (max-width: 700px) {
  .story-actions {
    flex-direction: column;
  }

  .story-actions button {
    width: 100%;
  }
}


.topic-picker-section {
  margin-top: 1.4rem;
  padding-top: 1.2rem;
  border-top: 1px solid var(--border);
  text-align: center;
}

.topic-picker-panel {
  margin: 1rem auto 0;
  max-width: 760px;
  text-align: left;
}

.topic-picker-panel label {
  display: block;
  margin-bottom: .5rem;
  font-weight: 600;
}

.topic-picker-row {
  display: flex;
  gap: .75rem;
}

.topic-picker-row input {
  flex: 1;
  min-width: 0;
}

@media (max-width: 700px) {
  .topic-picker-row {
    flex-direction: column;
  }

  .topic-picker-row button {
    width: 100%;
  }
}


/* v10: user-selected topic control at the top of the story page */
.stationery-page > .topic-picker-section {
  margin-top: 0;
  margin-bottom: 1.35rem;
  padding-top: 0;
  padding-bottom: 1.2rem;
  border-top: 0;
  border-bottom: 1px solid var(--border);
}

.stationery-page > .topic-picker-section + .prompt-section {
  margin-top: 0;
}


/* v11: topic picker replaces the current prompt inside the same box */
.topic-switch-button {
  margin-top: 1rem;
  color: var(--accent-dark);
  background: transparent;
  border-color: var(--accent);
}

.topic-picker-panel {
  width: 100%;
  max-width: none;
  text-align: left;
}

.topic-picker-panel label {
  display: block;
  margin-bottom: .65rem;
  color: var(--accent);
  font-size: .95rem;
  font-weight: 700;
}

.topic-picker-row {
  display: flex;
  gap: .75rem;
  width: 100%;
}

.topic-picker-row input {
  flex: 1;
  min-width: 0;
}

.topic-cancel-button {
  margin-top: .75rem;
  min-height: auto;
  padding: .35rem .65rem;
  color: var(--soft-ink);
  background: transparent;
  border-color: transparent;
  font-weight: 500;
}

@media (max-width: 700px) {
  .topic-picker-row {
    flex-direction: column;
  }

  .topic-picker-row button {
    width: 100%;
  }
}


/* v12: alphabetical story contents on the right */
.story-page-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  align-items: start;
  gap: 1.25rem;
}

.contents-sidebar {
  position: sticky;
  top: 1rem;
  max-height: calc(100vh - 2rem);
  overflow-y: auto;
  padding: 1.1rem;
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(39, 48, 46, .07);
}

.contents-sidebar h2 {
  margin: 0 0 .8rem;
  color: var(--accent-dark);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.15rem;
}

.contents-list {
  display: grid;
  gap: .25rem;
}

.contents-link {
  display: block;
  padding: .5rem .6rem;
  color: var(--ink);
  border-radius: 7px;
  text-decoration: none;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.contents-link:hover {
  background: #eef4f2;
}

.contents-link.current {
  color: #fff;
  background: var(--accent);
  font-weight: 700;
}

.contents-empty {
  margin: 0;
  color: var(--soft-ink);
  font-size: .9rem;
}

@media (max-width: 980px) {
  .story-page-grid {
    grid-template-columns: 1fr;
  }

  .contents-sidebar {
    position: static;
    max-height: none;
    order: 2;
  }

  .contents-list {
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  }
}


/* v13: previous-topic review mode */
.review-summary {
  margin: .35rem 0 1.1rem;
  padding: 1rem 1.1rem;
  color: var(--ink);
  background: #f7f3ea;
  border-left: 4px solid var(--accent);
  border-radius: 6px;
  font-size: 1rem;
  line-height: 1.6;
}

.review-question {
  margin-top: .75rem;
  color: var(--accent-dark);
  font-size: 1.08rem;
  font-weight: 700;
  line-height: 1.45;
}


/* v24: explicit saving state */
.answer-saving-wrap {
  position: relative;
}

.answer-saving-wrap.is-saving textarea {
  color: #777;
  background-color: #ecebea;
  opacity: .72;
  cursor: wait;
}

.saving-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #34413f;
  background: rgba(235, 235, 233, .86);
  border: 2px solid #a7b1ae;
  border-radius: 8px;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: .02em;
  cursor: wait;
  z-index: 5;
}

button:disabled,
input:disabled,
textarea:disabled {
  cursor: wait;
}

.story-actions {
  flex-wrap: wrap;
}
