:root {
  color-scheme: dark;
  font-family: Arial, Helvetica, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: #101114;
  color: #f2f4f8;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.65);
  padding: 16px;
}

.modal-overlay[hidden] {
  display: none !important;
}

.modal-card {
  width: min(680px, 100%);
  background: #1a1d24;
  border: 1px solid #2b3340;
  border-radius: 10px;
  padding: 14px;
}

.modal-card h2 {
  margin: 0 0 8px;
}

.modal-list {
  margin: 8px 0 0;
  padding-left: 20px;
  line-height: 1.4;
}

.container {
  max-width: 1050px;
  margin: 0 auto;
  padding: 12px 12px 24px;
}

.page-header h1 {
  margin: 0;
  font-size: 1.35rem;
}

.page-header p {
  margin-top: 4px;
  color: #bec6d1;
  font-size: 0.9rem;
}

.panel {
  background: #1a1d24;
  border: 1px solid #2b3340;
  border-radius: 8px;
  padding: 10px 12px;
  margin-top: 10px;
}

.panel h2 {
  margin-top: 0;
  font-size: 1rem;
}

.book-section-wrap {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 16px;
  margin-top: 10px;
  align-items: start;
}

.book-section-main {
  min-width: 0;
}

.book-section-sidebar {
  position: sticky;
  top: 12px;
}

.accessibility-sidebar-panel .accessibility-sidebar-grid {
  grid-template-columns: 1fr;
}

@media (max-width: 900px) {
  .book-section-wrap {
    grid-template-columns: 1fr;
  }

  .book-section-sidebar {
    position: static;
  }
}

.grid {
  display: grid;
  gap: 8px;
}

.two-col {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.three-col {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.four-col {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.top-gap {
  margin-top: 8px;
}

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
}

label.checkbox-label {
  flex-direction: row;
  align-items: center;
  cursor: pointer;
}

label.checkbox-label input[type="checkbox"] {
  width: auto;
  margin: 0;
  margin-right: 8px;
}

input,
select,
textarea,
button {
  font: inherit;
}

input,
select,
textarea {
  border: 1px solid #384353;
  border-radius: 6px;
  background: #101114;
  color: #f2f4f8;
  padding: 6px 8px;
  font-size: 13px;
}

textarea {
  resize: vertical;
}

/* Color picker group: named presets + custom picker */
.color-picker-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.color-picker-group label {
  margin: 0;
}

.color-input-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.color-input-row input[type="color"] {
  width: 36px;
  height: 32px;
  padding: 2px;
  cursor: pointer;
  border-radius: 4px;
}

.color-input-row input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 2px;
}

.color-input-row .color-preset {
  flex: 1;
  min-width: 0;
  font-size: 13px;
}

.color-name {
  font-size: 13px;
  color: #9fadbf;
}

button {
  border: 1px solid #4c5d75;
  border-radius: 6px;
  background: #2a3140;
  color: #f2f4f8;
  padding: 6px 10px;
  font-size: 13px;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.2s ease;
}

button.primary {
  background: #2d5bd1;
  border-color: #2d5bd1;
}

button.danger {
  background: #5f2730;
  border-color: #89323f;
}

button:hover {
  filter: brightness(1.08);
}

/* Fun button click animations (respects prefers-reduced-motion) */
@media (prefers-reduced-motion: no-preference) {
  button.animate-pop {
    animation: button-pop 0.4s ease;
  }
}

@keyframes button-pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.08); }
  70% { transform: scale(0.98); }
  100% { transform: scale(1); }
}

.section-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.hint {
  margin-top: 0;
  margin-bottom: 0;
  color: #b9c2ce;
  font-size: 12px;
}

.live-preview-heading {
  display: flex;
  align-items: center;
  gap: 8px;
}

.live-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: #d8fbe1;
  background: #1b6f3a;
  border: 1px solid #2aa861;
}

