/* ==========================================================================
   DIORAMA FACTORY — Foglio di stile Touch-First per iPadOS Safari
   ========================================================================== */

:root {
  --bg-top: #141923;
  --bg-bottom: #0b0e14;
  --panel-bg: rgba(20, 26, 36, 0.84);
  --panel-border: rgba(255, 255, 255, 0.14);
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --accent-yellow: #f59e0b;
  --accent-orange: #f97316;
  --accent-cyan: #06b6d4;
  --accent-emerald: #10b981;
  --accent-magenta: #ec4899;
  --accent-danger: #ef4444;
  --shadow-soft: 0 10px 28px rgba(0, 0, 0, 0.45);
  --radius-lg: 16px;
  --radius-md: 12px;
}

* {
  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;
  touch-action: none;
  background: radial-gradient(circle at 50% 42%, #232c3d 0%, #121722 58%, #090c12 100%);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Rounded", "Segoe UI", Roboto, sans-serif;
  color: var(--text-main);
}

#game-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  z-index: 1;
}

/* Vignettatura morbida per profondità diorama */
.vignette-overlay {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 2;
  background: radial-gradient(circle at center, transparent 58%, rgba(5, 8, 13, 0.55) 100%);
}

/* ==========================================================================
   Overlay SVG Cavi Logici
   ========================================================================== */

#wire-overlay {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 5;
}

.wire-path-bg {
  fill: none;
  stroke: rgba(10, 14, 22, 0.72);
  stroke-width: 7;
  stroke-linecap: round;
}

.wire-path {
  fill: none;
  stroke-width: 3.5;
  stroke-linecap: round;
  transition: stroke-width 0.15s ease;
}

.wire-path.trigger {
  stroke: #fbbf24;
  filter: url(#glow-yellow);
}

.wire-path.data {
  stroke: #22d3ee;
  filter: url(#glow-cyan);
}

.wire-hit-area {
  fill: none;
  stroke: transparent;
  stroke-width: 24;
  pointer-events: stroke;
  cursor: pointer;
}

.wire-cut-badge {
  pointer-events: auto;
  cursor: pointer;
}

.port-group {
  pointer-events: auto;
  cursor: pointer;
}

.port-hit {
  fill: rgba(255, 255, 255, 0.001);
}

.port-ring {
  fill: #0f172a;
  stroke-width: 3;
  transition: transform 0.15s ease;
}

.port-ring.trigger {
  stroke: #fbbf24;
}

.port-ring.data {
  stroke: #22d3ee;
}

.port-core {
  transition: r 0.15s ease;
}

.port-core.trigger {
  fill: #fbbf24;
}

.port-core.data {
  fill: #22d3ee;
}

.port-label {
  font-size: 10px;
  font-weight: 700;
  fill: #f1f5f9;
  text-anchor: middle;
  paint-order: stroke;
  stroke: rgba(10, 14, 22, 0.9);
  stroke-width: 3px;
  pointer-events: none;
}

/* ==========================================================================
   HUD Superiore
   ========================================================================== */

#top-hud {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: max(14px, env(safe-area-inset-left));
  right: max(14px, env(safe-area-inset-right));
  z-index: 20;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
}

#top-hud > * {
  pointer-events: auto;
}

.hud-left,
.hud-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.brand-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel-bg);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  box-shadow: var(--shadow-soft);
}

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent-yellow);
  box-shadow: 0 0 10px var(--accent-yellow);
}

.brand-title {
  font-weight: 800;
  font-size: 13px;
  letter-spacing: 0.08em;
}

.mode-switch {
  display: flex;
  background: var(--panel-bg);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-md);
  padding: 4px;
  box-shadow: var(--shadow-soft);
}

.mode-btn {
  min-height: 40px;
  padding: 0 14px;
  border: none;
  border-radius: 9px;
  background: transparent;
  color: var(--text-muted);
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.18s ease;
}

