/* ========== Reset & Variables ========== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f1117;
  --bg-card: #1a1d27;
  --bg-input: #252833;
  --bg-hover: #2a2d3a;
  --border: #333648;
  --border-focus: #6366f1;
  --text: #e4e5eb;
  --text-dim: #9395a5;
  --text-muted: #6b6e80;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --accent-glow: rgba(99, 102, 241, 0.25);
  --green: #22c55e;
  --green-dim: rgba(34, 197, 94, 0.15);
  --red: #ef4444;
  --red-dim: rgba(239, 68, 68, 0.12);
  --orange: #f59e0b;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
  --font: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', system-ui, -apple-system, sans-serif;
  --mono: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
}

html { font-size: 15px; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.6;
}

/* ========== App Container ========== */
.app {
  max-width: 760px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}

/* ========== Header ========== */
.header {
  text-align: center;
  margin-bottom: 32px;
}
.header-left {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 6px;
}
.logo {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #a78bfa);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 800;
  color: #fff;
  flex-shrink: 0;
}
.header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.subtitle {
  color: var(--text-dim);
  font-size: 0.9rem;
}

/* ========== Steps Bar ========== */
.steps-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  margin-bottom: 28px;
}
.step {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: default;
}
.step-num {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--bg-input);
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  transition: all 0.3s;
}
.step-label {
  font-size: 0.85rem;
  color: var(--text-muted);
  transition: color 0.3s;
}
.step.active .step-num {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.step.active .step-label {
  color: var(--text);
  font-weight: 600;
}
.step.done .step-num {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
}
.step.done .step-label { color: var(--green); }
.step-line {
  width: 40px;
  height: 2px;
  background: var(--border);
  margin: 0 12px;
  transition: background 0.3s;
}
.step-line.done { background: var(--green); }

/* ========== Panel ========== */
.panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  box-shadow: var(--shadow);
}
.panel h2 {
  font-size: 1.15rem;
  font-weight: 700;
  margin-bottom: 18px;
}
.hidden { display: none !important; }

/* ========== Form ========== */
.form-group { margin-bottom: 18px; }
.form-group label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-dim);
  margin-bottom: 6px;
}
.optional { font-weight: 400; color: var(--text-muted); }

/* API Key input with eye toggle */
.input-with-btn {
  display: flex;
  gap: 8px;
}
.input-password-wrap {
  flex: 1;
  position: relative;
}
.input-password-wrap input {
  width: 100%;
  padding-right: 40px;
}
.btn-eye {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 4px;
  display: flex;
  align-items: center;
  transition: color 0.2s;
}
.btn-eye:hover { color: var(--text); }
.btn-test {
  padding: 10px 18px;
  background: var(--bg-hover);
  color: var(--text-dim);
  font-size: 0.85rem;
  font-weight: 600;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s;
}
.btn-test:hover { background: var(--border); color: var(--text); }
.btn-test:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-test.testing {
  color: var(--accent);
  border-color: var(--accent);
}
.test-result {
  margin-top: 6px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  line-height: 1.5;
}
.test-result.success {
  background: var(--green-dim);
  color: var(--green);
  border: 1px solid rgba(34, 197, 94, 0.25);
}
.test-result.fail {
  background: var(--red-dim);
  color: var(--red);
  border: 1px solid rgba(239, 68, 68, 0.25);
}

.form-row {
  display: flex;
  gap: 12px;
}
.flex-1 { flex: 1; }
.flex-2 { flex: 2; }

input[type="text"],
input[type="password"],
select,
textarea {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.9rem;
  font-family: var(--font);
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
textarea { resize: vertical; line-height: 1.7; }
select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg width='12' height='8' viewBox='0 0 12 8' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='%239395a5' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}

/* ========== Radio Cards ========== */
.radio-group {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.radio-group.style-group {
  grid-template-columns: repeat(3, 1fr);
}
.radio-card {
  flex: 1;
  padding: 14px 16px;
  background: var(--bg-input);
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-align: center;
  transition: all 0.2s;
}
.radio-card input { display: none; }
.radio-card:hover { border-color: var(--text-muted); }
.radio-card.selected {
  border-color: var(--accent);
  background: rgba(99, 102, 241, 0.08);
}
.radio-label {
  display: block;
  font-weight: 700;
  font-size: 0.95rem;
  margin-bottom: 2px;
}
.radio-desc {
  font-size: 0.78rem;
  color: var(--text-muted);
}

/* ========== Device Cards ========== */
.device-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-bottom: 24px;
}
.device-card {
  padding: 18px 14px;
  background: var(--bg-input);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  text-align: center;
  transition: all 0.2s;
  position: relative;
}
.device-card:hover { border-color: var(--text-muted); }
.device-card.selected {
  border-color: var(--accent);
  background: rgba(99, 102, 241, 0.08);
}
.device-icon {
  width: 96px;
  height: 72px;
  margin: 0 auto 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
}
.device-card.selected .device-icon { color: var(--accent); }
.device-icon svg { width: 100%; height: 100%; }
.device-icon img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  filter: brightness(0.95);
  transition: filter 0.2s, transform 0.2s;
}
.device-card:hover .device-icon img { filter: brightness(1.1); }
.device-card.selected .device-icon img {
  filter: brightness(1.15) drop-shadow(0 0 12px rgba(99, 102, 241, 0.4));
  transform: scale(1.05);
}
.device-info h3 {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 2px;
}
.device-info p {
  font-size: 0.78rem;
  color: var(--text-muted);
}
.tag {
  display: inline-block;
  font-size: 0.68rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  margin-top: 4px;
}

/* ========== Buttons ========== */
.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 12px 24px;
  background: var(--accent);
  color: #fff;
  font-size: 0.95rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:active { transform: scale(0.98); }
