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

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 20px;
  position: relative;
  overflow-x: hidden;
}

.gradient-bg {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  z-index: -1;
}

.container {
  max-width: 650px;
  width: 100%;
  padding-top: 20px;
}

header {
  text-align: center;
  margin-bottom: 24px;
}

h1 {
  color: white;
  font-size: 2.2rem;
  font-weight: 800;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
  margin-bottom: 6px;
}

.subtitle {
  color: rgba(255, 255, 255, 0.8);
  font-size: 1rem;
}

.meme-card {
  background: #1e1e2e;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.meme-preview {
  position: relative;
  background: #000;
  min-height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.meme-preview img {
  max-width: 100%;
  max-height: 450px;
  display: block;
}

.meme-text {
  position: absolute;
  left: 0;
  right: 0;
  text-align: center;
  /* font-family controlled by JavaScript */
  font-size: 32px;
  color: white;
  text-transform: uppercase;
  text-shadow: 
    3px 3px 0 #000,
    -3px -3px 0 #000,
    3px -3px 0 #000,
    -3px 3px 0 #000,
    0 3px 0 #000,
    0 -3px 0 #000,
    3px 0 0 #000,
    -3px 0 0 #000;
  padding: 10px 20px;
  word-wrap: break-word;
  line-height: 1.1;
  transition: font-family 0s, all 0.15s ease;
  font-weight: bold;
}

.top-text {
  top: 5%;
}

.bottom-text {
  bottom: 5%;
}

.editor-panel {
  padding: 20px;
  background: #252536;
}

.text-section {
  margin-bottom: 16px;
}

.text-section:last-child {
  margin-bottom: 0;
}

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

.section-title {
  color: #fff;
  font-weight: 600;
  font-size: 0.9rem;
}

.toggle-options {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: #aaa;
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.toggle-options:hover {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
}

.toggle-options.active {
  background: #667eea;
  color: white;
}

.input-group {
  margin-bottom: 0;
}

.input-group input[type="text"] {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #3a3a4a;
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
  background: #1e1e2e;
  color: #fff;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.input-group input[type="text"]:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}

.input-group input[type="text"]::placeholder {
  color: #666;
}

.text-options {
  background: #1a1a2a;
  border-radius: 8px;
  padding: 12px;
  margin-top: 10px;
  border: 1px solid #3a3a4a;
}

.option-row {
  display: flex;
  gap: 12px;
  margin-bottom: 10px;
}

.option-row:last-child {
  margin-bottom: 0;
}

.option-group {
  flex: 1;
}

.option-group label {
  display: block;
  color: #888;
  font-size: 0.75rem;
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.option-group select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #3a3a4a;
  border-radius: 6px;
  font-size: 0.85rem;
  background: #252536;
  color: #fff;
  cursor: pointer;
}

.option-group select:focus {
  outline: none;
  border-color: #667eea;
}

.option-group input[type="range"] {
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: #3a3a4a;
  appearance: none;
  cursor: pointer;
}

.option-group input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #667eea;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.option-group input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.2);
}

.option-group input[type="color"] {
  width: 36px;
  height: 28px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  background: none;
  padding: 0;
}

.option-group input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 0;
}

.option-group input[type="color"]::-webkit-color-swatch {
  border: 2px solid #3a3a4a;
  border-radius: 4px;
}

.color-options {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.color-options label {
  margin-bottom: 0;
}

.size-value {
  color: #667eea;
  font-size: 0.75rem;
  font-weight: 600;
  margin-left: 8px;
}

.controls {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-top: 16px;
}

.control-btn {
  padding: 12px 16px;
  border: none;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  font-family: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.random-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.random-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.template-btn {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
  box-shadow: 0 4px 15px rgba(240, 147, 251, 0.3);
}

.template-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(240, 147, 251, 0.4);
}

.reset-btn {
  background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
  color: #333;
  box-shadow: 0 4px 15px rgba(252, 182, 159, 0.3);
}

.reset-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(252, 182, 159, 0.4);
}

.download-btn {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
  color: #1a1a2e;
  box-shadow: 0 4px 15px rgba(67, 233, 123, 0.3);
}

.download-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(67, 233, 123, 0.4);
}

