:root {
  --zone-external-bg: #dbeafe;
  --zone-dmz-bg: #fef9c3;
  --zone-internal-bg: #dcfce7;
  --zone-external-border: #93c5fd;
  --zone-dmz-border: #fde047;
  --zone-internal-border: #86efac;
  --primary: #1d4ed8;
  --primary-light: #eff6ff;
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --border: #e2e8f0;
  --card-bg: #fff;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.07), 0 2px 4px rgba(0,0,0,0.05);
  --shadow-lg: 0 10px 15px rgba(0,0,0,0.08), 0 4px 6px rgba(0,0,0,0.04);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: #f8fafc;
  color: var(--text-primary);
  min-height: 100vh;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ===== NAV ===== */
.top-nav {
  background: rgba(255,255,255,0.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
  position: sticky;
  top: 0;
  z-index: 200;
}
.top-nav-inner {
  height: 52px;
  max-width: 1000px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
}
.nav-back {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-secondary);
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 20px;
  white-space: nowrap;
  transition: all 0.15s;
}
.nav-back:hover { color: var(--primary); border-color: var(--primary); text-decoration: none; }
.nav-title { font-size: 0.9rem; font-weight: 800; color: var(--text-primary); flex: 1; }

/* ===== HERO ===== */
.tool-hero {
  background: linear-gradient(135deg, #1e3a5f 0%, #1d4ed8 60%, #3b82f6 100%);
  color: #fff;
  padding: 28px 20px 24px;
  text-align: center;
}
.tool-hero h1 { font-size: 1.4rem; font-weight: 900; margin-bottom: 6px; }
.tool-hero p { font-size: 0.85rem; opacity: 0.88; line-height: 1.6; }
.hero-badges { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-top: 12px; }
.hero-badge {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 20px;
  padding: 4px 12px;
  font-size: 0.75rem;
  font-weight: 600;
}

/* ===== MAIN LAYOUT ===== */
.tool-wrap {
  max-width: 1000px;
  margin: 0 auto;
  padding: 16px 12px 80px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ===== STATS ROW ===== */
.stats-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.stat-card {
  flex: 1;
  min-width: 100px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  text-align: center;
  box-shadow: var(--shadow);
}
.stat-num { font-size: 1.5rem; font-weight: 900; color: var(--primary); line-height: 1; }
.stat-label { font-size: 0.72rem; color: var(--text-muted); margin-top: 2px; font-weight: 600; }

/* ===== TOOL CARD ===== */
.tool-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.tool-card-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}
.tool-card-title { font-size: 0.88rem; font-weight: 800; color: var(--text-primary); }
.tool-card-body { padding: 0; }

/* ===== DEVICE PALETTE ===== */
.palette {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  padding: 12px 14px;
  background: #f8fafc;
  border-bottom: 1px solid var(--border);
}
.palette-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  cursor: grab;
  padding: 8px 10px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  background: var(--card-bg);
  transition: all 0.15s;
  user-select: none;
  -webkit-user-select: none;
}
.palette-item:hover { border-color: var(--primary); box-shadow: var(--shadow-md); transform: translateY(-1px); }
.palette-item:active { transform: scale(0.95); }
.palette-item.dragging-source { opacity: 0.4; }
.palette-icon { font-size: 1.4rem; line-height: 1; }
.palette-label { font-size: 0.62rem; font-weight: 700; color: var(--text-secondary); text-align: center; white-space: nowrap; }

/* ===== CANVAS AREA ===== */
.canvas-outer {
  position: relative;
  overflow: hidden;
  min-height: 380px;
  touch-action: none;
}
.canvas-inner {
  position: relative;
  width: 100%;
  height: 380px;
}

/* Zone bands */
.zone-band {
  position: absolute;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 10px;
  pointer-events: none;
}
.zone-band.external {
  top: 0;
  height: 33.33%;
  background: var(--zone-external-bg);
  border-bottom: 2px dashed var(--zone-external-border);
}
.zone-band.dmz {
  top: 33.33%;
  height: 33.33%;
  background: var(--zone-dmz-bg);
  border-bottom: 2px dashed var(--zone-dmz-border);
}
.zone-band.internal {
  top: 66.66%;
  height: 33.34%;
  background: var(--zone-internal-bg);
}
.zone-label {
  font-size: 0.65rem;
  font-weight: 700;
  opacity: 0.55;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

/* SVG connections overlay */
#connectionSvg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 5;
}
#connectionSvg line {
  stroke: #64748b;
  stroke-width: 2;
  stroke-dasharray: 5 3;
}
#connectionSvg line.active-conn { stroke: #1d4ed8; stroke-dasharray: none; stroke-width: 2.5; }
#connectionSvg line.error-conn { stroke: #ef4444; stroke-dasharray: none; }