.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn-secondary {
  padding: 10px 20px;
  background: var(--bg-input);
  color: var(--text-dim);
  font-size: 0.9rem;
  font-weight: 600;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
}
.btn-secondary:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.btn-row {
  display: flex;
  gap: 12px;
  margin-top: 20px;
}
.btn-row .btn-primary { flex: 1; }

/* ========== Template Tags ========== */
.template-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}
.template-tag {
  padding: 6px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: 0.8rem;
  color: var(--text-dim);
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}
.template-tag:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(99, 102, 241, 0.08);
}
.hint {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 14px;
}

/* ========== Reference image upload ========== */
.image-upload-block {
  margin-top: 16px;
  margin-bottom: 4px;
}
.image-upload-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.image-upload-label {
  font-size: 0.85rem;
  color: var(--text-dim);
  font-weight: 600;
}
.image-count {
  font-size: 0.78rem;
  color: var(--text-muted);
}
.image-upload-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.image-upload-btn {
  width: 88px;
  height: 88px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background: var(--bg-input);
  border: 1.5px dashed var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.72rem;
  transition: all 0.2s;
  flex-shrink: 0;
}
.image-upload-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(99, 102, 241, 0.06);
}
.image-upload-btn.disabled {
  opacity: 0.4;
  pointer-events: none;
}
.image-thumb {
  width: 88px;
  height: 88px;
  position: relative;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--bg-input);
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.image-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.image-thumb-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  line-height: 1;
  padding: 0;
  transition: background 0.15s;
}
.image-thumb-remove:hover {
  background: var(--red);
}

/* ========== Progress ========== */
.progress-section {
  margin-bottom: 24px;
}
.progress-steps {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 16px;
}
.p-step {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.88rem;
  color: var(--text-muted);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  transition: all 0.3s;
}
.p-step .p-icon {
  font-size: 0.6rem;
  width: 20px;
  text-align: center;
  flex-shrink: 0;
}
.p-step.running {
  color: var(--accent);
  background: rgba(99, 102, 241, 0.06);
  font-weight: 600;
}
.p-step.running .p-icon {
  animation: pulse 1s infinite;
}
.p-step.done {
  color: var(--green);
}
.p-step.done .p-icon::before {
  content: '';
}
.p-step.error {
  color: var(--red);
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

.progress-bar-wrap {
  height: 4px;
  background: var(--bg-input);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 8px;
}
.progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), #a78bfa);
  border-radius: 2px;
  transition: width 0.5s ease;
}
.progress-msg {
  font-size: 0.82rem;
  color: var(--text-muted);
}

/* ========== Preview ========== */
.preview-section {
  margin-bottom: 24px;
}
.preview-section h3 {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 12px;
}
.page-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
}
.page-tab {
  padding: 6px 16px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: 0.82rem;
  color: var(--text-dim);
  cursor: pointer;
  transition: all 0.2s;
}
.page-tab:hover { border-color: var(--text-muted); }
.page-tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.device-preview {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.key-grid {
  display: grid;
  gap: 8px;
  justify-content: center;
}
.key-grid.cols-5 { grid-template-columns: repeat(5, 88px); }
.key-grid.cols-3 { grid-template-columns: repeat(3, 88px); }

.key-cell {
  width: 88px;
  height: 78px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 6px;
  position: relative;
  overflow: hidden;
}
.key-cell.merged {
  grid-column: span 2;
  width: auto;
}
.key-cell.empty {
  background: transparent;
  border: 1px dashed var(--border);
  opacity: 0.3;
}
.key-cell.encoder {
  border-radius: 50%;
  width: 68px;
  height: 68px;
  border: 2px solid var(--accent);
  background: var(--bg-card);
  margin: 0 auto;
}
.key-cell .key-icon {
  width: 36px;
  height: 36px;
  border-radius: 6px;
  overflow: hidden;
}
.key-cell .key-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.key-cell .key-name {
  font-size: 0.65rem;
  color: var(--text-dim);
  text-align: center;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}
.key-cell .key-hotkey {
  font-size: 0.58rem;
  color: var(--text-muted);
  font-family: var(--mono);
}

.encoder-row {
  display: flex;
  gap: 16px;
  justify-content: center;
  margin-top: 4px;
}
.encoder-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-align: center;
  margin-top: 4px;
}

/* ========== Download ========== */
.download-section {
  text-align: center;
  padding: 20px 0;
}
.btn-download {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 14px 36px;
  background: var(--green);
  color: #fff;
  font-size: 1rem;
  font-weight: 700;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 4px 16px rgba(34, 197, 94, 0.3);
}
.btn-download:hover {
  background: #16a34a;
  transform: translateY(-1px);
  box-shadow: 0 6px 20px rgba(34, 197, 94, 0.4);
}
.btn-download svg { flex-shrink: 0; }
.download-hint {
  margin-top: 10px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* ========== Error ========== */
.error-section {
  background: var(--red-dim);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: var(--radius-sm);
  padding: 16px;
  text-align: center;
}
.error-msg {
  color: var(--red);
  font-size: 0.88rem;
  margin-bottom: 12px;
  word-break: break-all;
}

/* ========== Responsive ========== */
@media (max-width: 640px) {
  .app { padding: 16px 12px 40px; }
  .panel { padding: 20px 16px; }
  .device-cards { grid-template-columns: 1fr; }
  .form-row { flex-direction: column; }
  .radio-group { grid-template-columns: 1fr; }
  .steps-bar { gap: 0; }
  .step-label { display: none; }
  .step-line { width: 28px; margin: 0 8px; }
  .key-grid.cols-5 { grid-template-columns: repeat(5, 64px); gap: 4px; }
  .key-cell { width: 64px; height: 60px; }
  .key-cell.encoder { width: 52px; height: 52px; }
}
