* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: #fdf6ec;
  color: #3a3a3a;
}

.topbar {
  background: #ffffff;
  padding: 16px 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.topbar h1 {
  margin: 0;
  font-size: 20px;
  color: #ff6b6b;
}

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px;
}

.input-panel {
  display: flex;
  gap: 12px;
  margin-bottom: 24px;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 16px;
  padding: 10px 14px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.toolbar-select-all {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  cursor: pointer;
  user-select: none;
}

.toolbar-count {
  font-size: 13px;
  color: #666;
  flex: 1;
}

#exportPdfBtn {
  padding: 8px 20px;
  border: none;
  border-radius: 10px;
  background: #ff6b6b;
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

#exportPdfBtn:hover:not(:disabled) {
  background: #ff5252;
}

#exportPdfBtn:disabled {
  background: #ffb3b3;
  cursor: not-allowed;
}

#wordsInput {
  flex: 1;
  min-height: 80px;
  padding: 12px 14px;
  border-radius: 12px;
  border: 2px solid #ffd9d9;
  font-size: 15px;
  resize: vertical;
  outline: none;
}

#wordsInput:focus {
  border-color: #ff6b6b;
}

#generateBtn {
  padding: 0 28px;
  border: none;
  border-radius: 12px;
  background: #ff6b6b;
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

#generateBtn:hover {
  background: #ff5252;
}

#generateBtn:disabled {
  background: #ffb3b3;
  cursor: not-allowed;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 20px;
}

.card {
  background: #fff;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  transition: transform 0.15s;
}

.card:hover {
  transform: translateY(-3px);
}

.card-image-wrap {
  width: 100%;
  aspect-ratio: 3 / 4;
  background: #fef0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.card-image-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.card-delete-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  font-size: 15px;
  line-height: 24px;
  padding: 0;
  cursor: pointer;
  z-index: 1;
}

.card-delete-btn:hover {
  background: rgba(211, 51, 51, 0.85);
}

.card-select-checkbox {
  position: absolute;
  top: 8px;
  left: 8px;
  width: 20px;
  height: 20px;
  z-index: 1;
  cursor: pointer;
}

.card-select-checkbox:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.card-status {
  font-size: 13px;
  color: #999;
  text-align: center;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 4px solid #ffd9d9;
  border-top-color: #ff6b6b;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.card-footer {
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-word {
  font-weight: 700;
  color: #ff6b6b;
  font-size: 15px;
}

.card-meaning {
  font-size: 13px;
  color: #666;
}

.badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 8px;
  font-weight: 600;
}

.badge-pending, .badge-generating {
  background: #fff3cd;
  color: #a67c00;
}

.badge-success {
  background: #d4f4dd;
  color: #2a9d5c;
}

.badge-failed {
  background: #fddede;
  color: #d33;
}

.retry-btn {
  margin: 0 12px 12px;
  padding: 6px 0;
  border: none;
  border-radius: 8px;
  background: #ffefef;
  color: #d33;
  font-weight: 600;
  cursor: pointer;
}

.retry-btn:hover {
  background: #ffdada;
}

.error-msg {
  font-size: 12px;
  color: #d33;
  padding: 0 12px 8px;
  word-break: break-word;
}

/* Modal */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-overlay.open {
  display: flex;
}

.modal {
  background: #fff;
  border-radius: 16px;
  max-width: 640px;
  width: 90%;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
  padding: 24px;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 16px;
  border: none;
  background: none;
  font-size: 24px;
  cursor: pointer;
  color: #999;
}

.modal-image {
  width: 100%;
  max-width: 320px;
  margin: 0 auto 16px;
  display: block;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.modal-title {
  font-size: 22px;
  font-weight: 700;
  color: #ff6b6b;
  text-align: center;
}

.meta-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 12px 0 16px;
}

.meta-meaning-input,
.meta-sentence-input {
  padding: 10px 12px;
  border-radius: 10px;
  border: 2px solid #ffd9d9;
  outline: none;
  font-size: 14px;
}

.meta-meaning-input:focus,
.meta-sentence-input:focus {
  border-color: #ff6b6b;
}

.meta-regenerate-btn {
  padding: 8px 0;
  border: none;
  border-radius: 10px;
  background: #ffefef;
  color: #d33;
  font-weight: 600;
  cursor: pointer;
}