.mode-btn.active {
  background: var(--accent-yellow);
  color: #111827;
  box-shadow: 0 2px 10px rgba(245, 158, 11, 0.4);
}

/* Centro HUD: Obiettivi e Ordini */
.hud-center {
  flex: 1;
  max-width: 560px;
  min-width: 240px;
}

.objective-card {
  background: var(--panel-bg);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-lg);
  padding: 10px 14px;
  box-shadow: var(--shadow-soft);
}

.level-selector {
  display: flex;
  gap: 6px;
  margin-bottom: 6px;
}

.lvl-pill {
  min-width: 34px;
  height: 30px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-main);
  font-weight: 800;
  font-size: 12px;
  cursor: pointer;
}

.lvl-pill.active {
  background: var(--accent-cyan);
  color: #082f49;
  border-color: transparent;
}

.lvl-pill.completed {
  border-color: var(--accent-emerald);
  color: #34d399;
}

.objective-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.objective-title {
  font-weight: 800;
  font-size: 14px;
  color: #fff;
}

.goal-pill {
  background: rgba(16, 185, 129, 0.2);
  border: 1px solid rgba(16, 185, 129, 0.5);
  color: #6ee7b7;
  font-weight: 800;
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
  white-space: nowrap;
}

.objective-desc {
  margin: 4px 0 0;
  font-size: 12px;
  line-height: 1.35;
  color: #cbd5e1;
}

/* Sandbox HUD */
.sandbox-stats {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.stat-box {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.stat-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
}

.stat-value {
  font-size: 18px;
  font-weight: 900;
  color: var(--accent-yellow);
}

.sandbox-io-buttons {
  display: flex;
  gap: 6px;
}

.mini-btn {
  min-height: 34px;
  padding: 0 12px;
  border-radius: 8px;
  border: 1px solid var(--panel-border);
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-main);
  font-weight: 700;
  font-size: 12px;
  cursor: pointer;
}

.mini-btn:active {
  transform: scale(0.95);
}

.orders-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
}

.order-chip {
  flex: 1;
  min-width: 145px;
  background: rgba(15, 23, 42, 0.78);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  padding: 6px 10px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.order-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  font-weight: 800;
}

.order-sub {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-muted);
}

/* Pulsanti Controllo Simulazione (>= 48x48px ergonomici touch) */
.ctrl-btn {
  min-width: 48px;
  min-height: 48px;
  padding: 0 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--panel-border);
  background: var(--panel-bg);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  color: var(--text-main);
  font-weight: 800;
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  box-shadow: var(--shadow-soft);
  transition: transform 0.12s ease, background 0.18s ease;
}

.ctrl-btn:active {
  transform: scale(0.94);
}

.ctrl-btn.primary {
  background: linear-gradient(135deg, #10b981, #059669);
  border-color: rgba(255, 255, 255, 0.25);
  color: #ffffff;
}

.ctrl-btn.primary.running {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: #111827;
}

.ctrl-btn.icon-only {
  padding: 0;
  font-size: 18px;
}

/* ==========================================================================
   Dock Inferiore Touch-First
   ========================================================================== */

#bottom-dock {
  position: fixed;
  bottom: max(12px, env(safe-area-inset-bottom));
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  z-index: 20;
  display: flex;
  justify-content: center;
  pointer-events: none;
}

.dock-container {
  pointer-events: auto;
  max-width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--panel-bg);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 8px 10px;
  box-shadow: var(--shadow-soft);
}

.tool-palette {
  display: flex;
  align-items: center;
  gap: 8px;
  overflow-x: auto;
  padding: 2px;
  scrollbar-width: none;
}

.tool-palette::-webkit-scrollbar {
  display: none;
}

.tool-card {
  min-width: 68px;
  height: 64px;
  padding: 6px 8px;
  border-radius: 14px;
  border: 1.5px solid rgba(255, 255, 255, 0.1);
  background: rgba(30, 41, 59, 0.75);
  color: var(--text-main);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.12s ease, border-color 0.15s ease, background 0.15s ease;
}

