* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #f5f5f5;
  --card-bg: #fff;
  --text: #333;
  --text-light: #666;
  --primary: #4a90d9;
  --primary-hover: #357abd;
  --border: #e0e0e0;
  --header-h: 56px;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans SC", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

/* Header */
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-h);
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  z-index: 100;
  box-shadow: var(--shadow);
}

.header-inner {
  max-width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 16px;
}

.logo {
  font-size: 18px;
  font-weight: 700;
  color: var(--primary);
  white-space: nowrap;
}

.search-bar {
  flex: 1;
  display: flex;
  gap: 8px;
  max-width: 500px;
}

.search-bar input {
  flex: 1;
  min-width: 0;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
}

.search-bar input:focus { border-color: var(--primary); }

.header-stats {
  font-size: 13px;
  color: var(--text-light);
  white-space: nowrap;
}

.github-link {
  color: var(--text-light);
  font-size: 13px;
  white-space: nowrap;
  text-decoration: none;
}

.github-link:hover,
.github-link:focus-visible {
  color: var(--primary);
  text-decoration: underline;
}

.login-github-link {
  display: inline-block;
  margin-top: 18px;
}

.btn-logout {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card-bg);
  color: var(--text-light);
  font-size: 13px;
  cursor: pointer;
}

.btn-logout:hover:not(:disabled) {
  border-color: var(--primary);
  color: var(--primary);
}

.btn-logout:disabled { opacity: 0.5; cursor: wait; }

/* Layout */
.main-layout {
  margin-top: var(--header-h);
  min-height: calc(100vh - var(--header-h));
}

/* Content */
.content {
  width: 100%;
  padding: 16px;
}

/* Meme Grid */
.meme-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
}

.meme-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.15s, box-shadow 0.15s;
}

.meme-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.meme-card.active {
  outline: 2px solid var(--primary);
}

.meme-card-preview {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  background: var(--bg);
  display: block;
}

.meme-card-info {
  padding: 8px;
}

.meme-card-name {
  font-size: 12px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meme-card-keywords {
  font-size: 11px;
  color: var(--text-light);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 2px;
}

/* Pagination */
.pagination {
  display: flex;
  justify-content: center;
  gap: 4px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.page-btn {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card-bg);
  cursor: pointer;
  font-size: 13px;
  transition: all 0.15s;
}

.page-btn:hover { border-color: var(--primary); color: var(--primary); }
.page-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.page-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* Loading */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 32px;
  color: var(--text-light);
  font-size: 14px;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* Form Section */
.meme-form-section {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-top: 16px;
  overflow: hidden;
}

.form-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.form-header h2 { font-size: 16px; }

.btn-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: var(--text-light);
  line-height: 1;
  padding: 0 4px;
}

.btn-close:hover { color: var(--text); }

.form-body { padding: 16px; }

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--text-light);
}

/* Image Upload */
.image-upload-area {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.image-slot {
  width: 120px;
  height: 120px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color 0.2s;
  position: relative;
  overflow: hidden;
}

.image-slot:hover { border-color: var(--primary); }
.image-slot.has-image { border-style: solid; }

.image-slot img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-slot-placeholder {
  font-size: 24px;
  color: var(--border);
  pointer-events: none;
}

.image-slot-label {
  font-size: 11px;
  color: var(--text-light);
  margin-top: 4px;
  pointer-events: none;
}

.image-slot input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.image-slot-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: rgba(0,0,0,0.6);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

/* Text Inputs */
.text-input-row {
  margin-bottom: 8px;
}

.text-input-row textarea {
  width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  font-family: inherit;
  line-height: 1.5;
  outline: none;
  resize: vertical;
}

.text-input-row textarea:focus { border-color: var(--primary); }

/* Args Inputs */
.args-row {
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.args-row label {
  font-size: 13px;
  min-width: 80px;
  margin-bottom: 0;
}

.args-row input[type="text"],
.args-row input[type="number"],
.args-row select {
  flex: 1;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  outline: none;
}

.args-row input:focus,
.args-row select:focus { border-color: var(--primary); }

.args-row input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.args-help {
  font-size: 11px;
  color: var(--text-light);
}

/* Buttons */
.form-actions {
  display: flex;
  gap: 8px;
  padding-top: 8px;
}

.btn {
  padding: 8px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}

.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-preview {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-preview:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }

.btn-generate {
  background: var(--primary);
  color: #fff;
}

.btn-generate:hover:not(:disabled) { background: var(--primary-hover); }

.btn-download {
  background: #5cb85c;
  color: #fff;
}

.btn-download:hover:not(:disabled) { background: #4a9a4a; }

/* Result */
.result-area {
  padding: 16px;
  border-top: 1px solid var(--border);
  text-align: center;
}

.result-area img {
  max-width: 100%;
  max-height: 500px;
  border-radius: var(--radius);
}

/* Toast */
.toast-container {
  position: fixed;
  top: calc(var(--header-h) + 12px);
  right: 16px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 10px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  color: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
  animation: toast-in 0.3s ease;
  max-width: 300px;
}

.toast-info { background: var(--primary); }
.toast-error { background: #d9534f; }
.toast-success { background: #5cb85c; }

@keyframes toast-in {
  from { opacity: 0; transform: translateX(20px); }
  to { opacity: 1; transform: translateX(0); }
}

/* Login */
.login-page {
  min-height: 100vh;
  background: var(--bg);
}

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(100%, 380px);
  padding: 32px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.08);
}

.login-brand {
  color: var(--primary);
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.login-panel h1 {
  margin-top: 12px;
  font-size: 24px;
}

.login-subtitle {
  margin-top: 6px;
  color: var(--text-light);
  font-size: 14px;
}

#login-form {
  margin-top: 24px;
}

#login-form label {
  display: block;
  margin: 14px 0 6px;
  font-size: 13px;
  font-weight: 600;
}

#login-form input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font: inherit;
  outline: none;
}

#login-form input:focus { border-color: var(--primary); }

#login-submit {
  width: 100%;
  margin-top: 22px;
  padding: 10px 14px;
  border: 0;
  border-radius: var(--radius);
  background: var(--primary);
  color: #fff;
  font: inherit;
  cursor: pointer;
}

#login-submit:hover:not(:disabled) { background: var(--primary-hover); }
#login-submit:disabled { opacity: 0.6; cursor: wait; }

.login-error {
  margin-top: 12px;
  color: #d9534f;
  font-size: 13px;
}

/* Responsive */
@media (max-width: 768px) {
  .header-stats { display: none; }

  .search-bar { max-width: none; }

  .meme-grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  }

  .image-slot {
    width: 100px;
    height: 100px;
  }
}

@media (max-width: 480px) {
  .meme-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .form-actions {
    flex-wrap: wrap;
  }

  .btn { flex: 1; min-width: 80px; }
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #aaa; }
