:root[data-bs-theme="dark"] {
  --story-section-bg: #2b3035;
  --download-btn-bg: rgba(33, 37, 41, 0.9);
  --download-btn-hover: #2b3035;
}

:root[data-bs-theme="light"] {
  --story-section-bg: #f8f9fa;
  --download-btn-bg: rgba(255, 255, 255, 0.9);
  --download-btn-hover: white;
}

.story-section {
  display: flex;
  margin-bottom: 2rem;
  padding: 1rem;
  background: var(--story-section-bg);
  border-radius: 8px;
}

.image-container {
  flex: 0 0 300px;
  margin-right: 1rem;
  position: relative;
  display: flex;
  align-items: flex-start;
}

.image-container img {
  width: 100%;
  height: auto;
  border-radius: 4px;
  cursor: pointer;
  transition: transform 0.3s ease;
}

.image-container img:hover {
  transform: scale(1.02);
}

.image-container .download-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  background: var(--download-btn-bg);
  border: none;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.3s ease, opacity 0.3s ease;
  opacity: 0;
  color: #28a745;
}

.image-wrapper:hover .download-btn {
  opacity: 1;
}

.image-container .download-btn:hover {
  background: var(--download-btn-hover);
}

.image-wrapper {
  position: relative;
  flex: 1;
}

.regenerate-btn {
  position: absolute;
  top: 10px;
  right: 50px;
  background: var(--download-btn-bg);
  border: none;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.3s ease, opacity 0.3s ease;
  font-weight: bold;
  opacity: 0;
  color: #0d6efd;
}

.image-wrapper:hover .regenerate-btn {
  opacity: 1;
}

.regenerate-btn:hover {
  background: var(--download-btn-hover);
}

.regenerate-modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--bs-body-bg);
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 15px rgba(0,0,0,0.2);
  z-index: 1001;
  width: 90%;
  max-width: 500px;
}

.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  z-index: 1000;
}

.paragraph-container {
  flex: 1;
  padding: 0.5rem;
}

#characters {
  resize: vertical;
}

#themeToggle {
  width: 40px;
  height: 40px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bs-body-bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0.9;
}

.loading-overlay p {
  margin-top: 1rem;
  font-weight: 500;
}

.btn {
  transition: all 0.3s ease;
}

.btn:hover {
  transform: translateY(-2px);
}

.btn-link {
  color: inherit;
  text-decoration: none;
}

.btn-link:hover {
  color: var(--bs-primary);
}

#storyResult .btn-link {
  padding: 0;
  border: none;
  background: none;
  cursor: pointer;
}

#storyForm {
  max-width: 800px;
  margin: 0 auto;
}

.seed-info {
  font-size: 0.9em;
  color: var(--bs-secondary);
}

.row.align-items-end {
  margin-bottom: 1rem;
}

input[type="number"].form-control {
  height: calc(3rem + 2px);
}

/* Responsive design improvements */
@media (max-width: 768px) {
  .story-section {
    flex-direction: column;
  }

  .image-container {
    flex: none;
    width: 100%;
    margin-right: 0;
    margin-bottom: 1rem;
  }

  .image-wrapper {
    width: 100%;
  }

  .paragraph-container {
    width: 100%;
  }

  #storyForm {
    padding: 0.5rem;
  }

  .regenerate-modal {
    width: 95%;
    max-height: 90vh;
    overflow-y: auto;
    padding: 15px;
  }

  .btn {
    padding: 0.5rem;
    font-size: 0.9rem;
  }

  /* Adjust button containers for mobile */
  .d-flex.align-items-center.justify-content-between {
    flex-direction: column;
    gap: 1rem;
  }

  .d-flex.gap-2 {
    width: 100%;
    flex-direction: column;
  }

  .text-end.ms-3 {
    width: 100%;
    text-align: left !important;
    margin-left: 0 !important;
  }

  /* Make buttons full width on mobile */
  .btn-primary, .btn-secondary {
    width: 100%;
  }
}

/* Tablet-specific adjustments */
@media (min-width: 769px) and (max-width: 1024px) {
  .story-section {
    margin-bottom: 1.5rem;
  }

  .image-container {
    flex: 0 0 250px;
  }

  #storyForm {
    max-width: 90%;
  }
}

/* Ensure minimum touch target sizes */
@media (max-width: 1024px) {
  .download-btn,
  .regenerate-btn,
  .btn {
    min-width: 44px;
    min-height: 44px;
  }

  .form-control,
  .form-select {
    min-height: 44px;
  }

  input[type="number"].form-control {
    height: 44px;
  }
}

/* Improve form layout on smaller screens */
@media (max-width: 576px) {
  .row.align-items-end {
    margin-bottom: 0.5rem;
  }

  .col-md-6 {
    margin-bottom: 1rem;
  }

  .card {
    border-radius: 0;
    margin: -1rem;
  }

  .container {
    padding: 0.5rem;
  }

  h1 {
    font-size: 1.5rem;
    margin-bottom: 1rem;
  }

  .loading-overlay p {
    font-size: 0.9rem;
  }
}

/* Add accessibility improvements */
@media (any-pointer: coarse) {
  .btn,
  .form-control,
  .form-select {
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
}

/* Improve readability on different screen sizes */
@media (max-width: 768px) {
  body {
    font-size: 14px;
  }

  .form-label {
    font-size: 0.9rem;
  }

  .small, small {
    font-size: 0.8rem;
  }
}

/* Print styles */
@media print {
  .btn,
  .form-control,
  .regenerate-btn,
  .download-btn,
  #themeToggle {
    display: none !important;
  }

  .story-section {
    break-inside: avoid;
    page-break-inside: avoid;
  }
}

/* High contrast improvements */
@media (prefers-contrast: high) {
  :root[data-bs-theme="light"] {
    --story-section-bg: #ffffff;
    --download-btn-bg: rgba(0, 0, 0, 0.9);
  }

  :root[data-bs-theme="dark"] {
    --story-section-bg: #000000;
    --download-btn-bg: rgba(255, 255, 255, 0.9);
  }
}