:root {
  --saffron: #ff9933;
  --navy: #000080;
  --gold: #ffd700;
  --light-bg: #fffbf2;
  --dark-text: #333;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

[data-bs-theme="dark"] {
  --light-bg: #212529;
  --dark-text: #dee2e6;
}

body {
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif;
  margin: 0;
  background-color: var(--light-bg);
  color: var(--dark-text);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

header {
  background: var(--navy);
  color: white;
  padding: 15px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.logo {
  font-weight: bold;
  font-size: 1.2rem;
}

#helpBtn {
  width: 30px;
  height: 30px;
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.2s;
}

#helpBtn:hover {
  opacity: 0.8;
}

.thumbnail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 12px;
  padding: 20px;
}

/* Styles for the container of each thumbnail */
.image-container {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f0f0; /* Placeholder background */
  border-radius: 8px;
  overflow: hidden;
  aspect-ratio: 1; /* Maintain aspect ratio for the container */
  border: 3px solid transparent; /* Mimic original img-thumbnail border */
  transition: transform 0.2s; /* Apply hover effect to container */
  cursor: pointer; /* Apply cursor to container */
}

.image-container:hover {
  transform: scale(1.05);
}

/* Loading spinner for the container */
.image-container.loading::before {
  content: "";
  position: absolute;
  border: 4px solid rgba(0, 0, 0, 0.1); /* Light grey */
  border-top: 4px solid var(--saffron); /* Use saffron from theme */
  border-radius: 50%;
  width: 30px;
  height: 30px;
  animation: spin 1s linear infinite;
  z-index: 1; /* Ensure spinner is above image if it partially loads */
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* Styles for the actual image inside the container */
.image-container img {
  width: 100%;
  height: 100%; /* Fill the container */
  object-fit: cover;
  border-radius: 8px;
  opacity: 0; /* Start hidden */
  transition: opacity 0.3s ease-in-out;
}

/* When image is loaded, make it visible */
.image-container img.loaded {
  opacity: 1;
}

#editorSection {
  padding: 15px;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.canvas-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
  touch-action: none;
}

#imageWrapper {
  position: relative;
  max-height: 100%;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
  line-height: 0;
}

#mainImage {
  width: 100%;
  height: auto;
  max-height: 75vh;
  object-fit: contain;
  border-radius: 4px;
}

#textPreview {
  user-select: none;
  line-height: 1.4;
  min-width: 50px;
  word-wrap: break-word;
}

.emoji-btn {
  font-size: 1.2rem;
  padding: 4px 8px;
  line-height: 1;
  transition: transform 0.1s;
}
.emoji-btn:active {
  transform: scale(0.9);
}

.btn-group .btn.active {
  background-color: #6c757d;
  color: white;
}

.editor-controls {
  max-width: 500px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#textInput {
  padding: 12px;
  border: 2px solid #ddd;
  border-radius: 8px;
  font-size: 16px;
}

.color-palette {
  display: flex;
  gap: 8px;
  margin-bottom: 4px;
  flex-wrap: wrap;
  justify-content: center;
}

.color-swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid white;
  box-shadow: 0 0 0 1px #ddd;
  cursor: pointer;
  padding: 0;
  transition: transform 0.1s;
}

.color-swatch:active {
  transform: scale(0.9);
}

.tool-row {
  display: flex;
  gap: 10px;
  justify-content: space-around;
}

.control-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
}

.action-buttons {
  display: flex;
  gap: 10px;
}

.btn-primary {
  background: var(--saffron);
  color: white;
  border: none;
  padding: 12px;
  border-radius: 8px;
  font-weight: bold;
  flex: 2;
  cursor: pointer;
}

.btn-secondary {
  background: #eee;
  color: #555;
  border: none;
  padding: 12px;
  border-radius: 8px;
  flex: 1;
  cursor: pointer;
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.85);
  z-index: 1000;
  display: flex;
  align-items: flex-start; /* Better for scrolling on short screens */
  justify-content: center;
  overflow-y: auto;
  padding: 20px 10px;
}

.modal-content {
  background: white;
  padding: 20px;
  border-radius: 15px;
  width: 90%;
  max-width: 500px;
  text-align: center;
  position: relative;
  margin: auto;
}

#finalCanvas {
  max-width: 100%;
  max-height: 65vh; /* Prevents canvas from pushing buttons off-screen */
  width: auto;
  height: auto;
  border-radius: 8px;
  margin-bottom: 15px;
  display: block;
  margin-left: auto;
  margin-right: auto;
}

.modal-footer {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.help-text {
  text-align: left;
}

footer {
  padding: 20px;
  text-align: center;
  font-size: 0.9rem;
  color: #777;
}

footer a {
  color: var(--navy);
  text-decoration: none;
  font-weight: bold;
}

.hidden {
  display: none !important;
}

@media (max-width: 480px) {
  .thumbnail-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}
