:root {
  --blue-50: #eff6ff;
  --blue-100: #dbeafe;
  --blue-200: #bfdbfe;
  --blue-300: #93c5fd;
  --blue-400: #60a5fa;
  --blue-500: #3b82f6;
  --blue-600: #2563eb;
  --blue-700: #1d4ed8;
  --white: #ffffff;
  --gray-50: #f8fafc;
  --gray-100: #f1f5f9;
  --gray-200: #e2e8f0;
  --gray-300: #cbd5e1;
  --gray-400: #94a3b8;
  --gray-500: #64748b;
  --gray-600: #475569;
  --gray-700: #334155;
  --gray-800: #1e293b;
  --green-500: #22c55e;
  --green-600: #16a34a;
  --red-500: #ef4444;
  --orange-500: #f97316;
  --radius: 12px;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 16px rgba(59, 130, 246, 0.1);
  --shadow-lg: 0 8px 32px rgba(59, 130, 246, 0.15);
}

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

body {
  font-family: 'IBM Plex Sans Thai', sans-serif;
  background: var(--gray-50);
  color: var(--gray-800);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Header / Nav — แถวเดียว */
.header {
  width: 100%;
  background: var(--white);
  border-bottom: 1px solid var(--blue-100);
  padding: 10px 20px 10px 24px;
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.header-brand {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 12px;
  min-width: 0;
  flex: 1 1 auto;
}
.header-brand > svg {
  flex-shrink: 0;
}
.header-titles {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: baseline;
  gap: 0.5rem;
  min-width: 0;
}
.header-title {
  font-weight: 700;
  font-size: 18px;
  color: var(--blue-700);
  letter-spacing: -0.3px;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.header-sub {
  font-size: 12px;
  color: var(--gray-400);
  font-weight: 500;
  letter-spacing: 0.02em;
  flex-shrink: 0;
  white-space: nowrap;
}
.header-sub::before {
  content: '·';
  margin-right: 0.5rem;
  color: var(--gray-300);
  font-weight: 700;
}
.header-nav {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  margin-left: auto;
  flex-shrink: 0;
}
.header-nav .conn-status {
  margin-bottom: 0;
  padding: 8px 12px;
  font-size: 12px;
  flex-wrap: nowrap;
  min-width: 0;
  max-width: min(100%, 320px);
}
.header-nav .port-name {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.header-connect-btn {
  width: auto;
  min-width: 0;
  padding: 9px 14px;
  flex-shrink: 0;
}
.header .btn-danger.header-connect-btn {
  border-color: var(--red-500);
}

/* Main — 2 คอลัมน์: ซ้าย Firmware | ขวา Serial */
.main {
  align-self: stretch;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 32px 24px 48px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.app-layout {
  display: grid;
  grid-template-columns: minmax(320px, 1.15fr) minmax(300px, 1fr);
  gap: 24px;
  align-items: start;
  width: 100%;
}

.col-serial {
  position: sticky;
  top: 16px;
  align-self: start;
  min-width: 0;
}

.col-firmware {
  display: flex;
  flex-direction: column;
  gap: 24px;
  min-width: 0;
}

.card-monitor {
  display: flex;
  flex-direction: column;
  min-height: min(72vh, 720px);
  max-height: calc(100vh - 120px);
}

.card-monitor .monitor-output {
  flex: 1 1 auto;
  min-height: 200px;
  max-height: none;
  resize: vertical;
  overflow-y: auto;
}

.card {
  background: var(--white);
  border-radius: var(--radius);
  border: 1px solid var(--blue-100);
  padding: 28px;
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.2s;
}
.card:hover { box-shadow: var(--shadow-md); }

.step-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--blue-600);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 14px;
}
.step-num {
  width: 22px; height: 22px;
  background: var(--blue-600);
  color: var(--white);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px;
  font-weight: 700;
}
.step-num.done {
  background: var(--green-500);
}

.card-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--gray-800);
  margin-bottom: 6px;
}
.card-desc {
  font-size: 13px;
  color: var(--gray-500);
  line-height: 1.5;
  margin-bottom: 18px;
}

/* Select */
.select-wrap {
  position: relative;
}
.select-wrap select {
  width: 100%;
  padding: 12px 40px 12px 14px;
  border: 1.5px solid var(--blue-200);
  border-radius: 8px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  color: var(--gray-700);
  background: var(--white);
  appearance: none;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
  outline: none;
}
.select-wrap select:focus {
  border-color: var(--blue-500);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}
.select-arrow {
  position: absolute;
  right: 14px;
  top: 50%;
  transform: translateY(-50%);
  pointer-events: none;
  color: var(--blue-400);
}

/* Version info */
.version-info {
  margin-top: 14px;
  padding: 10px 14px;
  background: var(--blue-50);
  border-radius: 8px;
  font-size: 12px;
  color: var(--gray-600);
  line-height: 1.6;
  display: none;
}
.version-info.show { display: block; }
.version-info .mono {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  color: var(--blue-600);
}

/* Connect Button */
.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 13px 20px;
  border: none;
  border-radius: 8px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  outline: none;
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-outline {
  background: var(--white);
  border: 1.5px solid var(--blue-300);
  color: var(--blue-600);
}
.btn-outline:hover:not(:disabled) {
  background: var(--blue-50);
  border-color: var(--blue-500);
}

.btn-primary {
  background: var(--blue-600);
  color: var(--white);
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.3);
}
.btn-primary:hover:not(:disabled) {
  background: var(--blue-700);
  box-shadow: 0 4px 16px rgba(37, 99, 235, 0.35);
  transform: translateY(-1px);
}
.btn-primary:active:not(:disabled) {
  transform: translateY(0);
}