.spreads {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.spread-card {
  border: 1px solid #344154;
  border-radius: 6px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: #141820;
}

.spread-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.spread-title-row h3 {
  margin: 0;
}

.selected-images {
  margin: 0;
  color: #a9b3c1;
  font-size: 13px;
}

.image-prompt-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.image-prompt-row .image-prompt-label {
  margin: 0;
}

.image-prompt-row .copy-image-prompt {
  align-self: flex-start;
}

.wikimedia-search-block {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.wikimedia-search-row {
  display: flex;
  gap: 8px;
}

.wikimedia-search-row input {
  flex: 1;
  min-width: 0;
}

.wikimedia-results {
  margin-top: 8px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 8px;
}

.wikimedia-result-card {
  border: 1px solid #344154;
  background: #141820;
  border-radius: 6px;
  padding: 6px;
  text-align: left;
}

.wikimedia-result-card img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 4px;
}

.wikimedia-result-card .wikimedia-title {
  margin-top: 6px;
  font-size: 12px;
  color: #c6d0dd;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.download-link {
  border: 1px solid #4c5d75;
  border-radius: 6px;
  background: #2a3140;
  color: #f2f4f8;
  padding: 6px 10px;
  font-size: 13px;
  text-decoration: none;
}

.download-link.disabled {
  opacity: 0.6;
  pointer-events: none;
}

#image-tool-status {
  margin-top: 8px;
}

.image-preview-card h3 {
  margin: 0 0 8px;
  font-size: 14px;
}

.image-preview-box {
  min-height: 220px;
  border: 1px dashed #4c5d75;
  border-radius: 8px;
  background: #101114;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
}

.image-preview-box img {
  width: 100%;
  height: 100%;
  max-height: 420px;
  object-fit: contain;
}

.actions,
.story-input-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

#statusMessage {
  margin: 0;
  color: #c6d0dd;
}

.preview-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.preview-page {
  width: 100%;
  max-width: 980px;
  aspect-ratio: 11 / 8.5;
  border: 1px dashed #5c6880;
  border-radius: 8px;
  overflow: hidden;
  position: relative;
  margin: 0 auto;
  background: #ffffff;
}

.preview-page.odd {
  color: #ffffff;
}

.preview-zone {
  position: absolute;
  padding: 10px;
}

.preview-odd-text {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-weight: 700;
  word-break: break-word;
  line-height: 1.08;
}

.preview-odd-images {
  display: grid;
  gap: 8px;
}

.preview-odd-images.high-support {
  gap: 20%;
}

.preview-odd-images.count-1 {
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
}

.preview-odd-images.count-2 {
  grid-template-columns: 1fr 1fr;
}

.preview-odd-images.count-3 {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1.1fr 1fr;
}

.preview-odd-images.count-3 .preview-img-wrap:first-child {
  grid-column: 1 / span 2;
}

.preview-odd-images.count-4 {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}

.preview-img-wrap {
  width: 100%;
  height: 100%;
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-img-wrap img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.preview-story {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 42px;
  font-size: 34px;
  line-height: 1.25;
  transform: rotate(180deg);
  word-break: break-word;
  overflow: hidden;
}

.preview-label {
  margin: 6px 0 0;
  text-align: center;
  color: #9fadbf;
  font-size: 13px;
}

@media print {
  @page {
    size: letter landscape;
    margin: 0.25in;
  }

  body {
    background: #fff;
    color: #000;
  }

  .page-header,
  .panel:not(.preview-printable),
  .preview-label {
    display: none !important;
  }

  .container {
    max-width: none;
    margin: 0;
    padding: 0;
  }

  .preview-printable {
    border: none;
    background: transparent;
    margin: 0;
    padding: 0;
  }

  .preview-page {
    width: 100%;
    max-width: none;
    border: none;
    border-radius: 0;
    margin: 0;
    page-break-after: always;
    break-after: page;
  }

  .preview-page:last-child {
    page-break-after: auto;
    break-after: auto;
  }
}

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

@media (max-width: 640px) {
  .two-col,
  .three-col,
  .four-col {
    grid-template-columns: 1fr;
  }

  .wikimedia-search-row {
    flex-direction: column;
  }
}