.control-btn:active {
  transform: translateY(0);
}

.template-picker {
  background: #1e1e2e;
  border-radius: 16px;
  padding: 20px;
  margin-top: 16px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.picker-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  gap: 16px;
}

.picker-header h3 {
  color: #fff;
  font-size: 1rem;
  white-space: nowrap;
}

#templateSearch {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #3a3a4a;
  border-radius: 8px;
  background: #252536;
  color: #fff;
  font-size: 0.9rem;
}

#templateSearch:focus {
  outline: none;
  border-color: #667eea;
}

#templateSearch::placeholder {
  color: #666;
}

.template-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: 10px;
  max-height: 280px;
  overflow-y: auto;
  padding-right: 5px;
}

.template-grid::-webkit-scrollbar {
  width: 6px;
}

.template-grid::-webkit-scrollbar-track {
  background: #1a1a2a;
  border-radius: 3px;
}

.template-grid::-webkit-scrollbar-thumb {
  background: #3a3a4a;
  border-radius: 3px;
}

.template-grid::-webkit-scrollbar-thumb:hover {
  background: #4a4a5a;
}

.template-item {
  cursor: pointer;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid transparent;
  transition: all 0.2s ease;
  background: #252536;
}

.template-item:hover {
  border-color: #667eea;
  transform: scale(1.05);
}

.template-item.selected {
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.3);
}

.template-item img {
  width: 100%;
  height: 70px;
  object-fit: cover;
  display: block;
}

.template-item span {
  display: block;
  padding: 4px 6px;
  font-size: 0.65rem;
  text-align: center;
  color: #aaa;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.template-item.hidden {
  display: none;
}

footer {
  text-align: center;
  margin-top: 24px;
  color: rgba(255, 255, 255, 0.6);
  font-size: 0.85rem;
}

.shortcuts-hint {
  color: #667eea;
  cursor: pointer;
}

.shortcuts-hint:hover {
  text-decoration: underline;
}

.toast {
  position: fixed;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: rgba(30, 30, 46, 0.95);
  color: white;
  padding: 14px 24px;
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 500;
  opacity: 0;
  transition: all 0.4s ease;
  z-index: 1000;
  max-width: 90%;
  text-align: center;
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* Modal */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  backdrop-filter: blur(5px);
}

.modal-content {
  background: #1e1e2e;
  border-radius: 16px;
  padding: 24px;
  max-width: 400px;
  width: 90%;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.modal-content h3 {
  color: #fff;
  margin-bottom: 16px;
  text-align: center;
}

.shortcuts-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 20px;
}

.shortcut {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #aaa;
  font-size: 0.9rem;
}

.shortcut kbd {
  background: #252536;
  border: 1px solid #3a3a4a;
  border-radius: 4px;
  padding: 4px 8px;
  font-family: inherit;
  font-size: 0.8rem;
  color: #fff;
}

.close-modal {
  width: 100%;
  padding: 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 8px;
  color: white;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.close-modal:hover {
  transform: scale(1.02);
}

/* Card entrance animation */
@keyframes cardEntrance {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.meme-card.animate {
  animation: cardEntrance 0.3s ease-out;
}

/* Loading state */
.loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 40px;
  height: 40px;
  margin: -20px 0 0 -20px;
  border: 4px solid rgba(255, 255, 255, 0.2);
  border-top-color: #667eea;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Responsive */
@media (max-width: 600px) {
  .container {
    padding-top: 10px;
  }
  
  h1 {
    font-size: 1.8rem;
  }
  
  .controls {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .option-row {
    flex-direction: column;
  }
  
  .template-grid {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .picker-header {
    flex-direction: column;
    align-items: stretch;
  }
  
  .picker-header h3 {
    text-align: center;
    margin-bottom: 8px;
  }
}
