:root {
  --bg: #0f1117;
  --surface: #1a1d28;
  --surface2: #242838;
  --surface3: #2e3348;
  --primary: #6366f1;
  --primary-hover: #5558e6;
  --success: #22c55e;
  --warning: #f59e0b;
  --error: #ef4444;
  --text: #e4e4e7;
  --text2: #9ca3af;
  --text3: #6b7280;
  --border: #2d3148;
  --radius: 10px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  font-size: 14px;
  line-height: 1.5;
}

/* Header */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

header h1 {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.mode-selector {
  display: flex;
  gap: 2px;
  background: var(--bg);
  border-radius: 8px;
  padding: 3px;
}

.mode-selector button {
  padding: 6px 14px;
  border: none;
  background: transparent;
  color: var(--text3);
  cursor: pointer;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s;
}

.mode-selector button.active {
  background: var(--primary);
  color: #fff;
}

.mode-selector button:hover:not(.active) {
  color: var(--text);
}

.browser-warn {
  font-size: 12px;
  color: var(--warning);
}

.api-status {
  font-size: 12px;
  color: var(--text3);
}

/* Main */
main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* Buttons */
button {
  padding: 8px 14px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  cursor: pointer;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s;
  white-space: nowrap;
}

button:hover { background: var(--surface3); }
button:active { transform: scale(0.98); }
button.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
button.primary:hover { background: var(--primary-hover); }
button.small { padding: 6px 10px; font-size: 12px; }
button.lang-btn {
  min-width: 44px;
  font-weight: 600;
  border-color: var(--primary);
  color: var(--primary);
}
button.lang-btn:hover { background: var(--primary); color: #fff; }

/* Connection Panel */
.connection-panel {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

/* Device Grid */
.devices-panel h2 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text2);
}

.device-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
}

.device-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  transition: border-color 0.2s;
}

.device-card.disconnected { opacity: 0.45; }
.device-card.ota-done { border-color: var(--success); }
.device-card.ota-failed { border-color: var(--error); }

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

.device-name {
  font-weight: 600;
  font-size: 14px;
}

.device-badge {
  font-size: 10px;
  padding: 2px 7px;
  border-radius: 4px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.badge-usb { background: #1e3a5f; color: #60a5fa; }
.badge-ble { background: #3b1f4a; color: #c084fc; }

.device-info {
  font-size: 12px;
  color: var(--text2);
  line-height: 1.8;
}

.device-info span {
  color: var(--text3);
}

.device-actions {
  display: flex;
  gap: 4px;
  margin-top: 8px;
  margin-bottom: 6px;
}

.dev-btn {
  flex: 1;
  padding: 4px 6px;
  font-size: 11px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text2);
  border-radius: 4px;
  cursor: pointer;
  text-align: center;
  transition: all 0.15s;
}

.dev-btn:hover {
  background: var(--surface3);
  color: var(--text);
}

.dev-btn-ota {
  background: rgba(99, 102, 241, 0.15);
  border-color: rgba(99, 102, 241, 0.3);
  color: var(--primary);
}

.dev-btn-ota:hover {
  background: rgba(99, 102, 241, 0.25);
}

/* Progress */
.progress-wrap { margin-top: 10px; }

.progress-bar {
  height: 5px;
  background: var(--bg);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.4s ease;
  background: var(--primary);
}

.progress-fill.downloading { background: #3b82f6; }
.progress-fill.writing { background: #8b5cf6; }
.progress-fill.done { background: var(--success); }
.progress-fill.failed { background: var(--error); }

.ota-status {
  font-size: 11px;
  color: var(--text3);
  margin-top: 4px;
}

.ota-status.done { color: var(--success); }
.ota-status.failed { color: var(--error); }

/* Actions */
.actions-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.action-group {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

.action-group h3 {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 10px;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.btn-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.action-group .form-row {
  flex-wrap: wrap;
}

.action-group .form-row input {
  flex: 1;
  min-width: 120px;
}

.form-row {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
  align-items: center;
}

input, select {
  padding: 7px 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s;
}

input:focus, select:focus { border-color: var(--primary); }
input.wide { flex: 1; min-width: 0; }
select { min-width: 160px; cursor: pointer; }

/* Log */
.log-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

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

.log-header h3 {
  font-size: 13px;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

#log-output {
  max-height: 220px;
  overflow-y: auto;
  font-family: "SF Mono", Monaco, "Cascadia Code", "Fira Code", monospace;
  font-size: 12px;
  line-height: 1.7;
  background: var(--bg);
  border-radius: 6px;
  padding: 10px;
}

.log-entry { padding: 1px 0; }
.log-warn { color: var(--warning); }
.log-error { color: var(--error); }
.log-success { color: var(--success); }
.log-info { color: var(--text2); }

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text3);
}

.empty-state .hint {
  font-size: 12px;
  margin-top: 4px;
}

/* Loading Overlay */
.loading-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(2px);
}

.loading-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px 40px;
  text-align: center;
  min-width: 240px;
}

.loading-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  margin: 0 auto 16px;
  animation: spin 0.8s linear infinite;
}

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

.loading-text {
  font-size: 15px;
  font-weight: 500;
  margin-bottom: 8px;
}

.loading-countdown {
  font-size: 12px;
  color: var(--text3);
}

/* Responsive */
@media (max-width: 640px) {
  header { padding: 12px 16px; }
  main { padding: 16px; }
  .actions-panel { grid-template-columns: 1fr; }
  .device-grid { grid-template-columns: 1fr; }
}

/* Guide Modal */
.guide-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 32px;
  max-width: 460px;
  width: 90%;
}

.guide-box h2 {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 16px;
  text-align: center;
}

.guide-steps {
  list-style: none;
  counter-reset: step;
  padding: 0;
  margin: 0 0 16px;
}

.guide-steps li {
  counter-increment: step;
  padding: 8px 0 8px 36px;
  position: relative;
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
  border-bottom: 1px solid var(--border);
}

.guide-steps li:last-child { border-bottom: none; }

.guide-steps li::before {
  content: counter(step);
  position: absolute;
  left: 0;
  top: 8px;
  width: 24px;
  height: 24px;
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  font-size: 12px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}

.guide-tip {
  font-size: 12px;
  color: var(--text3);
  margin-bottom: 16px;
  text-align: center;
}

.guide-box button {
  display: block;
  margin: 0 auto;
  padding: 8px 32px;
}
