---
import SiteShell from "@/layouts/SiteShell.astro";
import firmwareDb from "@/flash-tool/firmware.json";
import type { Lang } from "@/site/locales";
import { localeHref } from "@/site/locales";
import { getChromeStrings } from "@/site/chrome-i18n";
import { getStrings } from "@/flash-tool/i18n";

import Zap from "@lucide/astro/icons/zap";
import Cable from "@lucide/astro/icons/cable";
import Unplug from "@lucide/astro/icons/unplug";
import SquareTerminal from "@lucide/astro/icons/square-terminal";
import Box from "@lucide/astro/icons/box";
import ArrowUpRight from "@lucide/astro/icons/arrow-up-right";

export function getStaticPaths() {
  return [{ params: { lang: "en" } }, { params: { lang: "zh-cn" } }];
}

const { lang } = Astro.params as { lang: Lang };
const s = getStrings(lang);
const chrome = getChromeStrings(lang);
const tutorialHref = localeHref(lang, "tutorial/");
const boardsHref = localeHref(lang, "tutorial/supported-list");

const quickLinks = [
  {
    icon: "🔧",
    title: s.quickLinkNoHardwareTitle,
    desc: s.quickLinkNoHardwareDesc,
    href: tutorialHref,
    external: false,
  },
  {
    icon: "📋",
    title: s.quickLinkBoardsTitle,
    desc: s.quickLinkBoardsDesc,
    href: boardsHref,
    external: true,
  },
];

const flashBootJson = JSON.stringify({
  lang,
  firmwareDb,
  strings: s,
  boardsHref,
}).replace(/</g, "\\u003c");
---

<SiteShell
  lang={lang}
  title={`${s.pageTitle} — ESP-Claw`}
  description={s.pageSubtitle}
  pagePath="flash"
  chrome={chrome}