.tool-card:active {
  transform: scale(0.93);
}

.tool-card.selected {
  background: rgba(245, 158, 11, 0.22);
  border-color: var(--accent-yellow);
  box-shadow: 0 0 16px rgba(245, 158, 11, 0.35);
}

.tool-icon {
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 19px;
}

.tool-name {
  font-size: 10.5px;
  font-weight: 700;
  text-align: center;
  line-height: 1.1;
  white-space: nowrap;
}

.dock-utilities {
  display: flex;
  gap: 8px;
  padding-left: 8px;
  border-left: 1px solid var(--panel-border);
}

.util-btn {
  min-width: 56px;
  height: 64px;
  border-radius: 14px;
  border: 1.5px solid rgba(255, 255, 255, 0.14);
  background: rgba(15, 23, 42, 0.85);
  color: var(--text-main);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  cursor: pointer;
}

.util-btn:active {
  transform: scale(0.93);
}

.util-btn.active {
  background: rgba(239, 68, 68, 0.25);
  border-color: var(--accent-danger);
  color: #fca5a5;
}

.util-ico {
  font-size: 20px;
  font-weight: 800;
  transition: transform 0.2s ease;
}

.util-lbl {
  font-size: 10px;
  font-weight: 700;
}

/* ==========================================================================
   Menu Contestuale Flottante
   ========================================================================== */

.context-menu {
  position: fixed;
  z-index: 30;
  transform: translate(-50%, -115%);
  background: rgba(15, 23, 42, 0.94);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1.5px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
  padding: 8px;
  box-shadow: 0 14px 34px rgba(0, 0, 0, 0.6);
  animation: popIn 0.16s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

@keyframes popIn {
  from {
    opacity: 0;
    transform: translate(-50%, -100%) scale(0.85);
  }
  to {
    opacity: 1;
    transform: translate(-50%, -115%) scale(1);
  }
}

.ctx-header {
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent-cyan);
  text-align: center;
  margin-bottom: 6px;
}

.ctx-actions {
  display: flex;
  gap: 6px;
}

.ctx-btn {
  min-width: 62px;
  min-height: 52px;
  padding: 6px 8px;
  border-radius: 11px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(30, 41, 59, 0.9);
  color: #f8fafc;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  cursor: pointer;
}

.ctx-btn:active {
  transform: scale(0.92);
}

.ctx-btn.danger {
  background: rgba(239, 68, 68, 0.2);
  border-color: rgba(239, 68, 68, 0.5);
  color: #fca5a5;
}

.ctx-btn:disabled {
  opacity: 0.35;
  pointer-events: none;
}

.ctx-ico {
  font-size: 17px;
}

/* ==========================================================================
   Toast & Modale Vittoria
   ========================================================================== */

.toast {
  position: fixed;
  top: 98px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 40;
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid var(--accent-cyan);
  color: #f8fafc;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
  box-shadow: var(--shadow-soft);
  pointer-events: none;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(6, 10, 18, 0.72);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-card {
  background: #161e2e;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 22px;
  padding: 28px;
  max-width: 420px;
  width: 100%;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.65);
}

.modal-badge {
  display: inline-block;
  background: rgba(16, 185, 129, 0.2);
  color: #34d399;
  border: 1px solid rgba(16, 185, 129, 0.45);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.08em;
  padding: 4px 12px;
  border-radius: 999px;
  margin-bottom: 10px;
}

.modal-card h2 {
  margin: 0 0 8px;
  font-size: 24px;
}

.modal-card p {
  margin: 0 0 22px;
  color: #cbd5e1;
  font-size: 14px;
  line-height: 1.45;
}

.modal-actions {
  display: flex;
  justify-content: center;
  gap: 12px;
}

.hidden {
  display: none !important;
}

@media (max-width: 900px) {
  .brand-badge {
    display: none;
  }
  .objective-desc {
    font-size: 11px;
  }
}