/* Placed devices */
.placed-device {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: grab;
  z-index: 10;
  user-select: none;
  -webkit-user-select: none;
  transform: translate(-50%, -50%);
  padding: 6px 8px;
  border-radius: 10px;
  border: 2px solid transparent;
  background: rgba(255,255,255,0.85);
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
  transition: border-color 0.15s, box-shadow 0.15s;
  min-width: 52px;
  text-align: center;
  touch-action: none;
}
.placed-device:active { cursor: grabbing; }
.placed-device.selected {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(29,78,216,0.2), 0 2px 8px rgba(0,0,0,0.12);
}
.placed-device.connecting-from {
  border-color: #f59e0b;
  box-shadow: 0 0 0 3px rgba(245,158,11,0.3);
}
.placed-device .dev-icon { font-size: 1.5rem; line-height: 1; }
.placed-device .dev-name { font-size: 0.6rem; font-weight: 700; color: var(--text-secondary); white-space: nowrap; }
.placed-device .dev-del {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #ef4444;
  color: #fff;
  font-size: 0.6rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-weight: 900;
  line-height: 1;
  border: 1.5px solid #fff;
  display: none;
}
.placed-device.selected .dev-del { display: flex; }

/* ===== TOOLBAR ===== */
.canvas-toolbar {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  background: #f8fafc;
}
.tb-btn {
  font-size: 0.78rem;
  font-weight: 700;
  padding: 7px 14px;
  border-radius: 8px;
  border: 1.5px solid var(--border);
  background: var(--card-bg);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}
.tb-btn:hover { border-color: var(--primary); color: var(--primary); }
.tb-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.tb-btn.danger { border-color: #fca5a5; color: #ef4444; }
.tb-btn.danger:hover { background: #fef2f2; }
.tb-btn.success { border-color: #6ee7b7; color: #059669; }
.tb-btn.success:hover { background: #f0fdf4; }

/* ===== SCORE / RESULT PANEL ===== */
.score-panel {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.score-header {
  padding: 12px 16px;
  font-size: 0.88rem;
  font-weight: 800;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}
.score-body { padding: 14px 16px; display: flex; flex-direction: column; gap: 10px; }
.score-big {
  text-align: center;
  padding: 16px;
  background: var(--primary-light);
  border-radius: var(--radius-sm);
  border: 2px solid #bfdbfe;
}
.score-num { font-size: 3rem; font-weight: 900; color: var(--primary); line-height: 1; }
.score-denom { font-size: 1rem; color: var(--text-muted); font-weight: 600; }
.score-label { font-size: 0.8rem; color: var(--text-secondary); margin-top: 4px; font-weight: 600; }
.score-detail { display: flex; flex-direction: column; gap: 6px; }
.score-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 0.82rem;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #f8fafc;
}
.score-row.pass { background: #f0fdf4; border-color: #86efac; }
.score-row.fail { background: #fef2f2; border-color: #fca5a5; }
.score-row-icon { font-size: 1rem; flex-shrink: 0; margin-top: 1px; }
.score-row-text { flex: 1; line-height: 1.5; color: var(--text-secondary); }
.score-row-text strong { color: var(--text-primary); }

/* ===== EXPLANATION PANEL ===== */
.explain-panel {
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: var(--radius);
  overflow: hidden;
}
.explain-header {
  padding: 10px 16px;
  font-size: 0.85rem;
  font-weight: 800;
  color: #92400e;
  border-bottom: 1px solid #fde68a;
  display: flex;
  align-items: center;
  gap: 6px;
}
.explain-body { padding: 14px 16px; font-size: 0.82rem; line-height: 1.75; color: #78350f; }
.explain-body h3 { font-size: 0.85rem; font-weight: 800; color: #92400e; margin: 10px 0 4px; }
.explain-body ul { padding-left: 18px; }
.explain-body ul li { margin-bottom: 4px; }

/* ===== EXAMPLE PANEL ===== */
.example-toggle-btn {
  width: 100%;
  padding: 10px 16px;
  font-size: 0.82rem;
  font-weight: 700;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--text-secondary);
  transition: all 0.15s;
}
.example-toggle-btn:hover { border-color: var(--primary); color: var(--primary); }
.example-panel {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: none;
}
.example-panel.open { display: block; }
.example-panel img { width: 100%; display: block; }
.example-body { padding: 14px 16px; font-size: 0.82rem; line-height: 1.7; color: var(--text-secondary); }
.example-body h3 { font-size: 0.88rem; font-weight: 800; color: var(--text-primary); margin: 12px 0 6px; }
.example-body h3:first-child { margin-top: 0; }
.example-body ul { padding-left: 18px; }
.example-body ul li { margin-bottom: 4px; }
.example-body code {
  font-family: monospace;
  background: #f1f5f9;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.8rem;
  color: #1d4ed8;
}

/* ===== TOAST ===== */
.toast {
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #1e293b;
  color: #fff;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 0.82rem;
  font-weight: 600;
  opacity: 0;
  transition: all 0.25s;
  z-index: 999;
  white-space: nowrap;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ===== MODE HINT ===== */
.mode-hint {
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  padding: 6px 14px;
  background: #f8fafc;
  border-bottom: 1px solid var(--border);
}
.mode-hint.connect-mode { color: #b45309; background: #fffbeb; }

/* ===== RESPONSIVE ===== */
@media (min-width: 640px) {
  .canvas-inner { height: 480px; }
  .zone-band.external,
  .zone-band.dmz { height: 33.33%; }
  .zone-band.internal { top: 66.66%; height: 33.34%; }
}