>
  <style slot="head">
    @import url("@/styles/homepage.css");

    @keyframes pulse {
      0%,
      100% {
        opacity: 1;
        transform: scale(1);
      }
      50% {
        opacity: 0.5;
        transform: scale(0.8);
      }
    }

    .page-header {
      padding: 3rem 3rem 2.5rem;
      border-bottom: 1px solid var(--border);
      background: radial-gradient(
        ellipse at 50% 0%,
        rgba(232, 54, 45, 0.06) 0%,
        transparent 60%
      );
      text-align: center;
    }

    .page-header h1 {
      font-size: clamp(1.6rem, 3vw, 2.2rem);
      font-weight: 700;
      letter-spacing: -0.025em;
      margin-bottom: 0.6rem;
    }

    .page-header p {
      font-size: 1rem;
      color: var(--text-secondary);
      max-width: 620px;
      margin: 0 auto;
    }

    main {
      flex: 1;
      padding: 2rem 2rem 5rem;
      max-width: 980px;
      margin: 0 auto;
      width: 100%;
    }

    .quick-links {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 0.85rem;
      margin-bottom: 1.5rem;
    }

    .quick-link-card {
      display: flex;
      align-items: center;
      gap: 0.85rem;
      padding: 0.85rem 1.1rem;
      background: var(--bg-card);
      border: 1px solid var(--border);
      border-radius: var(--radius-lg);
      text-decoration: none;
      color: inherit;
      transition:
        border-color 0.15s,
        background 0.15s;
    }

    .quick-link-card:hover {
      border-color: rgba(255, 255, 255, 0.16);
      background: var(--bg-card-hover);
    }

    .quick-link-icon {
      font-size: 1.4rem;
      flex-shrink: 0;
      line-height: 1;
    }

    .quick-link-text {
      min-width: 0;
    }

    .quick-link-title {
      font-size: 0.8rem;
      color: var(--text-primary);
      margin-bottom: 0.15rem;
    }

    .quick-link-desc {
      font-size: 0.9rem;
      font-weight: 600;
      color: var(--text-primary);
    }

    .quick-link-arrow {
      margin-left: auto;
      color: var(--text-muted);
      flex-shrink: 0;
    }

    .unsupported-banner,
    .inline-error {
      margin-bottom: 1rem;
      padding: 0.95rem 1.1rem;
      border-radius: var(--radius-md);
      font-size: 0.875rem;
      display: none;
    }

    .unsupported-banner.visible,
    .inline-error.visible {
      display: block;
    }

    .unsupported-banner,
    .inline-error {
      background: rgba(248, 113, 113, 0.08);
      border: 1px solid rgba(248, 113, 113, 0.2);
      color: #f87171;
    }

    .connect-bar {
      background: var(--bg-card);
      border: 1px solid var(--border);
      border-radius: var(--radius-lg);
      padding: 0.9rem 1.5rem;
      margin-bottom: 1.25rem;
      transition:
        border-color 0.3s,
        background 0.3s;
    }

    .connect-bar.is-connected {
      border-color: rgba(104, 211, 145, 0.22);
      background: linear-gradient(
        135deg,
        var(--bg-card) 0%,
        rgba(104, 211, 145, 0.025) 100%
      );
    }

    .connect-bar-row,
    .device-info-row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 1rem;
      flex-wrap: wrap;
    }

    .device-info-row {
      justify-content: flex-start;
      gap: 0.55rem;
    }

    .status-badge {
      display: inline-flex;
      align-items: center;
      gap: 0.4rem;
      font-size: 0.8rem;
      padding: 0.3rem 0.75rem;
      border-radius: 999px;
      font-weight: 500;
    }

    .status-dot {
      width: 7px;
      height: 7px;
      border-radius: 50%;
    }

    .status-badge.disconnected {
      color: var(--text-muted);
      background: rgba(255, 255, 255, 0.04);
      border: 1px solid var(--border);
    }

    .status-badge.connected {
      color: var(--green);
      background: var(--green-dim);
      border: 1px solid rgba(104, 211, 145, 0.2);
    }

    .status-badge.connected .status-dot {
      background: var(--green);
      animation: pulse 2s infinite;
    }

    .status-badge.disconnected .status-dot {
      background: var(--text-muted);
    }

    .device-meta-sep {
      color: var(--border);
      user-select: none;
    }

    .device-meta-item {
      font-size: 0.82rem;
      color: var(--text-secondary);
      font-family: "JetBrains Mono", monospace;
      font-weight: 600;
    }

    .tool-shell {
      padding: 0;
      display: grid;
      gap: 1.25rem;
    }

    .section-card {
      background: var(--bg-surface);
      border: 1px solid var(--border);
      border-radius: var(--radius-lg);
      padding: 1rem 1.1rem;
    }

    .section-card.selection-empty {
      padding: 0;
      background: transparent;
      border-style: dashed;
    }

    .section-card.selection-empty .selection-fields {
      display: none;
    }

    .section-label {
      font-size: 0.72rem;
      font-weight: 700;
      text-transform: uppercase;
      letter-spacing: 0.1em;
      color: var(--accent-soft);
      margin-bottom: 0.9rem;
      display: flex;
      align-items: center;
      gap: 0.5rem;
    }

    .section-label::after {
      content: "";
      flex: 1;
      height: 1px;
      background: var(--border-accent);
    }

    .field-label {
      font-size: 0.8rem;
      color: var(--text-secondary);
      font-weight: 500;
      display: block;
      margin-bottom: 0.4rem;
    }

    select,
    input {
      width: 100%;
      background: var(--bg-input);
      border: 1px solid var(--border);
      color: var(--text-primary);
      border-radius: var(--radius-sm);
      padding: 0.65rem 0.85rem;
      font-size: 0.9rem;
      font-family: inherit;
      transition: border-color 0.15s;
    }

    select:focus,
    input:focus {
      outline: none;
      border-color: rgba(232, 54, 45, 0.4);
    }

    select option {
      background: #1c2028;
    }

    .section-note {
      margin-top: 0.75rem;
      font-size: 0.8rem;
      line-height: 1.6;
      color: var(--text-secondary);
    }

    .selected-board {
      display: grid;
      gap: 0.6rem;
      margin-top: 0.7rem;
      font-size: 0.88rem;
    }

    .selected-board-top {
      display: flex;
      align-items: flex-start;
      justify-content: space-between;
      gap: 1rem;
      flex-wrap: wrap;
    }

    .selected-board[hidden] {
      display: none;
    }

    .tool-shell > [hidden] {
      display: none !important;
    }

    .selected-board-desc {
      font-size: 0.88rem;
      line-height: 1.6;
    }

    .selected-board-desc:empty {
      display: none;
    }

    .board-meta-list {
      display: grid;
      gap: 0.35rem;
      font-size: 0.76rem;
      color: var(--text-primary);
      min-width: 0;
      line-height: 1.6;
    }

    .board-meta-list strong {
      color: inherit;
      font-weight: 600;
      margin-right: 0.25rem;
    }

    select:disabled {
      opacity: 0.65;
      cursor: not-allowed;
      border-color: var(--border-accent);
    }

    .connect-bar-btns {
      display: flex;
      align-items: center;
      gap: 0.5rem;
      flex-shrink: 0;
    }

    .action-row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 1rem;
      flex-wrap: wrap;
    }

    .action-row-main,
    .action-row-end {
      display: flex;
      align-items: center;
      gap: 0.75rem;
      flex-wrap: wrap;
    }

    .console-toggle-btn.active {
      color: var(--text-primary);
      background: rgba(255, 255, 255, 0.08);
      border-color: rgba(255, 255, 255, 0.16);
    }

    .flash-hint {
      font-size: 0.82rem;
      color: var(--text-secondary);
    }

    .btn-link {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      min-height: 42px;
      padding: 0.65rem 1rem;
      border-radius: var(--radius-sm);
      border: 1px solid var(--border);
      color: var(--text-primary);
      text-decoration: none;
      font-weight: 600;
      background: var(--bg-surface);
      transition:
        border-color 0.15s,
        background 0.15s;
    }

    .btn-link:hover {
      border-color: rgba(255, 255, 255, 0.16);
      background: var(--bg-card-hover);
    }

    .btn-link.disabled {
      opacity: 0.4;
      pointer-events: none;
    }

    .selected-board-download {
      flex-shrink: 0;
      min-height: auto;
      padding: 0;
      border: none;
      border-radius: 0;
      background: transparent;
      color: var(--text-secondary);
      font-size: 0.76rem;
      font-weight: 500;
      line-height: 1.4;
      text-decoration: none;
    }

    .selected-board-download:hover {
      color: var(--text-primary);
      background: transparent;
      border: none;
    }

    .selected-board-download.disabled {
      opacity: 0.4;
      pointer-events: none;
    }

    .no-firmware-card {
      display: none;
      min-height: 330px;
      padding: 2.35rem 1.5rem 2.5rem;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      gap: 1rem;
      background: var(--bg-surface);
      border-radius: var(--radius-lg);
      text-align: center;
    }

    .no-firmware-card.visible {
      display: flex;
    }

    .no-firmware-icon {
      width: 60px;
      height: 60px;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      border-radius: 999px;
      color: var(--accent);
      background: rgba(232, 54, 45, 0.08);
      border: 1px solid rgba(232, 54, 45, 0.18);
      box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.03);
    }

    .no-firmware-icon :global(svg) {
      width: 24px;
      height: 24px;
    }

    .no-firmware-title {
      font-size: clamp(1rem, 1.4vw, 1.2rem);
      font-weight: 700;
      color: var(--text-primary);
      letter-spacing: -0.02em;
    }

    .no-firmware-desc {
      font-size: 1rem;
      line-height: 1.65;
      color: var(--text-secondary);
      max-width: 620px;
      margin: 0;
    }

    .no-firmware-link {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      gap: 0.55rem;
      min-height: 52px;
      margin-top: 0.45rem;
      padding: 0.85rem 1.45rem;
      border-radius: 12px;
      background: var(--accent);
      color: white;
      text-decoration: none;
      font-size: 1rem;
      font-weight: 700;
    }

    .no-firmware-link :global(svg) {
      width: 18px;
      height: 18px;
    }

    .console-tab-shell {
      background: rgba(9, 11, 15, 0.85);
      border: 1px solid rgba(255, 255, 255, 0.07);
      border-radius: var(--radius-lg);
      overflow: hidden;
    }

    .console-tab-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 0.75rem;
      padding: 0.85rem 1rem;
      border-bottom: 1px solid rgba(255, 255, 255, 0.07);
    }

    .console-tab-title {
      font-size: 0.88rem;
      font-weight: 700;
      color: #f3f7ff;
    }

    .console-toolbar {
      display: flex;
      gap: 0.5rem;
      flex-wrap: wrap;
    }

    .console-output {
      padding: 0.8rem 0.95rem;
      background: #07080a;
      border: none;
      font-family: "JetBrains Mono", monospace;
      font-size: 0.78rem;
      color: #c8d0e7;
      white-space: pre-wrap;
      word-break: break-word;
      overflow-y: auto;
      min-height: 240px;
      max-height: min(45vh, 400px);
    }

    .console-empty {
      color: #94a0bb;
    }

    .console-send-area {
      display: flex;
      gap: 0.5rem;
      padding: 0.65rem 1rem;
      border-top: 1px solid rgba(255, 255, 255, 0.07);
      background: rgba(0, 0, 0, 0.2);
    }

    .console-send-area input {
      flex: 1;
      background: rgba(255, 255, 255, 0.05);
      border: 1px solid rgba(255, 255, 255, 0.1);
      color: #c8d0e7;
      border-radius: var(--radius-sm);
      padding: 0.5rem 0.75rem;
      font-size: 0.83rem;
      font-family: "JetBrains Mono", monospace;
      transition: border-color 0.15s;
    }

    .console-send-area input:focus {
      outline: none;
      border-color: rgba(232, 54, 45, 0.35);
    }

    .console-send-area input:disabled {
      opacity: 0.4;
      cursor: not-allowed;
    }

    /* ── Flash Modal ── */
    .flash-modal-bg {
      position: fixed;
      inset: 0;
      background: rgba(0, 0, 0, 0.65);
      backdrop-filter: blur(4px);
      -webkit-backdrop-filter: blur(4px);
      z-index: 40;
      display: none;
      align-items: center;
      justify-content: center;
      padding: 1rem;
    }

    .flash-modal-bg.open {
      display: flex;
    }

    .flash-modal-shell {
      background: rgba(9, 11, 15, 0.98);
      border: 1px solid rgba(255, 255, 255, 0.08);
      border-radius: 16px;
      box-shadow:
        0 24px 60px rgba(0, 0, 0, 0.6),
        0 0 0 1px rgba(232, 54, 45, 0.06);
      width: min(680px, calc(100vw - 2rem));
      max-height: calc(100vh - 2rem);
      overflow-y: auto;
      overflow-x: hidden;
      animation: modal-in 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    }

    @keyframes modal-in {
      from {
        opacity: 0;
        transform: translateY(12px) scale(0.97);
      }
      to {
        opacity: 1;
        transform: translateY(0) scale(1);
      }
    }

    .modal-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 1rem;
      padding: 1rem 1.25rem;
      border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    }

    /* Step navigator */
    .modal-step-nav {
      display: flex;
      align-items: center;
      gap: 0.55rem;
      flex: 1;
    }

    .modal-step-item {
      display: inline-flex;
      align-items: center;
      gap: 0.4rem;
      font-size: 0.78rem;
      font-weight: 600;
      color: var(--text-muted);
      transition: color 0.2s;
    }

    .modal-step-num {
      width: 20px;
      height: 20px;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.06);
      border: 1px solid rgba(255, 255, 255, 0.1);
      display: inline-flex;
      align-items: center;
      justify-content: center;
      font-size: 0.7rem;
      font-weight: 700;
      transition:
        background 0.2s,
        border-color 0.2s,
        color 0.2s;
      flex-shrink: 0;
    }

    .modal-step-item.active .modal-step-num {
      background: var(--accent);
      border-color: var(--accent);
      color: #fff;
    }

    .modal-step-item.active {
      color: #f3f7ff;
    }

    .modal-step-item.done .modal-step-num {
      background: var(--green-dim);
      border-color: rgba(104, 211, 145, 0.35);
      color: var(--green);
    }

    .modal-step-item.done {
      color: var(--green);
    }

    .modal-step-sep {
      color: rgba(255, 255, 255, 0.15);
      font-size: 0.75rem;
      flex-shrink: 0;
    }

    .modal-close-btn {
      display: none;
      align-items: center;
      justify-content: center;
      width: 28px;
      height: 28px;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.05);
      border: 1px solid rgba(255, 255, 255, 0.1);
      color: var(--text-muted);
      font-size: 0.85rem;
      cursor: pointer;
      transition:
        background 0.15s,
        color 0.15s;
      flex-shrink: 0;
    }

    .modal-close-btn:hover {
      background: rgba(255, 255, 255, 0.1);
      color: var(--text-primary);
    }

    .modal-close-btn.visible {
      display: inline-flex;
    }

    /* Modal body sections */
    .modal-body-section {
      padding: 1.25rem;
    }

    .modal-flash-result {
      display: none;
      padding: 0.8rem 1rem;
      border-radius: var(--radius-md);
      font-size: 0.875rem;
      line-height: 1.5;
      margin-bottom: 1rem;
    }

    .modal-flash-result.visible {
      display: block;
    }

    .modal-flash-result.success {
      background: var(--green-dim);
      border: 1px solid rgba(104, 211, 145, 0.2);
      color: var(--green);
    }

    .modal-flash-result.error {
      background: rgba(248, 113, 113, 0.08);
      border: 1px solid rgba(248, 113, 113, 0.2);
      color: #f87171;
    }

    .modal-progress-wrap {
      display: none;
    }

    .modal-progress-wrap.visible {
      display: block;
    }

    .progress-label {
      display: flex;
      justify-content: space-between;
      gap: 1rem;
      font-size: 0.8rem;
      color: var(--text-secondary);
      margin-bottom: 0.4rem;
    }

    .progress-bar-bg {
      height: 6px;
      background: rgba(255, 255, 255, 0.07);
      border-radius: 999px;
      overflow: hidden;
    }

    .progress-bar {
      height: 100%;
      width: 0%;
      background: var(--accent);
      border-radius: 999px;
      transition: width 0.25s ease;
    }

    .progress-log {
      margin-top: 0.9rem;
      padding: 0.8rem 0.95rem;
      background: #07080a;
      border: 1px solid var(--border);
      border-radius: var(--radius-md);
      font-family: "JetBrains Mono", monospace;
      font-size: 0.78rem;
      color: #c8d0e7;
      white-space: pre-wrap;
      word-break: break-word;
      overflow-y: auto;
      max-height: 150px;
    }

    .reconnect-prompt {
      margin-top: 1rem;
      padding: 1rem;
      background: rgba(255, 255, 255, 0.03);
      border: 1px solid rgba(255, 255, 255, 0.08);
      border-radius: var(--radius-md);
    }

    .reconnect-title {
      font-size: 0.92rem;
      font-weight: 700;
      color: var(--text-primary);
      margin-bottom: 0.45rem;
    }

    .reconnect-desc {
      font-size: 0.84rem;
      line-height: 1.6;
      color: var(--text-secondary);
      margin: 0 0 0.85rem;
    }

    .reconnect-status {
      margin-top: 0.75rem;
      font-size: 0.82rem;
      color: var(--text-secondary);
      min-height: 1.2em;
    }

    /* Modal wifi section */
    .wifi-grid {
      display: grid;
      grid-template-columns: 1fr 1fr auto;
      gap: 0.75rem;
      align-items: end;
    }

    .wifi-actions {
      display: flex;
      align-items: center;
      gap: 0.75rem;
      flex-wrap: wrap;
    }

    .wifi-status {
      font-size: 0.82rem;
      color: var(--text-secondary);
      margin-top: 0.75rem;
    }

    .wifi-prompt {
      margin: 0 0 0.5rem;
      color: var(--text-secondary);
      line-height: 1.6;
      font-size: 0.88rem;
    }

    .wifi-same-network-hint {
      margin: 0 0 1rem;
      padding: 0.5rem 0.75rem;
      background: rgba(251, 191, 36, 0.08);
      border: 1px solid rgba(251, 191, 36, 0.3);
      border-radius: var(--radius-md);
      color: var(--text-secondary);
      line-height: 1.6;
      font-size: 0.85rem;
    }

    /* Modal ready box (step 3) */
    .modal-ready-box {
      padding: 1.5rem;
      background: rgba(104, 211, 145, 0.06);
      border: 1px solid rgba(104, 211, 145, 0.2);
      border-radius: var(--radius-lg);
      display: grid;
      gap: 0.5rem;
    }

    .ready-title {
      font-size: 1rem;
      font-weight: 700;
      color: var(--green);
      display: flex;
      align-items: center;
      gap: 0.5rem;
    }

    .ready-title::before {
      content: "✓";
      display: inline-flex;
      align-items: center;
      justify-content: center;
      width: 22px;
      height: 22px;
      border-radius: 50%;
      background: rgba(104, 211, 145, 0.15);
      font-size: 0.75rem;
      flex-shrink: 0;
    }

    .ready-desc {
      font-size: 0.86rem;
      color: var(--text-secondary);
      line-height: 1.55;
    }

    .ready-link {
      display: inline-flex;
      width: fit-content;
      margin-top: 0.35rem;
    }

    /* Modal collapsible terminal */
    .modal-terminal-wrap {
      border-top: 1px solid rgba(255, 255, 255, 0.06);
    }

    .modal-terminal-summary {
      display: flex;
      align-items: center;
      gap: 0.5rem;
      padding: 0.65rem 1.25rem;
      font-size: 0.77rem;
      font-weight: 600;
      color: var(--text-muted);
      cursor: pointer;
      list-style: none;
      user-select: none;
      transition: color 0.15s;
    }

    .modal-terminal-summary::-webkit-details-marker {
      display: none;
    }

    .modal-terminal-summary::before {
      content: "▶";
      font-size: 0.58rem;
      display: inline-block;
      transition: transform 0.15s;
      flex-shrink: 0;
    }

    details[open] > .modal-terminal-summary::before {
      transform: rotate(90deg);
    }

    .modal-terminal-summary:hover {
      color: var(--text-secondary);
    }

    .modal-terminal-output {
      padding: 0.75rem 1.25rem 1rem;
      background: #05060a;
      font-family: "JetBrains Mono", monospace;
      font-size: 0.77rem;
      color: #c8d0e7;
      white-space: pre-wrap;
      word-break: break-word;
      overflow-y: auto;
      max-height: 200px;
    }

    .modal-terminal-empty {
      color: #5a6480;
    }

    @media (max-width: 820px) {
      .wifi-grid {
        grid-template-columns: 1fr;
      }
    }

    @media (max-width: 700px) {
      .page-header {
        padding: 2rem 1.5rem;
      }

      main {
        padding: 1.25rem 1rem 4rem;
      }

      .connect-bar {
        padding: 0.75rem 1rem;
      }

      .quick-links {
        grid-template-columns: 1fr;
      }

      .no-firmware-card {
        min-height: 280px;
        padding: 2rem 1.1rem 2.2rem;
      }

      .no-firmware-desc {
        font-size: 0.94rem;
      }

      .no-firmware-link {
        width: 100%;
      }

      .flash-modal-shell {
        width: calc(100vw - 1.5rem);
      }

      .modal-step-item .modal-step-label {
        display: none;
      }
    }
  </style>

  <div class="page-header">
    <h1>{s.pageTitle}</h1>
    <p>{s.pageSubtitle}</p>
  </div>

  <main>
    <div class="quick-links">
      {quickLinks.map((card) => (
        <a
          href={card.href}
          class="quick-link-card"
          target={card.external ? "_blank" : undefined}
          rel={card.external ? "noopener noreferrer" : undefined}
        >
          <span class="quick-link-icon">{card.icon}</span>
          <span class="quick-link-text">
            <div class="quick-link-title">{card.title}</div>
            <div class="quick-link-desc">{card.desc}</div>
          </span>
          <span class="quick-link-arrow">{card.external ? "↗" : "→"}</span>
        </a>
      ))}
    </div>

    <div class="unsupported-banner" id="unsupported-banner">
      {s.webSerialUnsupported}
    </div>

    <div class="connect-bar" id="connect-bar">
      <div id="connect-bar-idle" class="connect-bar-row">
        <span class="status-badge disconnected">
          <span class="status-dot"></span>
          <span>{s.notConnected}</span>
        </span>
        <button class="btn-primary" id="connect-btn">
          <Cable class="button-icon" />
          <span id="connect-btn-label">{s.connectBtn}</span>
        </button>
      </div>

      <div id="connect-bar-active" class="connect-bar-row" style="display:none">
        <div class="device-info-row">
          <span class="status-badge connected" id="status-badge">
            <span class="status-dot"></span>
            <span id="status-text">{s.notConnected}</span>
          </span>
          <span class="device-meta-sep">·</span>
          <span class="device-meta-item" id="info-chip" style="display:none;">—</span>
          <span class="device-meta-sep" id="info-revision-sep" style="display:none;">·</span>
          <span class="device-meta-item" id="info-revision" style="display:none;">—</span>
          <span class="device-meta-sep" id="info-flash-sep" style="display:none;">·</span>
          <span class="device-meta-item" id="info-flash" style="display:none;">—</span>
          <span class="device-meta-sep" id="info-psram-sep" style="display:none;">·</span>
          <span class="device-meta-item" id="info-psram" style="display:none;">—</span>
        </div>
        <div class="connect-bar-btns">
          <button class="btn-secondary" id="disconnect-btn">
            <Unplug class="button-icon" />
            {s.disconnectBtn}
          </button>
        </div>
      </div>
    </div>

    <div class="inline-error" id="connect-error"></div>

    <div class="tool-shell">
      <section class="section-card" id="selection-card">
        <div class="selection-fields" id="selection-fields">
          <label class="section-label" for="chip-select">{s.chooseChipLabel}</label>
          <select id="chip-select"></select>

          <label class="section-label" for="brand-select" style="margin-top: 1rem;">
            {s.chooseBrandLabel}
          </label>
          <select id="brand-select"></select>

          <label class="section-label" for="board-select" style="margin-top: 1rem;">
            {s.chooseBoardLabel}
          </label>
          <select id="board-select"></select>

          <label class="section-label" for="console-output-select" style="margin-top: 1rem;">
            {s.chooseConsoleOutputLabel}
          </label>
          <select id="console-output-select"></select>
          <div class="selected-board" id="selected-board-summary" hidden>
            <div class="selected-board-top">
              <div class="board-meta-list" id="selected-board-meta"></div>
              <a class="selected-board-download" id="download-btn" href="#">
                {s.downloadFirmwareLocalLink}
              </a>
            </div>
            <div class="selected-board-desc" id="selected-board-desc"></div>
            <span id="selected-board-name" hidden>{s.noBoardSelected}</span>
          </div>
        </div>

        <div class="no-firmware-card" id="no-firmware-card">
          <div class="no-firmware-icon" aria-hidden="true">
            <Box />
          </div>
          <div class="no-firmware-title">{s.noFirmwareTitle}</div>
          <div class="no-firmware-desc">{s.noFirmwareDesc}</div>
          <a
            href={boardsHref}
            class="no-firmware-link"
            target="_blank"
            rel="noopener noreferrer"
          >
            {s.viewSupportedBoardsBtn}
            <ArrowUpRight />
          </a>
        </div>
      </section>

      <div id="console-panel" aria-live="polite" hidden>
        <div class="console-tab-shell">
          <div class="console-tab-header">
            <div class="console-tab-title">{s.consoleTitle}</div>
            <div class="console-toolbar">
              <button type="button" class="btn-secondary btn-sm" id="console-clear-btn">
                {s.consoleClearBtn}
              </button>
              <button type="button" class="btn-secondary btn-sm" id="console-reset-btn">
                {s.consoleResetBtn}
              </button>
            </div>
          </div>
          <div class="console-output" id="console-output">
            <span class="console-empty" id="console-empty">{s.consoleWaiting}</span>
          </div>
          <div class="console-send-area">
            <input
              type="text"
              id="console-send-input"
              placeholder={s.consoleSendPlaceholder}
              autocomplete="off"
              disabled
            />
            <button type="button" class="btn-primary btn-sm" id="console-send-btn" disabled>
              {s.consoleSendBtn}
            </button>
          </div>
        </div>
      </div>

      <section class="section-card">
        <div class="action-row">
          <div class="action-row-main">
            <button type="button" class="btn-primary" id="action-connect-btn">
              <Cable class="button-icon" />
              <span id="action-connect-btn-label">{s.connectBtn}</span>
            </button>
            <button type="button" class="btn-secondary" id="action-disconnect-btn" style="display:none;">
              <Unplug class="button-icon" />
              {s.disconnectBtn}
            </button>
            <button
              type="button"
              class="btn-secondary console-toggle-btn"
              id="console-toggle-btn"
              disabled
              title={s.consoleTabDisabledHint}
            >
              <SquareTerminal class="button-icon" />
              <span id="console-toggle-btn-label">{s.consoleToggleOpen}</span>
            </button>
          </div>
          <div class="action-row-end">
            <span class="flash-hint" id="flash-hint">{s.flashBtnDisabledNoDevice}</span>
            <button type="button" class="btn-primary" id="flash-btn" disabled>
              <Zap class="button-icon" />
              {s.flashBtn}
            </button>
          </div>
        </div>
      </section>
    </div>
  </main>

  <!-- Flash modal (three-step wizard) -->
  <div class="flash-modal-bg" id="flash-modal-bg">
    <div class="flash-modal-shell" id="flash-modal-shell">

      <!-- Modal header: step nav + close -->
      <div class="modal-header">
        <div class="modal-step-nav" role="list">
          <span class="modal-step-item active" id="modal-step1-indicator" role="listitem">
            <span class="modal-step-num">1</span>
            <span class="modal-step-label">{s.modalStep1Title}</span>
          </span>
          <span class="modal-step-sep" aria-hidden="true">›</span>
          <span class="modal-step-item" id="modal-step2-indicator" role="listitem">
            <span class="modal-step-num">2</span>
            <span class="modal-step-label">{s.modalStep2Title}</span>
          </span>
          <span class="modal-step-sep" aria-hidden="true">›</span>
          <span class="modal-step-item" id="modal-step3-indicator" role="listitem">
            <span class="modal-step-num">3</span>
            <span class="modal-step-label">{s.modalStep3Title}</span>
          </span>
        </div>
        <button
          type="button"
          class="modal-close-btn"
          id="modal-close-btn"
          aria-label={s.consoleCloseBtn}
        >✕</button>
      </div>

      <!-- Step 1: Download / Flash -->
      <div id="modal-step1-body" class="modal-body-section">
        <div class="modal-flash-result" id="modal-flash-result"></div>
        <div class="modal-progress-wrap" id="modal-progress-wrap">
          <div class="progress-label">
            <span id="modal-progress-stage">{s.progressLabel}</span>
            <span id="modal-progress-pct">0%</span>
          </div>
          <div class="progress-bar-bg">
            <div class="progress-bar" id="modal-progress-bar"></div>
          </div>
          <div class="progress-log" id="modal-progress-log"></div>
        </div>
        <div class="reconnect-prompt" id="modal-reconnect-prompt" hidden>
          <div class="reconnect-title">{s.postFlashReconnectTitle}</div>
          <p class="reconnect-desc">{s.postFlashReconnectDesc}</p>
          <button type="button" class="btn-primary" id="modal-reconnect-btn">
            {s.postFlashReconnectBtn}
          </button>
          <div class="reconnect-status" id="modal-reconnect-status"></div>
        </div>
      </div>

      <!-- Step 2: Wi-Fi Config -->
      <div id="modal-step2-body" class="modal-body-section" style="display:none">
        <p class="wifi-prompt" id="modal-wifi-prompt">{s.wifiPrompt}</p>
        <p class="wifi-same-network-hint">{s.wifiSameNetworkHint}</p>
        <div class="wifi-grid">
          <div>
            <label class="field-label" for="modal-wifi-ssid">{s.wifiSsidLabel}</label>
            <input type="text" id="modal-wifi-ssid" autocomplete="off" />
          </div>
          <div>
            <label class="field-label" for="modal-wifi-password">{s.wifiPasswordLabel}</label>
            <input
              type="password"
              id="modal-wifi-password"
              autocomplete="off"
              minlength="8"
            />
          </div>
          <div class="wifi-actions">
            <button type="button" class="btn-primary" id="modal-wifi-submit-btn">
              {s.wifiSubmitBtn}
            </button>
          </div>
        </div>
        <div class="wifi-status" id="modal-wifi-status"></div>
      </div>

      <!-- Step 3: Success -->
      <div id="modal-step3-body" class="modal-body-section" style="display:none">
        <div class="modal-ready-box">
          <div class="ready-title" id="modal-ready-title">{s.wifiReadyTitle}</div>
          <div class="ready-desc" id="modal-ready-desc">{s.wifiReadyDesc}</div>
          <a
            id="modal-ready-link"
            class="btn-link ready-link"
            href="#"
            target="_blank"
            rel="noopener noreferrer"
          ></a>
        </div>
      </div>

      <!-- Shared collapsible terminal (visible in steps 1 & 2) -->
      <details class="modal-terminal-wrap" id="modal-terminal-details">
        <summary class="modal-terminal-summary">
          {s.terminalLabel}
        </summary>
        <div class="modal-terminal-output" id="modal-terminal-output">
          <span class="modal-terminal-empty" id="modal-terminal-empty">{s.consoleWaiting}</span>
        </div>
      </details>

    </div>
  </div>

  <script is:inline type="application/json" id="flash-boot" set:html={flashBootJson} />
  <script>
    import "@/flash-tool/flash-page.ts";
  </script>
</SiteShell>
