* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: #3d1c0a;
  color: #fff6e5;
  overscroll-behavior: none;
}

.app-root {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  background: linear-gradient(180deg, #522810 0%, #3a1a08 100%);
}

/* ==================== TOP WOODEN HEADER ==================== */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  background:
    repeating-linear-gradient(90deg, rgba(255,205,140,0.05) 0 4px, rgba(20,8,2,0.08) 4px 14px),
    linear-gradient(180deg, #7f441d 0%, #592c0f 100%);
  border-bottom: 3px solid #2e1305;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.45);
  z-index: 20;
  flex-wrap: wrap;
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand-icon {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  border: 2px solid #ffd859;
  box-shadow: 0 2px 6px rgba(0,0,0,0.45);
  object-fit: cover;
}

.brand-title {
  font-weight: 900;
  font-size: clamp(0.96rem, 2.2vw, 1.24rem);
  letter-spacing: 0.5px;
  color: #ffe88f;
  text-shadow: 0 2px 3px rgba(0,0,0,0.6);
  white-space: nowrap;
}

.mode-tabs {
  display: inline-flex;
  background: #2f1206;
  padding: 3px;
  border-radius: 11px;
  border: 1.5px solid #8a4f23;
  gap: 3px;
}

.mode-tab {
  border: none;
  background: transparent;
  color: #e4c196;
  font-weight: 800;
  font-size: 0.82rem;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.mode-tab.active {
  background: linear-gradient(180deg, #ffe052 0%, #db9618 100%);
  color: #4b1b05;
  box-shadow: 0 2px 6px rgba(0,0,0,0.4);
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.wbtn {
  cursor: pointer;
  border: 2px solid #351606;
  border-radius: 10px;
  padding: 6px 10px;
  font-weight: 800;
  font-size: 0.8rem;
  color: #fff8ea;
  background: linear-gradient(180deg, #b56c34 0%, #7a3f18 100%);
  box-shadow: 0 3px 0 #351606, 0 4px 8px rgba(0,0,0,0.35), inset 0 1.5px 0 rgba(255,232,188,0.4);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  white-space: nowrap;
  transition: transform 0.08s ease, filter 0.12s ease;
}

.wbtn:hover { filter: brightness(1.08); }
.wbtn:active { transform: translateY(2px); box-shadow: 0 1px 0 #351606; }

.wbtn.gold {
  background: linear-gradient(180deg, #ffe055 0%, #d48c14 100%);
  color: #491a04;
}
.wbtn.green {
  background: linear-gradient(180deg, #35c75d 0%, #1b8739 100%);
  color: #ffffff;
}
.wbtn.blue {
  background: linear-gradient(180deg, #3c94ed 0%, #1b5ba8 100%);
  color: #ffffff;
}
.wbtn.red {
  background: linear-gradient(180deg, #e0463c 0%, #9e1f19 100%);
  color: #ffffff;
}
.wbtn.active-toggle {
  background: linear-gradient(180deg, #ffe055 0%, #d48c14 100%);
  color: #491a04;
  border-color: #fff0a8;
}

/* ==================== MISSION / SANDBOX BANNER ==================== */
.mission-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 7px 12px;
  background: linear-gradient(90deg, #4b210b 0%, #632d12 50%, #4b210b 100%);
  border-bottom: 2px solid #2b1104;
  z-index: 15;
  flex-wrap: wrap;
}

.mission-info {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 220px;
}

.mission-pill {
  background: linear-gradient(180deg, #ffe24d 0%, #e09d15 100%);
  color: #4a1a04;
  font-weight: 900;
  font-size: 0.78rem;
  padding: 4px 10px;
  border-radius: 999px;
  white-space: nowrap;
  box-shadow: 0 2px 4px rgba(0,0,0,0.35);
}

.mission-text-wrap {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.mission-title {
  font-weight: 900;
  font-size: 0.92rem;
  color: #ffea9c;
}

.mission-desc {
  font-size: 0.78rem;
  color: #f6dec0;
  line-height: 1.3;
}

.mission-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.demo-select {
  background: #2f1206;
  color: #ffea9c;
  border: 2px solid #965728;
  border-radius: 9px;
  padding: 5px 10px;
  font-weight: 800;
  font-size: 0.8rem;
  cursor: pointer;
}

/* ==================== MAIN WORKSPACE ==================== */
.workspace {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
  overflow: hidden;
}

/* Left Wooden Component Toolbox */
.toolbox {
  width: 192px;
  background:
    repeating-linear-gradient(180deg, rgba(255,205,140,0.03) 0 4px, rgba(20,8,2,0.06) 4px 14px),
    linear-gradient(180deg, #5e2f13 0%, #421e0a 100%);
  border-right: 3px solid #2b1104;
  box-shadow: 4px 0 14px rgba(0, 0, 0, 0.35);
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 12;
  flex-shrink: 0;
}

.tool-cat-title {
  font-size: 0.74rem;
  font-weight: 900;
  color: #ffd882;
  letter-spacing: 0.4px;
  padding: 3px 6px;
  background: rgba(34, 13, 4, 0.55);
  border-radius: 6px;
  border-left: 3px solid #f7c538;
}

.tool-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
}

.tool-card {
  cursor: grab;
  background: linear-gradient(180deg, #b87038 0%, #83441b 100%);
  border: 2px solid #3c1a08;
  border-radius: 12px;
  padding: 7px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  color: #fff7e6;
  box-shadow: 0 3px 0 #351606, inset 0 1.5px 0 rgba(255, 232, 185, 0.45);
  transition: transform 0.08s ease, filter 0.12s ease;
  touch-action: manipulation;
}

.tool-card:hover { filter: brightness(1.1); transform: translateY(-1px); }
.tool-card:active { transform: translateY(2px); box-shadow: 0 1px 0 #351606; }

.tool-emoji {
  font-size: 1.45rem;
  line-height: 1;
  filter: drop-shadow(0 2px 3px rgba(0,0,0,0.4));
}

.tool-name {
  font-size: 0.72rem;
  font-weight: 800;
  text-align: center;
  line-height: 1.15;
}

/* Workbench Canvas Container */
.canvas-stage {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #6c3b1b;
}

#circuitCanvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: default;
}

/* Floating View Controls (Top-Right of Canvas) */
.canvas-view-bar {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 5px;
  z-index: 10;
  pointer-events: auto;
}

.mini-pill-btn {
  cursor: pointer;
  border: 1.5px solid #421c08;
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 0.74rem;
  font-weight: 800;
  color: #ffeec9;
  background: rgba(54, 23, 9, 0.84);
  backdrop-filter: blur(4px);
  box-shadow: 0 3px 8px rgba(0,0,0,0.35);
  transition: all 0.14s ease;
}

.mini-pill-btn.on {
  background: linear-gradient(180deg, #ffe052 0%, #d89216 100%);
  color: #491a04;
  border-color: #fff2aa;
}

/* Subtle Helper Tip Bottom-Left */
.canvas-tip {
  position: absolute;
  top: 10px;
  left: 10px;
  background: rgba(42, 17, 6, 0.76);
  border: 1px solid rgba(255, 214, 145, 0.25);
  color: #ffe7b3;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 5px 11px;
  border-radius: 999px;
  pointer-events: none;
  max-width: calc(100% - 280px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  z-index: 8;
}

/* Warning Alert Ribbon */
.alert-banner {
  position: absolute;
  top: 46px;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(180deg, #d92b2b 0%, #991616 100%);
  color: #fff;
  border: 2px solid #ffe46b;
  border-radius: 12px;
  padding: 8px 16px;
  font-weight: 900;
  font-size: 0.84rem;
  box-shadow: 0 8px 20px rgba(0,0,0,0.55);
  z-index: 14;
  display: none;
  max-width: 90%;
  text-align: center;
  animation: shakeWarn 0.35s ease;
}

.alert-banner.show { display: block; }

@keyframes shakeWarn {
  0%, 100% { transform: translateX(-50%); }
  25% { transform: translateX(calc(-50% - 6px)); }
  75% { transform: translateX(calc(-50% + 6px)); }
}

/* Bottom Component Inspector Dock */
.inspector-dock {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%) translateY(130%);
  width: min(96%, 680px);
  background: linear-gradient(180deg, rgba(84, 41, 16, 0.96) 0%, rgba(52, 22, 8, 0.97) 100%);
  border: 2.5px solid #c9813d;
  border-radius: 16px;
  padding: 10px 14px;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.65);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 16;
  transition: transform 0.2s cubic-bezier(0.22, 1, 0.36, 1);
}

.inspector-dock.open {
  transform: translateX(-50%) translateY(0);
}

.insp-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.insp-name {
  font-weight: 900;
  font-size: 0.96rem;
  color: #ffe78a;
  display: flex;
  align-items: center;
  gap: 6px;
}

.insp-metrics {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.metric-chip {
  background: #2f1206;
  border: 1px solid #8a4f23;
  border-radius: 8px;
  padding: 3px 8px;
  font-size: 0.78rem;
  font-weight: 800;
  color: #fff3d6;
}

.metric-chip span { color: #ffe05a; }

.insp-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.slider-box {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(34, 13, 4, 0.65);
  padding: 5px 10px;
  border-radius: 10px;
  border: 1px solid #7c441c;
  flex: 1;
  min-width: 190px;
}

.slider-box label {
  font-size: 0.78rem;
  font-weight: 800;
  color: #ffd992;
  white-space: nowrap;
}

.slider-box input[type="range"] {
  flex: 1;
  accent-color: #ffd84a;
  cursor: pointer;
}

.led-swatches {
  display: inline-flex;
  gap: 6px;
  align-items: center;
}

.led-swatch {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2.5px solid #fff4d2;
  cursor: pointer;
  box-shadow: 0 2px 5px rgba(0,0,0,0.45);
}

.led-swatch.active {
  transform: scale(1.18);
  border-color: #ffe946;
  box-shadow: 0 0 10px #ffe946;
}

.insp-btns {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* ==================== MODALS & SUPERHERO CELEBRATION ==================== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(18, 7, 2, 0.78);
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 60;
}

.modal-backdrop.open { display: flex; }

.modal-card {
  width: 100%;
  max-width: 480px;
  max-height: 88vh;
  overflow-y: auto;
  border-radius: 22px;
  background: linear-gradient(180deg, #b86f38 0%, #86451b 100%);
  border: 3px solid #3c1a08;
  box-shadow: 0 22px 48px rgba(0,0,0,0.72), inset 0 3px 0 rgba(255,228,180,0.45);
  padding: 18px;
}

.modal-inner {
  background: #e5a363;
  border-radius: 15px;
  padding: 16px;
  color: #491b06;
  box-shadow: inset 0 2px 6px rgba(55, 20, 4, 0.35);
}

.hero-banner {
  background: linear-gradient(180deg, #ffe24a 0%, #f19a14 100%);
  color: #521802;
  border: 3px solid #fff2a6;
  border-radius: 14px;
  padding: 12px 16px;
  text-align: center;
  font-size: clamp(1.38rem, 5vw, 1.85rem);
  font-weight: 900;
  letter-spacing: 0.8px;
  text-shadow: 0 2px 0 rgba(255,255,255,0.65);
  box-shadow: 0 8px 18px rgba(0,0,0,0.4);
  margin-bottom: 12px;
  animation: heroPop 0.55s cubic-bezier(0.22, 1.4, 0.36, 1);
}

@keyframes heroPop {
  0% { transform: scale(0.5) rotate(-5deg); opacity: 0; }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

.hero-emblem {
  font-size: 3.4rem;
  text-align: center;
  line-height: 1.1;
  margin-bottom: 6px;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.35));
}

.hero-stars {
  text-align: center;
  font-size: 2rem;
  letter-spacing: 8px;
  margin-bottom: 8px;
}

.hero-sub {
  text-align: center;
  font-weight: 800;
  font-size: 1rem;
  line-height: 1.45;
  margin: 0 0 14px;
  color: #491b06;
}

.guide-box {
  background: rgba(255, 246, 228, 0.8);
  border-radius: 11px;
  padding: 10px 12px;
  margin-bottom: 9px;
  border: 1px solid rgba(112, 56, 18, 0.32);
  font-size: 0.88rem;
  line-height: 1.48;
}

.missions-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.mission-item-btn {
  cursor: pointer;
  text-align: left;
  background: rgba(255, 246, 228, 0.86);
  border: 2px solid #8a4b1f;
  border-radius: 12px;
  padding: 9px 10px;
  color: #4a1c06;
  font-weight: 800;
  font-size: 0.82rem;
  display: flex;
  flex-direction: column;
  gap: 3px;
  transition: transform 0.1s ease, background 0.15s ease;
}

.mission-item-btn:hover { background: #fff9ec; transform: translateY(-1px); }
.mission-item-btn.current { border-color: #d97706; background: #fff0b8; box-shadow: 0 0 0 2px rgba(255, 210, 60, 0.7); }
.mission-item-btn.cleared { border-color: #1f8a42; }

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.modal-actions .wbtn {
  flex: 1;
  padding: 11px 12px;
  font-size: 0.94rem;
}

#confettiCanvas {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 75;
}

.toast {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #2f1206;
  color: #ffea94;
  border: 2px solid #d49438;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 800;
  box-shadow: 0 8px 20px rgba(0,0,0,0.55);
  opacity: 0;
  pointer-events: none;
  transition: all 0.25s ease;
  z-index: 90;
  max-width: 92vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ==================== RESPONSIVE FOR MOBILE & PORTRAIT IPAD ==================== */
@media (max-width: 760px) {
  .workspace { flex-direction: column-reverse; }
  .toolbox {
    width: 100%;
    height: 112px;
    flex-direction: row;
    align-items: center;
    overflow-x: auto;
    overflow-y: hidden;
    border-right: none;
    border-top: 3px solid #2b1104;
    padding: 6px 8px;
    gap: 8px;
  }
  .tool-cat-title { display: none; }
  .tool-grid {
    display: flex;
    flex-direction: row;
    gap: 6px;
  }
  .tool-card {
    min-width: 74px;
    height: 86px;
    flex-shrink: 0;
  }
  .canvas-tip { display: none; }
  .brand-title { font-size: 0.95rem; }
  .missions-grid { grid-template-columns: 1fr; }
}