.btn-danger {
  background: var(--white);
  border: 1.5px solid var(--red-500);
  color: var(--red-500);
}
.btn-danger:hover:not(:disabled) {
  background: #fef2f2;
}

/* Connection status */
.conn-status {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 14px;
}
.conn-status.disconnected {
  background: var(--gray-100);
  color: var(--gray-500);
}
.conn-status.connected {
  background: #f0fdf4;
  color: var(--green-600);
}
.status-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.disconnected .status-dot { background: var(--gray-300); }
.connected .status-dot {
  background: var(--green-500);
  box-shadow: 0 0 6px rgba(34, 197, 94, 0.5);
  animation: pulse 2s infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 6px rgba(34, 197, 94, 0.5); }
  50% { box-shadow: 0 0 12px rgba(34, 197, 94, 0.8); }
}

.port-name {
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  background: rgba(0, 0, 0, 0.06);
  padding: 2px 8px;
  border-radius: 4px;
  margin-left: auto;
}

/* Progress */
.progress-wrap {
  margin-top: 16px;
  display: none;
}
.progress-wrap.show { display: block; }

.progress-label {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 500;
  color: var(--gray-600);
  margin-bottom: 8px;
}
.progress-bar {
  width: 100%;
  height: 8px;
  background: var(--blue-100);
  border-radius: 99px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--blue-500), var(--blue-400));
  border-radius: 99px;
  transition: width 0.3s ease;
}
.progress-fill.done {
  background: linear-gradient(90deg, var(--green-500), var(--green-600));
}
.progress-fill.error {
  background: var(--red-500);
}

/* Log */
.log-box {
  margin-top: 16px;
  background: var(--gray-800);
  border-radius: 8px;
  padding: 14px;
  max-height: 180px;
  overflow-y: auto;
  display: none;
}
.log-box.show { display: block; }
.log-line {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  line-height: 1.7;
  color: var(--gray-300);
}
.log-line.info { color: var(--blue-300); }
.log-line.success { color: var(--green-500); }
.log-line.error { color: var(--red-500); }
.log-line.warn { color: var(--orange-500); }

/* Result banner */
.result-banner {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  margin-top: 16px;
}
.result-banner.show { display: flex; }
.result-banner.success {
  background: #f0fdf4;
  color: var(--green-600);
  border: 1px solid #bbf7d0;
}
.result-banner.fail {
  background: #fef2f2;
  color: var(--red-500);
  border: 1px solid #fecaca;
}

/* Browser warning */
.browser-warn {
  background: #fffbeb;
  border: 1px solid #fde68a;
  color: #92400e;
  padding: 14px 18px;
  border-radius: var(--radius);
  font-size: 13px;
  line-height: 1.6;
  display: none;
  align-items: flex-start;
  gap: 10px;
}
.browser-warn.show { display: flex; }

/* Serial Monitor */
.monitor-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.75rem;
}

.monitor-baud {
  padding: 0.45rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--gray-200);
  background: var(--white);
  color: var(--gray-800);
  font-size: 0.85rem;
  min-width: 7rem;
}

.monitor-baud:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.monitor-btn-toggle {
  min-width: 7rem;
}

.monitor-btn-clear {
  margin-left: auto;
}

.monitor-output {
  width: 100%;
  box-sizing: border-box;
  padding: 0.75rem 1rem;
  border-radius: 10px;
  border: 1px solid var(--gray-200);
  background: var(--gray-800);
  color: var(--gray-300);
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  line-height: 1.45;
  resize: vertical;
  min-height: 12rem;
}

.monitor-send {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.monitor-send-input {
  flex: 1;
  min-width: 0;
  padding: 0.55rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--gray-200);
  background: var(--white);
  color: var(--gray-800);
  font-size: 0.9rem;
}

.monitor-send-input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.monitor-send-btn {
  flex-shrink: 0;
}

/* Footer */
.footer {
  text-align: center;
  font-size: 12px;
  color: var(--gray-400);
  padding: 20px;
}

/* Responsive */
@media (max-width: 900px) {
  .app-layout {
    grid-template-columns: 1fr;
  }
  .col-firmware {
    order: 1;
  }
  .col-serial {
    order: 2;
    position: static;
    max-width: 100%;
  }
  .card-monitor {
    min-height: auto;
    max-height: none;
  }
  .card-monitor .monitor-output {
    min-height: 220px;
  }
}

@media (max-width: 540px) {
  .main { padding: 24px 16px 40px; }
  .card { padding: 22px; }
}