.meta-regenerate-btn:hover {
  background: #ffdada;
}

.meta-regenerate-btn:disabled {
  color: #f2a5a5;
  cursor: not-allowed;
}

.version-list {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding-bottom: 8px;
  margin-bottom: 16px;
}

.version-item {
  position: relative;
  flex-shrink: 0;
  margin-top: 16px;
}

.version-thumb {
  width: 60px;
  height: 80px;
  border-radius: 8px;
  object-fit: cover;
  cursor: pointer;
  border: 2px solid transparent;
  flex-shrink: 0;
  display: block;
}

.version-thumb.active {
  border-color: #ff6b6b;
}

.version-thumb.failed-thumb {
  background: #fddede;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: #d33;
}

.version-delete-btn {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: none;
  background: #333;
  color: #fff;
  font-size: 12px;
  line-height: 18px;
  padding: 0;
  cursor: pointer;
}

.refine-panel {
  display: flex;
  gap: 8px;
}

.refine-panel input {
  flex: 1;
  padding: 10px 12px;
  border-radius: 10px;
  border: 2px solid #ffd9d9;
  outline: none;
}

.refine-panel input:focus {
  border-color: #ff6b6b;
}

.refine-panel button {
  padding: 0 20px;
  border: none;
  border-radius: 10px;
  background: #ff6b6b;
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.refine-panel button:disabled {
  background: #ffb3b3;
  cursor: not-allowed;
}

/* Auth overlay */
body.locked .topbar,
body.locked .container,
body.locked .modal-overlay {
  display: none;
}

.auth-overlay {
  position: fixed;
  inset: 0;
  background: #fdf6ec;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.auth-box {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  padding: 32px 28px;
  width: 90%;
  max-width: 320px;
  text-align: center;
}

.auth-box h2 {
  margin: 0 0 20px;
  font-size: 18px;
  color: #ff6b6b;
}

.auth-box input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 2px solid #ffd9d9;
  outline: none;
  font-size: 15px;
  margin-bottom: 14px;
}

.auth-box input:focus {
  border-color: #ff6b6b;
}

.auth-box button {
  width: 100%;
  padding: 10px 0;
  border: none;
  border-radius: 10px;
  background: #ff6b6b;
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.auth-box button:hover {
  background: #ff5252;
}

.auth-error {
  margin-top: 10px;
  min-height: 16px;
  font-size: 13px;
  color: #d33;
}

/* Mobile */
@media (max-width: 640px) {
  .topbar {
    padding: 12px 16px;
  }

  .topbar h1 {
    font-size: 17px;
  }

  .container {
    padding: 12px;
  }

  .input-panel {
    flex-direction: column;
    gap: 10px;
    margin-bottom: 16px;
  }

  #wordsInput {
    font-size: 16px;
  }

  #generateBtn {
    width: 100%;
    padding: 12px 0;
  }

  .toolbar {
    flex-wrap: wrap;
    gap: 10px;
    padding: 10px 12px;
    margin-bottom: 12px;
  }

  .toolbar-count {
    flex: 1 1 100%;
    order: 2;
  }

  #exportPdfBtn {
    order: 3;
    width: 100%;
    padding: 10px 0;
  }

  .grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 12px;
  }

  .card-word {
    font-size: 14px;
  }

  .card-meaning {
    font-size: 12px;
  }

  .card-delete-btn {
    width: 28px;
    height: 28px;
    font-size: 16px;
    line-height: 28px;
  }

  .card-select-checkbox {
    width: 22px;
    height: 22px;
  }

  .modal-overlay {
    align-items: flex-end;
  }

  .modal {
    width: 100%;
    max-height: 92vh;
    border-radius: 16px 16px 0 0;
    padding: 20px 16px;
  }

  .modal-image {
    max-width: 220px;
  }

  .modal-title {
    font-size: 19px;
  }

  .modal-close {
    width: 32px;
    height: 32px;
    font-size: 22px;
  }

  .refine-panel {
    flex-direction: column;
  }

  .refine-panel input {
    font-size: 16px;
  }

  .refine-panel button {
    padding: 12px 0;
  }
}

@media (max-width: 360px) {
  .grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 8px;
  }
}
